Debian Codegraph @

test/fennelview.fnl

raw
1 (local t (require :test.faith))
2 (local {: view &as fennel} (require :fennel))
3 (local {: generate} (fennel.dofile "test/generate.fnl"))
4
5 (fn count [t] (accumulate [c 0 _ (pairs t)] (+ c 1)))
6
7 (fn table= [a b deep=]
8 (let [miss-a []
9 miss-b []]
10 (each [k (pairs a)]
11 (when (deep= (. a k) (. b k))
12 (tset a k nil)
13 (tset b k nil)))
14 (each [k v (pairs a)]
15 (when (not= (type k) :table)
16 (tset miss-a (view k) v)))
17 (each [k v (pairs b)]
18 (when (not= (type k) :table)
19 (tset miss-b (view k) v)))
20 (or (= (count a) (count b))
21 (deep= miss-a miss-b))))
22
23 (fn deep= [a b]
24 (if (or (not= a a) (not= b b)) true
25 (= (type a) (type b) :table) (table= a b deep=)
26 (= (tostring a) (tostring b))))
27
28 (fn pp-list [x pp opts indent]
29 (values (icollect [i v (ipairs x)]
30 (let [v (pp v opts (+ 1 indent) true)]
31 (if (= i 1) (.. "(" v)
32 (= i (length x)) (.. " " v ")")
33 (.. " " v))))
34 true))
35
36 (fn test-generated []
37 (for [_ 1 16]
38 (let [item (generate 1)
39 viewed (view item)
40 round-tripped (fennel.eval viewed)]
41 ;; you would think assertEquals would work here but it doesn't!
42 ;; it is easy to confuse it with randomly generated tables.
43 (t.is (deep= item round-tripped) viewed))))
44
45 (fn test-userdata-handling []
46 (let [view-target {:my-userdata io.stdout}
47 expected-with-mt "{:my-userdata \"HI, I AM USERDATA\"}"
48 expected-without-mt "^%{%:my%-userdata %#%<file %([x0-9a-f]+%)%>%}$"]
49 (t.match expected-without-mt (view view-target {:one-line? true}) true)
50 (tset (getmetatable io.stdout) :__fennelview #"\"HI, I AM USERDATA\"")
51 (t.= (view view-target {:one-line? true})
52 expected-with-mt)
53 (tset (getmetatable io.stdout) :__fennelview nil)))
54
55 (fn padded-string [s]
56 ;; Transforms a string:
57 ;;
58 ;; "[foo
59 ;; | bar
60 ;; | baz]"
61 ;;
62 ;; Into:
63 ;;
64 ;; "[foo
65 ;; bar
66 ;; baz]"
67 ;;
68 ;; Preserves any whitespace after `|`, but removes any whitespace
69 ;; before this symbol, so the printed representation looks correct:
70 ;;
71 ;; [foo
72 ;; bar
73 ;; baz]
74 (-> (icollect [line (s:gmatch "([^\n]+)")]
75 (line:gsub "^%s*|" ""))
76 (table.concat "\n")))
77
78 (fn test-ast []
79 (let [l1 (fennel.list 1 2 3)]
80 (t.= "[[1 (1 2 3) 2]]" (view [[1 l1 2]]))
81 (t.= "(a {} [1 2])" (eval-compiler (view (view '(a {} [1 2])))))
82 (t.= "{:abc [(1 2 3)]}" (view {:abc [l1]}))
83 (t.= "[(1 2 3)]" (view [l1] {:one-line? true}))
84 (t.= "{:abc [(1 2 3)]}"
85 (view {:abc [(fennel.list 1 2 3)]} {:one-line? true}))
86 (t.= "[(\"a\" \"a b\" [1 2 3] {:a (1 2 3) :b {}})]"
87 (let [l2 (fennel.list "a" "a b" [1 2 3] {:a l1 :b []})]
88 (view [l2])))))
89
90 (fn test-numbers []
91 (t.= "123" (view 123))
92 (t.= "2.4" (view 2.4))
93 (t.= "1e+308" (view (fennel.eval (string.rep "9" 308))))
94 (t.= ".inf" (view (fennel.eval (string.rep "9" 309))))
95 (t.= ".inf" (view (fennel.eval "(/ 1 0)")))
96 (t.= "-.inf" (view (fennel.eval "(/ -1 0)")))
97 (t.= ".inf" (view (fennel.eval ".inf")))
98 (t.= "-.inf" (view (fennel.eval "-.inf")))
99 (t.= ".nan" (view (fennel.eval ".nan")))
100 (t.match ":pi%s+3.1415926" (fennel.view math))
101 (when math.type
102 (t.= "-9187201950435737472" (view (fennel.eval "-9187201950435737472")))
103 (t.= "141791343654238" (view (fennel.eval "141791343654238"))))
104 (t.= "1.4179134365423e+14" (view (fennel.eval "1.4179134365423e+14"))))
105
106 (fn test-cycles []
107 (let [t1 {:a 1 :b 2}
108 t2 {:tbl [1 :b] :foo 19}
109 sparse [:abc]]
110 (set t1.t t1)
111 (tset t2.tbl 3 t2)
112 (tset sparse 4 sparse)
113 (t.= (view t1) "@1{:a 1 :b 2 :t @1{...}}")
114 (t.= (view t2) "@1{:foo 19 :tbl [1 \"b\" @1{...}]}")
115 (t.= (view sparse {:max-sparse-gap 10}) "@1[\"abc\" nil nil @1[...]]")
116 (t.= "@1[@1[...]]"
117 (let [v1 []]
118 (table.insert v1 v1)
119 (view v1)))
120 (t.= "@1{:t1 @1{...}}"
121 (let [t1 {}]
122 (set t1.t1 t1)
123 (view t1)))
124 (t.= "@1{:t2 {:t1 @1{...}}}"
125 (let [t1 {}
126 t2 {:t1 t1}]
127 (set t1.t2 t2)
128 (view t1)))
129 (t.= "@1{@1{...} @1{...}}"
130 (let [t1 {}] (tset t1 t1 t1) (view t1)))
131 (t.= "[[[[[[[[[[...]]]]]]]]]]"
132 (let [v1 []]
133 (table.insert v1 v1)
134 (view v1 {:detect-cycles? false
135 :one-line? true
136 :depth 10}))))
137 (t.= (padded-string
138 "@1[1
139 | 2
140 | 3
141 | @2[1
142 | 2
143 | @1[...]]
144 | [1
145 | 2
146 | @2[...]]]")
147 (let [v1 [1 2 3]
148 v2 [1 2 v1]
149 v3 [1 2 v2]]
150 (table.insert v1 v2)
151 (table.insert v1 v3)
152 (view v1 {:line-length 1})))
153 (t.= "{{{{...} {...}} {{...} {...}}} {{{...} {...}} {{...} {...}}}}"
154 (let [t1 []]
155 (tset t1 t1 t1)
156 (view t1 {:detect-cycles? false :one-line? true :depth 4})))
157 (let [v1 []
158 v2 [v1]
159 v3 [v1 v2]
160 v4 [v2 v3]
161 v5 [v3 v4]
162 v6 [v4 v5]
163 v7 [v5 v6]
164 v8 [v6 v7]
165 v9 [v7 v8]
166 v10 [v8 v9]
167 v11 [v9 v10]]
168 (table.insert v1 v2)
169 (table.insert v1 v3)
170 (table.insert v1 v4)
171 (table.insert v1 v5)
172 (table.insert v1 v6)
173 (table.insert v1 v7)
174 (table.insert v1 v8)
175 (table.insert v1 v9)
176 (table.insert v1 v10)
177 (table.insert v1 v11)
178 (t.= (padded-string
179 "@1[@2[@1[...]]
180 | @3[@1[...] @2[...]]
181 | @4[@2[...] @3[...]]
182 | @5[@3[...] @4[...]]
183 | @6[@4[...] @5[...]]
184 | @7[@5[...] @6[...]]
185 | @8[@6[...] @7[...]]
186 | @9[@7[...] @8[...]]
187 | @10[@8[...] @9[...]]
188 | [@9[...] @10[...]]]")
189 (view v1))
190 (table.insert v2 v11)
191 (t.= (padded-string
192 "@1[@2[@1[...]
193 | @3[@4[@5[@6[@7[@1[...] @2[...]] @8[@2[...] @7[...]]]
194 | @9[@8[...] @6[...]]]
195 | @10[@9[...] @5[...]]]
196 | @11[@10[...] @4[...]]]]
197 | @7[...]
198 | @8[...]
199 | @6[...]
200 | @9[...]
201 | @5[...]
202 | @10[...]
203 | @4[...]
204 | @11[...]
205 | @3[...]]")
206 (view v1))))
207
208 (fn test-newline []
209 (let [s "hello\nworld!\n"]
210 (t.= (view s) "\"hello\nworld!\n\"")
211 (t.= (view s {:escape-newlines? true})
212 "\"hello\\nworld!\\n\"")))
213
214 (fn test-escapes []
215 (t.= (view ["\a" "\t"]) "[\"\\a\" \"\\t\"]")
216 (t.= (view "[\7-\13]") "\"[\\a-\\r]\"")
217 (t.= (view (string.char 27)) "\"\\027\""
218 "view should default string escapes to decimal")
219 (t.= (view ["\027" "\a"] {:byte-escape #(: "\\x%2x" :format $)})
220 "[\"\\x1b\" \"\\a\"]")
221 (t.= (view ["\027" "\a"] {:byte-escape #(: "\\%03o" :format $)})
222 "[\"\\033\" \"\\a\"]"))
223
224 (fn test-gaps []
225 (t.= "{1 1 3 3}" (view {1 1 3 3}))
226 (t.= (view {967216353 788}) "{967216353 788}"))
227
228 (fn test-utf8 []
229 (when _G.utf8
230 ;; make sure everything produced is valid utf-8
231 (for [_ 1 100]
232 (var x [])
233 (for [_ 1 100]
234 (table.insert x (string.char (math.random 0 255))))
235 (set x (view (table.concat x)))
236 (t.is (_G.utf8.len x) (.. "invalid utf-8: " x "\"")))
237 ;; make sure valid utf-8 doesn't get escaped
238 (for [_ 1 100]
239 (let [x []]
240 (for [_ 1 100]
241 (table.insert x (_G.utf8.char (if (= 0 (math.random 0 1))
242 (math.random 0x80 0xd7ff)
243 (math.random 0xe000 0x10ffff)))))
244 (t.not-match "\\" (view (table.concat x)))))
245 ;; validate utf-8 length
246 ;; this one is a little weird. since the only place utf-8 length is
247 ;; exposed is within the indentation code, we have to generate some
248 ;; fixed-size string to put in an object, then verify the output's
249 ;; length to be another fixed size.
250 (for [_ 1 100]
251 (let [x ["æ"]]
252 (for [_ 1 100]
253 (table.insert x (_G.utf8.char (if (= 0 (math.random 0 1))
254 (math.random 0x80 0xd7ff)
255 (math.random 0xe000 0x10ffff)))))
256 (t.= 112 (_G.utf8.len (view {(table.concat x) [1 2]})))))))
257
258 (fn test-seq-comments []
259 ;; a sequence containing a comment as its last item should have its closing
260 ;; delimiter on a new line.
261 (let [(_ok? ast) ((fennel.parser (fennel.string-stream "(print [1\n;hi\n])")
262 "" {:comments true}))]
263 (t.= (view ast) "(print [1\n ;hi\n ])"))
264 ;; a sequence containing a comment should print on multiple lines.
265 (let [(_ok? ast) ((fennel.parser (fennel.string-stream "(print [1;hi\n2])")
266 "" {:comments true}))]
267 (t.= (view ast) "(print [1\n ;hi\n 2])")))
268
269 (fn test-once-skip-opts []
270 (t.= (view "a" {:prefer-colon? {:once true}}) ":a")
271 (t.= (view ["a"] {:prefer-colon? {:once true}}) "[\"a\"]")
272 (t.= (view ["a" ["b"] "c"]
273 {:prefer-colon? {:once true :after {:once true}}})
274 "[:a [\"b\"] :c]")
275 (t.= (view ["a" ["b" ["c"]] "d"]
276 {:prefer-colon? {:once true :after {:once true :after {:once true}}}})
277 "[:a [:b [\"c\"]] :d]")
278 (t.= (view ["a" ["b" ["c" ["d"] "e"] "f"] "g"]
279 {:prefer-colon? {:once true :after {:once true :after {:once true}}}})
280 "[:a [:b [\"c\" [\"d\"] \"e\"] :f] :g]")
281 (let [vector (fn [...]
282 (->> {:__fennelview
283 (fn [t view opts indent]
284 (view t (doto opts
285 (tset :metamethod?
286 {:once false :after opts.metamethod?})
287 (tset :empty-as-sequence?
288 {:once true :after opts.empty-as-sequence?}))
289 indent))}
290 (setmetatable [...])))]
291 (t.= (view (vector)) "[]")
292 (t.= (view (vector [])) "[{}]")
293 (t.= (view (vector (vector))) "[[]]")))
294
295 (fn test-strings []
296 (t.= "\"123\"" (view "123"))
297 (t.= "\"123 \\\"456\\\" 789\"" (view "123 \"456\" 789"))
298 (t.= "\"ваыв\"" (view "ваыв")))
299
300 (fn test-sequence []
301 (t.= "{}" (view []))
302 (t.= "[]" (view [] {:empty-as-sequence? true}))
303 (t.= "[1 2 3]" (view [1 2 3]))
304 (t.= "[0\n 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n 10]"
305 (view [0 1 2 3 4 5 6 7 8 9 10] {:line-length 5}))
306 (t.= "[1 2 3]" (view [1 2 3] {:preprocess (fn [x] x)}))
307 (t.= (padded-string
308 "{:a
309 | [1
310 | 2]
311 | :b
312 | [1
313 | 2]
314 | :c
315 | [1
316 | 2]
317 | :d
318 | [1
319 | 2]}")
320 (view {:a [1 2] :b [1 2] :c [1 2] :d [1 2]} {:line-length 0}))
321 (t.= "[2 3 4]"
322 (view [1 2 3]
323 {:preprocess (fn [x] (if (= (type x) :number) (+ x 1) x))})))
324
325 (fn test-kv-table []
326 (t.= "{:a 1 \"a b\" 2}" (view {:a 1 "a b" 2}))
327 (t.= "{:a\n 1\n :b\n 52}" (view {:a 1 :b 52} {:line-length 1}))
328 (t.= "{:a 1 :b 5}" (view {:a 1 :b 5} {:one-line? true :line-length 1}))
329 (t.= "{1 1 15 5}" (view {1 1 15 5}))
330 (t.= "[{}]" (view [{}]))
331 (t.= "[{} {}]" (view (let [t {}] [t t]) {:detect-cycles? false}))
332 (t.= "{[[]] {[[]] [[[]]]}}" (view {[[]] {[[]] [[[]]]}}
333 {:empty-as-sequence? true}))
334 (t.= "[1\n 2\n [3 4]]" (view [1 2 [3 4]] {:line-length 7}))
335 (t.= "{{:a 1} {:b 2 :c 3}}" (view {{:a 1} {:b 2 :c 3}}))
336 (t.= "{-2 1}" (view {-2 1}))
337 (t.= (padded-string
338 "{\"ваыв\"
339 | {4
340 | {\"ваыв\" 5}
341 | 6
342 | \"aoeuaoeu\"}
343 | [1]
344 | [2 [3]]}")
345 (view {[1] [2 [3]] :ваыв {4 {:ваыв 5} 6 :aoeuaoeu}}
346 {:line-length 15}))
347 (t.= "{:a [1 2 3 4 5 6 7] :b [1 2 3 4 5 6 7] :c [1 2 3 4 5 6 7] :d [1 2 3 4 5 6 7]}"
348 (view {:a [1 2 3 4 5 6 7]
349 :b [1 2 3 4 5 6 7]
350 :c [1 2 3 4 5 6 7]
351 :d [1 2 3 4 5 6 7]}))
352 (t.= (padded-string
353 "[{:aaa
354 | [1
355 | 2
356 | 3]}]")
357 (view [{:aaa [1 2 3]}] {:line-length 0}))
358 (t.= (padded-string
359 "@1{:a
360 | 1
361 | :b
362 | @1{...}
363 | :c
364 | 2}")
365 (let [t1 {:a 1 :c 2}]
366 (set t1.b t1)
367 (view t1 {:line-length 1})))
368 (t.= (padded-string
369 "@1{:a 1
370 | :b @1{...}
371 | :c 2}")
372 (let [t1 {:a 1 :c 2}]
373 (set t1.b t1)
374 (view t1 {:line-length 10})))
375 (t.= (padded-string
376 "@1{:a
377 | 1
378 | :b
379 | [1
380 | @1{...}
381 | 2
382 | 3]
383 | :c
384 | 2}")
385 (let [t1 {:a 1 :c 2}
386 v1 [1 2 3]]
387 (set t1.b v1)
388 (table.insert v1 2 t1)
389 (view t1 {:line-length 1})))
390 (t.= "{0 1}" (view {0 1}))
391 (t.= "{-2 1 1 -2}" (view {-2 1 1 -2}))
392 (t.= "[1 nil nil nil 5]" (view {1 1 5 5} {:max-sparse-gap 5}))
393 (t.= "{:a \"b\"}"
394 (view (setmetatable {} {:__pairs #(values next {:a :b} nil)})))
395 (t.= "{11 1}" (view {11 1}))
396 (t.= (padded-string
397 "{:a [1 2 3 4 5 6 7 8]
398 | :b [1 2 3 4 5 6 7 8]
399 | :c [1 2 3 4 5 6 7 8]
400 | :d [1 2 3 4 5 6 7 8]}")
401 (view {:a [1 2 3 4 5 6 7 8]
402 :b [1 2 3 4 5 6 7 8]
403 :c [1 2 3 4 5 6 7 8]
404 :d [1 2 3 4 5 6 7 8]}))
405 (t.= (padded-string
406 "{:a
407 | [1 2 3 4 5 6 7 8]
408 | :b
409 | [1 2 3 4 5 6 7 8]
410 | :c
411 | [1 2 3 4 5 6 7 8]
412 | :d
413 | [1 2 3 4 5 6 7 8]}")
414 (view {:a [1 2 3 4 5 6 7 8]
415 :b [1 2 3 4 5 6 7 8]
416 :c [1 2 3 4 5 6 7 8]
417 :d [1 2 3 4 5 6 7 8]}
418 {:line-length 19}))
419 (t.= (padded-string
420 "{\"ƁƁƁ\"
421 | {\"ƁƁƁ\"
422 | {}
423 | \"ǍǍǍ\"
424 | {}}
425 | \"ǍǍǍ\"
426 | {}}")
427 (view {:ǍǍǍ {}
428 :ƁƁƁ {:ǍǍǍ {} :ƁƁƁ {}}}
429 {:line-length 1}))
430 (t.= "{1 1 5 5 :n 5}" (view {1 1 5 5 :n 5}))
431 (t.= "{1 \"a\" 1.2345 \"combination on my luggage\"}"
432 (view {1 :a 1.2345 "combination on my luggage"}))
433 (t.error ".*max%-sparse%-gap must be an integer.*"
434 #(view {1.1 :a 2.1 :b 3.1 :c} {:max-sparse-gap 1.1})
435 "max-sparse-gap does not allow decimals"))
436
437 (fn test-metamethod []
438 (let [mt (setmetatable [] {:__fennelview (fn [] "META")})]
439 (t.= (view mt) "META"))
440 (t.= (padded-string
441 "(:a
442 | \"a b\"
443 | [1 2 3]
444 | {:a
445 | (1
446 | 2
447 | 3)
448 | :b
449 | {}})")
450 (let [l1 (setmetatable [1 2 3] {:__fennelview pp-list})
451 l2 (setmetatable ["a" "a b" [1 2 3] {:a l1 :b []}]
452 {:__fennelview pp-list})]
453 (view l2)))
454 (t.= (.. "[\":colon \\\"quote\\\" \\\"depends\\\"\" \":colon "
455 "\\\"quote\\\" :depends\" \":colon \\\"quote\\\" \\\"depends\\\"\"]")
456 (let [styles (setmetatable [:colon :quote :depends]
457 {:__fennelview
458 #(icollect [_ s (ipairs $1)]
459 ($2 s $3 $4 (when (not= s :depends)
460 (= s :colon))))})]
461 (view [(view styles)
462 (view styles {:prefer-colon? true})
463 (view styles {:prefer-colon? false})]
464 {:one-line? true})))
465 (t.= "[\"empty-table\" [1] {:x \"empty-table\" :empty-table [2]}]"
466 (view [[] [1] {:x [] [] [2]}]
467 {:preprocess (fn [x] (if (and (= (type x) :table) (= (next x) nil))
468 :empty-table x))})))
469
470 (fn test-colon []
471 (t.= "{\"@foo.bar\" 42}" (view {"@foo.bar" 42})))
472
473 {: test-numbers
474 : test-strings
475 : test-sequence
476 : test-kv-table
477 : test-generated
478 : test-newline
479 : test-metamethod
480 : test-userdata-handling
481 : test-ast
482 : test-cycles
483 : test-escapes
484 : test-gaps
485 : test-utf8
486 : test-colon
487 : test-seq-comments
488 : test-once-skip-opts}