aoc/tests/Lubiana/Aoc/Y2023/D02/SetTest.php

60 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2023-12-10 11:49:11 +00:00
<?php
namespace Lubiana\Aoc\Y2023\D02;
use PHPUnit\Framework\TestCase;
final class SetTest extends TestCase
{
/**
* Test class :: Set
* Method :: fromStringInput
* This method takes an input string and parses it to create an instance of the class.
* The input string is expected to contain an integer and a color separated by a space.
* Test cases:
* Test that the method parses correctly formatted strings.
* Test that it gracefully handles strings with excessive whitespace.
*/
public function testFromStringInput()
{
// Test correctly formatted input
$input = "2 red";
$set = Set::fromStringInput($input);
$this->assertInstanceOf(Set::class, $set);
$this->assertEquals(2, $set->amount);
$this->assertEquals('red', $set->color);
// Test input with extra whitespace
$input = " 3 blue ";
$set = Set::fromStringInput($input);
$this->assertInstanceOf(Set::class, $set);
$this->assertEquals(3, $set->amount);
$this->assertEquals('blue', $set->color);
}
/**
* Test case for the fromInputList method of the Set class.
*/
public function testFromInputList()
{
$input = "2 Black,3 White,4 Red";
$sets = Set::fromInputList($input);
$this->assertIsArray($sets);
$this->assertCount(3, $sets);
$this->assertInstanceOf(Set::class, $sets[0]);
$this->assertSame(2, $sets[0]->amount);
$this->assertSame('Black', $sets[0]->color);
$this->assertInstanceOf(Set::class, $sets[1]);
$this->assertSame(3, $sets[1]->amount);
$this->assertSame('White', $sets[1]->color);
$this->assertInstanceOf(Set::class, $sets[2]);
$this->assertSame(4, $sets[2]->amount);
$this->assertSame('Red', $sets[2]->color);
}
}