Skip to content

Commit

Permalink
tests on minItems and contains
Browse files Browse the repository at this point in the history
  • Loading branch information
ityonemo committed Apr 4, 2023
1 parent 0738551 commit 2189f84
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 25 deletions.
6 changes: 6 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
# some files in gpt-3.5 have syntax errors.
subdirectories: ["bench"]
]
8 changes: 6 additions & 2 deletions lib/exonerate/combining/one_of.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ defmodule Exonerate.Combining.OneOf do

unquote(lambdas)
|> Enum.reduce_while(
{Exonerate.Tools.mismatch(data, unquote(resource), unquote(pointer), path, reason: "no matches"), 0},
{Exonerate.Tools.mismatch(data, unquote(resource), unquote(pointer), path,
reason: "no matches"
), 0},
fn
fun, {{:error, opts}, index} ->
case fun.(data, path) do
Expand Down Expand Up @@ -98,7 +100,9 @@ defmodule Exonerate.Combining.OneOf do

unquote(lambdas)
|> Enum.reduce_while(
{Exonerate.Tools.mismatch(data, unquote(resource), unquote(pointer), path, reason: "no matches"), 0},
{Exonerate.Tools.mismatch(data, unquote(resource), unquote(pointer), path,
reason: "no matches"
), 0},
fn
fun, {{:error, opts}, index} ->
case fun.(data, path) do
Expand Down
8 changes: 7 additions & 1 deletion lib/exonerate/context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,13 @@ defmodule Exonerate.Context do
quote do
defp unquote(Tools.call(resource, pointer, opts))(content, path) do
require Exonerate.Tools
Exonerate.Tools.mismatch(content, unquote(resource), unquote(type_failure_pointer), path)

Exonerate.Tools.mismatch(
content,
unquote(resource),
unquote(type_failure_pointer),
path
)
end
end,
opts
Expand Down
8 changes: 7 additions & 1 deletion lib/exonerate/filter/dependencies.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ defmodule Exonerate.Filter.Dependencies do
:ok
else
require Exonerate.Tools
Exonerate.Tools.mismatch(content, unquote(resource), unquote(schema_pointer), path)

Exonerate.Tools.mismatch(
content,
unquote(resource),
unquote(schema_pointer),
path
)
end
end
end)
Expand Down
51 changes: 37 additions & 14 deletions lib/exonerate/filter/format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ defmodule Exonerate.Filter.Format do
|> String.upcase()
|> NaiveDateTime.from_iso8601()
|> case do
{:ok, _} -> :ok
{:error, _} -> Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path)
{:ok, _} ->
:ok

{:error, _} ->
Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path)
end
end
end
Expand Down Expand Up @@ -106,8 +109,11 @@ defmodule Exonerate.Filter.Format do
|> String.upcase()
|> DateTime.from_iso8601()
|> case do
{:ok, %{utc_offset: _}, _} -> :ok
{:error, _} -> Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path)
{:ok, %{utc_offset: _}, _} ->
:ok

{:error, _} ->
Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path)
end
end
end
Expand All @@ -119,8 +125,11 @@ defmodule Exonerate.Filter.Format do
require Exonerate.Tools

case Date.from_iso8601(string) do
{:ok, _} -> :ok
{:error, _} -> Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path)
{:ok, _} ->
:ok

{:error, _} ->
Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path)
end
end
end
Expand All @@ -132,8 +141,11 @@ defmodule Exonerate.Filter.Format do
require Exonerate.Tools

case Time.from_iso8601(string) do
{:ok, _} -> :ok
{:error, _} -> Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path)
{:ok, _} ->
:ok

{:error, _} ->
Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path)
end
end
end
Expand All @@ -146,8 +158,11 @@ defmodule Exonerate.Filter.Format do

# NB ipv4strict means no "shortened ipv4 addresses"
case :inet.parse_ipv4strict_address(to_charlist(string)) do
{:ok, _} -> :ok
{:error, _} -> Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path)
{:ok, _} ->
:ok

{:error, _} ->
Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path)
end
end
end
Expand Down Expand Up @@ -257,7 +272,9 @@ defmodule Exonerate.Filter.Format do
:ok

tuple when elem(tuple, 0) == :error ->
Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path, reason: elem(tuple, 1))
Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path,
reason: elem(tuple, 1)
)
end
end
end
Expand Down Expand Up @@ -305,7 +322,9 @@ defmodule Exonerate.Filter.Format do
:ok

{:error, reason} ->
Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path, reason: reason)
Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path,
reason: reason
)
end
end
end
Expand All @@ -321,7 +340,9 @@ defmodule Exonerate.Filter.Format do
:ok

{:error, reason} ->
Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path, reason: reason)
Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path,
reason: reason
)
end
end
end
Expand All @@ -337,7 +358,9 @@ defmodule Exonerate.Filter.Format do
:ok

{:error, reason} ->
Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path, reason: reason)
Exonerate.Tools.mismatch(string, unquote(resource), unquote(pointer), path,
reason: reason
)
end
end
end
Expand Down
29 changes: 24 additions & 5 deletions lib/exonerate/type/array/find_iterator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,26 @@ defmodule Exonerate.Type.Array.FindIterator do

content
|> Enum.reduce_while(
{Exonerate.Tools.mismatch(content, unquote(resource), unquote(contains_pointer), path), 0, 0},
{Exonerate.Tools.mismatch(content, unquote(resource), unquote(contains_pointer), path),
0, 0},
fn
_item, {:ok, index, count} when index >= unquote(length) ->
{:halt, {:ok, index, count}}

_item, {:ok, index, count} ->
{:halt, {:ok, index + 1, count}}

item, {{:error, error_so_far}, index, count} ->
case unquote(contains_call)(item, Path.join(path, "#{index}")) do
:ok when count >= unquote(needed) ->
{:cont, {:ok, index + 1, count}}

:ok ->
{:cont, {:error, error_so_far}, index + 1, count + 1}
{:cont, {{:error, error_so_far}, index + 1, count + 1}}

Exonerate.Tools.error_match(error) ->
new_params = Keyword.update(error_so_far, :errors, [error], &[error | &1])
{:cont, {{:error, error_so_far}, index + 1}}
{:cont, {{:error, error_so_far}, index + 1, count}}
end
end
)
Expand All @@ -77,7 +81,21 @@ defmodule Exonerate.Type.Array.FindIterator do
path
)

{Exonerate.Tools.error_match(error), _} ->
{:ok, _, _} ->
:ok

{Exonerate.Tools.error_match(error), count, unquote(needed)} when count >= unquote(length) ->
:ok

{Exonerate.Tools.error_match(error), count, unquote(needed)} ->
Exonerate.Tools.mismatch(
content,
unquote(resource),
unquote(JsonPointer.join(pointer, "minContains")),
path
)

{Exonerate.Tools.error_match(error), _, _} ->
error
end
end
Expand All @@ -97,7 +115,8 @@ defmodule Exonerate.Type.Array.FindIterator do

content
|> Enum.reduce_while(
{Exonerate.Tools.mismatch(content, unquote(resource), unquote(contains_pointer), path), 0, 0},
{Exonerate.Tools.mismatch(content, unquote(resource), unquote(contains_pointer), path),
0, 0},
fn
item, {{:error, params}, index, count} ->
case unquote(contains_call)(item, path) do
Expand Down
8 changes: 7 additions & 1 deletion lib/exonerate/type/string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ defmodule Exonerate.Type.String do
unquote(filters)
else
require Exonerate.Tools
Exonerate.Tools.mismatch(string, unquote(resource), unquote(non_utf_error_pointer), path)

Exonerate.Tools.mismatch(
string,
unquote(resource),
unquote(non_utf_error_pointer),
path
)
end
end
end
Expand Down
29 changes: 28 additions & 1 deletion test/misc_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,32 @@ defmodule ExonerateTest.MiscTest do
end
end

test "array with minItems AND contains"
Exonerate.function_from_string(
:def,
:minitems_contains,
"""
{
"minItems": 2,
"contains": {"const": "foo"}
}
"""
)

describe "array with minItems AND contains" do
test "doesn't contain enough items" do
assert {:error, _} = minitems_contains(["foo"])
end

test "dosn't contain the right item" do
assert {:error, _} = minitems_contains(["bar", "baz"])
end

test "doesn't contain either the right item or enough items" do
assert {:error, _} = minitems_contains(["bar"])
end

test "contains the right item and enough items" do
assert :ok == minitems_contains(["foo", "bar"])
end
end
end
4 changes: 4 additions & 0 deletions test/readme_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule ExonerateTest.ReadmeTest do
use ExUnit.Case
doctest ExonerateTest.Readme
end
17 changes: 17 additions & 0 deletions test/support/readme.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# bootstraps content for the readme tests

readme = Path.join(__DIR__, "../../README.md")

[module, tests] =
readme
|> File.read!()
|> String.split("```elixir")
|> Enum.map(&hd(String.split(&1, "```")))
|> Enum.slice(2..-1)

Code.eval_string(module)

defmodule ExonerateTest.Readme do
@external_resource readme
@moduledoc tests
end

0 comments on commit 2189f84

Please sign in to comment.