-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
defmodule Machete.JSONMatcher do | ||
@moduledoc """ | ||
Defines a matcher that matches JSON documents | ||
""" | ||
|
||
import Machete.Mismatch | ||
import Machete.Operators | ||
|
||
defstruct matcher: nil | ||
|
||
@typedoc """ | ||
Describes an instance of this matcher | ||
""" | ||
@opaque t :: %__MODULE__{} | ||
|
||
@doc """ | ||
Matches against JSON documents whose deserialization matches a provided matcher | ||
Takes a matcher as its sole (mandatory) argument | ||
Examples: | ||
iex> assert "{}" ~> json(map()) | ||
true | ||
iex> assert ~s({"a": 1}) ~> json(%{"a" => 1}) | ||
true | ||
iex> assert "[]" ~> json(list()) | ||
true | ||
iex> assert "[1,2,3]" ~> json([1,2,3]) | ||
true | ||
iex> assert ~s("abc") ~> json(string()) | ||
true | ||
iex> assert "123" ~> json(integer()) | ||
true | ||
iex> assert "true" ~> json(boolean()) | ||
true | ||
iex> assert "null" ~> json(nil) | ||
true | ||
""" | ||
@spec json(term()) :: t() | ||
def json(matcher), do: struct!(__MODULE__, matcher: matcher) | ||
|
||
defimpl Machete.Matchable do | ||
def mismatches(%@for{} = a, b) when is_binary(b) do | ||
Jason.decode(b) | ||
|> case do | ||
{:ok, document} -> document ~>> a.matcher | ||
_ -> mismatch("#{inspect(b)} is not parseable JSON") | ||
end | ||
end | ||
|
||
def mismatches(%@for{}, b), do: mismatch("#{inspect(b)} is not a string") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule JSONMatcherTest do | ||
use ExUnit.Case, async: true | ||
use Machete | ||
|
||
import Machete.Mismatch | ||
|
||
doctest Machete.JSONMatcher | ||
|
||
test "produces a useful mismatch for non strings" do | ||
assert 1 ~>> json(term()) ~> mismatch("1 is not a string") | ||
end | ||
|
||
test "produces a useful mismatch for non-parseable strings" do | ||
assert "%" ~>> json(term()) ~> mismatch("\"%\" is not parseable JSON") | ||
end | ||
|
||
test "produces a useful mismatch for content mismatches" do | ||
assert "[1]" | ||
~>> json([]) | ||
~> mismatch("List is 1 elements in length, expected 0") | ||
end | ||
end |