-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
112 lines (95 loc) · 1.96 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"bytes"
"fmt"
"io/fs"
"os"
"strings"
"testing"
)
func getFS() fs.FS {
return os.DirFS("tests")
}
func isInputFilename(filename string) bool {
return !strings.HasSuffix(filename, ".a")
}
func getAnswerFilename(inputFilename string) string {
return fmt.Sprintf("%s.a", inputFilename)
}
func Test_run(t *testing.T) {
tests := loadTests(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer tt.Close()
wantOut, err := tt.WantOut()
if err != nil {
t.Fatal(err)
}
out := &bytes.Buffer{}
run(tt.in, out)
gotOut := out.String()
if gotOut != wantOut {
t.Errorf("got\t%#v\nexpected\t%#v", gotOut, wantOut)
}
})
}
}
type testCase struct {
name string
in fs.File
wantOut fs.File
}
func (c *testCase) WantOut() (string, error) {
wantOutBuf := &bytes.Buffer{}
_, err := wantOutBuf.ReadFrom(c.wantOut)
if err != nil {
return "", err
}
return wantOutBuf.String(), nil
}
func (c *testCase) Close() {
err := c.in.Close()
err2 := c.wantOut.Close()
if err != nil {
fmt.Printf("failed closing: %v", err)
}
if err2 != nil {
fmt.Printf("failed closing: %v", err)
}
}
func loadTests(t *testing.T) []*testCase {
var tests []*testCase
fsys := getFS()
err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("walking dir: %w", err)
}
if !d.IsDir() && isInputFilename(d.Name()) {
test, err := loadTest(path, d, fsys)
if err != nil {
return fmt.Errorf("loading test: %w", err)
}
tests = append(tests, test)
}
return nil
})
if err != nil {
t.Fatal(err)
}
return tests
}
func loadTest(path string, d fs.DirEntry, fsys fs.FS) (*testCase, error) {
inputFile, err := fsys.Open(path)
if err != nil {
return nil, err
}
outputFile, err := fsys.Open(getAnswerFilename(path))
if err != nil {
return nil, err
}
return &testCase{
name: d.Name(),
in: inputFile,
wantOut: outputFile,
}, nil
}