feat: complete atom implementation, new macros, predicates, and test coverage
## Runtime (src/jolt/core.janet, +637/-99) ### Atom system (complete) - core-atom: :validator and :meta options, watches table, atom-validate/atom-notify-watches helpers - New functions: swap-vals!, reset-vals!, compare-and-set!, set-validator!, get-validator, add-watch, remove-watch - Validator: blocks invalid reset!/swap!; watches: notification with old/new values; metadata support - All 7 functions registered in core-bindings ### New predicates - integer?, boolean?, list? — all registered in core-bindings ### New math - abs, rand, rand-int — registered in core-bindings ### Control flow macros (7 new) - cond, case (with one-of-many list support), condp, dotimes, while, if-not, when-first - Registered as macros: cond, case, for, dotimes, while, condp, if-not, when-first ### Threading macros (7) - -> (thread-first), ->> (thread-last), some->, some->>, cond->, cond->>, as-> - Renamed to core-thread-first/core-thread-last to avoid collision with comparison operators ### for comprehension - Full Clojure for macro: :when, :let modifiers, nested bindings - Uses map/mapcat expansion, correct last-group optimization ### Sequence operations (15 new) - last, butlast, second, ffirst, fnext, nfirst, nnext, nthnext, nthrest - drop-last, take-nth, map-indexed, keep, keep-indexed, reductions ### Nil-safety fixes - core-reverse, core-sort, core-sort-by, core-distinct now handle nil input ## Tests (7 new files, 361 assertions across all tests) ### Ported from Clojure test suite (4 files) - clojure-logic-test.janet: 82 assertions (if/and/or/not/some?/nil-punning) - clojure-control-test.janet: 56 assertions (do/when/if-let/cond/dotimes/while/loop/case) - clojure-macros-test.janet: 21 assertions (->/->>/some->/some->>/cond->/cond->>/as->) - clojure-for-test.janet: 4 assertions (:when/:let/nested for) ### Systematic coverage - clojure-atom-test.janet: 31 assertions (creation/reset!/swap!/swap-vals!/reset-vals!/CAS/validator/watches/meta) - systematic-coverage.janet: 120+ assertions (predicates/math/comparison/logic/collections/seq-ops/destructuring) - logic-test.janet: earlier ported logic tests ### Existing tests updated - lazy-test.janet, phase7-test.janet: updated for nil-safe core functions ## Result 47 test files, all passing. 19 new functions/macros implemented. Clojure atom semantics complete: validators, watches, metadata, CAS, swap-vals!/reset-vals!.
This commit is contained in:
parent
82bce033a5
commit
e2e7fa1447
12 changed files with 1462 additions and 98 deletions
100
test/logic-test.janet
Normal file
100
test/logic-test.janet
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
(use ../src/jolt/api)
|
||||
(defn ct-eval [ctx s] (eval-string ctx s))
|
||||
(print "Ported Logic Tests (from clojure/test-clojure/logic.clj)")
|
||||
|
||||
(print "1: test-if true/false/nil...")
|
||||
(let [ctx (init)]
|
||||
(assert (= :t (ct-eval ctx "(if true :t)")) "if true")
|
||||
(assert (= :t (ct-eval ctx "(if true :t :f)")) "if true with else")
|
||||
(assert (= nil (ct-eval ctx "(if false :t)")) "if false no else")
|
||||
(assert (= :f (ct-eval ctx "(if false :t :f)")) "if false with else")
|
||||
(assert (= nil (ct-eval ctx "(if nil :t)")) "if nil no else")
|
||||
(assert (= :f (ct-eval ctx "(if nil :t :f)")) "if nil with else"))
|
||||
(print " ok")
|
||||
|
||||
(print "2: test-if zero/empty is true...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(= :t (if 0 :t :f))")) "0 is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if 0.0 :t :f))")) "0.0 is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if \"\" :t :f))")) "empty string is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if () :t :f))")) "empty list is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if [] :t :f))")) "empty vector is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if {} :t :f))")) "empty map is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if #{} :t :f))")) "empty set is true"))
|
||||
(print " ok")
|
||||
|
||||
(print "3: test-if anything except nil/false is true...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(= :t (if 42 :t :f))")) "42 is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if 1.5 :t :f))")) "float is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if \"abc\" :t :f))")) "string is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if 'abc :t :f))")) "symbol is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if :kw :t :f))")) "keyword is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if '(1 2) :t :f))")) "list is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if [1 2] :t :f))")) "vector is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if {:a 1 :b 2} :t :f))")) "map is true")
|
||||
(assert (= true (ct-eval ctx "(= :t (if #{1 2} :t :f))")) "set is true"))
|
||||
(print " ok")
|
||||
|
||||
(print "4: test-nil-punning...")
|
||||
(let [ctx (init)]
|
||||
(assert (= :yes (ct-eval ctx "(if (first []) :no :yes)")) "first [] nil")
|
||||
(assert (= :yes (ct-eval ctx "(if (next [1]) :no :yes)")) "next [1] nil")
|
||||
(assert (= :no (ct-eval ctx "(if (rest [1]) :no :yes)")) "rest [1] non-nil")
|
||||
(assert (= :yes (ct-eval ctx "(if (seq nil) :no :yes)")) "seq nil")
|
||||
(assert (= :yes (ct-eval ctx "(if (seq []) :no :yes)")) "seq [] nil")
|
||||
(assert (= :no (ct-eval ctx "(if (lazy-seq nil) :no :yes)")) "lazy-seq nil non-nil")
|
||||
(assert (= :no (ct-eval ctx "(if (lazy-seq []) :no :yes)")) "lazy-seq [] non-nil"))
|
||||
(print " ok")
|
||||
|
||||
(print "5: test-and...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(and)")) "and empty")
|
||||
(assert (= true (ct-eval ctx "(and true)")) "and true")
|
||||
(assert (= nil (ct-eval ctx "(and nil)")) "and nil")
|
||||
(assert (= false (ct-eval ctx "(and false)")) "and false")
|
||||
(assert (ct-eval ctx "(= \"abc\" (and 1 true :kw 'abc \"abc\"))") "and chain last")
|
||||
(assert (= nil (ct-eval ctx "(and 1 true nil 'abc \"abc\")")) "and chain nil")
|
||||
(assert (= false (ct-eval ctx "(and 1 true 'abc \"abc\" false)")) "and chain false"))
|
||||
(print " ok")
|
||||
|
||||
(print "6: test-or...")
|
||||
(let [ctx (init)]
|
||||
(assert (= nil (ct-eval ctx "(or)")) "or empty")
|
||||
(assert (= true (ct-eval ctx "(or true)")) "or true")
|
||||
(assert (= nil (ct-eval ctx "(or nil)")) "or nil")
|
||||
(assert (= false (ct-eval ctx "(or false)")) "or false")
|
||||
(assert (= true (ct-eval ctx "(or nil false true)")) "or nil false true")
|
||||
(assert (= 1 (ct-eval ctx "(or nil false 1 2)")) "or picks first truthy")
|
||||
(assert (ct-eval ctx "(= \"abc\" (or nil false \"abc\" :kw))") "or picks string")
|
||||
(assert (= nil (ct-eval ctx "(or false nil)")) "or false nil -> nil")
|
||||
(assert (= false (ct-eval ctx "(or nil false)")) "or nil false -> false")
|
||||
(assert (= false (ct-eval ctx "(or nil nil nil false)")) "or chain to false")
|
||||
(assert (= true (ct-eval ctx "(or nil true false)")) "or nil true false"))
|
||||
(print " ok")
|
||||
|
||||
(print "7: test-not...")
|
||||
(let [ctx (init)]
|
||||
(assert (= true (ct-eval ctx "(not nil)")) "not nil")
|
||||
(assert (= true (ct-eval ctx "(not false)")) "not false")
|
||||
(assert (= false (ct-eval ctx "(not true)")) "not true")
|
||||
(assert (= false (ct-eval ctx "(not 0)")) "not 0")
|
||||
(assert (= false (ct-eval ctx "(not 42)")) "not 42")
|
||||
(assert (= false (ct-eval ctx "(not \"\")")) "not empty string")
|
||||
(assert (= false (ct-eval ctx "(not \"abc\")")) "not string")
|
||||
(assert (= false (ct-eval ctx "(not ())")) "not empty list")
|
||||
(assert (= false (ct-eval ctx "(not [])")) "not empty vector")
|
||||
(assert (= false (ct-eval ctx "(not {})")) "not empty map")
|
||||
(assert (= false (ct-eval ctx "(not #{})")) "not empty set"))
|
||||
(print " ok")
|
||||
|
||||
(print "8: test-some?...")
|
||||
(let [ctx (init)]
|
||||
(assert (= false (ct-eval ctx "(some? nil)")) "some? nil")
|
||||
(assert (= true (ct-eval ctx "(some? false)")) "some? false")
|
||||
(assert (= true (ct-eval ctx "(some? 0)")) "some? 0")
|
||||
(assert (= true (ct-eval ctx "(some? \"abc\")")) "some? string")
|
||||
(assert (= true (ct-eval ctx "(some? [])")) "some? empty vec"))
|
||||
(print " ok")
|
||||
|
||||
(print "\nAll Ported Logic tests passed!")
|
||||
Loading…
Add table
Add a link
Reference in a new issue