Carp/test/sumtypes.carp

52 lines
1.4 KiB
Plaintext

;; Generic tests on sumtypes.
(load "Test.carp")
(use Test)
(deftype A (F [(Fn [] Int)]))
(deftype (Nest a) (Nested [a]) (End []))
(def nest (Nest.Nested (Nest.Nested (Nest.Nested 123))))
(defn one-two-three [] 123)
(defn m [a]
(match a
(A.F f) (f)))
(deftest test
(assert-equal test
123
(m (A.F one-two-three))
"match adds lhs bindings to inner environments")
(assert-equal test
"matched!"
(match nest
(Nest.Nested (Nest.End))
"wrong!"
(Nest.Nested (Nest.Nested (Nest.End)))
"Also wrong!"
(Nest.Nested (Nest.Nested (Nest.Nested _)))
"matched!")
"Match matches nested sumtype constructors with underscores")
(assert-equal test
123
(match nest
(Nest.Nested (Nest.Nested (Nest.Nested x)))
x)
"Match matches nested sumtype constructors with variables")
(let [m (Maybe.Just @"hello")]
(assert-equal test
"hello"
(match-ref &m
(Maybe.Nothing) "wrong"
(Maybe.Just x ) x)
"match-ref works on simple sumtype"))
(let [n (Nest.Nested (Nest.Nested (Nest.Nested @"found")))]
(assert-equal test
"found"
(match-ref &n
(Nest.Nested (Nest.Nested (Nest.Nested s))) s)
"match-ref works on deeply nested sumtype"))
)