jolt/test/spec/multimethods-spec.janet
Yogthos a24e9bfba0 test(spec): state, multimethods, protocols — fix reify, get-method, hierarchies
Add spec suites for stateful refs (atoms/volatiles/delays/promises), multimethods
(dispatch + hierarchies), and protocols/types/records. Bugs caught and fixed:
- reify with multiple methods registered only the first (stepped i by 2 over
  one-element specs); now collects every method spec
- get-method/methods/remove-method evaluated their arg to the dispatch fn, but a
  multimethod's methods live on its var — now resolve the var (get-method and
  methods are special forms; remove-method fixed). get-method/methods added.
- no global hierarchy: 2-arg derive modified a throwaway, isa? was hardcoded
  false, parents missing, ancestors/descendants returned arrays (so contains?
  failed), and dispatch ignored derive. Now a global hierarchy backs the 1/2-arg
  derive/isa?/parents/ancestors/descendants (returning sets) and multimethod
  dispatch falls back to it.

Corrected a phase5 port assertion that encoded the old isa? bug.
conformance 218/218, jpm test green.
2026-06-05 00:24:18 -04:00

28 lines
1.6 KiB
Text

# Specification: multimethods & hierarchies.
(use ../support/harness)
(defspec "multimethods / dispatch"
["dispatch on value" "\"two\""
"(do (defmulti f identity) (defmethod f 1 [_] \"one\") (defmethod f 2 [_] \"two\") (f 2))"]
["dispatch on keyword fn" "\"circle\""
"(do (defmulti area :shape) (defmethod area :circle [_] \"circle\") (area {:shape :circle}))"]
[":default method" "\"other\""
"(do (defmulti f identity) (defmethod f 1 [_] \"one\") (defmethod f :default [_] \"other\") (f 99))"]
["no match throws" :throws
"(do (defmulti f identity) (defmethod f 1 [_] \"one\") (f 99))"]
["multiple args" "5"
"(do (defmulti g (fn [a b] a)) (defmethod g :add [_ b] b) (g :add 5))"]
["get-method" "\"one\""
"(do (defmulti f identity) (defmethod f 1 [_] \"one\") ((get-method f 1) 1))"]
["remove-method" :throws
"(do (defmulti f identity) (defmethod f 1 [_] \"one\") (remove-method f 1) (f 1))"])
(defspec "multimethods / hierarchies"
["derive + isa?" "true" "(do (derive ::child ::parent) (isa? ::child ::parent))"]
["isa? reflexive" "true" "(isa? ::x ::x)"]
["isa? unrelated" "false" "(do (derive ::a ::b) (isa? ::a ::c))"]
["parents" "true" "(do (derive ::c ::p) (contains? (parents ::c) ::p))"]
["ancestors" "true" "(do (derive ::c ::p) (derive ::p ::g) (contains? (ancestors ::c) ::g))"]
["descendants" "true" "(do (derive ::c ::p) (contains? (descendants ::p) ::c))"]
["dispatch via hierarchy" "\"animal\""
"(do (derive ::dog ::animal) (defmulti speak identity) (defmethod speak ::animal [_] \"animal\") (speak ::dog))"])