Flow Assertions v0.2.0 FlowAssertions.MapA View Source

Assertions that apply to Maps and structures and sometimes to keyword lists.

assert_fields/2 and assert_same_map/3 are the most important.

Link to this section Summary

Functions

Same as assert_fields but more pleasingly grammatical when testing only one field

Assert that the value of the map at the key matches a binding form.

Test the existence and value of multiple fields with a single assertion

An equality comparison of two maps that gives control over which fields should not be compared or should be compared differently.

Link to this section Functions

Same as assert_fields but more pleasingly grammatical when testing only one field:

assert_field(some_map, key: "value")

When checking existence, you don't have to use a list:

assert_field(some_map, :key)
Link to this macro

assert_field_shape(map, key, shape)

View Source (macro)

Assert that the value of the map at the key matches a binding form.

assert_field_shape(map, :field, %User{})
assert_field_shape(map, :field, [_ | _])

See FlowAssertions.MiscA.assert_shape/2 for more.

Link to this function

assert_fields(kvs, list_or_map)

View Source

Test the existence and value of multiple fields with a single assertion:

assert_fields(some_map, key1: 12, key2: "hello")

You can test just for existence:

assert_fields(some_map, [:key1, :key2]

The keyword list need not contain all of the fields in some_map.

Values in the keyword list are compared as with FlowAssertions.MiscA.assert_good_enough/2. For example, regular expressions can be used to check strings:

assert_fields(some_map, name: ~r/_cohort/)

assert_fields can also take a map as its second argument. That's useful when the map to be tested has non-keyword arguments:

assert_fields(string_map, %{"a" => 3})
Link to this function

assert_same_map(new, old, opts \\ [])

View Source

An equality comparison of two maps that gives control over which fields should not be compared or should be compared differently.

It is typically used after some old map has been transformed to make a new one.

To exclude some fields from the comparison:

  assert_same_map(new, old, ignoring: [:lock_version, :updated_at])

To compare only some of the keys:

  assert_same_map(new, old, comparing: [:name, :people])

To assert different values for particular fields:

  assert_same_map(new, old,
    except: [lock_version: old.lock_version + 1,
             people: &Enum.empty/1])

Note that the except comparison uses FlowAssertions.MiscA.assert_good_enough/2.

Note that if the first value is a struct, the second must have the same type:

  Assertion with == failed
  left:  %S{b: 3}
  right: %R{b: 3}