Current section

Files

Jump to
startest src startest@expect.erl
Raw

src/startest@expect.erl

-module(startest@expect).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([to_equal/2, to_not_equal/2, to_be_true/1, to_be_false/1, to_be_ok/1, to_be_error/1, to_be_some/1, to_be_none/1, to_throw/1, to_not_throw/1, to_loosely_equal/3]).
-spec to_equal(SED, SED) -> nil.
to_equal(Actual, Expected) ->
case Actual =:= Expected of
true ->
nil;
false ->
_pipe = {assertion_error,
gleam@string:concat(
[<<"Expected "/utf8>>,
gleam@string:inspect(Actual),
<<" to equal "/utf8>>,
gleam@string:inspect(Expected)]
),
gleam@string:inspect(Actual),
gleam@string:inspect(Expected)},
startest@assertion_error:raise(_pipe)
end.
-spec to_not_equal(SEE, SEE) -> nil.
to_not_equal(Actual, Expected) ->
case Actual /= Expected of
true ->
nil;
false ->
_pipe = {assertion_error,
gleam@string:concat(
[<<"Expected "/utf8>>,
gleam@string:inspect(Actual),
<<" to not equal "/utf8>>,
gleam@string:inspect(Expected)]
),
gleam@string:inspect(Actual),
gleam@string:inspect(Expected)},
startest@assertion_error:raise(_pipe)
end.
-spec to_be_true(boolean()) -> nil.
to_be_true(Actual) ->
_pipe = Actual,
to_equal(_pipe, true).
-spec to_be_false(boolean()) -> nil.
to_be_false(Actual) ->
_pipe = Actual,
to_equal(_pipe, false).
-spec to_be_ok({ok, SEF} | {error, any()}) -> SEF.
to_be_ok(Actual) ->
case Actual of
{ok, Value} ->
Value;
{error, _} ->
_pipe = {assertion_error,
gleam@string:concat(
[<<"Expected "/utf8>>,
gleam@string:inspect(Actual),
<<" to be Ok"/utf8>>]
),
gleam@string:inspect(Actual),
<<"Ok(_)"/utf8>>},
startest@assertion_error:raise(_pipe)
end.
-spec to_be_error({ok, any()} | {error, SEK}) -> SEK.
to_be_error(Actual) ->
case Actual of
{error, Error} ->
Error;
{ok, _} ->
_pipe = {assertion_error,
gleam@string:concat(
[<<"Expected "/utf8>>,
gleam@string:inspect(Actual),
<<" to be Error"/utf8>>]
),
gleam@string:inspect(Actual),
<<"Error(_)"/utf8>>},
startest@assertion_error:raise(_pipe)
end.
-spec to_be_some(gleam@option:option(SEN)) -> SEN.
to_be_some(Actual) ->
case Actual of
{some, Value} ->
Value;
none ->
_pipe = {assertion_error,
gleam@string:concat(
[<<"Expected "/utf8>>,
gleam@string:inspect(Actual),
<<" to be Some"/utf8>>]
),
gleam@string:inspect(Actual),
<<"Some(_)"/utf8>>},
startest@assertion_error:raise(_pipe)
end.
-spec to_be_none(gleam@option:option(any())) -> nil.
to_be_none(Actual) ->
case Actual of
none ->
nil;
{some, _} ->
_pipe = {assertion_error,
gleam@string:concat(
[<<"Expected "/utf8>>,
gleam@string:inspect(Actual),
<<" to be None"/utf8>>]
),
gleam@string:inspect(Actual),
<<"None"/utf8>>},
startest@assertion_error:raise(_pipe)
end.
-spec to_throw(fun(() -> any())) -> nil.
to_throw(F) ->
case exception_ffi:rescue(F) of
{error, _} ->
nil;
{ok, Value} ->
_pipe = {assertion_error,
gleam@string:concat(
[<<"Expected "/utf8>>,
gleam@string:inspect(F),
<<" to throw an error"/utf8>>]
),
gleam@string:inspect(Value),
gleam@string:inspect(nil)},
startest@assertion_error:raise(_pipe)
end.
-spec to_not_throw(fun(() -> SES)) -> SES.
to_not_throw(F) ->
case exception_ffi:rescue(F) of
{ok, Value} ->
Value;
{error, Exception} ->
_pipe = {assertion_error,
gleam@string:concat(
[<<"Expected "/utf8>>,
gleam@string:inspect(F),
<<" to not throw an error"/utf8>>]
),
gleam@string:inspect(nil),
gleam@string:inspect(Exception)},
startest@assertion_error:raise(_pipe)
end.
-spec format_float(float()) -> binary().
format_float(Value) ->
Repr = gleam@float:to_string(Value),
case gleam_stdlib:contains_string(Repr, <<"."/utf8>>) of
true ->
Repr;
false ->
<<Repr/binary, ".0"/utf8>>
end.
-spec to_loosely_equal(float(), float(), float()) -> nil.
to_loosely_equal(Actual, Expected, Tolerance) ->
case gleam@float:loosely_equals(Actual, Expected, Tolerance) of
true ->
nil;
false ->
_pipe = {assertion_error,
gleam@string:concat(
[<<"Expected "/utf8>>,
format_float(Actual),
<<" to loosely equal "/utf8>>,
format_float(Expected),
<<" with a tolerance of "/utf8>>,
format_float(Tolerance)]
),
format_float(Actual),
<<<<(format_float(Expected))/binary, " ± "/utf8>>/binary,
(format_float(Tolerance))/binary>>},
startest@assertion_error:raise(_pipe)
end.