Machete
Literate test matchers for ExUnit
Install / Use
/learn @mtrudel/MacheteREADME
Machete
Machete provides ergonomic match operators to help make your ExUnit tests more literate
The easiest way to explain Machete is to show it in action:
defmodule ExampleTest do
use ExUnit.Case
use Machete
test "example test" do
response = %{
id: 1,
name: "Moe Fonebone",
is_admin: false,
created_at: DateTime.utc_now()
}
assert response ~> %{
id: integer(positive: true),
name: string(),
is_admin: false,
created_at: datetime(roughly: :now, time_zone: :utc)
}
end
end
At its heart, Machete provides the following two things:
- A new
~>operator (the 'squiggle arrow') that does flexible matching of its left operator with its right operator - A set of parametric matchers such as
string()orinteger()which can match against general types. A comprehensive list of Machete's built-in matchers is available in the Machete documentation
These building blocks let you define test expectations that can match data against any combination of literals, variables, or parametrically defined matchers
When your matches fail, Machete provides useful error messages in ExUnit that point you directly at any failing matches using jq syntax
Matching literals & variables
Machete matches directly against literals & variables. The following examples will all match:
# Builtin type literals all match themselves:
assert 1 ~> 1
assert "abc" ~> "abc"
# Comparison is strict, using === semantics:
refute 1.0 ~> 1
refute "123" ~> 123
# Variables 'just work' everywhere; no pinning required:
a_number = 1
assert a_number ~> 1
assert 1 ~> a_number
assert a_number ~> a_number
# Date-like types are compared using the relevant `compare/2` function:
assert ~D[2021-02-01] ~> ~D[2021-02-01]
# Regexes match using =~ semantics:
assert "abc" ~> ~r/abc/
# Structs can be matched on a subset of their fields:
assert %User{name: "Moe"} ~> struct_like(User, name: string())
Matching collections
Machete matches collections via recursive descent; any nested fields / collections will be matched using the same heuristic:
# Maps have their content matched element by element:
assert %{a: 1} ~> %{a: 1}
# Same for lists and tuples:
assert [1,2,3] ~> [1,2,3]
assert {:ok, :boomer} ~> {:ok, :boomer}
# This same pattern applies recursively:
assert {:ok, %{a: [1,2,3]}} ~> {:ok, %{a: [1,2,3]}}
Parametric matchers
Machete comes with parametric matchers defined for a variety of types. A few illustrative examples follow; for more details, see the Machete Hexdocs:
# Matches strings
assert "abc" ~> string()
# Many parametric matchers can take optional arguments
# For example, this will match only positive integers
assert 1 ~> integer(positive: true)
# Parametric matchers make it easy to test fuzzy values:
almost_now = DateTime.utc_now()
Process.sleep(1)
assert almost_now ~> datetime(roughly: :now, time_zone: :utc)
# Of course, parametric matchers work within collections too:
assert %{name: "Moe Fonebone"} ~> %{name: string()}
# There are even parametric matchers for collections themselves:
assert [1,2,3] ~> list(elements: integer(), length: 3)
Installation
Machete is available in Hex, and can be
installed by adding machete to your list of dependencies in mix.exs:
def deps do
[
{:machete, "~> 0.2.8"}
]
end
Documentation is published on HexDocs
License
MIT
Related Skills
gh-issues
343.3kFetch GitHub issues, spawn sub-agents to implement fixes and open PRs, then monitor and address PR review comments. Usage: /gh-issues [owner/repo] [--label bug] [--limit 5] [--milestone v1.0] [--assignee @me] [--fork user/repo] [--watch] [--interval 5] [--reviews-only] [--cron] [--dry-run] [--model glm-5] [--notify-channel -1002381931352]
node-connect
343.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
92.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
Writing Hookify Rules
92.1kThis skill should be used when the user asks to "create a hookify rule", "write a hook rule", "configure hookify", "add a hookify rule", or needs guidance on hookify rule syntax and patterns.
