-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcombinator_test.go
244 lines (200 loc) · 6.57 KB
/
combinator_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package goparsify
import (
"testing"
"fmt"
"os"
"github.com/stretchr/testify/require"
)
func TestSeq(t *testing.T) {
parser := Seq("hello", "world")
t.Run("matches sequence", func(t *testing.T) {
node, p2 := runParser("hello world", parser)
assertSequence(t, node, "hello", "world")
require.Equal(t, "", p2.Get())
})
t.Run("returns errors", func(t *testing.T) {
_, p2 := runParser("hello there", parser)
require.Equal(t, "world", p2.Error.expected)
require.Equal(t, 6, p2.Error.pos)
require.Equal(t, 0, p2.Pos)
})
}
func TestMaybe(t *testing.T) {
t.Run("matches sequence", func(t *testing.T) {
node, p2 := runParser("hello world", Maybe("hello"))
require.Equal(t, "hello", node.Token)
require.Equal(t, " world", p2.Get())
})
t.Run("returns no errors", func(t *testing.T) {
node, p3 := runParser("hello world", Maybe("world"))
require.Equal(t, Result{}, node)
require.False(t, p3.Errored())
require.Equal(t, 0, p3.Pos)
})
}
func TestAny(t *testing.T) {
t.Run("Matches any", func(t *testing.T) {
node, p2 := runParser("hello world!", Any("hello", "world"))
require.Equal(t, "hello", node.Token)
require.Equal(t, 5, p2.Pos)
})
t.Run("Returns longest error", func(t *testing.T) {
_, p2 := runParser("hello world!", Any(
"nope",
Seq("hello", "world", "."),
Seq("hello", "brother"),
))
require.Equal(t, "offset 11: expected .", p2.Error.Error())
require.Equal(t, 11, p2.Error.Pos())
require.Equal(t, 0, p2.Pos)
})
t.Run("Accepts nil matches", func(t *testing.T) {
node, p2 := runParser("hello world!", Any(Exact("ffffff")))
require.Equal(t, Result{}, node)
require.Equal(t, 0, p2.Pos)
})
t.Run("overlapping longest match", func(t *testing.T) {
EnableLogging(os.Stdout)
p := Many(Any("ab", "a"))
t.Run("a ab", func(t *testing.T) {
node, ps := runParser("a ab", p)
require.False(t, ps.Errored())
require.Equal(t, "a", node.Child[0].Token)
require.Equal(t, "ab", node.Child[1].Token)
})
t.Run("ab a", func(t *testing.T) {
node, ps := runParser("ab a", p)
fmt.Println(node)
require.False(t, ps.Errored())
require.Equal(t, "ab", node.Child[0].Token)
require.Equal(t, "a", node.Child[1].Token)
})
})
}
func TestSome(t *testing.T) {
t.Run("Matches sequence with sep", func(t *testing.T) {
node, p2 := runParser("a,b,c,d,e,", Some(Chars("a-g"), ","))
require.False(t, p2.Errored())
assertSequence(t, node, "a", "b", "c", "d", "e")
require.Equal(t, 10, p2.Pos)
})
t.Run("Matches sequence without trailing sep", func(t *testing.T) {
node, p2 := runParser("a,b,c,d,e1111", Some(Chars("a-g"), ","))
require.False(t, p2.Errored())
assertSequence(t, node, "a", "b", "c", "d", "e")
require.Equal(t, "1111", p2.Get())
})
t.Run("Matches sequence without sep", func(t *testing.T) {
node, p2 := runParser("a,b,c,d,e,", Some(Any(Chars("a-g"), ",")))
assertSequence(t, node, "a", ",", "b", ",", "c", ",", "d", ",", "e", ",")
require.Equal(t, 10, p2.Pos)
})
t.Run("splits words automatically on space", func(t *testing.T) {
node, p2 := runParser("hello world", Some(Chars("a-z")))
assertSequence(t, node, "hello", "world")
require.Equal(t, "", p2.Get())
})
t.Run("Stops on error", func(t *testing.T) {
node, p2 := runParser("a,b,c,d,e,", Some(Chars("a-c"), ","))
assertSequence(t, node, "a", "b", "c")
require.Equal(t, 6, p2.Pos)
require.Equal(t, "d,e,", p2.Get())
})
}
func TestMany(t *testing.T) {
t.Run("Matches sequence with sep", func(t *testing.T) {
node, p2 := runParser("a,b,c,d,e,", Many(Chars("a-g"), Exact(",")))
assertSequence(t, node, "a", "b", "c", "d", "e")
require.Equal(t, 10, p2.Pos)
})
t.Run("Matches sequence without sep", func(t *testing.T) {
node, p2 := runParser("a,b,c,d,e,", Many(Any(Chars("abcdefg"), Exact(","))))
assertSequence(t, node, "a", ",", "b", ",", "c", ",", "d", ",", "e", ",")
require.Equal(t, 10, p2.Pos)
})
t.Run("Stops on error", func(t *testing.T) {
node, p2 := runParser("a,b,c,d,e,", Many(Chars("abc"), Exact(",")))
assertSequence(t, node, "a", "b", "c")
require.Equal(t, 6, p2.Pos)
require.Equal(t, "d,e,", p2.Get())
})
t.Run("Returns error if nothing matches", func(t *testing.T) {
_, p2 := runParser("a,b,c,d,e,", Many(Chars("def"), Exact(",")))
require.Equal(t, "offset 0: expected def", p2.Error.Error())
require.Equal(t, "a,b,c,d,e,", p2.Get())
})
}
type htmlTag struct {
Name string
}
func TestMap(t *testing.T) {
parser := Seq("<", Chars("a-zA-Z0-9"), ">").Map(func(n *Result) {
n.Result = htmlTag{n.Child[1].Token}
})
t.Run("success", func(t *testing.T) {
result, _ := runParser("<html>", parser)
require.Equal(t, htmlTag{"html"}, result.Result)
})
t.Run("error", func(t *testing.T) {
_, ps := runParser("<html", parser)
require.Equal(t, "offset 5: expected >", ps.Error.Error())
require.Equal(t, 0, ps.Pos)
})
}
func TestBind(t *testing.T) {
parser := Bind("true", true)
t.Run("success", func(t *testing.T) {
result, _ := runParser("true", parser)
require.Equal(t, true, result.Result)
})
t.Run("error", func(t *testing.T) {
result, ps := runParser("nil", parser)
require.Nil(t, result.Result)
require.Equal(t, "offset 0: expected true", ps.Error.Error())
require.Equal(t, 0, ps.Pos)
})
}
func TestCut(t *testing.T) {
t.Run("test any", func(t *testing.T) {
_, ps := runParser("var world", Any(Seq("var", Cut(), "hello"), "var world"))
require.Equal(t, "offset 4: expected hello", ps.Error.Error())
require.Equal(t, 0, ps.Pos)
})
t.Run("test many", func(t *testing.T) {
_, ps := runParser("hello <world", Many(Any(Seq("<", Cut(), Chars("a-z"), ">"), Chars("a-z"))))
require.Equal(t, "offset 12: expected >", ps.Error.Error())
require.Equal(t, 0, ps.Pos)
})
t.Run("test maybe", func(t *testing.T) {
_, ps := runParser("var", Maybe(Seq("var", Cut(), "hello")))
require.Equal(t, "offset 3: expected hello", ps.Error.Error())
require.Equal(t, 0, ps.Pos)
})
}
func TestMerge(t *testing.T) {
var bracer Parser
bracer = Seq("(", Maybe(&bracer), ")")
parser := Merge(bracer)
t.Run("success", func(t *testing.T) {
result, _ := runParser("((()))", parser)
require.Equal(t, "((()))", result.Token)
})
t.Run("error", func(t *testing.T) {
_, ps := runParser("((())", parser)
require.Equal(t, "offset 5: expected )", ps.Error.Error())
require.Equal(t, 0, ps.Pos)
})
}
func TestMapShorthand(t *testing.T) {
Chars("a-z").Map(func(n *Result) {
n.Result = n.Token
})
}
func assertSequence(t *testing.T, node Result, expected ...string) {
require.NotNil(t, node)
actual := []string{}
for _, child := range node.Child {
actual = append(actual, child.Token)
}
require.Equal(t, expected, actual)
}