Skip to content

Commit

Permalink
feat: add assert float equals
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Feb 27, 2019
1 parent 5114b2a commit bd63286
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Asserts.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

trait Asserts
{
const DEFAULT_PRECISION = 6;

public function assertJsonSubset($expected, $actual, string $message = null)
{
$actual = \json_encode($actual);
Expand All @@ -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)));
}
}

0 comments on commit bd63286

Please sign in to comment.