-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.ls
334 lines (294 loc) · 9.23 KB
/
test.ls
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
sexpr = require \./index.js
{ parse } = sexpr
test = (name, func) ->
(require \tape) name, (t) ->
func.call t # Make `this` refer to tape's asserts
t.end! # Automatically end tests
# Because writing out all the '{ type : \list content : [ ... ] }' stuff would
# be boring and unreadable, here's a dead simple DSL for simplifying that.
convert-toplevel = ->
switch typeof! it
| \Array => it.map convert
| _ => convert it
convert = ->
switch typeof! it
| \Null => null
| \Array => type : \list content : it.map convert
| \String =>
if it instanceof Object then type : \string content : it.to-string!
else type : \atom content : it
| otherwise =>
throw Error "Test error; invalid convenience template (got #that)"
delete-location-data = ->
if it is null then return it
delete it.location
if it.type is \list then it.content.for-each delete-location-data
else if typeof! it is \Array then it.for-each delete-location-data
return it
to = (input, output, description) -->
output = convert-toplevel output
test description, ->
input
|> parse
|> delete-location-data
|> @deep-equals _, output
#
# Basics
#
'' `to` [] <| "empty input"
' \t' `to` [] <| "empty input (just whitespace)"
'a' `to` [ \a] <| "atom"
'"a"' `to` [ new String \a ] <| "string"
'()' `to` [[]] <| "empty list"
' a ' `to` [ \a ] <| "whitespace is insignificant"
'((a b c)(()()))' `to` [[[\a \b \c] [[] []]]] <| "nested lists"
'((a b c) (() ()))' `to` [[[\a \b \c] [[] []]]] <| "nested lists with spacing"
'(a\nb)' `to` [[\a \b]] <| "newlines are not part of atoms"
'(a b)(a b)' `to` [[\a \b] [\a \b]] <| "multiple forms"
'()( )(\n)' `to` [[] [] []] <| "multiple empty forms"
'#!/bin/sh' `to` [] <| "lone shebang line ignored"
'#!' `to` [] <| "lone empty shebang line ignored"
'\n#!sh' `to` [ "#!sh" ] <| "shebang line not at beginning is atom"
'#!/bin/sh\n(a)' `to` [[ \a ]] <| "shebang line followed by form"
#
# Quoting operators
#
[ [\' \quote] [\` \quasiquote] [\, \unquote] [\,@ \unquote-splicing] ]
.for-each ([c, name]) ->
"#{c}a" `to` [[name, \a]] <| "#name'd atom"
"#c\"a\"" `to` [[name, new String \a]] <| "#name'd string"
"#c()" `to` [[name, []]] <| "#name'd empty list"
"#c(a b c)" `to` [[name, [\a \b \c]]] <| "#name'd list with contents"
"(#{c}a)" `to` [[[name, \a]]] <| "#name'd atom in a list"
"(a #c b)" `to` [[\a [name, \b]]] <| "whitespaced #name"
"(a #c#c b)" `to` [[\a [name, [name, \b]]]] <| "consecutive #{name}s nest"
"(a#{c}b)" `to` [[\a [name, \b]]] <| "#{name} acts as delimiter"
test "#name with nothing to apply to is an error" ->
(-> parse "(#c)") `@throws` sexpr.SyntaxError
#
# Special characters and escaping
#
char-escape = ->
switch it
| \\n => "\\n"
| \\t => "\\t"
| \\r => "\\r"
| _ => it
[ \' \` \" \; \\ " " '"' "\n" "\t" ] .for-each (c) ->
"a\\#{c}b" `to` [ "a#{c}b" ]
<| "escaped #{char-escape c} in an atom should parse"
[ \" "\\" ] .for-each (c) ->
"\"a\\#{c}b\"" `to` [ new String "a#{c}b" ]
<| "escaped #{char-escape c} in a string should parse"
[ [\b "\b"] [\f "\f"] [\n "\n"] [\r "\r"] [\t "\t"] [\v "\v"] [\0 "\0"] ]
.for-each ([char, escapedChar]) ->
"\"a\\#{char}b\"" `to` [ new String "a#{escapedChar}b" ]
<| "strings may contain \\#{char} escape"
test "special characters work" ->
<[ + / * £ $ % ^ & あ ]>.for-each ->
it `to` [ it ] <| "special character #it works as atom"
#
# Comments
#
";hi" `to` [] <| "only 1 comment"
";hi\n;yo" `to` [] <| "only comments"
"(\n; a\n;b\n\n)" `to` [[]] <| "empty list with comments inside"
"();hi" `to` [[]] <| "comment immediately following list"
"a;hi" `to` [ "a" ] <| "comment immediately following atom"
";(a comment)" `to` [] <| "comment looking like a form"
"(a ;)\nb)" `to` [ [\a \b] ] <| "form with close-paren-looking comment between"
'("a ;)"\n)' `to` [[new String "a ;)"]] <| "can't start comment in string"
#
# Location information
#
test "lone atom loc is correct" ->
parse "hi"
(typeof! ..) `@equals` \Array
..length is 1
..0
..type `@equals` \atom
..location
..start
..offset `@equals` 0
..line `@equals` 1
..column `@equals` 1
..end
..offset `@equals` 2
..line `@equals` 1
..column `@equals` 3
test "single-line string loc is correct" ->
parse '"hi"'
(typeof! ..) `@equals` \Array
..length is 1
..0
..type `@equals` \string
..location
..start
..offset `@equals` 0
..line `@equals` 1
..column `@equals` 1
..end
..offset `@equals` 4
..line `@equals` 1
..column `@equals` 5
test "multi-line string loc is correct" ->
parse '"hi\nthere"'
(typeof! ..) `@equals` \Array
..length is 1
..0
..type `@equals` \string
..location
..start
..offset `@equals` 0
..line `@equals` 1
..column `@equals` 1
..end
..offset `@equals` 10
..line `@equals` 2
..column `@equals` 7
test "string containing escapes has correct loc" ->
parse '"\\n\\t"'
(typeof! ..) `@equals` \Array
..length is 1
..0
..type `@equals` \string
..location
..start
..offset `@equals` 0
..line `@equals` 1
..column `@equals` 1
..end
..offset `@equals` 6
..line `@equals` 1
..column `@equals` 7
test "empty list loc is correct" ->
parse '()'
(typeof! ..) `@equals` \Array
..length is 1
..0
..type `@equals` \list
..location
..start
..offset `@equals` 0
..line `@equals` 1
..column `@equals` 1
..end
..offset `@equals` 2
..line `@equals` 1
..column `@equals` 3
test "2-element list loc is correct" ->
parse '(a b)'
(typeof! ..) `@equals` \Array
..length is 1
..0
..type `@equals` \list
..location
..start
..offset `@equals` 0
..line `@equals` 1
..column `@equals` 1
..end
..offset `@equals` 5
..line `@equals` 1
..column `@equals` 6
test "2-element list content loc is correct" ->
parse '(a b)'
(typeof! ..) `@equals` \Array
..length is 1
..0
..type `@equals` \list
..content
..0
..type `@equals` \atom
..location
..start
..offset `@equals` 1
..line `@equals` 1
..column `@equals` 2
..end
..offset `@equals` 2
..line `@equals` 1
..column `@equals` 3
..1
..type `@equals` \atom
..location
..start
..offset `@equals` 3
..line `@equals` 1
..column `@equals` 4
..end
..offset `@equals` 4
..line `@equals` 1
..column `@equals` 5
test "quote atom loc matches that of the quote character" ->
[ [\' \quote] [\` \quasiquote] [\, \unquote] [\,@ \unquote-splicing] ]
.for-each ([c, name]) ~>
parse "#{c}a"
(typeof! ..) `@equals` \Array
..length is 1
..0
..type `@equals` \list
..content.0
..type `@equals` \atom
..content `@equals` name
..location
..start
..offset `@equals` 0
..line `@equals` 1
..column `@equals` 1
..end
..offset `@equals` c.length
..line `@equals` 1
..column `@equals` (1 + c.length)
test "locations of multi-form parse are correct" ->
parse '(a b) (c)'
(typeof! ..) `@equals` \Array
..length is 2
..0
..type `@equals` \list
..content
..0
..type `@equals` \atom
..location
..start
..offset `@equals` 1
..line `@equals` 1
..column `@equals` 2
..end
..offset `@equals` 2
..line `@equals` 1
..column `@equals` 3
..1
..type `@equals` \atom
..location
..start
..offset `@equals` 3
..line `@equals` 1
..column `@equals` 4
..end
..offset `@equals` 4
..line `@equals` 1
..column `@equals` 5
..1
..type `@equals` \list
..content
..0
..type `@equals` \atom
..location
..start
..offset `@equals` 7
..line `@equals` 1
..column `@equals` 8
..end
..offset `@equals` 8
..line `@equals` 1
..column `@equals` 9
#
# Form errors
#
test "closing after the end is an error" ->
(-> parse "())") `@throws` sexpr.SyntaxError
test "incomplete string is an error" ->
(-> parse '"a') `@throws` sexpr.SyntaxError
test "incomplete form due to comment is an error" ->
(-> parse '(a;)') `@throws` sexpr.SyntaxError