| 1 |
(local t (require :test.faith)) |
| 2 |
(local fennel (require :fennel)) |
| 3 |
|
| 4 |
(macro == [form expected ?opts] |
| 5 |
`(let [(ok# val#) (pcall fennel.eval ,(view form) ,?opts)] |
| 6 |
(t.is ok# val#) |
| 7 |
(t.= ,expected val# ,(view form)))) |
| 8 |
|
| 9 |
(macro assert-no-iife [code ...] |
| 10 |
`(t.not-match "function _[0-9]+_" (fennel.compile-string ,(view code)) ,...)) |
| 11 |
|
| 12 |
(fn test-calculations [] |
| 13 |
(== (% 1 2 (- 1 2)) 0) |
| 14 |
(== (* 1 2 (/ 1 2)) 1) |
| 15 |
(== (*) 1) |
| 16 |
(== (+) 0) |
| 17 |
(== (+ 1 2 (- 1 2)) 2) |
| 18 |
(== (+ 1 2 (^ 1 2)) 4) |
| 19 |
(== (- 1) -1) |
| 20 |
(== (+ 99) 99) |
| 21 |
(== (* 32) 32) |
| 22 |
(== (/ 2) 0.5)) |
| 23 |
|
| 24 |
(local stderr io.stderr) |
| 25 |
(fn test-booleans [] |
| 26 |
(set io.stderr nil) ; silence deprecation warnings |
| 27 |
(== (not 39) false) |
| 28 |
(== (and true 12 "hey") "hey") |
| 29 |
(== (or false nil true 12 false) true) |
| 30 |
(== (or 11 true false) 11) |
| 31 |
(== (not true) false) |
| 32 |
(== (and 5) 5) |
| 33 |
(== (and 43 table false) false) |
| 34 |
(== (and true true (values)) true) |
| 35 |
(== (and true (values true false)) false) |
| 36 |
(== (or false (values false (do true))) true) |
| 37 |
(== (tostring (and _G.xyz (do _G.xyz.y) _G.xyz)) "nil") |
| 38 |
(== (not nil) true) |
| 39 |
(== (and true (values true false) true) true) |
| 40 |
(== (or) false) |
| 41 |
(== (and (values)) true) |
| 42 |
(== (or 5) 5) |
| 43 |
(== (and) true) |
| 44 |
;; the side effect rules are complicated |
| 45 |
(== (do (var i 1) (and true (values (set i 2) true)) i) 2) |
| 46 |
(== (do (var i 1) (and false (values (set i 2) true)) i) 1) |
| 47 |
(== (do (var i 1) (and true (values true (set i 2))) i) 2) |
| 48 |
(== (do (var i 1) (and true (values false (set i 2))) i) 1) |
| 49 |
(== (do (var i 1) (and true (do (values false (set i 2)))) i) 2) |
| 50 |
;; (and) should never return a second value under any circumstances |
| 51 |
(== (let [(x y) (and true (values true true))] y) nil) |
| 52 |
(== (let [(x y) (and true (values (values true true)))] y) nil) |
| 53 |
(== (let [(x y) (and true (#(values true true)))] y) nil) |
| 54 |
(== (let [(x y) (and true (do (values true true)))] y) nil) |
| 55 |
;; short-circuit special forms |
| 56 |
(== (let [t {:a 85}] (or true (tset t :a 1)) t.a) 85) |
| 57 |
;; short-circuit macros too |
| 58 |
(== (do (macro ts [t k v] `(tset ,t ,k ,v)) |
| 59 |
(let [t {:a 521}] (or true (ts t :a 1)) t.a)) 521) |
| 60 |
(== ((fn [...] (or (doto [...] (tset 4 4)) :never)) 1 2 3) |
| 61 |
[1 2 3 4]) |
| 62 |
;; short-circuit with methods |
| 63 |
(== (do (var i 1) (let [x {:method #(set i 2)}] (and false (: (#x) (#:method))) i)) 1) |
| 64 |
(== (do (var i 1) (let [x {:method #(set i 2)}] (and true (: (#x) (#:method))) i)) 2) |
| 65 |
(== (do (var i 1) (let [x {:method #nil}] (and false (: (#x) (#:method) (set i 2))) i)) 1) |
| 66 |
(== (do (var i 1) (let [x {:method #nil}] (and true (: (#x) (#:method) (set i 2))) i)) 2) |
| 67 |
;; address (not expr) bypassing short-circuit protections |
| 68 |
(== (do (var i 1) (fn i++! [] (set i (+ i 1)) i) (or true (not (do (i++!)))) i) |
| 69 |
1) |
| 70 |
(== (do (var i 1) (fn i++! [] (set i (+ i 1)) i) |
| 71 |
(or true (not (when true (i++!)))) |
| 72 |
i) |
| 73 |
1) |
| 74 |
(== (do (var i 1) (fn i++! [] (set i (+ i 1)) i) |
| 75 |
(or true (length (do [(i++!)]))) |
| 76 |
i) |
| 77 |
1) |
| 78 |
(== (do (var i 1) (fn i++! [] (set i (+ i 1)) i) (fn noop []) |
| 79 |
(or true (noop (not (let [x (i++!)] (* x 2))))) |
| 80 |
i) |
| 81 |
1) |
| 82 |
(== (do (var i 1) (fn i++! [] (set i (+ i 1))) (fn noop []) |
| 83 |
(or true (noop [1 2 (i++!)])) |
| 84 |
i) |
| 85 |
1) |
| 86 |
(== (do (var i 1) (fn i++! [] (set i (+ i 1)) i) |
| 87 |
(or true [:x :y (not (let [b (i++!)] b)) :z]) |
| 88 |
i) |
| 89 |
1) |
| 90 |
(== (do (var i 1) (fn i++! [] (set i (+ i 1)) i) |
| 91 |
(or true (and (let [x (i++!)] x) true)) |
| 92 |
i) |
| 93 |
1) |
| 94 |
(== (do (var i 1) (fn i++! [] (set i (+ i 1)) i) |
| 95 |
(or true (if (= i 1) (i++!))) |
| 96 |
i) |
| 97 |
1) |
| 98 |
(== (do (var i 1) (fn i++! [] (set i (+ i 1)) i) |
| 99 |
(or true (for [j 1 1] (i++!))) |
| 100 |
i) |
| 101 |
1) |
| 102 |
(== (do (var i 1) (fn i++! [] (set i (+ i 1)) i) |
| 103 |
(or true (each [j (ipairs [:item])] (i++!))) |
| 104 |
i) |
| 105 |
1) |
| 106 |
(== (do (var i 1) (fn i++! [] (set i (+ i 1)) i) |
| 107 |
(or true (while (= i 1) (i++!))) |
| 108 |
i) |
| 109 |
1) |
| 110 |
(== (do (var i 1) |
| 111 |
(or true (set i 2)) |
| 112 |
i) |
| 113 |
1) |
| 114 |
(== (let [t {:field 1}] |
| 115 |
(or true (tset t :field 2)) |
| 116 |
t.field) |
| 117 |
1) |
| 118 |
(== (let [t {}] |
| 119 |
(or true (fn t.field [])) |
| 120 |
t.field) |
| 121 |
nil) |
| 122 |
(== (do (var i 1) |
| 123 |
(or (lua "i = i + 1" "true") (lua "i = i + 1" "true")) |
| 124 |
i) |
| 125 |
2) |
| 126 |
(set io.stderr stderr)) |
| 127 |
|
| 128 |
(fn test-comparisons [] |
| 129 |
(== (= 1 1 2 2) false) |
| 130 |
(== (<= 88 32) false) |
| 131 |
(== (> -4 89) false) |
| 132 |
(== (not= 6 6 9) true) |
| 133 |
(== (> 2 0 -1) true) |
| 134 |
(== (~= 33 1) true) ; undocumented backwards-compat alias |
| 135 |
(== (< -4 89) true) |
| 136 |
(== (<= 5 1 91) false) |
| 137 |
(== (>= 22 (+ 21 1)) true) |
| 138 |
(== (not= 33 1) true) |
| 139 |
(== (> 2 0) true) |
| 140 |
(== (let [tbl {} |
| 141 |
f (fn [] (tset tbl :dbl (+ 1 (or (. tbl :dbl) 0))) 1)] |
| 142 |
(< 0 (f) 2) (. tbl :dbl)) 1)) |
| 143 |
|
| 144 |
(fn test-functions [] |
| 145 |
(== ((fn [a b c] (+ a b c)) 1 (values 8 2)) 11) |
| 146 |
(== ((fn [x] (* x 2)) 26) 52) |
| 147 |
(== ((fn f [a sin cos radC cx cy x y limit dis] sin) 8 529) 529) |
| 148 |
|
| 149 |
(== (do |
| 150 |
(fn func [] {:map #$1}) |
| 151 |
(macro foo [] `(let [a# (func)] ((. a# :map) 123))) |
| 152 |
(foo) |
| 153 |
:yeah) |
| 154 |
"yeah") |
| 155 |
|
| 156 |
(== ((lambda [[x &as t]] t) [10]) [10]) |
| 157 |
(== ((lambda [x ...] (+ x 2)) 4) 6) |
| 158 |
(== ((lambda [x _ y] (+ x y)) 4 5 6) 10) |
| 159 |
(== ((lambda [x] (+ x 2)) 4) 6) |
| 160 |
|
| 161 |
(== (if (= nil ((fn [a]) 1)) :pass :fail) "pass") |
| 162 |
(== (if (= nil ((lambda [a]) 1)) :lambda-works) "lambda-works") |
| 163 |
|
| 164 |
(== (let [(ok e) (pcall (lambda [x] (+ x 2)))] |
| 165 |
(string.match e "Missing argument x")) |
| 166 |
"Missing argument x") |
| 167 |
(== (let [(ok val) (pcall (λ [?x] (+ (or ?x 1) 8)))] (and ok val)) 9) |
| 168 |
(== (let [add (fn [x y z] (+ x y z)) f2 (partial add 1 2)] (f2 6)) 9) |
| 169 |
(== (let [add (fn [x y] (+ x y)) add2 (partial add)] (add2 99 2)) 101) |
| 170 |
(== (let [add (fn [x y] (+ x y)) inc (partial add 1)] (inc 99)) 100) |
| 171 |
(== (let [f (fn [x y f2] (+ x (f2 y))) |
| 172 |
f2 (fn [x y] (* x (+ 2 y))) |
| 173 |
f3 (fn [f] (fn [x] (f 5 x)))] |
| 174 |
(f 9 5 (f3 f2))) 44) |
| 175 |
(== (let [f (partial #(doto $1 (table.insert $2)) [])] |
| 176 |
(f 1) (f 2) (f 3)) [1 2 3]) |
| 177 |
(== (let [f (partial + (math.random 10))] |
| 178 |
(= (f 1) (f 1) (f 1))) true) |
| 179 |
(== (let [t {:x 1} f (partial + t.x)] |
| 180 |
[(f 1) (do (set t.x 2) (f 1))]) [2 2]) |
| 181 |
(== (pcall (lambda [string] nil) 1) true) |
| 182 |
(== (do |
| 183 |
(tset (getmetatable ::) :__call (fn [s t] (. t s))) |
| 184 |
(let [res (:answer {:answer 42})] |
| 185 |
(tset (getmetatable ::) :__call nil) |
| 186 |
res)) |
| 187 |
42) |
| 188 |
(== (do |
| 189 |
(var a 11) |
| 190 |
(let [f (fn [] (set a (+ a 2)))] |
| 191 |
(f) (f) a)) |
| 192 |
15) |
| 193 |
(== ((fn [a & [b {: c}]] (string.format a (+ b c))) "haha %s" 4 {:c 3}) |
| 194 |
"haha 7") |
| 195 |
(== ((fn [& {1 _ 2 _ 3 x}] x) :one :two :three) "three") |
| 196 |
(== (tail! (select 1 :hi)) |
| 197 |
"hi") |
| 198 |
(== (if (= 1 1) (tail! (select 1 :yes)) (tail! (select 1 :no))) |
| 199 |
"yes")) |
| 200 |
|
| 201 |
(fn test-conditionals [] |
| 202 |
(== (if _G.non-existent 1 (* 3 9)) 27) |
| 203 |
(== (if false "yep" "nope") "nope") |
| 204 |
(== (if false :y true :x :trailing :condition) "x") |
| 205 |
(== (let [b :original b (if false :not-this)] (or b nil)) nil) |
| 206 |
(== (let [x 1 y 2] (if (= (* 2 x) y) "yep")) "yep") |
| 207 |
(== (let [x 3 res (if (= x 1) :ONE (= x 2) :TWO true :???)] res) "???") |
| 208 |
(== (let [x {:y 2}] (if false "yep" (< 1 x.y 3) "uh-huh" "nope")) "uh-huh") |
| 209 |
(== (do |
| 210 |
(var [a z] [0 0]) |
| 211 |
(when true (set a 192) (set z 12)) |
| 212 |
(+ z a)) 204) |
| 213 |
(== (do |
| 214 |
(var a 884) |
| 215 |
(when nil (set a 192)) |
| 216 |
a) |
| 217 |
884) |
| 218 |
(== (do |
| 219 |
(var i 0) |
| 220 |
(var s 0) |
| 221 |
(while (let [l 11] |
| 222 |
(< i l)) |
| 223 |
(set s (+ s i)) |
| 224 |
(set i (+ 1 i))) |
| 225 |
s) |
| 226 |
55) |
| 227 |
(== (do |
| 228 |
(var x 12) |
| 229 |
(if true (set x 22) 0) |
| 230 |
x) |
| 231 |
22) |
| 232 |
(== (do |
| 233 |
(when (= 12 88) |
| 234 |
(os.exit 1)) |
| 235 |
false) |
| 236 |
false) |
| 237 |
(== (while (let [f false] f) |
| 238 |
(lua :break)) |
| 239 |
nil) |
| 240 |
(== (if _G.abc :abc true 55 :else) 55) |
| 241 |
(== (select :# (if false 3)) 1)) |
| 242 |
|
| 243 |
(fn test-core [] |
| 244 |
(== (table.concat ["ab" "cde"] ",") "ab,cde") |
| 245 |
(== (do |
| 246 |
(var [x y] [3 2]) |
| 247 |
(set (x y) (do (local [x y] [(* x 3) 0]) (values x y))) |
| 248 |
(+ x y)) |
| 249 |
9) |
| 250 |
(== (+ (. {:a 93 :b 4} :a) (. [1 2 3] 2)) 95) |
| 251 |
(== (do (global a_b :global) (local a-b :local) a_b) "global" |
| 252 |
{:env {}}) |
| 253 |
(== (do (tset {} :a 1) 1) 1) |
| 254 |
(== (: {:foo (fn [self] (.. self.bar 2)) :bar :baz} :foo) "baz2") |
| 255 |
(== (do (local x#x# 90) x#x#) 90) |
| 256 |
(== (let [xx (let [xx 1] (* xx 2))] xx) 2) |
| 257 |
(== (let [t {}] (set t.a :multi) (. t :a)) "multi") |
| 258 |
(== (let [t []] (tset t :a (let [{: a} {:a :bcd}] a)) t.a) "bcd") |
| 259 |
(== (let [t {:supported-chars {:x true}} |
| 260 |
field1 :supported-chars |
| 261 |
field2 :y] |
| 262 |
(set (. t field1 field2) true) t.supported-chars.y) true) |
| 263 |
(== (let [t {} tt [[]] value-two :hehe] |
| 264 |
(var x nil) |
| 265 |
(set ((. t 1) x (. tt 1 1)) (values :lol 2 :hey)) |
| 266 |
(set (. t 2) value-two) |
| 267 |
(set [(. t 3)] [:lmao]) |
| 268 |
(set x 87) |
| 269 |
(.. x (table.concat t " ") (table.concat (. tt 1)))) |
| 270 |
"87lol hehe lmaohey") |
| 271 |
(== (do (set (. _G :dynamic-set-global?) true) _G.dynamic-set-global?) |
| 272 |
true |
| 273 |
{:env {:_G {}}}) |
| 274 |
(== (let [set-x #(do (set (. $1 :x) $2) $1)] (set-x {} :aw-yiss)) |
| 275 |
{:x :aw-yiss}) |
| 276 |
(== (let [t [8 9]] (set [(. t 1) (. t 2)] [(. t 2) (. t 1)]) t) |
| 277 |
[9 8]) |
| 278 |
(== (let [x 17] (. 17)) 17) |
| 279 |
(== (let [my-tbl {} k :key] (tset my-tbl k :val) my-tbl.key) "val") |
| 280 |
(== (let [t {} _ (tset t :a 84)] (. t :a)) 84) |
| 281 |
(== (let [x 3 y nil z 293] z) 293) |
| 282 |
(== (let [k 5 t {: k}] t.k) 5) |
| 283 |
(== (let [t [[21]]] (+ (. (. t 1) 1) (. t 1 1))) 42) |
| 284 |
(== (let [t []] (table.insert t "lo") (. t 1)) "lo") |
| 285 |
(== (pcall #(each [e {}] nil)) false) |
| 286 |
(== (do |
| 287 |
(var x 12) ;; (set x 99) |
| 288 |
x) |
| 289 |
12 {:scope (fennel.scope)}) |
| 290 |
(== (do (var x 1) (let [_ (set x 92)] x)) 92) |
| 291 |
(== (do (var n 0) (let [f (fn [] (set n 96))] (f) n)) 96) |
| 292 |
(== (do (var i 0) (each [_ ((fn [] (pairs [1])))] (set i 1)) i) 1) |
| 293 |
(== (do (var a 0) |
| 294 |
(for [_ 1 3] |
| 295 |
(let [] |
| 296 |
(table.concat []) |
| 297 |
(set a 33))) |
| 298 |
a) 33) |
| 299 |
(== (do |
| 300 |
(local a 3) |
| 301 |
(let [b 2] |
| 302 |
(set-forcibly! a 7) |
| 303 |
(set-forcibly! b 6) |
| 304 |
(+ a b))) 13) |
| 305 |
(== (do (global x 1) (global x 284) x) 284 {:env {}}) |
| 306 |
(== (do |
| 307 |
(var a nil) |
| 308 |
(var b nil) |
| 309 |
(local ret (fn [] a)) |
| 310 |
(set (a b) (values 4 5)) |
| 311 |
(ret)) 4) |
| 312 |
(when (not _G.getfenv) |
| 313 |
(== (type _ENV) :table)) |
| 314 |
;; ensure sparse tables don't print with a ton of nils in them |
| 315 |
(t.is (not (string.find (fennel.compileString "{1 :a 999999 :b}") :nil)))) |
| 316 |
|
| 317 |
(fn test-if [] |
| 318 |
(== (if (values 1 2) 3 4) 3) |
| 319 |
(== (if (values 1) 3 4) 3) |
| 320 |
(== (if _G.nothing :no true :yes) :yes) |
| 321 |
(== (if true :haha-yesss) :haha-yesss) |
| 322 |
(== (let [x (if false 3 (values 2 5))] x) 2) |
| 323 |
(== (do (fn myfn [x y z] (+ x y z)) |
| 324 |
(myfn 1 (if 1 (values 2 5) 3) 4)) |
| 325 |
7) |
| 326 |
(== (do (fn myfn [x y z] (+ x y z)) |
| 327 |
(myfn 1 (if 1 2 3) 4)) |
| 328 |
7) |
| 329 |
(== (do (fn myfn [x y z] (+ x y z)) |
| 330 |
(myfn 1 4 (if 1 2 3))) |
| 331 |
7)) |
| 332 |
|
| 333 |
(fn test-destructuring [] |
| 334 |
(== ((fn dest [a [b c] [d]] (+ a b c d)) 5 [9 7] [2]) 23) |
| 335 |
(== ((lambda [[a & b]] (+ a (. b 2))) [90 99 4]) 94) |
| 336 |
(== (do (global (a b) ((fn [] (values 4 29)))) (+ a b)) 33 {:env {}}) |
| 337 |
(== (do (global [a b c d] [4 2 43 7]) (+ (* a b) (- c d))) 44 {:env {}}) |
| 338 |
(== (let [(a [b [c] d]) ((fn [] (values 4 [2 [1] 9])))] (+ a b c d)) 16) |
| 339 |
(== (let [(a [b [c] d]) (values 4 [2 [1] 9])] (+ a b c d)) 16) |
| 340 |
(== (let [(a b) ((fn [] (values 4 2)))] (+ a b)) 6) |
| 341 |
(== (let [({: x} y) (values {:x 10} 20)] (+ x y)) 30) |
| 342 |
(== (let [[a & b] (setmetatable {} {:__fennelrest #42})] b) 42) |
| 343 |
(== (let [[a & b] (setmetatable {} {:__fennelrest #false})] b) false) |
| 344 |
(== (let [[a [b c] d] [4 [2 43] 7]] (+ (* a b) (- c d))) 44) |
| 345 |
(== (let [[a b & c] [1 2 3 4 5]] (+ a (. c 2) (. c 3))) 10) |
| 346 |
(== (let [[a b c d] [4 2 43 7]] (+ (* a b) (- c d))) 44) |
| 347 |
(== (let [[a b c] [4 2]] (or c :missing)) "missing") |
| 348 |
(== (let [[a b] [9 2 49]] (+ a b)) 11) |
| 349 |
(== (let [x 1 x (if (= x 1) 2 3)] x) 2) |
| 350 |
(== (let [{: a & r} {:a 1 :b 2}] a) 1) |
| 351 |
(== (let [{: a & r} {:a 1 :b 2}] r) {:b 2}) |
| 352 |
(== (let [{: a : b} {:a 3 :b 5}] (+ a b)) 8) |
| 353 |
(== (let [{:a [x y z]} {:a [1 2 4]}] (+ x y z)) 7) |
| 354 |
(== (let [{:a x :b y} {:a 2 :b 4}] (+ x y)) 6) |
| 355 |
(== (let [[a & b] (setmetatable [1 2 3 4 5] |
| 356 |
{:__fennelrest |
| 357 |
(fn [t k] |
| 358 |
[((or table.unpack _G.unpack) |
| 359 |
t (+ k 1))])})] |
| 360 |
b) [3 4 5]) |
| 361 |
(== (do (local (-a -b) ((fn [] (values 4 29)))) (+ -a -b)) 33) |
| 362 |
(== (do (local [a b c &as t] [1 2 3]) (+ c (. t 2))) 5) |
| 363 |
(== (do (local {:a x &as t} {:a 2 :b 4}) (+ x t.b)) 6) |
| 364 |
(== (do |
| 365 |
(var [a [b c]] [1 [2 3]]) |
| 366 |
(set a 2) |
| 367 |
(set c 8) |
| 368 |
(+ a b c)) |
| 369 |
12) |
| 370 |
(== (do |
| 371 |
(var x 0) (each [_ [a b] (ipairs [[1 2] [3 4]])] |
| 372 |
(set x (+ x (* a b)))) |
| 373 |
x) 14) |
| 374 |
(== (let [[a b & c &as t] [1 2 3 4]] a) 1) |
| 375 |
(== (let [[a b & c &as t] [1 2 3 4]] b) 2) |
| 376 |
(== (let [[a b & c &as t] [1 2 3 4]] c) [3 4]) |
| 377 |
(== (let [[a b & c &as t] [1 2 3 4]] t) [1 2 3 4]) |
| 378 |
;; to-be-closed |
| 379 |
(when (= "Lua 5.4" _VERSION) |
| 380 |
(== (let [t {}] |
| 381 |
(with-open [t2 (setmetatable {} {:__close #(set t.closed? true)})] |
| 382 |
(set t2.what :ever)) |
| 383 |
t.closed?) |
| 384 |
true {:toBeClosed true}) |
| 385 |
(== (with-open [c (io.open "README.md")] (type c)) |
| 386 |
"userdata" {:toBeClosed true}))) |
| 387 |
|
| 388 |
(fn test-edge [] |
| 389 |
(let [env {}] |
| 390 |
(set env._G env) |
| 391 |
(== (do (local x (lua "y = 4" "6")) (* _G.y x)) 24 {: env})) |
| 392 |
(== (length [(if (= (+ 1 1) 2) (values 1 2 3 4 5) (values 1 2 3))]) 5) |
| 393 |
(== (let [(a b c d e f g) (if (= (+ 1 1) 3) |
| 394 |
nil |
| 395 |
((or table.unpack _G.unpack) [1 2 3 4 5 6 7]))] |
| 396 |
(+ a b c d e f g)) 28) |
| 397 |
(== (select "#" (if (= 1 (- 3 2)) (values 1 2 3 4 5) :onevalue)) 5) |
| 398 |
(== (let [(a b c d e f g) (if (= (+ 1 1) 2) (values 1 2 3 4 5 6 7))] |
| 399 |
(+ a b c d e f g)) 28) |
| 400 |
(== (do |
| 401 |
(local a_0_ (or (getmetatable {}) {:b-c {}})) |
| 402 |
(tset (. a_0_ :b-c) :d 12) |
| 403 |
(. a_0_ :b-c :d)) |
| 404 |
12) |
| 405 |
(== (tostring (. {} 12)) "nil") |
| 406 |
;; ensure that the over-zealous workaround for the |
| 407 |
;; (let [pairs #(pairs $)] pairs) bug doesn't affect normal code |
| 408 |
(== (do (type _G) (let [type :string] type)) "string") |
| 409 |
(== (let [(_ m) (pcall #(. 1 1))] |
| 410 |
(m:match "attempt to index a number")) |
| 411 |
"attempt to index a number") |
| 412 |
;; ensure (. (some-macro) k1 ...) doesn't allow invalid Lua output |
| 413 |
(== (do (macro identity [...] ...) (. (identity {:x 1 :y 2 :z 3}) :y)) |
| 414 |
2) |
| 415 |
(== (. (let [t (let [t {} k :a] (tset t k 123) t) k :b] |
| 416 |
(tset t k 321) |
| 417 |
t) :a) 123) |
| 418 |
(== (tostring (. :hello 12)) "nil") |
| 419 |
(== (let [t {} _ (set t.field :let-side)] t.field) "let-side") |
| 420 |
(== (do |
| 421 |
(local c1 20) |
| 422 |
(local c2 40) |
| 423 |
(fn xyz [A B] (and A B)) |
| 424 |
(xyz (if (and c1 c2) true false) 52)) |
| 425 |
52) |
| 426 |
(== (length [(values 1 2 3 4 5)]) 5) |
| 427 |
(== (let [x (if 3 4 5)] x) 4) |
| 428 |
(== (tostring (let [t {:st {:v 5 :f #(+ $.v $2)}} |
| 429 |
x (#(+ $ $2) 1 3)] |
| 430 |
(t.st:f x) |
| 431 |
nil)) "nil") |
| 432 |
(== (let [x [:a] y x] |
| 433 |
(tset (or x y) 2 :b) |
| 434 |
(. y 2)) |
| 435 |
:b) |
| 436 |
;; we have to let fn names escape into surrounding scope |
| 437 |
(== (let [f (fn abc-def [] 99)] (abc-def)) 99) |
| 438 |
(== (let [x [#(tset $1 $2 $3)] y x] |
| 439 |
(: x 1 2 :b) |
| 440 |
(. y 2)) |
| 441 |
:b) |
| 442 |
;; regression test for kv-rest codegen |
| 443 |
(== (let [{& a+b+c} {:key :value}] |
| 444 |
a+b+c.key) |
| 445 |
:value)) |
| 446 |
|
| 447 |
(fn test-hashfn [] |
| 448 |
(== (#$.foo {:foo :bar}) "bar") |
| 449 |
(== (#$2.foo.bar.baz nil {:foo {:bar {:baz :quux}}}) "quux") |
| 450 |
(== (#(+ $ 2) 3) 5) |
| 451 |
(== (#(+ $1 $2) 3 4) 7) |
| 452 |
(== (#(+ $1 45) 1) 46) |
| 453 |
(== (#(+ $3 $4) 1 1 3 4) 7) |
| 454 |
(== (#[(select :# $...) $...] :a :b :c) [3 "a" "b" "c"]) |
| 455 |
(== [(#$... 85 96)] [85 96]) |
| 456 |
(== (+ (#$ 1) (#$2 2 3)) 4) |
| 457 |
(== (let [f #(+ $ $1 $2)] |
| 458 |
(f 1 2)) 4) |
| 459 |
(== (let [f #(+ $1 45)] |
| 460 |
(f 1)) 46) |
| 461 |
(== (let [f #(do #(values $...))] |
| 462 |
(table.concat [((f) 1 2 3)])) "123") |
| 463 |
(== (let [f #(do (local a 1) (local b (+ $1 $1 a)) (+ a b))] |
| 464 |
(f 1)) 4) |
| 465 |
(== (let [t {:x 41} f #(set $.x 86)] (f t) t.x) 86)) |
| 466 |
|
| 467 |
(fn test-method-calls [] |
| 468 |
;; method calls work |
| 469 |
(== (: :hello :find :e) 2) |
| 470 |
;; method calls work on identifiers that aren't valid lua |
| 471 |
(== (let [f {:+ #(+ $2 $3 $4)}] (f:+ 1 2 9)) 12) |
| 472 |
;; method calls work non-native with no args |
| 473 |
(== (let [f {:+ #18}] (f:+)) 18) |
| 474 |
;; method calls don't double up side effects |
| 475 |
(== (do (var a 0) |
| 476 |
(let [f (fn [] (set a (+ a 1)) :hi)] (: (f) :find :h)) |
| 477 |
a) 1) |
| 478 |
;; method calls don't emit illegal semicolon |
| 479 |
(== (do (fn x [y] (y.obj:method) 77) (x {:obj {:method #$2}})) 77) |
| 480 |
;; avoid ambiguous calls |
| 481 |
(== (let [state0 {"sheep one" {} :sheep-1 {}}] |
| 482 |
(tset (. state0 "sheep one") :x state0.sheep-1.x) |
| 483 |
(tset (. state0 "sheep one") :y state0.sheep-1.y) |
| 484 |
(type state0)) :table) |
| 485 |
;; method calls work with varg |
| 486 |
(== ((fn [...] (: ... :gsub :foo :bar)) :foofoo) :barbar) |
| 487 |
(== (let [x {:foo (fn [self arg1] (.. self.bar arg1)) :bar :baz}] |
| 488 |
(x:foo :quux)) |
| 489 |
"bazquux") |
| 490 |
(== (let [x {:y {:foo (fn [self arg1] (.. self.bar arg1)) :bar :baz}}] |
| 491 |
(x.y:foo :quux)) |
| 492 |
"bazquux")) |
| 493 |
|
| 494 |
(fn test-values [] |
| 495 |
(== (let [(x y) (values :x (do :y))] [x y]) |
| 496 |
[:x :y]) |
| 497 |
(== [(values 1 2 (values 3 4) 4)] |
| 498 |
[1 2 3 4]) |
| 499 |
(assert-no-iife [(values 1 2 (values 3 4) 4)]) |
| 500 |
(== (let [(x y z Z) (values :x (do :y) (values :z (do :Z)))] [x y z Z]) |
| 501 |
[:x :y :z :Z]) |
| 502 |
(== (let [t {:field :hi} f #t] |
| 503 |
(tset (f :bork :bork) :field (values (do :BYE) (do :HI))) |
| 504 |
t) |
| 505 |
{:field :BYE}) |
| 506 |
(== (do (var i 0) (fn i++ [] (set i (+ 1 i)) i) |
| 507 |
[(values (i++) (values (do (i++)) (do (i++))))]) |
| 508 |
[2 1 3]) |
| 509 |
(== (do (macro myvalues [x y z] `(values ,x ,y ,z)) |
| 510 |
[(values 1 2 (myvalues 3 4))]) |
| 511 |
[1 2 3 4])) |
| 512 |
|
| 513 |
(fn test-pick-values [] |
| 514 |
(== [(pick-values 4 0 (#(values 1 2 3 4)))] [0 1 2 3]) |
| 515 |
(== (do (var i 0) (fn i++ [] (set i (+ 1 i)) i) |
| 516 |
[(pick-values 3 (i++) (do (i++)) (do (i++)))]) |
| 517 |
[2 1 3]) |
| 518 |
(== (select :# (pick-values 3)) |
| 519 |
3) |
| 520 |
(== [(pick-values 0 (select 1 :a :b :c))] |
| 521 |
[]) |
| 522 |
(== [(pick-values 4 :a :b :c (values :d :e))] |
| 523 |
["a" "b" "c" "d"]) |
| 524 |
;; ensure pick-values output respects nval, e.g. in middle of table literal |
| 525 |
(== [:X (pick-values 2 :Y :YY) :Z] [:X :Y :Z]) |
| 526 |
(== [:X (pick-values 0 :YY) :Y] [:X nil :Y]) |
| 527 |
(t.= "return (select(1, \"x\", \"y\", \"z\"))" |
| 528 |
(fennel.compile-string "(pick-values 1 (select 1 :x :y :z))")) |
| 529 |
(== [(if (= 1 1) (pick-values 1 (select 1 :x :y :z)) :bork)] |
| 530 |
[:x]) |
| 531 |
(== (let [ret [(pick-values 3 :x :y (do :z) :A (do :B))]] |
| 532 |
[ret (length ret)]) |
| 533 |
[[:x :y :z] 3]) |
| 534 |
(== (do (var i 0) (fn i++ [] (set i (+ i 1)) i) |
| 535 |
(doto [(pick-values 1 (i++) (i++) (i++))] |
| 536 |
(table.insert i))) |
| 537 |
[3 3]) |
| 538 |
;; Ensure (pick-values 0 ...) emits nil when needed, and keeps side effects |
| 539 |
(== (do (var i 0) (fn i++ [] (set i (+ i 1)) i) |
| 540 |
(doto [:X (pick-values 0 (i++) (i++) (i++)) :Y] |
| 541 |
;; Using tset because, in luajit and luajit only, |
| 542 |
;; (doto [:X (values) :Y] (table.insert :Z)) returns [:X 3 :Y] |
| 543 |
(tset 4 i))) |
| 544 |
[:X nil :Y 3]) |
| 545 |
(== (do (var i 0) (fn i++ [] (set i (+ i 1)) i) |
| 546 |
(doto [(pick-values 2 (i++) (select 1 (i++) :X) (values (i++) (i++)))] |
| 547 |
(table.insert i))) |
| 548 |
[1 2 4]) |
| 549 |
(== (let [pack (or table.pack #(doto [$...] (tset :n (select :# $...)))) |
| 550 |
t (pack (pick-values 3 (pick-values 1 (values :x :y :z))))] |
| 551 |
[t.n ((or _G.unpack table.unpack) t)]) |
| 552 |
[3 :x]) |
| 553 |
(== (let [pack (or table.pack #(doto [$...] (tset :n (select :# ...))))] |
| 554 |
(pack (pick-values 5 :x (do :y) (do :z)))) |
| 555 |
{1 :x 2 :y 3 :z :n 5})) |
| 556 |
|
| 557 |
(fn test-with-open [] |
| 558 |
(== (do |
| 559 |
(var fh nil) |
| 560 |
(local (ok msg) (pcall #(with-open [f (io.tmpfile)] |
| 561 |
(set fh f) |
| 562 |
(error :bork!)))) |
| 563 |
[(io.type fh) ok (msg:match :bork!)]) |
| 564 |
["closed file" false "bork!"]) |
| 565 |
(== (do |
| 566 |
(var (fh1 fh2) nil) |
| 567 |
[(with-open [f1 (io.tmpfile) f2 (io.tmpfile)] |
| 568 |
(set [fh1 fh2] [f1 f2]) |
| 569 |
(f1:write :asdf) |
| 570 |
(f1:seek :set 0) |
| 571 |
(f1:read :*a)) |
| 572 |
(io.type fh1) |
| 573 |
(io.type fh2)]) |
| 574 |
["asdf" "closed file" "closed file"]) |
| 575 |
(== [(with-open [proc1 (io.popen "echo hi") proc2 (io.popen "echo bye")] |
| 576 |
(values (proc1:read) (proc2:read)))] |
| 577 |
["hi" "bye"]) |
| 578 |
(== (do |
| 579 |
(var fh nil) |
| 580 |
(local (ok msg) (pcall #(with-open [f (io.tmpfile)] |
| 581 |
(set fh f) |
| 582 |
(error {:bork! :bark!})))) |
| 583 |
[(io.type fh) ok (case msg {:bork! :bark!} msg _ "didn't match")]) |
| 584 |
["closed file" false {:bork! :bark!}]) |
| 585 |
(== (do |
| 586 |
(var fh nil) |
| 587 |
(local (ok msg) (pcall (fn [...] |
| 588 |
(with-open [f (io.tmpfile)] |
| 589 |
(set fh f) |
| 590 |
(error [...]))) :bork! :bark!)) |
| 591 |
[(io.type fh) ok (case msg [:bork! :bark!] msg _ "didn't match")]) |
| 592 |
["closed file" false [:bork! :bark!]])) |
| 593 |
|
| 594 |
(fn test-comment [] |
| 595 |
(t.= "--[[ hello world ]]\nreturn nil" |
| 596 |
(fennel.compile-string "(comment hello world)")) |
| 597 |
(t.= "--[[ \"hello\nworld\" ]]\nreturn nil" |
| 598 |
(fennel.compile-string "(comment \"hello\nworld\")")) |
| 599 |
(t.= "--[[ \"hello]\\]lol\" ]]\nreturn nil" |
| 600 |
(fennel.compile-string "(comment \"hello]]lol\")"))) |
| 601 |
|
| 602 |
(fn test-nest [] |
| 603 |
(let [env (collect [k v (pairs _G) &into {: fennel}] k v) |
| 604 |
nested (fennel.dofile "src/fennel.fnl" {: env :compilerEnv env})] |
| 605 |
(t.= 51 (fennel.eval "((fn [] (+ 49 2)))")) |
| 606 |
(t.= fennel.version nested.version))) |
| 607 |
|
| 608 |
(fn test-sym [] |
| 609 |
(t.= "return \"f_1_auto.foo:bar\"" |
| 610 |
(fennel.compile-string |
| 611 |
"(eval-compiler (string.format \"%q\" (view `f#.foo:bar)))"))) |
| 612 |
|
| 613 |
(fn test-stable-kv-output [] |
| 614 |
(let [add-keys "(macro add-keys [t ...] |
| 615 |
(faccumulate [t t i 1 (select :# ...) 2] |
| 616 |
(let [(k v) (select i ...)] (doto t (tset k v)))))" |
| 617 |
cases [["{:a 1 :b 2 :2 :s2 2 :n2 true :btrue :true :strue}" |
| 618 |
"{a = 1, b = 2, [\"2\"] = \"s2\", [2] = \"n2\", [true] = \"btrue\", [\"true\"] = \"strue\"}" |
| 619 |
"original table literal key order should be preserved"] |
| 620 |
[(.. add-keys "\n" |
| 621 |
"(add-keys {:c 3 :a 1} :b 2 :d 4 :2 :b 2 :b1 [9] :tbl9 true :t :true :t1)") |
| 622 |
"{c = 3, a = 1, [2] = \"b1\", [true] = \"t\", [\"2\"] = \"b\", b = 2, d = 4, [\"true\"] = \"t1\", [{9}] = \"tbl9\"}" |
| 623 |
"added keys should be sorted: numbers>booleans>strings>tables>other"]]] |
| 624 |
(each [_ [input expected msg] (ipairs cases)] |
| 625 |
(t.= (: (fennel.compile-string input) :gsub "^return%s*" "") |
| 626 |
expected msg)))) |
| 627 |
|
| 628 |
{: test-booleans |
| 629 |
: test-calculations |
| 630 |
: test-comparisons |
| 631 |
: test-conditionals |
| 632 |
: test-core |
| 633 |
: test-destructuring |
| 634 |
: test-edge |
| 635 |
: test-functions |
| 636 |
: test-hashfn |
| 637 |
: test-if |
| 638 |
: test-pick-values |
| 639 |
: test-values |
| 640 |
: test-with-open |
| 641 |
: test-method-calls |
| 642 |
: test-comment |
| 643 |
: test-nest |
| 644 |
: test-sym |
| 645 |
: test-stable-kv-output |
| 646 |
} |