Current section

Files

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

src/dream_test@matchers@comparison.erl

-module(dream_test@matchers@comparison).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/dream_test/matchers/comparison.gleam").
-export([be_greater_than/2, be_less_than/2, be_at_least/2, be_at_most/2, be_between/3, be_in_range/3, be_greater_than_float/2, be_less_than_float/2]).
-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(
" Comparison matchers for dream_test.\n"
"\n"
" These matchers compare numeric values.\n"
" They're re-exported through `dream_test/assertions/should`.\n"
"\n"
" ## Integer Matchers\n"
"\n"
" ```gleam\n"
" import dream_test/assertions/should.{\n"
" should, be_greater_than, be_less_than, be_at_least,\n"
" be_at_most, be_between, be_in_range, or_fail_with,\n"
" }\n"
"\n"
" count\n"
" |> should()\n"
" |> be_greater_than(0)\n"
" |> or_fail_with(\"Count should be positive\")\n"
"\n"
" score\n"
" |> should()\n"
" |> be_in_range(0, 100)\n"
" |> or_fail_with(\"Score should be 0-100\")\n"
" ```\n"
"\n"
" ## Float Matchers\n"
"\n"
" ```gleam\n"
" average\n"
" |> should()\n"
" |> be_greater_than_float(0.0)\n"
" |> or_fail_with(\"Average should be positive\")\n"
" ```\n"
).
-file("src/dream_test/matchers/comparison.gleam", 62).
-spec check_greater_than(integer(), integer()) -> dream_test@types:match_result(integer()).
check_greater_than(Actual, Threshold) ->
case Actual > Threshold of
true ->
{match_ok, Actual};
false ->
Payload = {comparison_failure,
erlang:integer_to_binary(Actual),
<<"> "/utf8, (erlang:integer_to_binary(Threshold))/binary>>,
<<"be_greater_than"/utf8>>},
{match_failed,
{assertion_failure,
<<"be_greater_than"/utf8>>,
<<""/utf8>>,
{some, Payload}}}
end.
-file("src/dream_test/matchers/comparison.gleam", 52).
?DOC(
" Assert that an integer is greater than a threshold.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" count_items()\n"
" |> should()\n"
" |> be_greater_than(0)\n"
" |> or_fail_with(\"Should have at least one item\")\n"
" ```\n"
).
-spec be_greater_than(dream_test@types:match_result(integer()), integer()) -> dream_test@types:match_result(integer()).
be_greater_than(Value_or_result, Threshold) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_greater_than(Actual, Threshold)
end.
-file("src/dream_test/matchers/comparison.gleam", 103).
-spec check_less_than(integer(), integer()) -> dream_test@types:match_result(integer()).
check_less_than(Actual, Threshold) ->
case Actual < Threshold of
true ->
{match_ok, Actual};
false ->
Payload = {comparison_failure,
erlang:integer_to_binary(Actual),
<<"< "/utf8, (erlang:integer_to_binary(Threshold))/binary>>,
<<"be_less_than"/utf8>>},
{match_failed,
{assertion_failure,
<<"be_less_than"/utf8>>,
<<""/utf8>>,
{some, Payload}}}
end.
-file("src/dream_test/matchers/comparison.gleam", 93).
?DOC(
" Assert that an integer is less than a threshold.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" response_time_ms\n"
" |> should()\n"
" |> be_less_than(100)\n"
" |> or_fail_with(\"Response should be under 100ms\")\n"
" ```\n"
).
-spec be_less_than(dream_test@types:match_result(integer()), integer()) -> dream_test@types:match_result(integer()).
be_less_than(Value_or_result, Threshold) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_less_than(Actual, Threshold)
end.
-file("src/dream_test/matchers/comparison.gleam", 144).
-spec check_at_least(integer(), integer()) -> dream_test@types:match_result(integer()).
check_at_least(Actual, Minimum) ->
case Actual >= Minimum of
true ->
{match_ok, Actual};
false ->
Payload = {comparison_failure,
erlang:integer_to_binary(Actual),
<<">= "/utf8, (erlang:integer_to_binary(Minimum))/binary>>,
<<"be_at_least"/utf8>>},
{match_failed,
{assertion_failure,
<<"be_at_least"/utf8>>,
<<""/utf8>>,
{some, Payload}}}
end.
-file("src/dream_test/matchers/comparison.gleam", 134).
?DOC(
" Assert that an integer is at least a minimum value (>=).\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" user.age\n"
" |> should()\n"
" |> be_at_least(18)\n"
" |> or_fail_with(\"User must be at least 18\")\n"
" ```\n"
).
-spec be_at_least(dream_test@types:match_result(integer()), integer()) -> dream_test@types:match_result(integer()).
be_at_least(Value_or_result, Minimum) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_at_least(Actual, Minimum)
end.
-file("src/dream_test/matchers/comparison.gleam", 185).
-spec check_at_most(integer(), integer()) -> dream_test@types:match_result(integer()).
check_at_most(Actual, Maximum) ->
case Actual =< Maximum of
true ->
{match_ok, Actual};
false ->
Payload = {comparison_failure,
erlang:integer_to_binary(Actual),
<<"<= "/utf8, (erlang:integer_to_binary(Maximum))/binary>>,
<<"be_at_most"/utf8>>},
{match_failed,
{assertion_failure,
<<"be_at_most"/utf8>>,
<<""/utf8>>,
{some, Payload}}}
end.
-file("src/dream_test/matchers/comparison.gleam", 175).
?DOC(
" Assert that an integer is at most a maximum value (<=).\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" password.length\n"
" |> should()\n"
" |> be_at_most(128)\n"
" |> or_fail_with(\"Password must be at most 128 characters\")\n"
" ```\n"
).
-spec be_at_most(dream_test@types:match_result(integer()), integer()) -> dream_test@types:match_result(integer()).
be_at_most(Value_or_result, Maximum) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_at_most(Actual, Maximum)
end.
-file("src/dream_test/matchers/comparison.gleam", 229).
-spec check_between(integer(), integer(), integer()) -> dream_test@types:match_result(integer()).
check_between(Actual, Min, Max) ->
case (Actual > Min) andalso (Actual < Max) of
true ->
{match_ok, Actual};
false ->
Payload = {comparison_failure,
erlang:integer_to_binary(Actual),
<<<<(erlang:integer_to_binary(Min))/binary, " < value < "/utf8>>/binary,
(erlang:integer_to_binary(Max))/binary>>,
<<"be_between"/utf8>>},
{match_failed,
{assertion_failure,
<<"be_between"/utf8>>,
<<""/utf8>>,
{some, Payload}}}
end.
-file("src/dream_test/matchers/comparison.gleam", 218).
?DOC(
" Assert that an integer is between two values (exclusive).\n"
"\n"
" The value must be strictly greater than `min` and strictly less than `max`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" port\n"
" |> should()\n"
" |> be_between(1024, 65535)\n"
" |> or_fail_with(\"Port must be between 1024 and 65535\")\n"
" ```\n"
).
-spec be_between(dream_test@types:match_result(integer()), integer(), integer()) -> dream_test@types:match_result(integer()).
be_between(Value_or_result, Min, Max) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_between(Actual, Min, Max)
end.
-file("src/dream_test/matchers/comparison.gleam", 273).
-spec check_in_range(integer(), integer(), integer()) -> dream_test@types:match_result(integer()).
check_in_range(Actual, Min, Max) ->
case (Actual >= Min) andalso (Actual =< Max) of
true ->
{match_ok, Actual};
false ->
Payload = {comparison_failure,
erlang:integer_to_binary(Actual),
<<<<(erlang:integer_to_binary(Min))/binary,
" <= value <= "/utf8>>/binary,
(erlang:integer_to_binary(Max))/binary>>,
<<"be_in_range"/utf8>>},
{match_failed,
{assertion_failure,
<<"be_in_range"/utf8>>,
<<""/utf8>>,
{some, Payload}}}
end.
-file("src/dream_test/matchers/comparison.gleam", 262).
?DOC(
" Assert that an integer is within a range (inclusive).\n"
"\n"
" The value must be >= `min` and <= `max`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" score\n"
" |> should()\n"
" |> be_in_range(0, 100)\n"
" |> or_fail_with(\"Score must be 0-100\")\n"
" ```\n"
).
-spec be_in_range(
dream_test@types:match_result(integer()),
integer(),
integer()
) -> dream_test@types:match_result(integer()).
be_in_range(Value_or_result, Min, Max) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_in_range(Actual, Min, Max)
end.
-file("src/dream_test/matchers/comparison.gleam", 314).
-spec check_greater_than_float(float(), float()) -> dream_test@types:match_result(float()).
check_greater_than_float(Actual, Threshold) ->
case Actual > Threshold of
true ->
{match_ok, Actual};
false ->
Payload = {comparison_failure,
gleam_stdlib:float_to_string(Actual),
<<"> "/utf8, (gleam_stdlib:float_to_string(Threshold))/binary>>,
<<"be_greater_than_float"/utf8>>},
{match_failed,
{assertion_failure,
<<"be_greater_than_float"/utf8>>,
<<""/utf8>>,
{some, Payload}}}
end.
-file("src/dream_test/matchers/comparison.gleam", 304).
?DOC(
" Assert that a float is greater than a threshold.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" average\n"
" |> should()\n"
" |> be_greater_than_float(0.0)\n"
" |> or_fail_with(\"Average should be positive\")\n"
" ```\n"
).
-spec be_greater_than_float(dream_test@types:match_result(float()), float()) -> dream_test@types:match_result(float()).
be_greater_than_float(Value_or_result, Threshold) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_greater_than_float(Actual, Threshold)
end.
-file("src/dream_test/matchers/comparison.gleam", 358).
-spec check_less_than_float(float(), float()) -> dream_test@types:match_result(float()).
check_less_than_float(Actual, Threshold) ->
case Actual < Threshold of
true ->
{match_ok, Actual};
false ->
Payload = {comparison_failure,
gleam_stdlib:float_to_string(Actual),
<<"< "/utf8, (gleam_stdlib:float_to_string(Threshold))/binary>>,
<<"be_less_than_float"/utf8>>},
{match_failed,
{assertion_failure,
<<"be_less_than_float"/utf8>>,
<<""/utf8>>,
{some, Payload}}}
end.
-file("src/dream_test/matchers/comparison.gleam", 348).
?DOC(
" Assert that a float is less than a threshold.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" error_rate\n"
" |> should()\n"
" |> be_less_than_float(0.01)\n"
" |> or_fail_with(\"Error rate should be under 1%\")\n"
" ```\n"
).
-spec be_less_than_float(dream_test@types:match_result(float()), float()) -> dream_test@types:match_result(float()).
be_less_than_float(Value_or_result, Threshold) ->
case Value_or_result of
{match_failed, Failure} ->
{match_failed, Failure};
{match_ok, Actual} ->
check_less_than_float(Actual, Threshold)
end.