Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
19 changes: 19 additions & 0 deletions src/Assert/DataType/AssertFloat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Testo\Assert\DataType;

use Testo\Assert\Traits\NumericTrait;

/**
* Assertion utilities for integer data type.
*/
class AssertFloat
{
use NumericTrait;

public function __construct(
public float $value,
) {}
}
47 changes: 46 additions & 1 deletion src/Assert/Traits/NumericTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Testo\Assert\Traits;

use Testo\Assert\DataType\AssertInt;
use Testo\Assert\State\AssertException;
use Testo\Assert\StaticState;

Expand Down Expand Up @@ -59,4 +58,50 @@ public function greaterThanOrEqual(int|float $min, string $message = ''): self
showDiff: false,
));
}

/**
* Asserts that numeric value is less than the 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 lessThan(int|float $max, string $message = ''): self
{
if ($this->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,
));
}
}
47 changes: 0 additions & 47 deletions tests/Assert/Self/AssertInt.php

This file was deleted.

85 changes: 85 additions & 0 deletions tests/Assert/Self/AssertNumeric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Tests\Assert\Self;

use Testo\Assert;
use Testo\Attribute\Test;
use Testo\Expect;

/**
* Assertion examples.
*/
final class AssertNumeric
{
#[Test]
public function checkDataType(): void
{
// These assertions check incoming data type
Assert::int(42);
Assert::float(42.1);
}

#[Test]
public function checkGreaterThan(): void
{
// actual is greater than min threshold
Assert::int(42)->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);
}
}