From d23efa1ff2dd262a78b81410e4b2e91c815740e6 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sat, 17 Sep 2022 19:53:13 +0200 Subject: [PATCH] MDL-75757 phpunit: Add some tolerance to a few failing assertions Recently, PHPUnit (all versions) did some changes about how the comparators worked and this has caused some float comparisons to stop working. We need to move them to assertEqualsWithDelta(), allowing a small tolerance to workaround the floats comparison problem. 0.00001 has been decided. And applied to all the similar assertions within the unittest function, so if more cases are added to them better be copied with the agreed tolerance. --- .../deferredcbm/tests/behaviourtype_test.php | 6 +++--- question/engine/tests/helpers.php | 5 +++++ question/type/multianswer/tests/question_test.php | 11 +++++------ 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/question/behaviour/deferredcbm/tests/behaviourtype_test.php b/question/behaviour/deferredcbm/tests/behaviourtype_test.php index a4892e6247e..bfdf933e7a2 100644 --- a/question/behaviour/deferredcbm/tests/behaviourtype_test.php +++ b/question/behaviour/deferredcbm/tests/behaviourtype_test.php @@ -139,8 +139,8 @@ class qbehaviour_deferredcbm_type_test extends qbehaviour_walkthrough_test_base } public function test_calculate_bonus() { - $this->assertEquals(0.05, $this->behaviourtype->calculate_bonus(1, 1/2)); - $this->assertEquals(-0.01, $this->behaviourtype->calculate_bonus(2, 9/10)); - $this->assertEquals(0, $this->behaviourtype->calculate_bonus(3, 1)); + $this->assertEqualsWithDelta(0.05, $this->behaviourtype->calculate_bonus(1, 1 / 2), question_testcase::GRADE_DELTA); + $this->assertEqualsWithDelta(-0.01, $this->behaviourtype->calculate_bonus(2, 9 / 10), question_testcase::GRADE_DELTA); + $this->assertEqualsWithDelta(0, $this->behaviourtype->calculate_bonus(3, 1), question_testcase::GRADE_DELTA); } } diff --git a/question/engine/tests/helpers.php b/question/engine/tests/helpers.php index 13d48b93fff..41147843e73 100644 --- a/question/engine/tests/helpers.php +++ b/question/engine/tests/helpers.php @@ -479,6 +479,11 @@ abstract class data_loading_method_test_base extends advanced_testcase { abstract class question_testcase extends advanced_testcase { + /** + * Tolerance accepted in some unit tests when float operations are involved. + */ + const GRADE_DELTA = 0.00000005; + public function assert($expectation, $compare, $notused = '') { if (get_class($expectation) === 'question_pattern_expectation') { diff --git a/question/type/multianswer/tests/question_test.php b/question/type/multianswer/tests/question_test.php index 9390ad2bced..8bb053a1310 100644 --- a/question/type/multianswer/tests/question_test.php +++ b/question/type/multianswer/tests/question_test.php @@ -14,7 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . - /** * Unit tests for the multianswer question definition class. * @@ -199,7 +198,7 @@ class qtype_multianswer_question_test extends advanced_testcase { 2 => array('sub1_answer' => 'Owl', 'sub2_answer' => $right), ); $finalgrade = $question->compute_final_grade($responses, 1); - $this->assertEquals(1 / 3 * (1 - 0.2) + 2 / 3 * (1 - 2 * 0.2), $finalgrade); + $this->assertEqualsWithDelta(1 / 3 * (1 - 0.2) + 2 / 3 * (1 - 2 * 0.2), $finalgrade, question_testcase::GRADE_DELTA); // Get subquestion 1 right at 3rd try and subquestion 2 right at 2nd try. $responses = array(0 => array('sub1_answer' => 'Dog', 'sub2_answer' => $wrong), @@ -208,7 +207,7 @@ class qtype_multianswer_question_test extends advanced_testcase { 3 => array('sub1_answer' => 'Owl', 'sub2_answer' => $right), ); $finalgrade = $question->compute_final_grade($responses, 1); - $this->assertEquals(1 / 3 * (1 - 2 * 0.2) + 2 / 3 * (1 - 0.2), $finalgrade); + $this->assertEqualsWithDelta(1 / 3 * (1 - 2 * 0.2) + 2 / 3 * (1 - 0.2), $finalgrade, question_testcase::GRADE_DELTA); // Get subquestion 1 right at 4th try and subquestion 2 right at 1st try. $responses = array(0 => array('sub1_answer' => 'Dog', 'sub2_answer' => $right), @@ -217,7 +216,7 @@ class qtype_multianswer_question_test extends advanced_testcase { 3 => array('sub1_answer' => 'Owl', 'sub2_answer' => $right), ); $finalgrade = $question->compute_final_grade($responses, 1); - $this->assertEquals(1 / 3 * (1 - 3 * 0.2) + 2 / 3, $finalgrade); + $this->assertEqualsWithDelta(1 / 3 * (1 - 3 * 0.2) + 2 / 3, $finalgrade, question_testcase::GRADE_DELTA); // Get subquestion 1 right at 4th try and subquestion 2 right 3rd try. // Subquestion 2 was right at 1st try, but last change is at 3rd try. @@ -227,7 +226,7 @@ class qtype_multianswer_question_test extends advanced_testcase { 3 => array('sub1_answer' => 'Owl', 'sub2_answer' => $right), ); $finalgrade = $question->compute_final_grade($responses, 1); - $this->assertEquals(1 / 3 * (1 - 3 * 0.2) + 2 / 3 * (1 - 2 * 0.2), $finalgrade); + $this->assertEqualsWithDelta(1 / 3 * (1 - 3 * 0.2) + 2 / 3 * (1 - 2 * 0.2), $finalgrade, question_testcase::GRADE_DELTA); // Incomplete responses. Subquestion 1 is right at 4th try and subquestion 2 at 3rd try. $responses = array(0 => array('sub1_answer' => 'Dog'), @@ -236,6 +235,6 @@ class qtype_multianswer_question_test extends advanced_testcase { 3 => array('sub1_answer' => 'Owl', 'sub2_answer' => $right), ); $finalgrade = $question->compute_final_grade($responses, 1); - $this->assertEquals(1 / 3 * (1 - 3 * 0.2) + 2 / 3 * (1 - 2 * 0.2), $finalgrade); + $this->assertEqualsWithDelta(1 / 3 * (1 - 3 * 0.2) + 2 / 3 * (1 - 2 * 0.2), $finalgrade, question_testcase::GRADE_DELTA); } }