-
Notifications
You must be signed in to change notification settings - Fork 29
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
9 changed files
with
202 additions
and
32 deletions.
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
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
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,53 @@ | ||
defmodule Norm.Spec.Union do | ||
@moduledoc false | ||
# Provides the struct for unions of specifications | ||
|
||
defstruct specs: [] | ||
|
||
def new(specs) do | ||
%__MODULE__{specs: specs} | ||
end | ||
|
||
defimpl Norm.Conformer.Conformable do | ||
alias Norm.Conformer | ||
alias Norm.Conformer.Conformable | ||
|
||
def conform(%{specs: specs}, input, path) do | ||
result = | ||
specs | ||
|> Enum.map(fn spec -> Conformable.conform(spec, input, path) end) | ||
|> Conformer.group_results | ||
|
||
if Enum.any?(result.ok) do | ||
{:ok, Enum.at(result.ok, 0)} | ||
else | ||
{:error, List.flatten(result.error)} | ||
end | ||
end | ||
end | ||
|
||
if Code.ensure_loaded?(StreamData) do | ||
defimpl Norm.Generatable do | ||
def gen(%{specs: specs}) do | ||
case Enum.reduce(specs, [], &to_gen/2) do | ||
{:error, error} -> | ||
{:error, error} | ||
|
||
generators -> | ||
{:ok, StreamData.one_of(generators)} | ||
end | ||
end | ||
|
||
def to_gen(_, {:error, error}), do: {:error, error} | ||
def to_gen(spec, generators) do | ||
case Norm.Generatable.gen(spec) do | ||
{:ok, g} -> | ||
[g | generators] | ||
|
||
{:error, error} -> | ||
{:error, error} | ||
end | ||
end | ||
end | ||
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
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,29 @@ | ||
defmodule Norm.UnionTest do | ||
use ExUnit.Case, async: true | ||
import ExUnitProperties, except: [gen: 1] | ||
import Norm | ||
|
||
describe "conforming" do | ||
test "returns the first match" do | ||
union = one_of([:foo, spec(is_binary())]) | ||
|
||
assert :foo == conform!(:foo, union) | ||
assert "chris" == conform!("chris", union) | ||
assert {:error, errors} = conform(123, union) | ||
assert errors == [ | ||
"val: 123 fails: is not an atom.", | ||
"val: 123 fails: is_binary()" | ||
] | ||
end | ||
end | ||
|
||
describe "generation" do | ||
property "randomly selects one of the options" do | ||
union = one_of([:foo, spec(is_binary())]) | ||
|
||
check all e <- gen(union) do | ||
assert e == :foo || is_binary(e) | ||
end | ||
end | ||
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