Current section
Files
Jump to
Current section
Files
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 support chaining.\n"
" They're re-exported through `dream_test/assertions/should`.\n"
"\n"
" ## Chaining\n"
"\n"
" Both `be_ok` and `be_error` extract their inner values, allowing you to\n"
" chain additional matchers:\n"
"\n"
" ```gleam\n"
" import dream_test/assertions/should.{should, be_ok, be_error, equal, or_fail_with}\n"
"\n"
" // Check that it's Ok, then check the inner value\n"
" parse_int(\"42\")\n"
" |> should()\n"
" |> be_ok()\n"
" |> equal(42)\n"
" |> or_fail_with(\"Should parse to 42\")\n"
"\n"
" // Check that it's Error, then check the error value\n"
" validate(invalid_input)\n"
" |> should()\n"
" |> be_error()\n"
" |> equal(ValidationError(\"email required\"))\n"
" |> or_fail_with(\"Should fail with email error\")\n"
" ```\n"
).
-file("src/dream_test/matchers/result.gleam", 65).
-spec check_is_ok({ok, GAK} | {error, any()}) -> dream_test@types:match_result(GAK).
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", 58).
?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"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" parse_int(\"42\")\n"
" |> should()\n"
" |> be_ok()\n"
" |> or_fail_with(\"Should parse successfully\")\n"
" ```\n"
"\n"
" ## Chaining\n"
"\n"
" ```gleam\n"
" Ok(\"hello\")\n"
" |> should()\n"
" |> be_ok()\n"
" |> equal(\"hello\")\n"
" |> or_fail_with(\"Should be Ok with 'hello'\")\n"
" ```\n"
).
-spec be_ok(dream_test@types:match_result({ok, GAE} | {error, any()})) -> dream_test@types:match_result(GAE).
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", 114).
-spec check_is_error({ok, any()} | {error, GAW}) -> dream_test@types:match_result(GAW).
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", 107).
?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"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" parse_int(\"not a number\")\n"
" |> should()\n"
" |> be_error()\n"
" |> or_fail_with(\"Should fail to parse\")\n"
" ```\n"
"\n"
" ## Chaining\n"
"\n"
" ```gleam\n"
" Error(\"invalid\")\n"
" |> should()\n"
" |> be_error()\n"
" |> equal(\"invalid\")\n"
" |> or_fail_with(\"Should be Error with 'invalid'\")\n"
" ```\n"
).
-spec be_error(dream_test@types:match_result({ok, any()} | {error, GAQ})) -> dream_test@types:match_result(GAQ).
be_error(Value_or_result) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_is_error(Actual)
end.