-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
65 lines (62 loc) · 1.59 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
package vera
import (
"testing"
)
func TestParseEval(t *testing.T) {
type testCase struct {
input string
expected []bool
}
for _, c := range []testCase{
{"a", []bool{false, true}},
{"(a)", []bool{false, true}},
{"a&b", []bool{false, false, false, true}},
{"a|b", []bool{false, true, true, true}},
{"a^b", []bool{false, true, true, false}},
{"a>b", []bool{true, true, false, true}},
{"a=b", []bool{true, false, false, true}},
{"0", []bool{false}},
{"1", []bool{true}},
{"a|!0", []bool{true, true}},
{"(a & b)", []bool{false, false, false, true}},
{"!(a > b)", []bool{false, false, true, false}},
{"!!(a = b)", []bool{true, false, false, true}},
{"(a = b) | b", []bool{true, true, false, true}},
} {
stmt, truth, err := Parse(c.input)
if err != nil {
t.Fatalf("error occurred while parsing: %v (input: %s)", err, c.input)
}
for _, exp := range c.expected {
if stmt.Eval(truth) != exp {
t.Fatalf("expected %t for evalation of '%s' at %s", exp, c.input, truth)
}
truth.Val++
}
}
}
func TestParseStmtToString(t *testing.T) {
type testCase struct {
input string
expected string
}
for _, c := range []testCase{
{"a", "a"},
{"(a)", "a"},
{"!a", "!a"},
{"(!a)", "!a"},
{"!(a)", "!a"},
{"a&b", "a & b"},
{"(a&b)>c", "(a & b) > c"},
{"(a&!0)>!!1", "(a & !0) > 1"},
} {
stmt, _, err := Parse(c.input)
if err != nil {
t.Fatalf("error occurred while parsing: %v (input: %s)", err, c.input)
}
stmtStr := stmt.String()
if stmtStr != c.expected {
t.Fatalf("expected %s; got %s (input: %s)", c.expected, stmtStr, c.input)
}
}
}