2020-04-30 00:55:35 +03:00
|
|
|
;; Generic tests on sumtypes.
|
|
|
|
|
|
|
|
(load "Test.carp")
|
|
|
|
(use Test)
|
|
|
|
|
|
|
|
(deftype A (F [(Fn [] Int)]))
|
2020-04-30 01:05:27 +03:00
|
|
|
(deftype (Nest a) (Nested [a]) (End []))
|
2020-04-30 00:55:35 +03:00
|
|
|
|
|
|
|
(def nest (Nest.Nested (Nest.Nested (Nest.Nested 123))))
|
|
|
|
|
|
|
|
(defn one-two-three [] 123)
|
|
|
|
|
|
|
|
(defn m [a]
|
|
|
|
(match a
|
|
|
|
(A.F f) (f)))
|
|
|
|
|
2020-04-30 13:45:55 +03:00
|
|
|
(deftest test
|
2020-04-30 00:55:35 +03:00
|
|
|
(assert-equal test
|
|
|
|
123
|
|
|
|
(m (A.F one-two-three))
|
|
|
|
"match adds lhs bindings to inner environments")
|
|
|
|
(assert-equal test
|
2020-04-30 13:45:55 +03:00
|
|
|
"matched!"
|
2020-04-30 01:05:27 +03:00
|
|
|
(match nest
|
|
|
|
(Nest.Nested (Nest.End))
|
|
|
|
"wrong!"
|
|
|
|
(Nest.Nested (Nest.Nested (Nest.End)))
|
|
|
|
"Also wrong!"
|
2020-04-30 00:55:35 +03:00
|
|
|
(Nest.Nested (Nest.Nested (Nest.Nested _)))
|
|
|
|
"matched!")
|
|
|
|
"Match matches nested sumtype constructors with underscores")
|
|
|
|
(assert-equal test
|
2020-04-30 13:45:55 +03:00
|
|
|
123
|
|
|
|
(match nest
|
2020-04-30 00:55:35 +03:00
|
|
|
(Nest.Nested (Nest.Nested (Nest.Nested x)))
|
|
|
|
x)
|
|
|
|
"Match matches nested sumtype constructors with variables")
|
2020-05-05 15:08:07 +03:00
|
|
|
(let [m (Maybe.Just @"hello")]
|
|
|
|
(assert-equal test
|
|
|
|
"hello"
|
2020-04-30 13:56:04 +03:00
|
|
|
(match-ref &m
|
|
|
|
(Maybe.Nothing) "wrong"
|
2020-05-05 15:08:07 +03:00
|
|
|
(Maybe.Just x ) x)
|
|
|
|
"match-ref works on simple sumtype"))
|
|
|
|
(let [n (Nest.Nested (Nest.Nested (Nest.Nested @"found")))]
|
|
|
|
(assert-equal test
|
|
|
|
"found"
|
2020-04-30 13:45:55 +03:00
|
|
|
(match-ref &n
|
2020-05-05 15:08:07 +03:00
|
|
|
(Nest.Nested (Nest.Nested (Nest.Nested s))) s)
|
|
|
|
"match-ref works on deeply nested sumtype"))
|
2020-04-30 00:55:35 +03:00
|
|
|
)
|