-
Notifications
You must be signed in to change notification settings - Fork 3
/
kleene_test.go
92 lines (80 loc) · 2.18 KB
/
kleene_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
/*
(c) 2019 Launix, Inh. Carl-Philip Hänsch
Author: Tim Kluge
Dual licensed with custom aggreements or GPLv3
*/
package packrat
import "testing"
func TestKleene(t *testing.T) {
input := "Hello Hello Hello"
scanner := NewScanner[int](input, SkipWhitespaceRegex)
helloParser := NewAtomParser(1, "Hello", false, true)
helloAndWorldParser := NewKleeneParser(func (s string, a ...int) int {
r := 0
for _, v := range a {
r += v
}
return r
}, helloParser, nil)
n, err := Parse(helloAndWorldParser, scanner)
if err != nil {
t.Error(err)
} else {
if n.Payload != 3 {
t.Error("Kleene combinator doesn't produce 3 children")
}
}
irregularInput := "Sonne"
irregularScanner := NewScanner[int](irregularInput, SkipWhitespaceRegex)
irregularParser := NewKleeneParser(func (s string, a ...int) int {
r := 0
for _, v := range a {
r += v
}
return r
}, helloParser, nil)
in, ierr := ParsePartial(irregularParser, irregularScanner)
if ierr != nil {
t.Error("Kleene combinator doesn't match irregular input")
}
if in.Payload != 0 {
t.Error("Kleene combinator doesn't produce zero children for irregular input")
}
}
func TestKleeneSeparator(t *testing.T) {
input := " Hello, Hello, Hello"
scanner := NewScanner[int](input, SkipWhitespaceRegex)
helloParser := NewAtomParser(2, "Hello", false, true)
sepParser := NewAtomParser(0, ",", false, true)
helloAndWorldParser := NewKleeneParser(func (s string, a ...int) int {
r := 0
for _, v := range a {
r += v
}
return r
}, helloParser, sepParser)
n, err := Parse(helloAndWorldParser, scanner)
if err != nil {
t.Error(err)
} else {
if n.Payload != 6 {
t.Error("Kleene combinator doesn't produce 3 children")
}
}
irregularInput := "Sonne"
irregularScanner := NewScanner[int](irregularInput, SkipWhitespaceRegex)
irregularParser := NewKleeneParser(func (s string, a ...int) int {
r := 9
for _, v := range a {
r += v
}
return r
}, helloParser, nil)
in, ierr := ParsePartial(irregularParser, irregularScanner)
if ierr != nil {
t.Error("Kleene combinator doesn't match irregular input")
}
if in.Payload != 9 {
t.Error("Kleene combinator doesn't produce zero children for irregular input")
}
}