slovo/tests/result-helpers.slo
2026-05-22 08:38:43 +02:00

72 lines
1.6 KiB
Plaintext

(module main)
(fn i32_ok () -> (result i32 i32)
(ok i32 i32 42))
(fn i32_err () -> (result i32 i32)
(err i32 i32 7))
(fn text_ok () -> (result string i32)
(ok string i32 "value"))
(fn text_err () -> (result string i32)
(err string i32 9))
(fn observe_i32_ok () -> bool
(std.result.is_ok (i32_ok)))
(fn observe_i32_err () -> bool
(std.result.is_err (i32_err)))
(fn unwrap_i32_ok () -> i32
(std.result.unwrap_ok (i32_ok)))
(fn unwrap_i32_err () -> i32
(std.result.unwrap_err (i32_err)))
(fn observe_text_ok () -> bool
(std.result.is_ok (text_ok)))
(fn observe_text_err () -> bool
(std.result.is_err (text_err)))
(fn unwrap_text_ok () -> string
(std.result.unwrap_ok (text_ok)))
(fn unwrap_text_err () -> i32
(std.result.unwrap_err (text_err)))
(fn legacy_i32_ok () -> bool
(is_ok (i32_ok)))
(fn legacy_text_err () -> i32
(unwrap_err (text_err)))
(test "std result i32 observers"
(if (std.result.is_ok (i32_ok))
(std.result.is_err (i32_err))
false))
(test "std result i32 unwraps"
(= (+ (std.result.unwrap_ok (i32_ok)) (std.result.unwrap_err (i32_err))) 49))
(test "std result string observers"
(if (std.result.is_ok (text_ok))
(std.result.is_err (text_err))
false))
(test "std result string unwraps"
(if (= (std.result.unwrap_ok (text_ok)) "value")
(= (std.result.unwrap_err (text_err)) 9)
false))
(test "legacy result helpers still work"
(if (is_ok (i32_ok))
(= (unwrap_err (text_err)) 9)
false))
(fn main () -> i32
(if (std.result.is_ok (i32_ok))
(std.result.unwrap_ok (i32_ok))
(std.result.unwrap_err (i32_err))))