View Source Moar.Assertions (Moar v1.33.0)

ExUnit assertions.

See also: siiibo/assert_match which is similar to this module's assert_eq function but with pattern matching.

Link to this section Summary

Functions

Asserts that the left list or map contains all of the items in the right list or map, or contains the single right element if it's not a list or map. Returns left or raises ExUnit.AssertionError.

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, returning the result of the action.

Refute that a condition is changed after performing an action, returning the result of the action.

Link to this section Types

@type assert_eq_opts() ::
  {:except, list()}
  | {:ignore_order, boolean()}
  | {:ignore_whitespace, :leading_and_trailing}
  | {:only, list()}
  | {:returning, any()}
  | {:whitespace, :squish | :trim}
  | {:within, number() | {number(), Moar.Duration.time_unit()}}

Link to this section Functions

Link to this function

assert_contains(left, right)

View Source
@spec assert_contains(map(), map()) :: map()
@spec assert_contains(list(), list() | any()) :: list()

Asserts that the left list or map contains all of the items in the right list or map, or contains the single right element if it's not a list or map. Returns left or raises ExUnit.AssertionError.

iex> assert_contains([1, 2, 3], [1, 3])
[1, 2, 3]

iex> assert_contains(%{a: 1, b: 2, c: 3}, %{a: 1, c: 3})
%{a: 1, b: 2, c: 3}

iex> assert_contains([1, 2, 3], 2)
[1, 2, 3]
Link to this function

assert_eq(left, right, opts \\ [])

View Source
@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, unless left is a string and right is a Regex, in which case they will be compared using the =~ operator.

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:

  • except: ~w[a b]a - ignore the given keys when comparing maps.
  • ignore_order: boolean - if the left and right values are lists, ignores the order when checking equality.
  • ignore_whitespace: :leading_and_trailing - if the left and right values are strings, ignores leading and trailing space when checking equality. deprecated: see :whitespace option
  • only: ~w[a b]a - only consider the given keys when comparing maps.
  • returning: value - returns value if the assertion passes, rather than returning the left value.
  • whitespace: :squish - when left and right are strings, squishes via Moar.String.squish/1 before comparing.
  • whitespace: :trim - when left and right are strings, trims via String.trim/1 before comparing.
  • within: delta - asserts that the left and right values are within delta of each other.
  • within: {delta, time_unit} - like within: delta but performs time comparisons in the specified time_unit. See Moar.Duration for more about time units. If left and right are 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> assert_eq(%{a: 1, b: 2, c: 3}, %{a: 1, b: 100, c: 3}, except: [:b])
%{a: 1, b: 2, c: 3}

iex> assert_eq([1, 2], [2, 1], ignore_order: true)
[1, 2]

iex> assert_eq(%{a: 1, b: 2, c: 3}, %{a: 1, b: 100, c: 3}, only: [:a, :c])
%{a: 1, b: 2, c: 3}

iex> map = %{a: 1, b: 2}
iex> map |> Map.get(:a) |> assert_eq(1, returning: map)
%{a: 1, b: 2}

iex> assert_eq("foo bar", "  foo bar\n", whitespace: :trim)
"foo bar"

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"
Link to this function

assert_recent(datetime, recency \\ {10, :second})

View Source
@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.

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}
Link to this macro

assert_that(command, list)

View Source (macro)
@spec assert_that(any(), changes: any(), from: any(), to: any()) :: Macro.t()

Asserts that a pre-condition and a post-condition are true after performing an action, returning the result of the action.

To use an anonymous function as the action, wrap it in parentheses and call it with .().

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
:ok
...>
iex> assert_that Agent.update(agent, fn s -> s + 1 end),
...>     changes: Agent.get(agent, fn s -> s end),
...>     to: 2
:ok
...>
iex> assert_that (fn -> Agent.update(agent, fn s -> s + 1 end) end).(),
...>     changes: Agent.get(agent, fn s -> s end),
...>     to: 3
:ok
Link to this macro

refute_that(command, list)

View Source (macro)
@spec refute_that(any(), [{:changes, any()}]) :: Macro.t()

Refute that a condition is changed after performing an action, returning the result of the 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)
1

iex> refute_that Function.identity(5),
...>     changes: %{a: 1}
5