Files
rose-ash/spec/tests/test-canonical.sx
giles 8bba02fbc9 Use match for value dispatch in evaluator and compiler
Convert large cond chains doing string equality dispatch to use the
match special form: step-eval-list (42 arms), step-continue (31 arms),
compile-list (30 arms), ho-setup-dispatch (7 arms), value-matches-type?
(10 arms). Also fix test-canonical.sx to use defsuite/deftest format
and load canonical.sx in both test runners.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 07:53:16 +00:00

98 lines
2.8 KiB
Plaintext

(defsuite
"canonical-serialize"
(deftest "nil" (assert= (canonical-serialize nil) "nil"))
(deftest
"booleans"
(assert= (canonical-serialize true) "true")
(assert= (canonical-serialize false) "false"))
(deftest
"integers"
(assert= (canonical-serialize 0) "0")
(assert= (canonical-serialize 42) "42")
(assert= (canonical-serialize -7) "-7"))
(deftest
"strings"
(assert= (canonical-serialize "hello") "\"hello\"")
(assert= (canonical-serialize "") "\"\"")
(assert= (canonical-serialize "a\"b") "\"a\\\"b\""))
(deftest
"symbols"
(assert= (canonical-serialize (quote deref)) "deref")
(assert= (canonical-serialize (quote swap!)) "swap!"))
(deftest
"keywords"
(assert= (canonical-serialize (make-keyword "class")) ":class")
(assert= (canonical-serialize (make-keyword "arity")) ":arity"))
(deftest "empty list" (assert= (canonical-serialize (list)) "()"))
(deftest
"flat list"
(assert= (canonical-serialize (list 1 2 3)) "(1 2 3)"))
(deftest
"nested list"
(assert=
(canonical-serialize
(list
(quote div)
(make-keyword "class")
"flex"
(list (quote h2) "title")))
"(div :class \"flex\" (h2 \"title\"))"))
(deftest
"dict keys sorted"
(let
((d {:zebra 1 :middle 3 :alpha 2}))
(assert= (canonical-serialize d) "{:alpha 2 :middle 3 :zebra 1}")))
(deftest
"dict with nested values"
(let
((d {:b "hello" :a (list 1 2)}))
(assert= (canonical-serialize d) "{:a (1 2) :b \"hello\"}"))))
(defsuite
"content-id"
(deftest
"same expression same CID"
(assert= (content-id (list 1 2 3)) (content-id (list 1 2 3))))
(deftest
"different expression different CID"
(assert
(not (= (content-id (list 1 2 3)) (content-id (list 1 2 4))))))
(deftest
"CID is a hex string"
(let
((cid (content-id 42)))
(assert (string? cid))
(assert= (len cid) 64)))
(deftest
"short CID is 16 chars"
(let ((cid (content-id-short 42))) (assert= (len cid) 16)))
(deftest
"short CID is prefix of full CID"
(let
((full (content-id 42)) (short (content-id-short 42)))
(assert= short (slice full 0 16)))))
(defsuite
"bytecode-module"
(deftest
"make and query"
(let
((m (make-bytecode-module 1 "abc123" (list 1 2 3))))
(assert (bytecode-module? m))
(assert= (bytecode-module-version m) 1)
(assert= (bytecode-module-source-hash m) "abc123")))
(deftest
"non-module fails predicate"
(assert (not (bytecode-module? (list 1 2 3))))
(assert (not (bytecode-module? "hello")))))
(defsuite
"provenance"
(deftest
"make provenance record"
(let
((p (make-provenance "src-cid" "bc-cid" "v1" "js")))
(assert= (first p) (quote provenance))
(assert= (nth p 2) "src-cid")
(assert= (nth p 4) "bc-cid"))))