View Source Machete.StructLikeMatcher (Machete v0.3.0)

Defines a matcher that matches structs only on a specified list of fields

Summary

Types

t()

Describes an instance of this matcher

Functions

Matches structs only on a specified list of fields

Types

@opaque t()

Describes an instance of this matcher

Functions

Link to this function

struct_like(type, fields)

View Source
@spec struct_like(
  struct(),
  Enum.t()
) :: t()

Matches structs only on a specified list of fields

Structs can be tricky to match because they have default values for fields which are not specified at the point of creation. After creation these default values are otherwise indistinguishable from user-specified ones, and often get in the way of effective matching. This matcher provides a convenient way around this by allowing the user to describe both the type of struct expected as well as a set of matchers for fields within the struct; only the fields explicitly listed in the matcher are examined.

Fields can be specified via a map or keyword list in a manner identical to that of Kernel.struct/2. Fields may be matched using any matcher, not just literal values.

Examples:

iex> assert %URI{host: "example.com", path: "/abc"} ~> struct_like(URI, %{host: "example.com"})
true

iex> assert %URI{host: "example.com", path: "/abc"} ~> struct_like(URI, host: "example.com")
true

iex> assert %URI{host: "example.com", path: "/abc"} ~> struct_like(URI, host: string())
true

iex> refute %URI{host: "example.com"} ~> struct_like(URI, host: "example.com", path: "/abc")
false