| 1 |
(local t (require :test.faith)) |
| 2 |
(local fennel (require :fennel)) |
| 3 |
(local friend (require :fennel.friend)) |
| 4 |
|
| 5 |
;; This can only be used to assert failures on special forms; macros will be |
| 6 |
;; expanded before this code ever sees it. |
| 7 |
(macro assert-fail [form expected] |
| 8 |
`(let [(ok# msg#) (pcall fennel.compile-string (macrodebug ,form true) |
| 9 |
{:allowedGlobals ["pairs" "next" "ipairs" "_G" "print"] |
| 10 |
:correlate true |
| 11 |
:warn #nil})] |
| 12 |
(t.is (not ok#) (.. "Expected failure: " ,(tostring form))) |
| 13 |
(t.match ,expected msg#))) |
| 14 |
|
| 15 |
;; use this only when you can't use the above macro |
| 16 |
(fn test-failures [failures] |
| 17 |
(each [code expected-msg (pairs failures)] |
| 18 |
(let [(ok? msg) (pcall fennel.compile-string code |
| 19 |
{:allowedGlobals ["pairs" "next" "ipairs" "_G"] |
| 20 |
:unfriendly true :correlate true |
| 21 |
:warn #nil})] |
| 22 |
(t.is (not ok?) (.. "Expected compiling " code " to fail.")) |
| 23 |
(t.is (msg:find expected-msg 1 true) |
| 24 |
(.. "Expected to find\n" (fennel.view expected-msg) |
| 25 |
"\n in\n" (fennel.view msg)))))) |
| 26 |
|
| 27 |
(fn test-names [] |
| 28 |
(assert-fail (local + 6) "overshadowed by a special form") |
| 29 |
(assert-fail (macro comment [] "wat") "overshadowed by a special form") |
| 30 |
(assert-fail (do each) "tried to reference a special form")) |
| 31 |
|
| 32 |
(fn test-global-fails [] |
| 33 |
(assert-fail (fn global [] 1) "overshadowed") |
| 34 |
(assert-fail (fn global-caller [] (hey)) "unknown identifier") |
| 35 |
(assert-fail (global 48 :forty-eight) "unable to bind number 48") |
| 36 |
(assert-fail (do (global good (fn [] nil)) (good) (BAD)) "BAD") |
| 37 |
(assert-fail (global let 1) "tried to reference a special form") |
| 38 |
(assert-fail (hey) "unknown identifier") |
| 39 |
(assert-fail (let [bl 8 a bcd] nil) "unknown identifier") |
| 40 |
(assert-fail (let [global 1] 1) "overshadowed") |
| 41 |
(assert-fail (do (local a-b 1) (global [a_b] [2])) |
| 42 |
"global a_b conflicts with local") |
| 43 |
(assert-fail (do (local a-b 1) (global a_b 2)) |
| 44 |
"global a_b conflicts with local") |
| 45 |
(assert-fail (do ((fn [] |
| 46 |
(require-macros :test.macros) |
| 47 |
(global x1 (->1 99 (+ 31))))) |
| 48 |
(->1 23 (+ 1))) |
| 49 |
"unknown identifier") |
| 50 |
;; strict mode applies to macro modules too |
| 51 |
(test-failures {"(import-macros t :test.bad.unknown-global)" |
| 52 |
"unknown identifier"})) |
| 53 |
|
| 54 |
(fn test-fn-fails [] |
| 55 |
(assert-fail (fn [12]) "expected symbol for function parameter") |
| 56 |
(assert-fail (fn [:huh] 4) "expected symbol for function parameter") |
| 57 |
(assert-fail (fn [] [...]) "unexpected vararg") |
| 58 |
(assert-fail (fn [false] 4) "expected symbol for function parameter") |
| 59 |
(assert-fail (fn [nil] 4) "expected symbol for function parameter") |
| 60 |
(assert-fail (fn) "expected parameters") |
| 61 |
(assert-fail (fn abc:def [x] (+ x 2)) "unexpected multi symbol abc:def") |
| 62 |
(assert-fail #[$ $...] "$ and $... in hashfn are mutually exclusive") |
| 63 |
(assert-fail #(values ...) "use $... in hashfn") |
| 64 |
(assert-fail (fn [a & b c] nil) |
| 65 |
"expected rest argument before last parameter") |
| 66 |
(assert-fail (fn [...] (+ ...)) "tried to use vararg with operator") |
| 67 |
(assert-fail (fn eugh.lol []) "expected local table eugh") |
| 68 |
(test-failures {"(lambda x)" "expected arg list" |
| 69 |
"(fn [a & {3 3}] nil)" "unable to bind number 3"})) |
| 70 |
|
| 71 |
(fn test-macro-fails [] |
| 72 |
(test-failures |
| 73 |
{"(macros {:m (fn [t] `(each [mykey (pairs ,t)] (print mykey)))}) (m [])" |
| 74 |
"tried to bind mykey without gensym" |
| 75 |
"(macros {:m (fn [t] `(fn [xabc] (+ xabc 9)))}) ((m 4))" |
| 76 |
"tried to bind xabc without gensym" |
| 77 |
"(macros {:m (fn [y] `(let [x 1] (+ x ,y)))}) (m 4)" |
| 78 |
"tried to bind x without gensym" |
| 79 |
"(macros {:foo {:bar (fn [] `(print :test))}})" |
| 80 |
"expected each macro to be function" |
| 81 |
"(macro m [] (getmetatable :foo)) (m)" |
| 82 |
"Illegal metatable" |
| 83 |
"(import-macros test :test.macros) (test.asdf)" |
| 84 |
"macro not found in imported macro module" |
| 85 |
;; macros should shadow locals as values, not just when calling: |
| 86 |
"(let [t {:b 2}] (import-macros t :test.macros) t.b)" |
| 87 |
"tried to reference a macro" |
| 88 |
"(import-macros {: asdf} :test.macros)" |
| 89 |
"macro asdf not found in module test.macros" |
| 90 |
"(import-macros m :test.bad.macro-no-return-table)" |
| 91 |
"expected macros to be table" |
| 92 |
"(macros {:noop #nil} {:identity #$})" "Expected one table argument" |
| 93 |
"(macro xyz [t] ,t)" "tried to use unquote outside quote" |
| 94 |
"(macros (do :BORK))" "Expected one table argument"})) |
| 95 |
|
| 96 |
(fn test-binding-fails [] |
| 97 |
(assert-fail (let [x {:foo (fn [self] self.bar) :bar :baz}] x:foo) |
| 98 |
"multisym method calls may only be in call position") |
| 99 |
(assert-fail (local () 1) "at least one value") |
| 100 |
(assert-fail (set abc:def 2) "cannot set method sym") |
| 101 |
(assert-fail (let [nil 1] 9) "unable to bind") |
| 102 |
(assert-fail (let [[a & c d] [1 2]] c) |
| 103 |
"rest argument before last parameter") |
| 104 |
(assert-fail (local abc&d 19) "invalid character: &") |
| 105 |
(assert-fail (set a 19) "expected local a") |
| 106 |
(assert-fail (set a.b 2) "expected local a") |
| 107 |
(assert-fail (let [pairs #(pairs $)] pairs) "aliased by a local") |
| 108 |
(assert-fail (let [x 1] (set-forcibly! x 2) (set x 3) x) "expected var") |
| 109 |
(assert-fail (set) "Compile error: expected name and value") |
| 110 |
(assert-fail (do (set [a b c] [1 2 3]) (+ a b c)) "expected local") |
| 111 |
(assert-fail (let [:x 1] 1) "unable to bind") |
| 112 |
(assert-fail (let [next #(next $)] print) "aliased by a local") |
| 113 |
(assert-fail (let [x 1 y] 8) "expected even number of name/value bindings") |
| 114 |
(assert-fail (let [false 1] 9) "unable to bind boolean false") |
| 115 |
(assert-fail (let [b 9 q (.)] q) "Compile error: expected table") |
| 116 |
(assert-fail (local ipairs #(ipairs $)) "aliased by a local") |
| 117 |
(assert-fail (let [x 1]) "expected body") |
| 118 |
(assert-fail (let [t {:a 1}] (+ t.a BAD)) "BAD") |
| 119 |
(assert-fail (local 47 :forty-seven) "unable to bind number 47") |
| 120 |
(assert-fail (set (. 98 1) true) "needs symbol target") |
| 121 |
(assert-fail (do (var t {}) (set (. t) true)) "needs at least one key") |
| 122 |
(assert-fail (set (. FAKEGLOBAL :x) true) "unknown identifier") |
| 123 |
(assert-fail (set [(. FAKEGLOBAL :x)] [true]) "unknown identifier") |
| 124 |
(assert-fail (let [{: x &as foo} 8] 42) "could not destructure literal") |
| 125 |
(assert-fail (let [{: x &as foo} nil] 42) "could not destructure literal") |
| 126 |
(assert-fail (let [[first &as list & rest] []] true) "&as argument before last parameter") |
| 127 |
(assert-fail (let [[first "&as" list] []] true) "unable to bind string") |
| 128 |
(assert-fail (let [{(+ 1 1) v} {}] true) "expected key to be a literal") |
| 129 |
(test-failures {"(local a~b 3)" "invalid character: ~" |
| 130 |
"(let [t []] (set t.:x :y))" "malformed multisym: t.:x" |
| 131 |
"(let [t []] (set t::x :y))" "malformed multisym: t::x" |
| 132 |
"(let [t []] (set t:.x :y))" "malformed multisym: t:.x" |
| 133 |
"(let [x {:y {:foo (fn [self] self.bar) :bar :baz}}] x:y:foo)" |
| 134 |
"method must be last component of multisym: x:y:foo"})) |
| 135 |
|
| 136 |
(fn parse-fail [code] |
| 137 |
#(each [p (assert (fennel.parser code))] (assert p))) |
| 138 |
|
| 139 |
(fn test-parse-fails [] |
| 140 |
(t.error "malformed multisym" (parse-fail "(foo:)")) |
| 141 |
(t.error "malformed multisym" (parse-fail "(foo.bar:)")) |
| 142 |
(t.error "unknown:3:0: Parse error: expected closing delimiter %)" |
| 143 |
(parse-fail "(do\n\n")) |
| 144 |
(t.error "unknown:3:3: Parse error: unexpected closing delimiter %)" |
| 145 |
(parse-fail "\n\n(+))")) |
| 146 |
(t.error "mismatched closing delimiter }, expected %]" |
| 147 |
(parse-fail "(fn \n[})"))) |
| 148 |
|
| 149 |
(fn test-core-fails [] |
| 150 |
(test-failures |
| 151 |
{"\n\n(let [f (lambda []\n(local))] (f))" "unknown:4:0: " |
| 152 |
"\n\n(let [x.y 9] nil)" "unknown:3:0: Compile error in 'let': unexpected multi" |
| 153 |
"\n(when)" "unknown:2:0: Compile error in 'when'" |
| 154 |
"()" "expected a function, macro, or special" |
| 155 |
"(789)" "cannot call literal value" |
| 156 |
"(do\n\n\n(each \n[x (pairs {})] (when)))" "unknown:5:15: " |
| 157 |
"(each [k v (pairs {})] (BAD k v))" "BAD" |
| 158 |
"(f" "expected closing delimiter )" |
| 159 |
"(match [1 2 3] [a & b c] nil)" "rest argument before last parameter" |
| 160 |
"(not true false)" "expected one argument" |
| 161 |
"(print @)" "invalid character: @" |
| 162 |
"(x(y))" "expected whitespace before opening delimiter (" |
| 163 |
"(x[1 2])" "expected whitespace before opening delimiter [" |
| 164 |
"(eval-compiler (assert-compile false \"oh no\" 123))" "oh no" |
| 165 |
"(partial)" "expected a function" |
| 166 |
"(#)" "expected one argument" |
| 167 |
"\"\\!\"" "invalid escape sequence" |
| 168 |
"(doto)" "missing subject" |
| 169 |
;; validity check on iterator clauses |
| 170 |
"(each [k (do-iter) :igloo 33] nil)" "unexpected iterator clause: igloo" |
| 171 |
;; "(each [(i x) y (do-iter)] (print x))" "unexpected bindings in iterator" |
| 172 |
;; "(each [i x (y) (do-iter)] (print x))" "unexpected bindings in iterator" |
| 173 |
"(for [i 1 3 2 other-stuff] nil)" "unexpected arguments" |
| 174 |
"(do\n\n\n(each \n[x 34 (pairs {})] 21))" |
| 175 |
"unknown:5:0: Compile error in 'x': unable to bind number 34" |
| 176 |
"(faccumulate [a {} 1 2 3] (print a))" |
| 177 |
"unknown:1:0: Compile error in '1': unable to bind number 1" |
| 178 |
"(with-open [(x y z) (values 1 2 3)])" |
| 179 |
"with-open only allows symbols in bindings" |
| 180 |
"([])" "cannot call literal value table" |
| 181 |
"(let [((x)) 1] (do))" "can't nest multi-value destructuring" |
| 182 |
"(tail! (if false (print :one) (print :two)))" |
| 183 |
"Expected a function call as argument" |
| 184 |
"(tail! [])" |
| 185 |
"Expected a function call as argument" |
| 186 |
"(do (tail! (print :x)) (print :y))" |
| 187 |
"Must be in tail position" |
| 188 |
"((values))" "cannot call literal value"})) |
| 189 |
|
| 190 |
(fn test-match-fails [] |
| 191 |
(test-failures |
| 192 |
{"(match :hey true false def)" "even number of pattern/body pairs" |
| 193 |
"(match :hey)" "at least one pattern/body pair" |
| 194 |
"(match)" "missing subject" |
| 195 |
"(match :subject ((pattern)) :body)" "can't nest multi-value destructuring" |
| 196 |
"(match :subject [(pattern)] :body)" "can't nest multi-value destructuring" |
| 197 |
"(match :subject (where (where pattern)) :body)" "can't nest (where) pattern" |
| 198 |
;; (where (or)) shape is allowed |
| 199 |
"(match :subject (where (or (where pattern))) :body)" "can't nest (where) pattern" ;; perhaps this should be allowed in the future |
| 200 |
"(match :subject [(where pattern)] :body)" "can't nest (where) pattern" |
| 201 |
"(match :subject ((where pattern)) :body)" "can't nest (where) pattern" |
| 202 |
"(match :subject (or :subject x) :body)" "(or) must be used in (where) patterns" |
| 203 |
"(case :subject (= x) :body)" "(=) must be used in (where) patterns" |
| 204 |
"(match :subject [(or pattern)] :body)" "can't nest (or) pattern" |
| 205 |
"(match :subject ((or pattern)) :body)" "can't nest (or) pattern" |
| 206 |
"(match [1] (where (or [_ a] [a b]) b) :body)" "unknown identifier" |
| 207 |
"(match [1] (where (or [_ a] [a b])) b)" "unknown identifier"})) |
| 208 |
|
| 209 |
(fn test-macro [] |
| 210 |
(tset fennel.macro-loaded :test.macros nil) |
| 211 |
(let [code "(import-macros {: fail-one} :test.macros) (fail-one 1)" |
| 212 |
(ok? msg) (pcall fennel.compile-string code {:correlate true})] |
| 213 |
(t.is (not ok?)) |
| 214 |
(t.match "test.macros.fnl:3: oh no" msg) |
| 215 |
;; sometimes it's "in function f" and sometimes "in upvalue f" |
| 216 |
(t.match ".*test.macros.fnl:3: in %w+ 'def'.*" msg) |
| 217 |
(t.match ".*test.macros.fnl:4: in %w+ 'abc'.*" msg) |
| 218 |
(t.not-match "fennel.compiler" msg)) |
| 219 |
(let [(ok? msg) (pcall fennel.eval "(require-macros 100)")] |
| 220 |
(t.is (not ok?)) |
| 221 |
(t.match ".*module name must compile to string.*" msg))) |
| 222 |
|
| 223 |
(fn no-codes [s] (s:gsub "\027%[[0-9]m" "")) |
| 224 |
|
| 225 |
;; automated tests for suggestions are rudimentary because the usefulness of the |
| 226 |
;; output is so subjective. to see a full catalog of suggestions, run the script |
| 227 |
;; test/bad/friendly.sh and review that output. |
| 228 |
(fn test-suggestions [] |
| 229 |
(let [(_ msg) (pcall fennel.dofile "test/bad/set-local.fnl") |
| 230 |
(_ parse-msg) (pcall fennel.dofile "test/bad/odd-table.fnl") |
| 231 |
(_ assert-msg) (pcall fennel.eval |
| 232 |
"(eval-compiler (assert-compile nil \"bad\" 1))") |
| 233 |
(_ msg4) (pcall fennel.eval "(abc] ;; msg4") |
| 234 |
(_ msg5) (pcall fennel.eval "(let {:a 1}) ;; msg5") |
| 235 |
(_ msg6) (pcall fennel.eval "(for [:abc \n \"def t\"] nil)") |
| 236 |
(_ msg7) (pcall fennel.eval "(match) ;; msg7") |
| 237 |
(_ msg-custom-pinpoint) (pcall fennel.eval "(asdf 123)" |
| 238 |
{:error-pinpoint [">>>" "<<<"]}) |
| 239 |
(_ msg-custom-pinpoint2) (pcall fennel.eval "(asdf]" |
| 240 |
{:error-pinpoint [">>>" "<<<"]}) |
| 241 |
(_ msg-custom-pinpoint3) (pcall fennel.eval |
| 242 |
"(icollect [_ _ \n(pairs [])]\n)" |
| 243 |
{:error-pinpoint [">>>" "<<<"]})] |
| 244 |
;; use the standard prefix |
| 245 |
(t.match "^%S+:%d+:%d+: Compile error: .+" msg) |
| 246 |
(t.match "^%S+:%d+:%d+: Parse error: .+" parse-msg) |
| 247 |
;; show the raw error message |
| 248 |
(t.match "expected var x" msg) |
| 249 |
;; offer suggestions |
| 250 |
(t.match "Try declaring x using var" msg) |
| 251 |
;; show the code and point out the identifier at fault |
| 252 |
(t.match "(set x 3)" (no-codes msg)) |
| 253 |
;; parse error |
| 254 |
(t.match "{:a 1 :b 2 :c}" (no-codes parse-msg)) |
| 255 |
;; non-table AST in assertion |
| 256 |
(t.match "bad" assert-msg) |
| 257 |
;; source should be part of the error message |
| 258 |
(t.match "msg4" msg4) |
| 259 |
(t.match "msg5" msg5) |
| 260 |
(t.match "unable to bind string abc" msg6) |
| 261 |
(t.match "msg7" msg7) |
| 262 |
;; custom error pinpointing works |
| 263 |
(t.match ">>>asdf<<<" msg-custom-pinpoint) |
| 264 |
(t.match ">>>]<<<" msg-custom-pinpoint2) |
| 265 |
(t.match ">>>%(icollect" msg-custom-pinpoint3))) |
| 266 |
|
| 267 |
(fn doer [] |
| 268 |
;; this plugin does not detach in subsequent tests, so we must check that |
| 269 |
;; it only fires once, exactly for our specific test. |
| 270 |
;; https://github.com/bakpakin/Fennel/pull/427#issuecomment-1138286136 |
| 271 |
(var fired-once false) |
| 272 |
(fn [ast] |
| 273 |
(when (not fired-once) |
| 274 |
(set fired-once true) |
| 275 |
(friend.assert-compile false "test-macro-traces plugin failed successfully" ast)))) |
| 276 |
|
| 277 |
(fn test-macro-traces [] |
| 278 |
;; we want to trigger an error from inside a built-in macro and make sure we |
| 279 |
;; don't get built-in macro trace info in the error messages. |
| 280 |
(let [(_ err) (pcall fennel.eval "\n\n(match 5 b (print 5))" |
| 281 |
{:plugins [{:plugin-from :test-macro-traces |
| 282 |
:do (doer) |
| 283 |
:versions [(fennel.version:gsub "-dev" "")]}] |
| 284 |
:filename "matcher.fnl"})] |
| 285 |
(t.match "matcher.fnl:3" err)) |
| 286 |
(let [(_ err) (pcall fennel.eval "(match 5 b)")] |
| 287 |
(t.not-match "fennel.compiler.macroexpand" err))) |
| 288 |
|
| 289 |
;; This does not prevent: |
| 290 |
;; (print (local abc :def)) (can't rely on nval) |
| 291 |
;; (if (fn abc []) :yes :no) (can't prevent function from being constructed) |
| 292 |
(fn test-disallow-locals [] |
| 293 |
(assert-fail (print (local xaby 10) xaby) "can't introduce local here") |
| 294 |
(assert-fail (if (var x 10) (print x) (print x)) "can't introduce var") |
| 295 |
(assert-fail (print (local abc :def)) "can't introduce local here") |
| 296 |
(assert-fail (or (local x 10) x) "can't introduce local")) |
| 297 |
|
| 298 |
(fn test-parse-warnings [] |
| 299 |
(let [warnings []] |
| 300 |
((fennel.parser "\n\n(print\"\"token\"\")" |
| 301 |
"filename.fnl" |
| 302 |
{:warn #(table.insert warnings {:message $1 :line $4 :col $5})})) |
| 303 |
;; specifically interested in the line and column numbers |
| 304 |
(t.= [{:message "expected whitespace before string" :line 3 :col 6} |
| 305 |
{:message "expected whitespace before token" :line 3 :col 8} |
| 306 |
{:message "expected whitespace before string" :line 3 :col 13}] |
| 307 |
warnings)) |
| 308 |
(let [warnings []] |
| 309 |
((fennel.parser "(do\n (\"string\":sub 1 1)\n (\"string\"false)\n (\"string\"0))" |
| 310 |
"filename.fnl" |
| 311 |
{:warn #(table.insert warnings {:message $1 :line $4 :col $5})})) |
| 312 |
(t.= [{:message "expected whitespace before token" :line 2 :col 11} |
| 313 |
{:message "expected whitespace before token" :line 3 :col 11} |
| 314 |
{:message "expected whitespace before token" :line 4 :col 11}] |
| 315 |
warnings))) |
| 316 |
|
| 317 |
{: test-global-fails |
| 318 |
: test-fn-fails |
| 319 |
: test-binding-fails |
| 320 |
: test-macro-fails |
| 321 |
: test-match-fails |
| 322 |
: test-core-fails |
| 323 |
: test-suggestions |
| 324 |
: test-macro |
| 325 |
: test-parse-fails |
| 326 |
: test-macro-traces |
| 327 |
: test-disallow-locals |
| 328 |
: test-names |
| 329 |
: test-parse-warnings} |