Packages
mneme
0.9.2
0.10.2
0.10.1
0.10.0
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.9.0-alpha.1
0.9.0-alpha.0
0.8.2
0.8.1
0.8.0
0.7.0
0.6.1
0.6.0
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.3.0-rc.1
0.3.0-rc.0
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
Snapshot testing tool using familiar assertions
Current section
Files
Jump to
Current section
Files
lib/mneme/assertion/pattern.ex
defmodule Mneme.Assertion.Pattern do
@moduledoc false
alias __MODULE__
@enforce_keys [:expr]
defstruct [:expr, guard: nil, notes: []]
@type t :: %Pattern{
expr: term(),
guard: Macro.t() | nil,
notes: [String.t()]
}
@doc """
Creates a new pattern.
"""
def new(expr, other_attrs \\ []) do
struct(Pattern, Keyword.put(other_attrs, :expr, expr))
end
@doc """
Combines a list of patterns into a single list pattern.
"""
def combine(patterns) when is_list(patterns) do
{exprs, {guard, notes}} =
Enum.map_reduce(
patterns,
{nil, []},
fn %Pattern{expr: expr, guard: g1, notes: n1}, {g2, n2} ->
{expr, {combine_guards(g1, g2), n1 ++ n2}}
end
)
Pattern.new(exprs, guard: guard, notes: notes)
end
defp combine_guards(nil, guard), do: guard
defp combine_guards(guard, nil), do: guard
defp combine_guards(g1, {_, meta, _} = g2), do: {:and, meta, [g2, g1]}
end