Skip to content

Commit

Permalink
Enhancement: Allow comparing Minors
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Dec 26, 2023
1 parent 27bbfab commit bda87bf
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,11 @@ declare(strict_types=1);
use Ergebnis\Version;

$one = Version\Minor::fromString('1');
$two = Version\Minor::fromString('1');
$three = Version\Minor::fromString('2');
$two = Version\Minor::fromString('2');

$one->equals($two); // true
$one->equals($three); // false

$one->equals($two); // true
$one->compare($two); // -1
$one->compare($one); // 0
$two->compare($one); // 1
```

### Create a `Patch` from an `int`
Expand Down
1 change: 1 addition & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<code>provideInvalidStringValue</code>
<code>provideValidStringValue</code>
<code>provideValueAndBumpedValue</code>
<code>provideValueOtherValueAndResult</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Unit/PatchTest.php">
Expand Down
4 changes: 2 additions & 2 deletions src/Minor.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public function bump(): self
return new self((string) ($valueCastedToInt + 1));
}

public function equals(self $other): bool
public function compare(self $other): int
{
return $this->value === $other->value;
return $this->value <=> $other->value;
}
}
50 changes: 37 additions & 13 deletions test/Unit/MinorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,47 @@ public static function provideValueAndBumpedValue(): \Generator
}
}

public function testEqualsReturnsFalseWhenValuesAreDifferent(): void
{
$faker = self::faker()->unique();

$one = Minor::fromString((string) $faker->numberBetween(0));
$two = Minor::fromString((string) $faker->numberBetween(0));
#[Framework\Attributes\DataProvider('provideValueOtherValueAndResult')]
public function testCompareReturnsResultOfComparingValues(
string $value,
string $otherValue,
int $result,
): void {
$one = Minor::fromString($value);
$two = Minor::fromString($otherValue);

self::assertFalse($one->equals($two));
self::assertSame($result, $one->compare($two));
}

public function testEqualsReturnsTrueWhenValueIsSame(): void
/**
* @return \Generator<string, array{0: string, 1: string, 2: int}>
*/
public static function provideValueOtherValueAndResult(): \Generator
{
$value = (string) self::faker()->numberBetween(0);

$one = Minor::fromString($value);
$two = Minor::fromString($value);
$values = [
'less' => [
'0',
'1',
-1,
],
'same' => [
'1',
'1',
0,
],
'greater' => [
'2',
'1',
1,
],
];

self::assertTrue($one->equals($two));
foreach ($values as $key => [$value, $otherValue, $result]) {
yield $key => [
$value,
$otherValue,
$result,
];
}
}
}

0 comments on commit bda87bf

Please sign in to comment.