test: strengthen battlefield boundary coverage
This commit is contained in:
+22
-10
@@ -13,7 +13,7 @@ final readonly class Battlefield
|
|||||||
private array $terrain;
|
private array $terrain;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array<array-key, mixed> $terrain
|
* @param array<string, Terrain> $terrain
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public int $width,
|
public int $width,
|
||||||
@@ -27,10 +27,29 @@ final readonly class Battlefield
|
|||||||
$validatedTerrain = [];
|
$validatedTerrain = [];
|
||||||
|
|
||||||
foreach ($terrain as $key => $terrainType) {
|
foreach ($terrain as $key => $terrainType) {
|
||||||
if (!$terrainType instanceof Terrain) {
|
if (!self::isTerrain($terrainType)) {
|
||||||
throw new InvalidArgumentException("Invalid terrain value at coordinate {$key}.");
|
throw new InvalidArgumentException("Invalid terrain value at coordinate {$key}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$position = self::positionFromTerrainKey($key);
|
||||||
|
|
||||||
|
if (!$this->contains($position)) {
|
||||||
|
throw new InvalidArgumentException("Terrain coordinate {$key} is outside the battlefield.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$validatedTerrain[$key] = $terrainType;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->terrain = $validatedTerrain;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function isTerrain(mixed $terrain): bool
|
||||||
|
{
|
||||||
|
return $terrain instanceof Terrain;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function positionFromTerrainKey(mixed $key): Position
|
||||||
|
{
|
||||||
if (!is_string($key) || preg_match('/^(\d+):(\d+)$/', $key, $matches) !== 1) {
|
if (!is_string($key) || preg_match('/^(\d+):(\d+)$/', $key, $matches) !== 1) {
|
||||||
throw new InvalidArgumentException("Invalid terrain coordinate: {$key}.");
|
throw new InvalidArgumentException("Invalid terrain coordinate: {$key}.");
|
||||||
}
|
}
|
||||||
@@ -41,14 +60,7 @@ final readonly class Battlefield
|
|||||||
throw new InvalidArgumentException("Invalid terrain coordinate: {$key}.");
|
throw new InvalidArgumentException("Invalid terrain coordinate: {$key}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->contains($position)) {
|
return $position;
|
||||||
throw new InvalidArgumentException("Terrain coordinate {$key} is outside the battlefield.");
|
|
||||||
}
|
|
||||||
|
|
||||||
$validatedTerrain[$key] = $terrainType;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->terrain = $validatedTerrain;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function contains(Position $position): bool
|
public function contains(Position $position): bool
|
||||||
|
|||||||
@@ -19,6 +19,22 @@ final class BattlefieldTest extends TestCase
|
|||||||
new Battlefield(7, 16);
|
new Battlefield(7, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testItAcceptsMinimumAndMaximumDimensions(): void
|
||||||
|
{
|
||||||
|
$minimum = new Battlefield(8, 8);
|
||||||
|
$maximum = new Battlefield(16, 16);
|
||||||
|
|
||||||
|
self::assertSame([8, 8], [$minimum->width, $minimum->height]);
|
||||||
|
self::assertSame([16, 16], [$maximum->width, $maximum->height]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItRejectsDimensionsAboveTheSupportedRange(): void
|
||||||
|
{
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
|
||||||
|
new Battlefield(16, 17);
|
||||||
|
}
|
||||||
|
|
||||||
public function testItRejectsNonCanonicalTerrainCoordinates(): void
|
public function testItRejectsNonCanonicalTerrainCoordinates(): void
|
||||||
{
|
{
|
||||||
$this->expectException(InvalidArgumentException::class);
|
$this->expectException(InvalidArgumentException::class);
|
||||||
@@ -37,9 +53,8 @@ final class BattlefieldTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->expectException(InvalidArgumentException::class);
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
|
||||||
$terrain = ['0:0' => 'forest'];
|
// @phpstan-ignore argument.type
|
||||||
|
new Battlefield(8, 8, ['0:0' => 'forest']);
|
||||||
new Battlefield(8, 8, $terrain);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testReachableRejectsAStartOutsideTheBattlefield(): void
|
public function testReachableRejectsAStartOutsideTheBattlefield(): void
|
||||||
@@ -90,4 +105,17 @@ final class BattlefieldTest extends TestCase
|
|||||||
'0:1' => 2,
|
'0:1' => 2,
|
||||||
], $reachable);
|
], $reachable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testReachableReplacesAHighCostRouteWithALowerCostDetour(): void
|
||||||
|
{
|
||||||
|
$battlefield = new Battlefield(8, 8, [
|
||||||
|
'1:0' => Terrain::Forest,
|
||||||
|
'2:0' => Terrain::Forest,
|
||||||
|
'3:0' => Terrain::Forest,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$reachable = $battlefield->reachable(new Position(0, 0), 7, []);
|
||||||
|
|
||||||
|
self::assertSame(6, $reachable['4:0']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user