idoneal - main_test.go
1 package main
2
3 import (
4 "flag"
5 "fmt"
6 "io"
7 "os"
8 "strings"
9 "testing"
10
11 "vimagination.zapto.org/idoneal/tests"
12 )
13
14 var exitCode int
15
16 func init() {
17 Exit = func(code int) {
18 exitCode = code
19 }
20 FS = tests.FS
21 flagErrorHandler = flag.ContinueOnError
22 }
23
24 func testOutput(t *testing.T, flag, ext string) {
25 t.Helper()
26 for n, test := range [...]string{"a", "b", "c", "d", "e"} {
27 f, _ := FS.Open(fmt.Sprintf("%s.%s", test, ext))
28 expected, _ := io.ReadAll(f)
29 for m, compressed := range [...]string{"", ".gz"} {
30 var stdout, stderr strings.Builder
31 Stdout = &stdout
32 Stderr = &stderr
33 os.Args = append(os.Args[:1], flag, fmt.Sprintf("%s.fastq%s", test, compressed))
34 main()
35 if stderr.Len() > 0 {
36 t.Errorf("test %d.%d: unexpected error: %s", n+1, m+1, stderr.String())
37 } else {
38 if output := stdout.String(); output != string(expected) {
39 t.Errorf("test %d.%d: expecting output %q, got %q", n+1, m+1, expected, output)
40 }
41 }
42 }
43 }
44 }
45
46 func TestMainCountSequences(t *testing.T) {
47 testOutput(t, "-s", "sequences")
48 }
49
50 func TestMainCountSequencesLong(t *testing.T) {
51 testOutput(t, "--sequences", "sequences")
52 }
53
54 func TestMainCountNucleotides(t *testing.T) {
55 testOutput(t, "-n", "nucleotides")
56 }
57
58 func TestMainCountNucleotidesLong(t *testing.T) {
59 testOutput(t, "--nucleotides", "nucleotides")
60 }
61