-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix fasten hook to properly return errors
- Loading branch information
Showing
2 changed files
with
58 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule Construct.Integration.Hooks.FastenTest do | ||
use Construct.TestCase | ||
|
||
defmodule IP do | ||
@behaviour Construct.Type | ||
|
||
def cast(term) when is_binary(term) do | ||
:inet.parse_address(String.to_charlist(term)) | ||
end | ||
|
||
def cast(_) do | ||
:error | ||
end | ||
end | ||
|
||
defmodule User do | ||
use Construct | ||
use Construct.Hooks.Fasten | ||
|
||
structure do | ||
field :huid, :string | ||
field :ip, IP | ||
end | ||
end | ||
|
||
test "#make returns structs with anonymous functions" do | ||
params = %{ | ||
"huid" => "9767dcf6-9413-44dd-af9a-2af0188ae12b", | ||
"ip" => "127.0.0.1" | ||
} | ||
|
||
assert {:ok, %User{ | ||
huid: params["huid"], | ||
ip: {127, 0, 0, 1} | ||
}} == User.make(params) | ||
end | ||
|
||
test "#make with invalid params" do | ||
assert {:error, %{huid: :invalid}} = | ||
User.make(%{"huid" => 42, "ip" => 42}) | ||
|
||
assert {:error, %{huid: :invalid}} = | ||
User.make(%{"huid" => 42, "ip" => "127.0.0.1.1"}) | ||
|
||
assert {:error, %{ip: :einval}} = | ||
User.make(%{"huid" => "9767dcf6-9413-44dd-af9a-2af0188ae12b", "ip" => "127.0.0.1.1"}) | ||
end | ||
end |