diff --git a/src/Assert.php b/src/Assert.php index fd47936..2c94244 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -4,6 +4,7 @@ namespace Testo; +use Testo\Assert\DataType\AssertFloat; use Testo\Assert\DataType\AssertInt; use Testo\Assert\DataType\AssertString; use Testo\Assert\State\AssertException; @@ -251,6 +252,21 @@ public static function int( return new AssertInt($actual); } + /** + * Asserts that the given value is of `float` data type. + * + * @param mixed $actual The actual value to check for `float` data type. + * @throws AssertException when the assertion fails. + */ + public static function float( + mixed $actual, + ): AssertFloat { + \is_float($actual) or StaticState::fail(AssertTypeFailure::create('float', $actual)); + + StaticState::log('Assert type int for ' . Support::stringify($actual)); + return new AssertFloat($actual); + } + /** * Fails the test. * diff --git a/src/Assert/DataType/AssertFloat.php b/src/Assert/DataType/AssertFloat.php new file mode 100644 index 0000000..86dc326 --- /dev/null +++ b/src/Assert/DataType/AssertFloat.php @@ -0,0 +1,19 @@ +value < $max) { + StaticState::log('Assert `' . $this->value . '` is less than `' . $max . '`', $message); + return $this; + } + + StaticState::fail(AssertException::compare( + $max, + $this->value, + $message, + pattern: 'Failed asserting that value `%2$s` is less than `%1$s`.', + showDiff: false, + )); + } + + /** + * Asserts that the numeric value is less than or equal to given maximum. + * + * @param int|float $max Maximum threshold to compare with. + * @param string $message Optional message for the assertion. + * @throws AssertException when the assertion fails. + */ + public function lessThanOrEqual(int|float $max, string $message = ''): self + { + if ($this->value <= $max) { + StaticState::log('Assert `' . $this->value . '` is less than or equal to `' . $max . '`', $message); + return $this; + } + + StaticState::fail(AssertException::compare( + $max, + $this->value, + $message, + pattern: 'Failed asserting that value `%2$s` is less than or equal to `%1$s`.', + showDiff: false, + )); + } } diff --git a/tests/Assert/Self/AssertInt.php b/tests/Assert/Self/AssertInt.php deleted file mode 100644 index b2db2d1..0000000 --- a/tests/Assert/Self/AssertInt.php +++ /dev/null @@ -1,47 +0,0 @@ -greaterThan(41); - - Expect::exception(Assert\State\AssertException::class); - Assert::int(42)->greaterThan(43); - } - - #[Test] - public function checkIntGreaterThanOrEqual(): void - { - // actual is greater than or equal to min threshold - Assert::int(42)->greaterThanOrEqual(41); - Assert::int(42)->greaterThanOrEqual(42); - - Expect::exception(Assert\State\AssertException::class); - Assert::int(42)->greaterThan(43); - } -} diff --git a/tests/Assert/Self/AssertNumeric.php b/tests/Assert/Self/AssertNumeric.php new file mode 100644 index 0000000..782ca79 --- /dev/null +++ b/tests/Assert/Self/AssertNumeric.php @@ -0,0 +1,85 @@ +greaterThan(41); + Assert::float(42.1)->greaterThan(42.0); + + Expect::exception(Assert\State\AssertException::class); + Assert::int(42)->greaterThan(43); + + Expect::exception(Assert\State\AssertException::class); + Assert::float(42.1)->greaterThan(42.2); + } + + #[Test] + public function checkGreaterThanOrEqual(): void + { + // actual is greater than or equal to min threshold + Assert::int(42)->greaterThanOrEqual(41); + Assert::int(42)->greaterThanOrEqual(42); + + Assert::float(42.1)->greaterThanOrEqual(42.0); + Assert::float(42.1)->greaterThanOrEqual(42.1); + + Expect::exception(Assert\State\AssertException::class); + Assert::int(42)->greaterThanOrEqual(43); + + Expect::exception(Assert\State\AssertException::class); + Assert::float(42.1)->greaterThanOrEqual(43.0); + } + + #[Test] + public function checkLessThan(): void + { + // actual is less than max threshold + Assert::int(42)->lessThan(43); + Assert::float(42.1)->lessThan(42.2); + + Expect::exception(Assert\State\AssertException::class); + Assert::int(42)->lessThan(42); + + Expect::exception(Assert\State\AssertException::class); + Assert::int(42.1)->lessThan(42.0); + } + + #[Test] + public function checkLessThanOrEqual(): void + { + // actual is less than or equal to max threshold + Assert::int(41)->lessThanOrEqual(42); + Assert::int(42)->lessThanOrEqual(42); + + Assert::float(42.0)->lessThanOrEqual(42.1); + Assert::float(42.1)->lessThanOrEqual(42.1); + + Expect::exception(Assert\State\AssertException::class); + Assert::int(43)->lessThanOrEqual(42); + + Expect::exception(Assert\State\AssertException::class); + Assert::float(42.1)->lessThanOrEqual(42.0); + } +}