Skip to content

Commit

Permalink
test: add tests for Vx.Type.constraints/2
Browse files Browse the repository at this point in the history
  • Loading branch information
tlux committed Mar 26, 2024
1 parent 2a9c305 commit 98fc920
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/vx/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ defmodule Vx.Type do
when type: t(atom) | custom(module, atom)
def constraints(type, name) do
type
|> constraints()
|> resolve()
|> Map.fetch!(:constraints)
|> Enum.filter(&match?(%Constraint{name: ^name}, &1))
|> Enum.reverse()
end

defp resolve(%{__type__: %__MODULE__{} = type}), do: type
Expand Down
35 changes: 35 additions & 0 deletions test/vx/type_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,41 @@ defmodule Vx.TypeTest do
end
end

describe "constraints/2" do
setup do
{:ok,
constraints: [
%Vx.Constraint{name: :foo, value: "foo", fun: fn _ -> :ok end},
%Vx.Constraint{name: :bar, value: "foo", fun: fn _ -> :ok end},
%Vx.Constraint{name: :foo, value: "bar", fun: fn _ -> :ok end}
]}
end

test "plain type", %{constraints: constraints} do
type = %{
Vx.Type.new(:foo, fn _ -> :ok end)
| constraints: Enum.reverse(constraints)
}

assert [
%Vx.Constraint{name: :foo, value: "foo"},
%Vx.Constraint{name: :foo, value: "bar"}
] = Vx.Type.constraints(type, :foo)
end

test "wrapped type", %{constraints: constraints} do
schema =
Map.update!(Vx.Integer.t(), :__type__, fn type ->
%{type | constraints: Enum.reverse(constraints)}
end)

assert [
%Vx.Constraint{name: :foo, value: "foo"},
%Vx.Constraint{name: :foo, value: "bar"}
] = Vx.Type.constraints(schema, :foo)
end
end

describe "of/1" do
test "plain type" do
of = [Vx.String.t(), Vx.Integer.t()]
Expand Down

0 comments on commit 98fc920

Please sign in to comment.