jolt/test/cljs-port-test.janet
Yogthos 0e08f65016 Phases 15-16: SCI bootstrap, Janet interop, eval, lazy-cat, CLJS ported tests
- SCI bootstrap complete: all 9 SCI source files load (317 forms, 0 failures)
- prefer-method/remove-method/remove-all-methods promoted to special forms
- eval special form (interpreter + compiler) with eval-test.janet
- lazy-cat macro with structural equality tests in lazy-test.janet
- Janet-native interop via . special form on tables/structs:
  field access (. obj :key), method calls (. obj method args...)
  fn* form compilation support, .- reader sugar
  interop-test.janet with 7 test sections (14 assertions)
- New core bindings: with-meta, var-dynamic?, load-string
- ^:dynamic def handler, core-str nil handling, core-meta for with-meta
- 7 new CLJS ported test files: cljs-port-6 through -10, cljs-core-test, cljs-collections-test
- test-sci-runtime.janet verifies SCI namespaces/types/Var/IBox/IVar
- 317/317 tests pass, 0 failing scripts, 440+ assertions across 31 test files
- README updated with Janet interop documentation
2026-06-03 23:44:49 -04:00

131 lines
5.8 KiB
Text

(use ../src/jolt/api)
(defn ct-eval [ctx s] (eval-string ctx s))
(print "CLJS Ported Tests")
(print "1: math...")
(let [ctx (init)]
(assert (= 3 (ct-eval ctx "(+ 1 2)")) "+")
(assert (= 1 (ct-eval ctx "(- 3 2)")) "-")
(assert (= 6 (ct-eval ctx "(* 2 3)")) "*")
(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 " ok")
(print "2: predicates...")
(let [ctx (init)]
(assert (= true (ct-eval ctx "(nil? nil)")) "nil?")
(assert (= false (ct-eval ctx "(nil? 1)")) "nil? false")
(assert (= false (ct-eval ctx "(not true)")) "not")
(assert (= true (ct-eval ctx "(not false)")) "not false")
(assert (= true (ct-eval ctx "(some? 1)")) "some?")
(assert (= true (ct-eval ctx "(number? 42)")) "number?")
(assert (= true (ct-eval ctx "(fn? inc)")) "fn?")
(assert (= true (ct-eval ctx "(keyword? :foo)")) "keyword?")
(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?"))
(print " ok")
(print "3: comparison...")
(let [ctx (init)]
(assert (= true (ct-eval ctx "(= 1 1)")) "=")
(assert (= true (ct-eval ctx "(not= 1 2)")) "not= true")
(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 " ok")
(print "4: vectors...")
(let [ctx (init)]
(assert (= true (ct-eval ctx "(= :a (nth [:a :b :c :d] 0))")) "nth")
(assert (= true (ct-eval ctx "(= 4 (count (conj [1 2 3] 4)))")) "conj count")
(assert (= true (ct-eval ctx "(= 1 (first [1 2 3]))")) "first")
(assert (= true (ct-eval ctx "(= 2 (count (rest [1 2 3])))")) "rest count")
(assert (= true (ct-eval ctx "(= 3 (count [1 2 3]))")) "count"))
(print " ok")
(print "5: maps...")
(let [ctx (init)]
(assert (= true (ct-eval ctx "(= 1 (get {:a 1} :a))")) "get")
(assert (= true (ct-eval ctx "(nil? (get {:a 1} :z))")) "get missing")
(assert (= true (ct-eval ctx "(= :d (get {:a 1} :z :d))")) "get default")
(assert (= true (ct-eval ctx "(= 2 (count (assoc {:a 1} :b 2)))")) "assoc count")
(assert (= true (ct-eval ctx "(= 1 (count (dissoc {:a 1 :b 2} :a)))")) "dissoc count")
(assert (= true (ct-eval ctx "(contains? {:a 1} :a)")) "contains?")
(assert (= true (ct-eval ctx "(= 3 (count {:a 1 :b 2 :c 3}))")) "count")
(assert (= true (ct-eval ctx "(= 2 (count (keys {:a 1 :b 2})))")) "keys"))
(print " ok")
(print "6: sets...")
(let [ctx (init)]
(assert (= true (ct-eval ctx "(set? #{1 2 3})")) "set?")
(assert (= true (ct-eval ctx "(= 4 (count (conj #{1 2 3} 4)))")) "conj count")
(assert (= true (ct-eval ctx "(= 2 (count (disj #{1 2 3} 3)))")) "disj count")
(assert (= true (ct-eval ctx "(= 3 (count #{1 2 3}))")) "count")
(assert (= true (ct-eval ctx "(= #{1 2 3} #{3 2 1})")) "= order"))
(print " ok")
(print "7: seq ops...")
(let [ctx (init)]
(assert (= true (ct-eval ctx "(nil? (seq []))")) "seq empty")
(assert (= true (ct-eval ctx "(= 3 (count (map inc [1 2 3])))")) "map count")
(assert (= true (ct-eval ctx "(= 2 (count (filter odd? [1 2 3 4])))")) "filter count")
(assert (= true (ct-eval ctx "(= 6 (reduce + [1 2 3]))")) "reduce")
(assert (= true (ct-eval ctx "(= 3 (count (take 3 [1 2 3 4 5])))")) "take count")
(assert (= true (ct-eval ctx "(= 2 (count (drop 3 [1 2 3 4 5])))")) "drop count")
(assert (= true (ct-eval ctx "(= 3 (count (reverse [1 2 3])))")) "reverse count")
(assert (= true (ct-eval ctx "(every? even? [2 4 6])")) "every?"))
(print " ok")
(print "8: atoms...")
(let [ctx (init)]
(assert (= true (ct-eval ctx "(= 0 (deref (atom 0)))")) "deref")
(assert (= true (ct-eval ctx "(= 1 (let [a (atom 0)] (swap! a inc) (deref a)))")) "swap!")
(assert (= true (ct-eval ctx "(atom? (atom 0))")) "atom?"))
(print " ok")
(print "9: special forms...")
(let [ctx (init)]
(assert (= true (ct-eval ctx "(= 30 (let [x 10 y 20] (+ x y)))")) "let")
(assert (= true (ct-eval ctx "(= :a (if true :a :b))")) "if true")
(assert (= true (ct-eval ctx "(= :b (if false :a :b))")) "if false")
(assert (= true (ct-eval ctx "(= 2 (do 1 2))")) "do")
(assert (= true (ct-eval ctx "(= 3 (loop [x 0] (if (< x 3) (recur (inc x)) x)))")) "loop")
(assert (= true (ct-eval ctx "(= :caught (try (throw 42) (catch Exception e :caught)))")) "try"))
(print " ok")
(print "10: macros...")
(let [ctx (init)]
(ct-eval ctx "(defn add [a b] (+ a b))")
(assert (= true (ct-eval ctx "(= 7 (add 3 4))")) "defn")
(assert (= true (ct-eval ctx "(= 42 (when true 42))")) "when")
(assert (= true (ct-eval ctx "(= 3 (and 1 2 3))")) "and")
(assert (= true (ct-eval ctx "(= 1 (or 1 2 3))")) "or")
(assert (= true (ct-eval ctx "(= 49 ((fn [x] (* x x)) 7))")) "fn"))
(print " ok")
(print "11: higher-order...")
(let [ctx (init)]
(assert (= true (ct-eval ctx "(= 3 ((comp inc inc) 1))")) "comp")
(assert (= true (ct-eval ctx "(= 3 ((partial + 1 2)))")) "partial")
(assert (= true (ct-eval ctx "(= 5 ((constantly 5) :anything))")) "constantly")
(assert (= true (ct-eval ctx "(= 3 (identity 3))")) "identity"))
(print " ok")
(print "12: constructors...")
(let [ctx (init)]
(assert (= true (ct-eval ctx "(= 3 (count (vector 1 2 3)))")) "vector count")
(assert (= true (ct-eval ctx "(= 2 (count (hash-map :a 1 :b 2)))")) "hash-map count")
(assert (= true (ct-eval ctx "(= 3 (count (hash-set 1 2 3)))")) "hash-set count")
(assert (= true (ct-eval ctx "(= 3 (count (zipmap [:a :b :c] [1 2 3])))")) "zipmap count"))
(print " ok")
(print "\nAll CLJS Ported Tests passed!")