Skip to content

Commit 56fcd2a

Browse files
devonestesJosé Valim
authored andcommitted
Change assert_in_delta better handle a delta of 0 (#7189)
Previously if the two values that we were checking were within a given delta were equal, they would fail if the expected delta was `0`.
1 parent 900043c commit 56fcd2a

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

lib/ex_unit/lib/ex_unit/assertions.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,9 +689,9 @@ defmodule ExUnit.Assertions do
689689
message =
690690
message ||
691691
"Expected the difference between #{inspect(value1)} and " <>
692-
"#{inspect(value2)} (#{inspect(diff)}) to be less than #{inspect(delta)}"
692+
"#{inspect(value2)} (#{inspect(diff)}) to be less than or equal to #{inspect(delta)}"
693693

694-
assert diff < delta, message
694+
assert diff <= delta, message
695695
end
696696

697697
@doc """

lib/ex_unit/test/ex_unit/assertions_test.exs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,11 +667,16 @@ defmodule ExUnit.AssertionsTest do
667667
end
668668
end
669669

670+
test "assert in delta works with equal values and a delta of zero" do
671+
assert_in_delta(10, 10, 0)
672+
end
673+
670674
test "assert in delta error" do
671675
"This should never be tested" = assert_in_delta(10, 12, 1)
672676
rescue
673677
error in [ExUnit.AssertionError] ->
674-
"Expected the difference between 10 and 12 (2) to be less than 1" = error.message
678+
"Expected the difference between 10 and 12 (2) to be less than or equal to 1" =
679+
error.message
675680
end
676681

677682
test "assert in delta with message" do

0 commit comments

Comments
 (0)