Current section

Files

Jump to
dream_test src dream_test@matchers@result.erl
Raw

src/dream_test@matchers@result.erl

-module(dream_test@matchers@result).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/dream_test/matchers/result.gleam").
-export([be_ok/1, be_error/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Result matchers for dream_test.\n"
"\n"
" These matchers work with `Result(a, e)` values and are re-exported through\n"
" `dream_test/matchers`.\n"
"\n"
" `be_ok()` unwraps `Ok(value)` so you can keep matching on the inner value.\n"
" `be_error()` unwraps `Error(value)` so you can match on the error value.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" Ok(\"hello\")\n"
" |> should\n"
" |> be_ok()\n"
" |> be_equal(\"hello\")\n"
" |> or_fail_with(\"expected Ok(\\\"hello\\\")\")\n"
" ```\n"
).
-file("src/dream_test/matchers/result.gleam", 59).
-spec check_is_ok({ok, HOZ} | {error, any()}) -> dream_test@types:match_result(HOZ).
check_is_ok(Actual) ->
case Actual of
{ok, Value} ->
{match_ok, Value};
{error, Error} ->
Payload = {result_failure,
<<<<"Error("/utf8, (gleam@string:inspect(Error))/binary>>/binary,
")"/utf8>>,
true},
{match_failed,
{assertion_failure,
<<"be_ok"/utf8>>,
<<""/utf8>>,
{some, Payload}}}
end.
-file("src/dream_test/matchers/result.gleam", 50).
?DOC(
" Assert that a `Result` is `Ok` and extract its value.\n"
"\n"
" If the assertion passes, the `Ok` value is passed to subsequent matchers.\n"
" This enables chaining like `be_ok() |> be_equal(\"...\")`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" Ok(\"hello\")\n"
" |> should\n"
" |> be_ok()\n"
" |> be_equal(\"hello\")\n"
" |> or_fail_with(\"expected Ok(\\\"hello\\\")\")\n"
" ```\n"
"\n"
" ## Parameters\n"
"\n"
" - `value_or_result`: the `MatchResult(Result(a, e))` produced by `should` (or a previous matcher)\n"
"\n"
" ## Returns\n"
"\n"
" A `MatchResult(a)`:\n"
" - On `Ok(value)`, the chain continues with the unwrapped `value`.\n"
" - On `Error(_)`, the chain becomes failed and later matchers are skipped.\n"
).
-spec be_ok(dream_test@types:match_result({ok, HOT} | {error, any()})) -> dream_test@types:match_result(HOT).
be_ok(Value_or_result) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_is_ok(Actual)
end.
-file("src/dream_test/matchers/result.gleam", 112).
-spec check_is_error({ok, any()} | {error, HPL}) -> dream_test@types:match_result(HPL).
check_is_error(Actual) ->
case Actual of
{error, Error} ->
{match_ok, Error};
{ok, Value} ->
Payload = {result_failure,
<<<<"Ok("/utf8, (gleam@string:inspect(Value))/binary>>/binary,
")"/utf8>>,
false},
{match_failed,
{assertion_failure,
<<"be_error"/utf8>>,
<<""/utf8>>,
{some, Payload}}}
end.
-file("src/dream_test/matchers/result.gleam", 103).
?DOC(
" Assert that a `Result` is `Error` and extract the error value.\n"
"\n"
" If the assertion passes, the error value is passed to subsequent matchers.\n"
" This enables chaining like `be_error() |> be_equal(\"...\")`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" Error(\"nope\")\n"
" |> should\n"
" |> be_error()\n"
" |> be_equal(\"nope\")\n"
" |> or_fail_with(\"expected Error(\\\"nope\\\")\")\n"
" ```\n"
"\n"
" ## Parameters\n"
"\n"
" - `value_or_result`: the `MatchResult(Result(a, e))` produced by `should` (or a previous matcher)\n"
"\n"
" ## Returns\n"
"\n"
" A `MatchResult(e)`:\n"
" - On `Error(value)`, the chain continues with the unwrapped error `value`.\n"
" - On `Ok(_)`, the chain becomes failed and later matchers are skipped.\n"
).
-spec be_error(dream_test@types:match_result({ok, any()} | {error, HPF})) -> dream_test@types:match_result(HPF).
be_error(Value_or_result) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_is_error(Actual)
end.