FlowAssertions.MapA (Flow Assertions v0.7.1) 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/2 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.

Same as refute_fields/2, but for a single field.

Fail if any of the fields in the field_list are present.

Take a map and a field. Return the single element in the field's value.

Link to this section Functions

Same as assert_fields/2 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}
Link to this function

refute_field(some_map, field)

View Source

Same as refute_fields/2, but for a single field.

%{a: 1} |> refute_field(:a)    # fails
Link to this function

refute_fields(some_map, field_list)

View Source

Fail if any of the fields in the field_list are present.

%{a: 1} |> refute_fields([:a, :b])    # fails
Link to this function

with_singleton_content(map, field)

View Source

Take a map and a field. Return the single element in the field's value.

with_singleton_content(%{a: [1]}, :a)   # returns `1`

This is typically used with fields that take list values. Often, you only want to test the empty list and a singleton list. (When testing functions that produce their values with Enum.map/2 or for, creating a second list element gains you nothing.) Using with_singleton_content, it's convenient to apply assertions to the single element:

view_model
|> assert_assoc_loaded(:service_gaps)
|> with_singleton_content(:service_gaps)
   |> assert_shape(%VM.ServiceGap{})
   |> Ex.Datespan.assert_datestrings(:first)

If field does not exist or isn't an Enum, with_singleton_content will fail in the same way FlowAssertions.EnumA.singleton_content/1 does.