-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
- Loading branch information
There are no files selected for viewing
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 | ||
""" | ||
|
||
@opts name: :number | ||
|
||
use(Drops.Type, union([:integer, :float])) | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
defmodule Drops.Contract.Types.IntegerTest do | ||
Check failure on line 1 in test/contract/types/number_test.exs GitHub Actions / Build and test (1.14.5, 25.3)
Check warning on line 1 in test/contract/types/number_test.exs GitHub Actions / Build and test (1.15.7, 26.1)
Check warning on line 1 in test/contract/types/number_test.exs GitHub Actions / Build and test (1.14.5, 26.2)
|
||
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 | ||
Check failure on line 31 in test/contract/types/number_test.exs GitHub Actions / Build and test (1.15.7, 26.1)
Check failure on line 31 in test/contract/types/number_test.exs GitHub Actions / Build and test (1.14.5, 26.2)
Check failure on line 31 in test/contract/types/number_test.exs GitHub Actions / Build and test (1.14.5, 26.2)
Check failure on line 31 in test/contract/types/number_test.exs GitHub Actions / Build and test (1.14.5, 26.1)
|
||
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 | ||
Check failure on line 48 in test/contract/types/number_test.exs GitHub Actions / Build and test (1.15.7, 26.1)
Check failure on line 48 in test/contract/types/number_test.exs GitHub Actions / Build and test (1.14.5, 26.2)
Check failure on line 48 in test/contract/types/number_test.exs GitHub Actions / Build and test (1.14.5, 26.2)
Check failure on line 48 in test/contract/types/number_test.exs GitHub Actions / Build and test (1.14.5, 26.1)
|
||
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 | ||
Check failure on line 65 in test/contract/types/number_test.exs GitHub Actions / Build and test (1.15.7, 26.1)
Check failure on line 65 in test/contract/types/number_test.exs GitHub Actions / Build and test (1.14.5, 26.2)
Check failure on line 65 in test/contract/types/number_test.exs GitHub Actions / Build and test (1.14.5, 26.2)
Check failure on line 65 in test/contract/types/number_test.exs GitHub Actions / Build and test (1.14.5, 26.1)
|
||
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 |