Skip to content

Commit 617c86c

Browse files
ondrej-tuceks-m-i-t-a
authored andcommitted
Rename check funs and refactor generate_data fun
1 parent 1600bab commit 617c86c

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

test/validators_test.exs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,52 @@ defmodule ValidatorsTest do
1717

1818
property "should verify if input value is an integer" do
1919
forall value <- generate_data() do
20-
check_fn(&Validators.integer/2, value)
20+
check_fn_with_value(&Validators.integer/2, value)
2121
end
2222
end
2323

2424
property "should verify if input value is a float" do
2525
forall value <- generate_data() do
26-
check_fn(&Validators.float/2, value)
26+
check_fn_with_value(&Validators.float/2, value)
2727
end
2828
end
2929

3030
property "should verify if input value is less than required value" do
3131
forall {value, limit} <- {generate_data(), number()} do
32-
check_fn(&Validators.less_than/3, value, limit)
32+
check_fn_with_value_and_limit(&Validators.less_than/3, value, limit)
3333
end
3434
end
3535

3636
property "should verify if input value is less or equal to required value" do
3737
forall {value, limit} <- {generate_data(), number()} do
38-
check_fn(&Validators.at_most/3, value, limit)
38+
check_fn_with_value_and_limit(&Validators.at_most/3, value, limit)
3939
end
4040
end
4141

4242
property "should verify if input value is greater than required value" do
4343
forall {value, limit} <- {generate_data(), number()} do
44-
check_fn(&Validators.greater_than/3, value, limit)
44+
check_fn_with_value_and_limit(&Validators.greater_than/3, value, limit)
4545
end
4646
end
4747

4848
property "should verify if input value is greater or equal to required value" do
4949
forall {value, limit} <- {generate_data(), number()} do
50-
check_fn(&Validators.at_least/3, value, limit)
50+
check_fn_with_value_and_limit(&Validators.at_least/3, value, limit)
5151
end
5252
end
5353

5454
property "should verify if input value is is between two numbers" do
5555
forall {value, n1, n2} <- {generate_data(), number(), number()} do
56-
check_fn(&Validators.in_range/4, value, [min(n1, n2), max(n1, n2)])
56+
value
57+
|> Kernel.to_string()
58+
|> Validators.in_range(min(n1, n2), max(n1, n2), @error_msg)
59+
|> check_result(value, @error_msg)
5760
end
5861
end
5962

6063
property "should verify if input value is equal to required value" do
6164
forall {value, limit} <- {generate_data(), number()} do
62-
check_fn(&Validators.equal_to/3, value, Enum.random([value, limit]))
65+
check_fn_with_value_and_limit(&Validators.equal_to/3, value, Enum.random([value, limit]))
6366
end
6467
end
6568

@@ -89,35 +92,28 @@ defmodule ValidatorsTest do
8992
# Generator
9093

9194
def generate_data() do
92-
binary =
93-
let str <- binary() do
94-
str <> "x"
95-
end
95+
only_string =
96+
such_that(
97+
v <- binary(),
98+
when: not Regex.match?(~r/^[+-]?([0-9]*[.])?[0-9]+$/, v)
99+
)
96100

97101
oneof([
98102
number(),
99-
binary,
100-
""
103+
only_string
101104
])
102105
end
103106

104107
# Private
105108

106-
defp check_fn(fun, value) do
109+
defp check_fn_with_value(fun, value) do
107110
value
108111
|> Kernel.to_string()
109112
|> fun.(@error_msg)
110113
|> check_result(value, @error_msg)
111114
end
112115

113-
defp check_fn(fun, value, [min, max]) do
114-
value
115-
|> Kernel.to_string()
116-
|> fun.(min, max, @error_msg)
117-
|> check_result(value, @error_msg)
118-
end
119-
120-
defp check_fn(fun, value, limit) do
116+
defp check_fn_with_value_and_limit(fun, value, limit) do
121117
value
122118
|> Kernel.to_string()
123119
|> fun.(limit, @error_msg)

0 commit comments

Comments
 (0)