Skip to content

Commit

Permalink
Fix mutation coverage (#485)
Browse files Browse the repository at this point in the history
Fix mutation tests
  • Loading branch information
veewee authored Sep 3, 2024
1 parent b899ad0 commit 8e8ca30
Show file tree
Hide file tree
Showing 11 changed files with 491 additions and 133 deletions.
198 changes: 101 additions & 97 deletions composer.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions config/infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
},
"DecrementInteger": {
"ignore": [
"Psl\\DataStructure\\PriorityQueue::peek"
"Psl\\DataStructure\\PriorityQueue::peek",
"Psl\\DateTime\\TemporalConvenienceMethodsTrait::since"
]
},
"FunctionCallRemoval": {
Expand All @@ -42,7 +43,8 @@
},
"IncrementInteger": {
"ignore": [
"Psl\\DataStructure\\PriorityQueue::peek"
"Psl\\DataStructure\\PriorityQueue::peek",
"Psl\\DateTime\\TemporalConvenienceMethodsTrait::since"
]
},
"LogicalNot": {
Expand All @@ -58,6 +60,7 @@
},
"Throw_": {
"ignore": [
"Psl\\DateTime\\DateTime::__construct",
"Psl\\File\\ReadHandle::__construct",
"Psl\\File\\WriteHandle::__construct",
"Psl\\File\\ReadWriteHandle::__construct",
Expand Down
9 changes: 4 additions & 5 deletions src/Psl/DateTime/DateTimeConvenienceMethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,15 @@ public function plusMonths(int $months): static
return $this;
}

if (0 > $months) {
if ($months < 1) {
return $this->minusMonths(-$months);
}

$plus_years = Math\div($months, MONTHS_PER_YEAR);
$months_left = $months - ($plus_years * MONTHS_PER_YEAR);
$target_month = $this->getMonth() + $months_left;

if ($target_month > 12) {
if ($target_month > MONTHS_PER_YEAR) {
$plus_years++;
$target_month = $target_month - MONTHS_PER_YEAR;
}
Expand Down Expand Up @@ -438,7 +438,7 @@ public function minusMonths(int $months): static
return $this;
}

if (0 > $months) {
if ($months < 1) {
return $this->plusMonths(-$months);
}

Expand Down Expand Up @@ -616,10 +616,9 @@ public function toString(null|DateStyle $date_style = null, null|TimeStyle $time
$timestamp = $this->getTimestamp();

/**
* @psalm-suppress InvalidOperand
* @psalm-suppress ImpureMethodCall
*/
return Internal\create_intl_date_formatter($date_style, $time_style, null, $timezone ?? $this->getTimezone(), $locale)
->format($timestamp->getSeconds() + ($timestamp->getNanoseconds() / NANOSECONDS_PER_SECOND));
->format($timestamp->getSeconds());
}
}
29 changes: 13 additions & 16 deletions src/Psl/DateTime/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,27 +674,24 @@ public function toString(int $max_decimals = 3): string
$sec_sign = $this->seconds < 0 || $this->nanoseconds < 0 ? '-' : '';
$sec = Math\abs($this->seconds);

/** @var list<array{string, string}> $values */
$values = [
[((string) $this->hours), 'hour(s)'],
[((string) $this->minutes), 'minute(s)'],
[$sec_sign . ((string) $sec) . $decimal_part, 'second(s)'],
];

// $end is the sizeof($values), use static value for better performance.
$end = 3;
while ($end > 0 && $values[$end - 1][0] === '0') {
--$end;
$containsHours = $this->hours !== 0;
$containsMinutes = $this->minutes !== 0;
$concatenatedSeconds = $sec_sign . ((string) $sec) . $decimal_part;
$containsSeconds = $concatenatedSeconds !== '0';

/** @var list<string> $output */
$output = [];
if ($containsHours) {
$output[] = ((string) $this->hours) . ' hour(s)';
}

$start = 0;
while ($start < $end && $values[$start][0] === '0') {
++$start;
if ($containsMinutes || ($containsHours && $containsSeconds)) {
$output[] = ((string) $this->minutes) . ' minute(s)';
}

$output = [];
for ($i = $start; $i < $end; ++$i) {
$output[] = $values[$i][0] . ' ' . $values[$i][1];
if ($containsSeconds) {
$output[] = $concatenatedSeconds . ' second(s)';
}

return ([] === $output) ? '0 second(s)' : Str\join($output, ', ');
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/Collection/MutableSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function testArrayAccess(): void

static::assertTrue(isset($set['foo']));
static::assertSame('foo', $set['foo']);

unset($set['foo']);
static::assertFalse(isset($set['foo']));

Expand Down Expand Up @@ -191,6 +191,16 @@ public function testOffsetGetThrowsForInvalidOffsetType(): void
$set[false];
}

public function testFromArrayKeysConstructor()
{
$set = MutableSet::fromArrayKeys(['foo' => 1, 'bar' => 1, 'baz' => 1]);

static::assertCount(3, $set);
static::assertTrue($set->contains('foo'));
static::assertTrue($set->contains('bar'));
static::assertTrue($set->contains('baz'));
}

/**
* @template T of array-key
*
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/Collection/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ protected function createFromList(array $items): Set
{
return Set::fromArray($items);
}

public function testFromArrayKeysConstructor()
{
$set = Set::fromArrayKeys(['foo' => 1, 'bar' => 1, 'baz' => 1]);

static::assertCount(3, $set);
static::assertTrue($set->contains('foo'));
static::assertTrue($set->contains('bar'));
static::assertTrue($set->contains('baz'));
}
}
Loading

0 comments on commit 8e8ca30

Please sign in to comment.