/
parse_test.go
75 lines (67 loc) · 1.19 KB
/
parse_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
package arithmetic
import (
"bufio"
"fmt"
"os"
"strings"
"testing"
)
func Test_Parse(t *testing.T) {
file, err := os.Open("tests.txt")
if err != nil {
t.Log(err)
t.Fail()
return
}
defer file.Close()
type Case struct {
line int
in string
out string
err bool
}
scanner := bufio.NewScanner(file)
var i int
var cases []Case
for scanner.Scan() {
i++
line := scanner.Text()
idx := strings.LastIndex(line, "=")
if idx == -1 {
continue
}
res := strings.TrimSpace(line[idx+1:])
cases = append(cases, Case{
line: i,
in: strings.TrimSpace(line[:idx]),
out: res,
err: res == "error",
})
}
err = scanner.Err()
if err != nil {
t.Log(err)
t.Fail()
}
for _, c := range cases {
out, err := Parse(c.in)
if (err != nil) != c.err {
if err == nil {
t.Logf("line %d [%s = %s]: expected error\n", c.line, c.in, c.out)
t.Fail()
} else {
t.Logf("line %d [%s = %s]: unexpected error %s\n", c.line, c.in, c.out, err)
t.Fail()
}
continue
}
if err != nil {
continue
}
if fmt.Sprintf("%v", out) != c.out {
t.Logf("line %d [%s = %s]: unexpected output: %v\n", c.line, c.in, c.out, out)
t.Fail()
continue
}
}
}