Refactor framework to support more years
This commit is contained in:
@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use AdventOfCode21\AbstractCommand;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
abstract class AbstractTestCase extends TestCase
|
||||
{
|
||||
public AbstractCommand $command;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->command = $this->setupDay();
|
||||
}
|
||||
|
||||
public function testPart1(): void
|
||||
{
|
||||
$this->assertSame(static::$part1ExampleResult, $this->command->part1ExampleResult());
|
||||
$this->assertSame(static::$part1Result, $this->command->part1Result());
|
||||
}
|
||||
|
||||
public function testPart2(): void
|
||||
{
|
||||
$this->assertSame(static::$part2ExampleResult, $this->command->part2ExampleResult());
|
||||
$this->assertSame(static::$part2Result, $this->command->part2Result());
|
||||
}
|
||||
|
||||
abstract public function setupDay(): AbstractCommand;
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use AdventOfCode21\Day1;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class Day1Test extends AbstractTestCase
|
||||
{
|
||||
public static int $part1ExampleResult = 7;
|
||||
public static int $part1Result = 1688;
|
||||
|
||||
public static int $part2ExampleResult = 5;
|
||||
public static int $part2Result = 1728;
|
||||
|
||||
public function setupDay(): Day1
|
||||
{
|
||||
return new class() extends Day1 {
|
||||
use ReturnTestableResults;
|
||||
};
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use AdventOfCode21\Day2;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class Day2Test extends AbstractTestCase
|
||||
{
|
||||
public static int $part1ExampleResult = 150;
|
||||
public static int $part1Result = 1654760;
|
||||
|
||||
public static int $part2ExampleResult = 900;
|
||||
public static int $part2Result = 1956047400;
|
||||
|
||||
public function setupDay(): Day2
|
||||
{
|
||||
return new class() extends Day2 {
|
||||
use ReturnTestableResults;
|
||||
};
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use AdventOfCode21\Day3;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class Day3Test extends AbstractTestCase
|
||||
{
|
||||
public static int $part1ExampleResult = 198;
|
||||
public static int $part1Result = 3309596;
|
||||
|
||||
public static string $part2ExampleResult = 'n/a';
|
||||
public static string $part2Result = 'n/a';
|
||||
|
||||
public function setupDay(): Day3
|
||||
{
|
||||
return new class() extends Day3 {
|
||||
use ReturnTestableResults;
|
||||
};
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use AdventOfCode21\Day4;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class Day4Test extends AbstractTestCase
|
||||
{
|
||||
public static int $part1ExampleResult = 4512;
|
||||
public static int $part1Result = 60368;
|
||||
|
||||
public static int $part2ExampleResult = 1924;
|
||||
public static int $part2Result = 17435;
|
||||
|
||||
public function setupDay(): Day4
|
||||
{
|
||||
return new class() extends Day4 {
|
||||
use ReturnTestableResults;
|
||||
};
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use AdventOfCode21\Day6;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class Day6Test extends AbstractTestCase
|
||||
{
|
||||
public static int $part1ExampleResult = 5934;
|
||||
public static int $part1Result = 350917;
|
||||
|
||||
public static int $part2ExampleResult = 26984457539;
|
||||
public static int $part2Result = 1592918715629;
|
||||
|
||||
public function setupDay(): Day6
|
||||
{
|
||||
return new class() extends Day6 {
|
||||
use ReturnTestableResults;
|
||||
};
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use AdventOfCode21\Day7;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class Day7Test extends AbstractTestCase
|
||||
{
|
||||
public static int $part1ExampleResult = 37;
|
||||
public static int $part1Result = 344297;
|
||||
|
||||
public static int $part2ExampleResult = 168;
|
||||
public static int $part2Result = 97164301;
|
||||
|
||||
public function setupDay(): Day7
|
||||
{
|
||||
return new class() extends Day7 {
|
||||
use ReturnTestableResults;
|
||||
};
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Symfony\Component\Console\Input\StringInput;
|
||||
use Symfony\Component\Console\Output\NullOutput;
|
||||
|
||||
trait ReturnTestableResults
|
||||
{
|
||||
public function part1ExampleResult(): int|string|null
|
||||
{
|
||||
$this->initialize(new StringInput(''), new NullOutput());
|
||||
|
||||
return $this->part1($this->exampleData);
|
||||
}
|
||||
|
||||
public function part1Result(): int|string|null
|
||||
{
|
||||
$this->initialize(new StringInput(''), new NullOutput());
|
||||
|
||||
return $this->part1($this->data);
|
||||
}
|
||||
|
||||
public function part2ExampleResult(): int|string|null
|
||||
{
|
||||
$this->initialize(new StringInput(''), new NullOutput());
|
||||
|
||||
return $this->part2($this->exampleData);
|
||||
}
|
||||
|
||||
public function part2Result(): int|string|null
|
||||
{
|
||||
$this->initialize(new StringInput(''), new NullOutput());
|
||||
|
||||
return $this->part2($this->data);
|
||||
}
|
||||
}
|
82
tests/SolutionsTest.php
Normal file
82
tests/SolutionsTest.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use trizz\AdventOfCode\ExecuteDay;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use trizz\AdventOfCode\Solution;
|
||||
|
||||
class SolutionsTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider loadSolutions
|
||||
*/
|
||||
public function testSolutionPart1Example(Solution $class): void
|
||||
{
|
||||
$this->runPart($class, part: 1, testExample: true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider loadSolutions
|
||||
* @depends testSolutionPart1Example
|
||||
*/
|
||||
public function testSolutionPart1(Solution $class): void
|
||||
{
|
||||
$this->runPart($class, part: 1, testExample: false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider loadSolutions
|
||||
* @depends testSolutionPart1
|
||||
*/
|
||||
public function testSolutionPart2Example(Solution $class): void
|
||||
{
|
||||
$this->runPart($class, part: 2, testExample: true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider loadSolutions
|
||||
* @depends testSolutionPart2Example
|
||||
*/
|
||||
public function testSolutionPart2(Solution $class): void
|
||||
{
|
||||
$this->runPart($class, part: 2, testExample: false);
|
||||
}
|
||||
|
||||
private function runPart(Solution $class, int $part, bool $testExample): void
|
||||
{
|
||||
if (
|
||||
($testExample && $class->hasExampleData())
|
||||
|| (!$testExample && $class->hasData())
|
||||
) {
|
||||
$expectedResult = $class::${'part'.$part.($testExample ? 'Example' : null).'Result'};
|
||||
if ($expectedResult) {
|
||||
$result = $class->{'part'.$part.'Data'}(useExampleData: $testExample);
|
||||
self::assertSame($expectedResult, $result);
|
||||
} else {
|
||||
$this->markTestSkipped('No '.($testExample ? 'example' : 'expected').' data for part '.$part.'.');
|
||||
}
|
||||
} else {
|
||||
$this->markTestSkipped('No example and expected data for part '.$part.'.');
|
||||
}
|
||||
}
|
||||
|
||||
public function loadSolutions(): array
|
||||
{
|
||||
$classes = [];
|
||||
for ($year = 15; $year <= date('y'); $year++) {
|
||||
if (is_dir(__DIR__.'/../src/Y'.$year)) {
|
||||
for ($day = 1; $day < 26; $day++) {
|
||||
$className = sprintf("trizz\\AdventOfCode\\Y%d\\Day%d", $year, $day);
|
||||
if (class_exists($className)) {
|
||||
$class = new $className();
|
||||
$class->loadData();
|
||||
$classes['Year \''.$year.' / Day '.$day] = [$class];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $classes;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user