Carp/test/result.carp

143 lines
4.5 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
2019-03-26 12:23:27 +03:00
(success? &(the (Result Int String) (Success 1)))
"success? works with Success"
)
(assert-false test
2019-03-26 12:23:27 +03:00
(success? &(the (Result Int String) (Error @"error")))
"success? works with Error"
)
(assert-true test
2019-03-26 12:23:27 +03:00
(error? &(the (Result Int String) (Error @"error")))
"error? works with Error"
)
(assert-false test
2019-03-26 12:23:27 +03:00
(error? &(the (Result Int String) (Success 1)))
"error? works with Success"
)
2019-02-13 21:31:47 +03:00
(assert-equal test
&(Success 1)
2020-01-14 00:36:20 +03:00
&(apply (Success 0) &Int.inc &Int.dec)
2019-02-13 21:31:47 +03:00
"apply works with Success"
)
(assert-equal test
&(Error -1)
2020-01-14 00:36:20 +03:00
&(apply (Error 0) &Int.inc &Int.dec)
2019-02-13 21:31:47 +03:00
"apply works with Error"
)
(assert-true test
2020-01-14 00:36:20 +03:00
(error? &(map (Error @"hi") &Int.inc))
2019-02-13 21:31:47 +03:00
"map works with Error"
)
(assert-equal test
&(Success 2)
2020-01-14 00:36:20 +03:00
&(map (the (Result Int String) (Success 1)) &Int.inc)
2019-02-13 21:31:47 +03:00
"map works with Success"
)
2020-01-22 14:12:17 +03:00
(assert-true test
(success? &(map-error (Success @"hi") &Int.inc))
"map-error works with Success"
)
(assert-equal test
&(Error 2)
&(map-error (the (Result String Int) (Error 1)) &Int.inc)
"map-error works with Error"
)
2019-02-13 21:31:47 +03:00
(assert-true test
2020-01-14 00:36:20 +03:00
(error? &(and-then (Error @"hi") &(fn [x] (Success (Int.inc x)))))
2019-02-13 21:31:47 +03:00
"and-then works with Error"
)
(assert-equal test
&(Success 2)
2019-03-26 12:23:27 +03:00
&(and-then (the (Result Int String) (Success 1))
2020-01-14 00:36:20 +03:00
&(fn [x] (Success (Int.inc x))))
2019-02-13 21:31:47 +03:00
"and-then works with Success"
)
(assert-equal test
2
2019-03-26 12:23:27 +03:00
(unwrap-or-zero (the (Result Int String) (Success 2)))
"unwrap-or-zero works with Success"
)
(assert-equal test
0
2019-03-26 16:30:00 +03:00
(unwrap-or-zero (Error @"error"))
"unwrap-or-zero works with Error"
)
(assert-equal test
&(Error 5)
2019-03-26 12:23:27 +03:00
&(or-else (the (Result Int String) (Error @"error"))
2020-01-14 00:36:20 +03:00
&(fn [x] (Error (String.length &x))))
"or-else works with Error"
)
(assert-equal test
&(Success 1)
2020-01-14 00:36:20 +03:00
&(or-else (Success 1) &(fn [x] (Error (String.length &x))))
"or-else works with Success"
)
(assert-equal test
5
2019-03-26 12:23:27 +03:00
(unwrap-or-else (the (Result Int String) (Error @"error"))
2020-01-14 00:36:20 +03:00
&(fn [s] (String.length &s)))
"unwrap-or-else works with Error"
)
(assert-equal test
1
2020-01-14 00:36:20 +03:00
(unwrap-or-else (Success 1) &(fn [s] (String.length &s)))
"unwrap-or-else works with Success"
)
(assert-equal test
1
2019-03-26 12:23:27 +03:00
(unsafe-from-success (the (Result Int String) (Success 1)))
"unsafe-from-success works with Success"
)
(assert-equal test
1
2019-03-26 12:23:27 +03:00
(from-success (the (Result Int String) (Success 1)) 0)
"from-success works with Success"
)
(assert-equal test
0
2019-03-26 16:30:00 +03:00
(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)
2019-03-26 12:23:27 +03:00
&(to-maybe (the (Result Int String) (Success 1)))
"to-maybe works with Success"
)
(assert-equal test
&(Maybe.Nothing)
2019-03-26 12:23:27 +03:00
&(to-maybe (the (Result Int String) (Error @"error")))
"to-maybe works with Error"
2020-04-29 15:01:41 +03:00
)
(assert-equal test
123
(match (the (Result (Result Int String) String) (Success (Success 123)))
(Success (Error _)) 0
(Error _) 0
(Success (Success x)) x)
"matching on nested results"
)
2019-02-13 21:31:47 +03:00
)