Carp/test/result.carp
2019-02-13 19:31:47 +01:00

111 lines
2.3 KiB
Plaintext

(load "Test.carp")
(use-all Test Result)
(deftest test
(assert-equal test
1
(apply (Success 0) Int.inc Int.dec)
"apply works with Success"
)
(assert-equal test
-1
(apply (Error 0) Int.inc Int.dec)
"apply works with Error"
)
(assert-true test
(error? &(map (Error "hi") Int.inc))
"map works with Error"
)
(assert-equal test
&(Success 2)
&(map (the (Result Int String) (Success 1)) Int.inc)
"map works with Success"
)
(assert-true test
(error? &(and-then (Error "hi") (fn [x] (Success (Int.inc x)))))
"and-then works with Error"
)
(assert-equal test
&(Success 2)
&(and-then (the (Result Int String) (Success 1))
(fn [x] (Success (Int.inc x))))
"and-then works with Success"
)
)
; (defn and-then [a f]
; (match a
; (Success x) (f x)
; (Error x) (Error x)))
;
; (defn unwrap-or-zero [a]
; (match a
; (Success x) x
; (Error _) (zero)))
;
; (defn and [a b]
; (match a
; (Success _) b
; (Error x) (Error x)))
;
; (defn or [a b]
; (match a
; (Success x) (Success x)
; (Error _) b))
;
; (defn or-else [a f]
; (match a
; (Success x) (Success x)
; (Error x) (f x)))
;
; (defn unwrap-or-else [a f]
; (match a
; (Success x) x
; (Error x) (f x)))
;
; (defn unsafe-from-success [a]
; (match a
; (Success x) x))
;
; (defn from-success [a dflt]
; (match a
; (Error _) dflt
; (Success x) x))
;
; (defn unsafe-from-error [a]
; (match a
; (Error x) x))
;
; (defn from-error [a dflt]
; (match a
; (Success _) dflt
; (Error x) x))
;
; (defn to-maybe [a]
; (match a
; (Success x) (Maybe.Just x)
; (Error _) (Maybe.Nothing)))
;
; (defn success? [a]
; (match @a
; (Error _) false
; (Success _) true))
;
; (defn error? [a]
; (match @a
; (Error _) true
; (Success _) false))
;
; (defn = [a b]
; (match @a
; (Success x)
; (match @b
; (Error _) false
; (Success y) (= x y))
; (Error x)
; (match @b
; (Success _) false
; (Error y) (= x y))))
;)