View Source Moar.Assertions (Moar v1.15.0)
ExUnit assertions.
Link to this section Summary
Functions
Asserts that the left and right values are equal. Returns the left value unless the assertion fails,
or unless the :returning option is used.
Asserts that datetime is within recency of now (in UTC), returning datetime if the assertion succeeeds.
Asserts that a pre-condition and a post-condition are true after performing an action.
Refute that a condition is changed after performing an action.
Link to this section Types
@type assert_eq_opts() :: {:ignore_order, boolean()} | {:returning, any()} | {:within, number() | {number(), Moar.Duration.time_unit()}}
Link to this section Functions
@spec assert_eq(left :: any(), right :: any(), opts :: [assert_eq_opts()]) :: any()
Asserts that the left and right values are equal. Returns the left value unless the assertion fails,
or unless the :returning option is used.
Uses assert left == right under the hood.
Style note: the authors prefer to use assert in most cases, using assert_eq only when the extra options
are helpful or when they want to make assertions in a pipeline.
Options:
ignore_order: boolean- if theleftandrightvalues are lists, ignores the order when checking equality.returning: value- returnsvalueif the assertion passes, rather than returning theleftvalue.within: delta- asserts that theleftandrightvalues are withindeltaof each other.within: {delta, time_unit}- likewithin: deltabut performs time comparisons in the specifiedtime_unit. SeeMoar.Durationfor more about time units. Ifleftandrightare strings, they are parsed as ISO8601 dates.
examples
Examples
iex> import Moar.Assertions
iex> %{a: 1} |> Map.put(:b, 2) |> assert_eq(%{a: 1, b: 2})
%{a: 1, b: 2}
iex> map = %{a: 1, b: 2}
iex> map |> Map.get(:a) |> assert_eq(1, returning: map)
%{a: 1, b: 2}
iex> assert_eq(4/28, 0.14, within: 0.01)
0.14285714285714285
iex> inserted_at = ~U[2022-01-02 03:00:00Z]
iex> updated_at = ~U[2022-01-02 03:04:00Z]
iex> assert_eq(inserted_at, updated_at, within: {10, :minute})
~U[2022-01-02 03:00:00Z]
iex> inserted_at = "2022-01-02T03:00:00Z"
iex> updated_at = "2022-01-02T03:04:00Z"
iex> assert_eq(inserted_at, updated_at, within: {10, :minute})
"2022-01-02T03:00:00Z"
@spec assert_recent(DateTime.t() | NaiveDateTime.t() | binary(), Moar.Duration.t()) :: DateTime.t() | NaiveDateTime.t() | binary()
Asserts that datetime is within recency of now (in UTC), returning datetime if the assertion succeeeds.
Uses assert_eq(datetime, now, within: recency) under the hood.
datetimecan be aDateTime, aNaiveDateTime, or an ISO8601-formatted UTC datetime string.recencyis aMoar.Durationand defaults to{10, :second}.
iex> five_seconds_ago = Moar.DateTime.add(DateTime.utc_now(), {-5, :second})
iex> assert_recent five_seconds_ago
iex> twenty_seconds_ago = Moar.DateTime.add(DateTime.utc_now(), {-20, :second})
iex> assert_recent twenty_seconds_ago, {25, :second}
Asserts that a pre-condition and a post-condition are true after performing an action.
examples
Examples
iex> {:ok, agent} = Agent.start(fn -> 0 end)
...>
iex> assert_that Agent.update(agent, fn s -> s + 1 end),
...> changes: Agent.get(agent, fn s -> s end),
...> from: 0,
...> to: 1
...>
iex> assert_that Agent.update(agent, fn s -> s + 1 end),
...> changes: Agent.get(agent, fn s -> s end),
...> to: 2
Refute that a condition is changed after performing an action.
examples
Examples
iex> {:ok, agent} = Agent.start(fn -> 0 end)
...>
iex> refute_that Function.identity(1),
...> changes: Agent.get(agent, fn s -> s end)