Machete.StructLikeMatcher (Machete v0.3.11)
View SourceDefines a matcher that matches structs only on a specified list of fields
Summary
Functions
Matches structs only on a specified list of fields
Types
Functions
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> assert %URI{host: "example.com", path: "/abc"} ~> struct_like(URI)
true
iex> refute %URI{host: "example.com"} ~> struct_like(URI, host: "example.com", path: "/abc")
false