jolt/test/phase6-final.janet
Yogthos c366963256 Phase 6: comprehensive compile-mode tests + bug fixes
47 new compile-mode tests: collections, math, predicates, comparison,
seq operations (map/filter/reduce/take/drop), special forms (let/if/loop/
try/quote), macros (defn/when/and/or/fn/if-let), complex nesting.

Bugs fixed:
- emit-vector-expr: use (tuple ...) instead of bare tuple
  (Janet eval treats bare tuples as function calls)
- make-symbol: / at position 0 → unqualified symbol
  (was parsing as {:ns "" :name ""})
- core-renames: add missing fn? entry
2026-06-02 16:10:11 -04:00

81 lines
3.8 KiB
Text

(use ../src/jolt/api)
(defn ct-eval [ctx s] (eval-string ctx s))
(print "Phase 6: comprehensive compile-mode tests...")
(let [ctx (init {:compile? true})]
(print " collections...")
(assert (= true (ct-eval ctx "(vector? [1 2])")) "vector?")
(assert (= true (ct-eval ctx "(map? {:a 1})")) "map?")
(assert (= true (ct-eval ctx "(fn? inc)")) "fn?")
(assert (= [1 2 3 4] (ct-eval ctx "(conj [1 2 3] 4)")) "conj")
(assert (= 1 (ct-eval ctx "(first [1 2 3])")) "first")
(assert (= [2 3] (ct-eval ctx "(rest [1 2 3])")) "rest")
(assert (= 1 (ct-eval ctx "(get {:a 1 :b 2} :a)")) "get map")
(assert (= nil (ct-eval ctx "(get {:a 1} :z)")) "get missing")
(assert (= 3 (ct-eval ctx "(count {:a 1 :b 2 :c 3})")) "count map")
(assert (= [1 2 3] (ct-eval ctx "(into [1] [2 3])")) "into")
(print " core math...")
(assert (= 3 (ct-eval ctx "(+ 1 2)")) "+")
(assert (= 1 (ct-eval ctx "(- 3 2)")) "-")
(assert (= 6 (ct-eval ctx "(* 2 3)")) "*")
(assert (= 2 (ct-eval ctx "(/ 4 2)")) "/")
(assert (= 3 (ct-eval ctx "(inc 2)")) "inc")
(assert (= 1 (ct-eval ctx "(dec 2)")) "dec")
(assert (= 1 (ct-eval ctx "(quot 5 3)")) "quot")
(assert (= 2 (ct-eval ctx "(rem 5 3)")) "rem")
(assert (= 2 (ct-eval ctx "(mod 5 3)")) "mod")
(assert (= 3 (ct-eval ctx "(max 1 2 3)")) "max")
(assert (= 1 (ct-eval ctx "(min 1 2 3)")) "min")
(print " predicates...")
(assert (= true (ct-eval ctx "(nil? nil)")) "nil?")
(assert (= false (ct-eval ctx "(nil? 1)")) "nil? false")
(assert (= true (ct-eval ctx "(zero? 0)")) "zero?")
(assert (= true (ct-eval ctx "(pos? 5)")) "pos?")
(assert (= true (ct-eval ctx "(neg? -1)")) "neg?")
(assert (= true (ct-eval ctx "(even? 4)")) "even?")
(assert (= true (ct-eval ctx "(odd? 3)")) "odd?")
(assert (= false (ct-eval ctx "(not true)")) "not")
(assert (= true (ct-eval ctx "(some? 1)")) "some?")
(assert (= true (ct-eval ctx "(string? \"hello\")")) "string?")
(assert (= true (ct-eval ctx "(number? 42)")) "number?")
(assert (= true (ct-eval ctx "(keyword? :foo)")) "keyword?")
(assert (= true (ct-eval ctx "(= 1 1)")) "=")
(assert (= true (ct-eval ctx "(< 1 2)")) "<")
(assert (= true (ct-eval ctx "(> 2 1)")) ">")
(assert (= true (ct-eval ctx "(<= 1 1)")) "<=")
(assert (= true (ct-eval ctx "(>= 2 2)")) ">=")
(print " seq operations...")
(assert (= [2 3 4] (ct-eval ctx "(map inc [1 2 3])")) "map")
(assert (= [2 4] (ct-eval ctx "(filter even? [1 2 3 4])")) "filter")
(assert (= [1 3] (ct-eval ctx "(remove even? [1 2 3 4])")) "remove")
(assert (= 6 (ct-eval ctx "(reduce + [1 2 3])")) "reduce")
(assert (= [1 2 3] (ct-eval ctx "(take 3 [1 2 3 4 5])")) "take")
(assert (= [4 5] (ct-eval ctx "(drop 3 [1 2 3 4 5])")) "drop")
(print " special forms...")
(assert (= 30 (ct-eval ctx "(let [x 10 y 20] (+ x y))")) "let")
(assert (= :a (ct-eval ctx "(if true :a :b)")) "if true")
(assert (= :b (ct-eval ctx "(if false :a :b)")) "if false")
(assert (= 3 (ct-eval ctx "(loop [x 0] (if (< x 3) (recur (inc x)) x))")) "loop")
(assert (= "caught" (ct-eval ctx "(try (throw 42) (catch Exception e \"caught\"))")) "try")
(assert (= 42 (ct-eval ctx "'42")) "quote literal")
(print " macros...")
(ct-eval ctx "(defn add [a b] (+ a b))")
(assert (= 7 (ct-eval ctx "(add 3 4)")) "defn")
(assert (= 42 (ct-eval ctx "(when true 42)")) "when true")
(assert (= 3 (ct-eval ctx "(and 1 2 3)")) "and")
(assert (= 1 (ct-eval ctx "(or 1 2 3)")) "or")
(assert (= 49 (ct-eval ctx "((fn [x] (* x x)) 7)")) "fn macro")
(assert (= 2 (ct-eval ctx "(if-let [x 1] (inc x) 0)")) "if-let")
(print " complex...")
(assert (= 6 (ct-eval ctx "(let [f (fn [n] (loop [i 0 acc 0] (if (< i n) (recur (inc i) (+ acc i)) acc)))] (f 4))")) "nested")
(assert (= 15 (ct-eval ctx "(reduce + (map inc [0 1 2 3 4]))")) "reduce+map"))
(print "\nAll Phase 6 tests passed!")