From 2fbc4ac6537c2f6fa59a45e36e5a1d8e467c2611 Mon Sep 17 00:00:00 2001 From: Devon Estes Date: Mon, 8 Jan 2018 19:12:14 +0100 Subject: [PATCH] Change `assert_in_delta` better handle a delta of 0 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`. --- lib/ex_unit/lib/ex_unit/assertions.ex | 4 ++-- lib/ex_unit/test/ex_unit/assertions_test.exs | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/ex_unit/lib/ex_unit/assertions.ex b/lib/ex_unit/lib/ex_unit/assertions.ex index 78301d69f60..bdcf4b6c917 100644 --- a/lib/ex_unit/lib/ex_unit/assertions.ex +++ b/lib/ex_unit/lib/ex_unit/assertions.ex @@ -689,9 +689,9 @@ defmodule ExUnit.Assertions do message = message || "Expected the difference between #{inspect(value1)} and " <> - "#{inspect(value2)} (#{inspect(diff)}) to be less than #{inspect(delta)}" + "#{inspect(value2)} (#{inspect(diff)}) to be less than or equal to #{inspect(delta)}" - assert diff < delta, message + assert diff <= delta, message end @doc """ diff --git a/lib/ex_unit/test/ex_unit/assertions_test.exs b/lib/ex_unit/test/ex_unit/assertions_test.exs index fd04c31cbd3..6eb96d8bd2d 100644 --- a/lib/ex_unit/test/ex_unit/assertions_test.exs +++ b/lib/ex_unit/test/ex_unit/assertions_test.exs @@ -667,11 +667,16 @@ defmodule ExUnit.AssertionsTest do end end + test "assert in delta works with equal values and a delta of zero" do + assert_in_delta(10, 10, 0) + end + test "assert in delta error" do "This should never be tested" = assert_in_delta(10, 12, 1) rescue error in [ExUnit.AssertionError] -> - "Expected the difference between 10 and 12 (2) to be less than 1" = error.message + "Expected the difference between 10 and 12 (2) to be less than or equal to 1" = + error.message end test "assert in delta with message" do