-
Notifications
You must be signed in to change notification settings - Fork 7
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
11 changed files
with
199 additions
and
29 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
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,12 @@ | ||
defmodule Drops.Types.Number do | ||
@moduledoc ~S""" | ||
Drops.Types.Number is a struct that represents a number type | ||
that can be either an integer or a float | ||
## Examples | ||
""" | ||
|
||
use(Drops.Type, union([:integer, :float])) | ||
|
||
@opts name: :number | ||
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
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,71 @@ | ||
defmodule Drops.Contract.Types.NumberTest do | ||
use Drops.ContractCase | ||
|
||
describe "number/0" do | ||
contract do | ||
schema do | ||
%{required(:test) => number()} | ||
end | ||
end | ||
|
||
test "returns success with valid data", %{contract: contract} do | ||
assert {:ok, %{test: 312}} = contract.conform(%{test: 312}) | ||
end | ||
|
||
test "returns error with invalid data", %{contract: contract} do | ||
assert_errors(["test must be a number"], contract.conform(%{test: :invalid})) | ||
end | ||
end | ||
|
||
describe "number/1 with an extra predicate" do | ||
contract do | ||
schema do | ||
%{required(:test) => number(:odd?)} | ||
end | ||
end | ||
|
||
test "returns success with valid data", %{contract: contract} do | ||
assert {:ok, %{test: 311}} = contract.conform(%{test: 311}) | ||
end | ||
|
||
test "returns error with invalid data", %{contract: contract} do | ||
assert_errors(["test must be a number"], contract.conform(%{test: :invalid})) | ||
assert_errors(["test must be odd"], contract.conform(%{test: 312})) | ||
end | ||
end | ||
|
||
describe "number/1 with an extra predicate with args" do | ||
contract do | ||
schema do | ||
%{required(:test) => number(gt?: 2)} | ||
end | ||
end | ||
|
||
test "returns success with valid data", %{contract: contract} do | ||
assert {:ok, %{test: 312}} = contract.conform(%{test: 312}) | ||
end | ||
|
||
test "returns error with invalid data", %{contract: contract} do | ||
assert_errors(["test must be a number"], contract.conform(%{test: :invalid})) | ||
assert_errors(["test must be greater than 2"], contract.conform(%{test: 0})) | ||
end | ||
end | ||
|
||
describe "number/1 with extra predicates" do | ||
contract do | ||
schema do | ||
%{required(:test) => number([:even?, gt?: 2])} | ||
end | ||
end | ||
|
||
test "returns success with valid data", %{contract: contract} do | ||
assert {:ok, %{test: 312}} = contract.conform(%{test: 312}) | ||
end | ||
|
||
test "returns error with invalid data", %{contract: contract} do | ||
assert_errors(["test must be a number"], contract.conform(%{test: :invalid})) | ||
assert_errors(["test must be even"], contract.conform(%{test: 311})) | ||
assert_errors(["test must be greater than 2"], contract.conform(%{test: 0})) | ||
end | ||
end | ||
end |