FlowAssertions.MiscA (Flow Assertions v0.6.0) View Source

Miscellaneous, including assertions for common idioms like {:ok, <content>}

Link to this section Summary

Functions

The chaining version of assert x === y.

Synonym for assert_equal.

Check if value is an :error or an {:error, <content>} tuple.

A variant of assert_error/2 for three-element :error tuples.

Like assert x == y, but with special handling for predicates and regular expressions.

Check if a value is an :ok or an {:ok, <content>} tuple.

Assert that the value matches a binding form.

Fail unless the value given is a three-element tuple with the first element :error and the second a required subcategory of error. Returns the third element.

Fails with an AssertionError unless the argument is of the form {:error, content}. Returns the content value.

Think of it as a form of equality with special handling for functions and regular expressions.

Fails with an AssertionError unless the argument is of the form {:ok, content}. Returns the content value.

Extract the :id field from an {:ok, %{id: id, ...}} value.

Link to this section Functions

The chaining version of assert x === y.

This is useful for cases where you're adding onto a pipeline of assertions or content-extraction checks (like ok_content/1).

value_to_check |> ok_content |> assert_equal(3)

Note that the comparison is done with ===, so 1 is not equal to 1.0.

Synonym for assert_equal.

Link to this function

assert_error(value_to_check)

View Source

Check if value is an :error or an {:error, <content>} tuple.

value_to_check |> assert_error(:error)
value_to_check |> assert_error({:error, "any value is accepted"})

See also error_content/1, which takes an :error tuple and returns the second element.

Link to this function

assert_error2(value_to_check, expected_subtype)

View Source

A variant of assert_error/2 for three-element :error tuples.

value_to_check |> assert_error(:error, :constraint)

Sometimes it's useful for an :error tuple to identify different kinds of errors. For example, Phoenix form processing errors might be due to either :validation or :constraint errors and reported in a tuple like {:error, :constraint, <message>}

This function checks that the second element is as required. The third element is ignored.

See also error2_content/2, which takes such a tuple and returns the third element.

Link to this function

assert_good_enough(value_to_check, predicate)

View Source

Like assert x == y, but with special handling for predicates and regular expressions.

By default assert_good_enough uses == to test the left side against the right. However:

  • If the right side is a regular expression and the left side is not, the two are compared with =~ rather than ==.

  • If the right side is a function and the left side is not, the function is applied to the value. Any "falsy" value is a failure.

assert_good_enough?(1, &odd/1)
assert_good_enough?("string", ~r/s.r..g/)

See also good_enough?/2

Link to this function

assert_ok(value_to_check)

View Source

Check if a value is an :ok or an {:ok, <content>} tuple.

value_to_check |> assert_ok(:ok)
value_to_check |> assert_ok({:ok, "any value is accepted"})

See also ok_content/1, which takes an :ok tuple and returns the second element.

Link to this macro

assert_shape(value_to_check, shape)

View Source (macro)

Assert that the value matches a binding form.

value_to_check |> assert_shape(%User{})
value_to_check |> assert_shape(thing, [_ | _])

Note that this is a macro that uses the match operator, so all of Elixir's pattern matching is available. For example, you can use a map to partially match a structure:

make_user("fred") |> assert_shape(%{name: "fred"})

Or you can pin a value:

make_user("fred") |> assert_shape(%{name: ^chosen_name})

See also FlowAssertions.StructA.assert_struct_named/2.

Link to this function

error2_content(value_to_check, second)

View Source

Fail unless the value given is a three-element tuple with the first element :error and the second a required subcategory of error. Returns the third element.

error_content is used to let the rest of an assertion chain operate on the content:

|> ReservationApi.create(ready, @institution)
|> error2_content(:constraint)
|> assert_equals("some error message")

See also assert_error2/2.

Fails with an AssertionError unless the argument is of the form {:error, content}. Returns the content value.

error_content is used to let the rest of an assertion chain operate on the content:

|> ReservationApi.create(ready, @institution)
|> error_content
|> assert_equals("some error message")

See also assert_error/1.

Link to this function

good_enough?(actual, expected)

View Source

Think of it as a form of equality with special handling for functions and regular expressions.

good_enough?(1, &odd/1)             # true
good_enough?("string", ~r/s.r..g/)  # true

If the second argument is a regular expression and the first is a string, the two are compared with =~.

If both arguments are regular expressions, their source fields are compared.

If the second argument is a function and the first is not, the function is applied to the first argument. good_enough? returns true iff the result is truthy.

Otherwise, the two are compared with ==.

See also assert_good_enough/2

Fails with an AssertionError unless the argument is of the form {:ok, content}. Returns the content value.

ok_content is used to let the rest of an assertion chain operate on the content:

|> ReservationApi.create(ready, @institution)
|> ok_content
|> assert_field(count: 5)

See also assert_ok/1.

Link to this function

ok_content(actual, struct_name)

View Source

Combines ok_content/1 and FlowAssertions.StructA.assert_struct_named/2.

|> VM.accept_form(params)
|> ok_content(Changeset)
|> assert_no_changes

In addition to checking that the value is an {:ok, content} tuple, it checks that the content is a value of the named struct before returning it.

Extract the :id field from an {:ok, %{id: id, ...}} value.

Shorthand for ok_content(x).id.