Current section
Files
Jump to
Current section
Files
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 are re-exported through\n"
" `dream_test/matchers`.\n"
"\n"
" `be_some()` unwraps `Some(value)` so you can keep matching on the inner\n"
" value. `be_none()` asserts the option is empty.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" Some(42)\n"
" |> should\n"
" |> be_some()\n"
" |> be_equal(42)\n"
" |> or_fail_with(\"expected Some(42)\")\n"
" ```\n"
).
-file("src/dream_test/matchers/option.gleam", 59).
-spec check_is_some(gleam@option:option(HNL)) -> dream_test@types:match_result(HNL).
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", 50).
?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"
" This enables chaining like `be_some() |> be_equal(42)`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" Some(42)\n"
" |> should\n"
" |> be_some()\n"
" |> be_equal(42)\n"
" |> or_fail_with(\"expected Some(42)\")\n"
" ```\n"
"\n"
" ## Parameters\n"
"\n"
" - `value_or_result`: the `MatchResult(Option(a))` produced by `should` (or a previous matcher)\n"
"\n"
" ## Returns\n"
"\n"
" A `MatchResult(a)`:\n"
" - On `Some(value)`, the chain continues with the unwrapped `value`.\n"
" - On `None`, the chain becomes failed and later matchers are skipped.\n"
).
-spec be_some(dream_test@types:match_result(gleam@option:option(HNH))) -> dream_test@types:match_result(HNH).
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", 102).
-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", 93).
?DOC(
" Assert that an `Option` is `None`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" None\n"
" |> should\n"
" |> be_none()\n"
" |> or_fail_with(\"expected None\")\n"
" ```\n"
"\n"
" ## Parameters\n"
"\n"
" - `value_or_result`: the `MatchResult(Option(a))` produced by `should` (or a previous matcher)\n"
"\n"
" ## Returns\n"
"\n"
" A `MatchResult(Nil)` that continues the chain with `Nil` on success.\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.