-
Notifications
You must be signed in to change notification settings - Fork 4
/
parse_test.go
49 lines (44 loc) · 1.28 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
package gotalog
import "testing"
import "strings"
type testCase struct {
s string
shouldFail bool
commandCount int
}
var cases = []testCase{
{"foo(bar,baz).", false, 1},
{"foo(bar,baz)", true, 1},
{"foo(bar,baz)?", false, 1},
{"foo(bar,X)?", false, 1},
{"foo(X,bar)?", false, 1},
{"foo(bar,baz)~", false, 1},
{"foo(bar,baz) :- quux(bar, baz).", false, 1},
{"foo(bar,baz) :- quux(bar, baz), woz(bar).", false, 1},
{"foo(bar).foo(baz).quux(bar,baz).", false, 3},
{"foo(bar,baz) :- quux(bar, baz).", false, 1},
{"foo(bar,baz) :- quux(bar, baz), woz(bar)?", true, 1},
{"foo(X)?", false, 1},
{" \t\tfoo(X) :- baz ( X ) .", false, 1},
{"", false, 0},
{"foo(bar,baz). \n", false, 1},
{`% Transitive closure test from Guo & Gupta
r(X, Y) :- r(X, Z), r(Z, Y).
r(X, Y) :- p(X, Y), q(Y). %other comment
p(a, b). p(b, d). p(b, c).
q(b). q(c).
r(a, Y)?
`, false, 8},
}
func TestParse(t *testing.T) {
for i, v := range cases {
cmds, err := Parse(strings.NewReader(v.s))
if (err != nil) != v.shouldFail {
t.Errorf("Case %v: Expected success: %v, got error: %v", i, v.shouldFail, err)
}
if !v.shouldFail && len(cmds) != v.commandCount {
t.Errorf("Case %v: wrong number of commands generated. Got %v, expected %v",
i, len(cmds), v.commandCount)
}
}
}