-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
92 lines (76 loc) · 2.06 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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"path"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
const (
baseTestCasesDir = "./testcases"
inputFileName = "in.jsonl"
outputFileName = "out.jsonl"
)
func TestInputOutputCases(t *testing.T) {
cases := listSubDirs(baseTestCasesDir)
Convey("Authorizer application", t, func() {
Convey("Panics in case of error", func() {
input, output := bytes.NewReader([]byte(`not a json`)), bytes.NewBuffer(nil)
So(func() { testMain(input, output) }, ShouldPanic)
})
for _, caseName := range cases {
Convey(fmt.Sprintf(`Correctly handles test case "%s"`, caseName), func() {
input, expectedBuf := getTestCase(caseName)
outputBuf := bytes.NewBuffer(nil)
testMain(input, outputBuf)
output, expected := readLines(outputBuf), readLines(expectedBuf)
So(len(output), ShouldEqual, len(expected))
for i := range expected {
So(output[i], ShouldEqual, expected[i])
}
})
}
})
}
func testMain(in io.Reader, out io.Writer) {
prevIn, prevOut := stdin, stdout
defer func() { stdin, stdout = prevIn, prevOut }()
stdin, stdout = in, out
main()
}
func listSubDirs(path string) []string {
files, err := ioutil.ReadDir(path)
if err != nil {
panic(err)
}
subdirs := make([]string, len(files))
for i, file := range files {
if !file.IsDir() {
panic("Unexpected non-directory in " + path + ": " + file.Name())
}
subdirs[i] = file.Name()
}
return subdirs
}
func getTestCase(caseName string) (input io.Reader, expectedOutput io.Reader) {
inputFile := path.Join(baseTestCasesDir, caseName, inputFileName)
outputFile := path.Join(baseTestCasesDir, caseName, outputFileName)
return readFile(inputFile), readFile(outputFile)
}
func readFile(path string) io.Reader {
content, err := ioutil.ReadFile(path)
So(err, ShouldBeNil)
return bytes.NewReader(content)
}
func readLines(in io.Reader) []string {
lines := []string{}
scanner := bufio.NewScanner(in)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
So(scanner.Err(), ShouldBeNil)
return lines
}