Current section

Files

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

src/dream_test@matchers@option.erl

-module(dream_test@matchers@option).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/dream_test/matchers/option.gleam").
-export([be_some/1, be_none/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(
" Option matchers for dream_test.\n"
"\n"
" These matchers work with `Option(a)` values and support chaining.\n"
" They're re-exported through `dream_test/assertions/should`.\n"
"\n"
" ## Chaining\n"
"\n"
" The `be_some` matcher extracts the inner value, allowing you to chain\n"
" additional matchers:\n"
"\n"
" ```gleam\n"
" import dream_test/assertions/should.{should, be_some, equal, or_fail_with}\n"
"\n"
" // Check that it's Some, then check the inner value\n"
" find_user(id)\n"
" |> should()\n"
" |> be_some()\n"
" |> equal(expected_user)\n"
" |> or_fail_with(\"Should find the expected user\")\n"
" ```\n"
).
-file("src/dream_test/matchers/option.gleam", 58).
-spec check_is_some(gleam@option:option(FVM)) -> dream_test@types:match_result(FVM).
check_is_some(Actual) ->
case Actual of
{some, Value} ->
{match_ok, Value};
none ->
Payload = {option_failure, <<"None"/utf8>>, true},
{match_failed,
{assertion_failure,
<<"be_some"/utf8>>,
<<""/utf8>>,
{some, Payload}}}
end.
-file("src/dream_test/matchers/option.gleam", 51).
?DOC(
" Assert that an `Option` is `Some` and extract its value.\n"
"\n"
" If the assertion passes, the inner value is passed to subsequent matchers.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" find_user(id)\n"
" |> should()\n"
" |> be_some()\n"
" |> or_fail_with(\"User should exist\")\n"
" ```\n"
"\n"
" ## Chaining\n"
"\n"
" ```gleam\n"
" Some(42)\n"
" |> should()\n"
" |> be_some()\n"
" |> equal(42)\n"
" |> or_fail_with(\"Should be Some(42)\")\n"
" ```\n"
).
-spec be_some(dream_test@types:match_result(gleam@option:option(FVI))) -> dream_test@types:match_result(FVI).
be_some(Value_or_result) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_is_some(Actual)
end.
-file("src/dream_test/matchers/option.gleam", 91).
-spec check_is_none(gleam@option:option(any())) -> dream_test@types:match_result(nil).
check_is_none(Actual) ->
case Actual of
none ->
{match_ok, nil};
{some, Value} ->
Payload = {option_failure,
<<<<"Some("/utf8, (gleam@string:inspect(Value))/binary>>/binary,
")"/utf8>>,
false},
{match_failed,
{assertion_failure,
<<"be_none"/utf8>>,
<<""/utf8>>,
{some, Payload}}}
end.
-file("src/dream_test/matchers/option.gleam", 84).
?DOC(
" Assert that an `Option` is `None`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" find_deleted_user(id)\n"
" |> should()\n"
" |> be_none()\n"
" |> or_fail_with(\"Deleted user should not exist\")\n"
" ```\n"
).
-spec be_none(dream_test@types:match_result(gleam@option:option(any()))) -> dream_test@types:match_result(nil).
be_none(Value_or_result) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_is_none(Actual)
end.