Debian Codegraph @

test/parser.fnl

raw
1 (local t (require :test.faith))
2 (local fennel (require :fennel))
3 (local utils (require :fennel.utils))
4
5 (fn == [a b ?msg]
6 (t.= (fennel.view a) (fennel.view b) ?msg))
7
8 (fn parse= [expected str ?msg]
9 (let [(ok? parsed) ((fennel.parser str))]
10 (t.is ok?)
11 (t.= expected parsed ?msg)))
12
13 (fn test-basics []
14 (t.= "\\" (fennel.eval "\"\\\\\""))
15 (t.= "abc\n\240" (fennel.eval "\"abc\n\\240\""))
16 (t.= "abc\"def" (fennel.eval "\"abc\\\"def\""))
17 (t.= "abc\240" (fennel.eval "\"abc\\240\""))
18 (t.= 150000 (fennel.eval "150_000"))
19 (t.= "\n5.2" (fennel.eval "\"\n5.2\""))
20 (t.= "zero" (fennel.eval "(let [_0 :zero] _0)")
21 "leading underscore should be symbol, not number")
22 (t.= "foo\nbar" (fennel.eval "\"foo\\\nbar\"")
23 "backslash+newline should be just a newline like Lua")
24 (t.= [true (fennel.sym "&abc")]
25 [((fennel.parser (fennel.string-stream "&abc ")))]))
26
27 (fn test-spicy-numbers []
28 (when math.type
29 (t.= 34 (- (fennel.eval "141791343654234")
30 (fennel.eval "141791343654200"))))
31 (t.= 1.41791343654238e+14 (fennel.eval "1.41791343654238e+14"))
32 (t.= 14179134365.125 (fennel.eval "14179134365.125"))
33 (t.match "1%.41791343654238e%+0?15"
34 (fennel.view (fennel.eval "1.41791343654238e+15")))
35 (t.match "2%.3456789012e%+0?76"
36 (fennel.view (fennel.eval (.. "23456789012" (string.rep "0" 66)))))
37 (t.match "1%.23456789e%-0?13"
38 (fennel.view (fennel.eval "1.23456789e-13")))
39 (t.= ".inf" (fennel.view (fennel.eval "1e+999999")))
40 (t.= "-.inf" (fennel.view (fennel.eval "-1e+999999")))
41 (t.= ".inf" (fennel.view (fennel.eval "+.inf")))
42 (t.= "nan" (tostring (fennel.eval ".nan")))
43 ;; ensure we consistently treat nan as symbol even on 5.1
44 (t.= :not-really (fennel.eval "(let [nan :not-really] nan)"))
45 (t.= :nah (fennel.eval "(let [-nan :nah] -nan)")))
46
47 (fn test-escapes []
48 (parse= " " "\"\\032\"")
49 (parse= " " "\"\\x20\"")
50 (parse= " " "\"\\u{20}\"")
51 (parse= "\t\n\v" "\"\\t\\n\\v\"")
52 ;; extra unicode cases
53 (parse= "\x24" "\"\\u{24}\"")
54 (parse= "\xC2\xA2" "\"\\u{a2}\"")
55 (parse= "\xE2\x82\xAC" "\"\\u{20ac}\"")
56 (parse= "\xF0\xA4\xAD\xA2" "\"\\u{24b62}\"")
57 (parse= "\x7F" "\"\\u{7f}\"")
58 (parse= "\xC2\x80" "\"\\u{80}\"")
59 (parse= "\xDF\xBF" "\"\\u{7ff}\"")
60 (parse= "\xE0\xA0\x80" "\"\\u{800}\"")
61 (parse= "\xEF\xBF\xBF" "\"\\u{ffff}\"")
62 (parse= "\xF0\x90\x80\x80" "\"\\u{10000}\"")
63 (parse= "\xF4\x8F\xBF\xBF" "\"\\u{10ffff}\""))
64
65 (fn test-comments []
66 (let [(ok? ast) ((fennel.parser (fennel.string-stream ";; abc")
67 "" {:comments true}))]
68 (t.is ok?)
69 (t.= :table (type (utils.comment? ast)))
70 (t.= ";; abc" (tostring ast)))
71 (let [code "{;; one\n1 ;; hey\n2 ;; what\n:is \"up\" ;; here\n}"
72 (ok? ast) ((fennel.parser (fennel.string-stream code)
73 "" {:comments true}))
74 mt (getmetatable ast)]
75 (== mt.comments
76 {:keys {:is [(fennel.comment ";; what")]
77 1 [(fennel.comment ";; one")]}
78 :values {2 [(fennel.comment ";; hey")]}
79 :last [(fennel.comment ";; here")]})
80 (t.= mt.keys [1 :is])
81 (t.is ok?))
82 (let [code (table.concat ["{:this table"
83 ";; has a comment"
84 ";; with multiple lines in it!!!"
85 ":and \"we don't want to lose the comments\""
86 ";; so let's keep em; all the comments are"
87 ": good ; and we want them to be kept"
88 "}"] "\n")
89 (ok? ast) ((fennel.parser (fennel.string-stream code)
90 "" {:comments true}))]
91 (t.is ok? ast)
92 (== (. (getmetatable ast) :comments :keys)
93 {:and [(fennel.comment ";; has a comment")
94 (fennel.comment ";; with multiple lines in it!!!")]
95 :good [(fennel.comment ";; so let's keep em; all the comments are")]})
96 (== (. (getmetatable ast) :comments :last)
97 [(fennel.comment "; and we want them to be kept")]))
98 (let [(_ ast) ((fennel.parser "(do\n; a\n(print))" "-" {:comments true}))]
99 (== ["do" "; a" "(print)"] (icollect [_ x (ipairs ast)] (tostring x)))
100 ;; top-level version
101 (== ["do" "; a" "(print)"]
102 (icollect [_ x (fennel.parser ":do\n; a\n(print)" "-" {:comments true})]
103 (tostring x)))))
104
105 (fn test-control-codes []
106 (for [i 1 31]
107 (let [code (.. "\"" (string.char i) (tostring i) "\"")
108 expected (.. (string.char i) (tostring i))]
109 (t.= (fennel.eval code) expected
110 (.. "Failed to parse control code " i)))))
111
112 (fn test-prefixes []
113 (let [code "\n\n`(let\n ,abc #(+ 2 3))"
114 (ok? ast) ((fennel.parser code))]
115 (t.is ok?)
116 (t.= ast.line 3)
117 (t.= (. ast 2 2 :line) 4)
118 (t.= (. ast 2 3 :line) 4)))
119
120 (fn line-col [{: line : col}] [line col])
121
122 (fn test-source-meta []
123 (let [code "\n\n ( let [x 5 \n y {:z 66}]\n (+ x y.z))"
124 (ok? ast) ((fennel.parser code))
125 [let* [_ _ _ tbl]] ast
126 [_ seq] ast]
127 (t.is ok?)
128 (t.= (line-col ast) [3 2] "line and column on lists")
129 (t.= (line-col let*) [3 5] "line and column on symbols")
130 (t.= (line-col (getmetatable seq)) [3 9]
131 "line and column on sequences")
132 (t.= (line-col (getmetatable tbl)) [4 10]
133 "line and column on tables"))
134 (let [code "abc\nxyz"
135 parser (fennel.parser code)
136 (ok? abc) (parser)
137 (ok2? xyz) (parser)]
138 (t.is (and ok? ok2?))
139 (t.= (tostring abc) (code:sub abc.bytestart abc.byteend))
140 (t.= (tostring xyz) (code:sub xyz.bytestart xyz.byteend))
141 ;; but wait! sub is tolerant of going on beyond the last byte!
142 (t.= (length code) xyz.byteend))
143 ;; now let's try that again with tables
144 (let [code "[1]\n{a 4} (true)"
145 parser (fennel.parser code)
146 (ok? seq) (parser)
147 (ok2? kv) (parser)
148 (ok3? list) (parser)
149 seq-source (getmetatable seq)
150 kv-source (getmetatable kv)]
151 (t.is (and ok? ok2? ok3?))
152 (t.= (fennel.view seq) (code:sub seq-source.bytestart seq-source.byteend))
153 (t.= (fennel.view kv) (code:sub kv-source.bytestart kv-source.byteend))
154 (t.= (fennel.view list) (code:sub list.bytestart list.byteend))
155 ;; but wait! sub is tolerant of going on beyond the last byte!
156 (t.= (length code) list.byteend))
157 (let [code " # "
158 (ok? ast) ((fennel.parser code))]
159 (t.is ok?)
160 ; (t.= (line-col ast [1 3]))
161 (t.= (tostring ast) (code:sub ast.bytestart ast.byteend))))
162
163
164 (fn test-plugin-hooks []
165 (var parse-error-called nil)
166 (let [code "(there is a parse error here (((("
167 plugin {:versions [(fennel.version:gsub "-dev" "")]
168 :parse-error #(set parse-error-called true)}]
169 (t.is (not (pcall (fennel.parser code "" {:plugins [plugin]})))
170 "parse error is expected")
171 (t.is parse-error-called "plugin wasn't called")))
172
173 {: test-basics
174 : test-spicy-numbers
175 : test-control-codes
176 : test-comments
177 : test-prefixes
178 : test-source-meta
179 : test-escapes
180 : test-plugin-hooks}