Carp/test/result.carp

125 lines
4.0 KiB
Plaintext
Raw Normal View History

2019-02-13 21:31:47 +03:00
(load "Test.carp")
(use-all Test Result)
(deftest test
(assert-true test
(success? &(the (Result Int (Ref String)) (Success 1)))
"success? works with Success"
)
(assert-false test
(success? &(the (Result Int (Ref String)) (Error "error")))
"success? works with Error"
)
(assert-true test
(error? &(the (Result Int (Ref String)) (Error "error")))
"error? works with Error"
)
(assert-false test
(error? &(the (Result Int (Ref String)) (Success 1)))
"error? works with Success"
)
2019-02-13 21:31:47 +03:00
(assert-equal test
&(Success 1)
&(apply (Success 0) Int.inc Int.dec)
2019-02-13 21:31:47 +03:00
"apply works with Success"
)
(assert-equal test
&(Error -1)
&(apply (Error 0) Int.inc Int.dec)
2019-02-13 21:31:47 +03:00
"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 (Ref String)) (Success 1)) Int.inc)
2019-02-13 21:31:47 +03:00
"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 (Ref String)) (Success 1))
2019-02-13 21:31:47 +03:00
(fn [x] (Success (Int.inc x))))
"and-then works with Success"
)
(assert-equal test
2
(unwrap-or-zero (the (Result Int (Ref String)) (Success 2)))
"unwrap-or-zero works with Success"
)
(assert-equal test
0
(unwrap-or-zero (Error "error"))
"unwrap-or-zero works with Error"
)
(assert-equal test
&(Error 5)
&(or-else (the (Result Int (Ref String)) (Error "error"))
(fn [x] (Error (String.length x))))
"or-else works with Error"
)
(assert-equal test
&(Success 1)
&(or-else (Success 1) (fn [x] (Error (String.length x))))
"or-else works with Success"
)
(assert-equal test
5
(unwrap-or-else (the (Result Int (Ref String)) (Error "error"))
String.length)
"unwrap-or-else works with Error"
)
(assert-equal test
1
(unwrap-or-else (Success 1) String.length)
"unwrap-or-else works with Success"
)
(assert-equal test
1
(unsafe-from-success (the (Result Int (Ref String)) (Success 1)))
"unsafe-from-success works with Success"
)
(assert-equal test
1
(from-success (the (Result Int (Ref String)) (Success 1)) 0)
"from-success works with Success"
)
(assert-equal test
0
(from-success (Error "error") 0)
"from-success works with Error"
)
(assert-equal test
"error"
&(unsafe-from-error (the (Result Int String) (Error @"error")))
"unsafe-from-Error works with Error"
)
(assert-equal test
"error"
&(from-error (the (Result Int String) (Error @"error"))
@"success")
"from-error works with Error"
)
(assert-equal test
"success"
&(from-error (Success 1) @"success")
"from-error works with Success"
)
(assert-equal test
&(Maybe.Just 1)
&(to-maybe (the (Result Int (Ref String)) (Success 1)))
"to-maybe works with Success"
)
(assert-equal test
&(Maybe.Nothing)
&(to-maybe (the (Result Int (Ref String)) (Error "error")))
"to-maybe works with Error"
)
2019-02-13 21:31:47 +03:00
)