From bd63286058019daf380b59ffb4bd487236103fc7 Mon Sep 17 00:00:00 2001 From: Jitendra Adhikari Date: Wed, 27 Feb 2019 18:32:49 +0700 Subject: [PATCH] feat: add assert float equals --- src/Asserts.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Asserts.php b/src/Asserts.php index 6504306..2bfa6aa 100644 --- a/src/Asserts.php +++ b/src/Asserts.php @@ -13,6 +13,8 @@ trait Asserts { + const DEFAULT_PRECISION = 6; + public function assertJsonSubset($expected, $actual, string $message = null) { $actual = \json_encode($actual); @@ -31,4 +33,20 @@ public function assertJsonSubsets($expected, /* more expected ...*/ $actual) $this->assertJsonSubset($expect, $actual, "Data set #$i: "); } } + + public function assertFloatEquals(float $expected, float $actual, int $precision = null, string $message = null) + { + $precision = $precision ?? static::DEFAULT_PRECISION; + + if (\function_exists('bccomp')) { + $this->assertSame(0, \bccomp($expected, $actual, $precision), $message); + + return; + } + + $expected = \round($expected, $precision); + $actual = \round($actual, $precision); + + $this->assertEquals($expected, $actual, $message, \pow(10, 0 - \abs($precision))); + } }