Replace phpunit with pest

This commit is contained in:
2023-12-03 16:50:09 +01:00
parent 7ded60ce9c
commit eac27d04d1
12 changed files with 968 additions and 112 deletions

3
tests/AoC_2021Test.php Normal file
View File

@ -0,0 +1,3 @@
<?php
testYear(21);

3
tests/AoC_2022Test.php Normal file
View File

@ -0,0 +1,3 @@
<?php
testYear(22);

3
tests/AoC_2023Test.php Normal file
View File

@ -0,0 +1,3 @@
<?php
testYear(23);

82
tests/Pest.php Normal file
View File

@ -0,0 +1,82 @@
<?php
/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/
// uses(Tests\TestCase::class)->in('Feature');
/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/
use trizz\AdventOfCode\Solution;
expect()->extend('toBeOne', fn() => $this->toBe(1));
/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/
/**
* @return array<string, class-string>
*/
function loadSolutions(int $year): array
{
$classes = [];
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)) {
$classes["Year '".$year.' / Day '.$day] = $className;
}
}
}
return $classes;
}
function runTestForDay($class, string $name, $testDataMethod, $expectedResult, $isExampleData): void
{
$fullName = $name.' / '.($isExampleData ? 'Example' : 'Input');
test($fullName, function () use ($isExampleData, $class, $testDataMethod, $expectedResult) : void {
expect($class->{$testDataMethod}($isExampleData))->toBe($class::${$expectedResult});
})->skip(!$class->hasExampleData() || $class::${$expectedResult} === null);
}
function testYear(int $year): void
{
$solutions = loadSolutions($year);
foreach ($solutions as $name => $className) {
describe('Y'.$year, static function () use ($name, $className) : void {
/** @var Solution $class */
$class = new $className();
$class->loadData();
runTestForDay($class, $name.' / Part 1', 'part1Data', 'part1ExampleResult', true);
runTestForDay($class, $name.' / Part 2', 'part2Data', 'part2ExampleResult', true);
runTestForDay($class, $name.' / Part 1', 'part1Data', 'part1Result', false);
runTestForDay($class, $name.' / Part 2', 'part2Data', 'part2Result', false);
})->group('Y'.$year);
}
}

View File

@ -1,91 +0,0 @@
<?php
namespace Tests;
use PHPUnit\Framework\TestCase;
use trizz\AdventOfCode\Solution;
/**
* @internal
*/
final class SolutionsTest extends TestCase
{
/**
* @dataProvider loadSolutions
*/
public function testSolutionPart1Example(Solution $solution): void
{
$this->runPart($solution, part: 1, testExample: true);
}
/**
* @dataProvider loadSolutions
*
* @depends testSolutionPart1Example
*/
public function testSolutionPart1(Solution $solution): void
{
$this->runPart($solution, part: 1, testExample: false);
}
/**
* @dataProvider loadSolutions
*
* @depends testSolutionPart1
*/
public function testSolutionPart2Example(Solution $solution): void
{
$this->runPart($solution, part: 2, testExample: true);
}
/**
* @dataProvider loadSolutions
*
* @depends testSolutionPart2Example
*/
public function testSolutionPart2(Solution $solution): void
{
$this->runPart($solution, part: 2, testExample: false);
}
/**
* @return array<string, array<int, Solution>>
*/
public static 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)) {
/** @var Solution $class */
$class = new $className();
$class->loadData();
$classes["Year '".$year.' / Day '.$day] = [$class];
}
}
}
}
return $classes;
}
private function runPart(Solution $solution, int $part, bool $testExample): void
{
if (
($testExample && $solution->hasExampleData())
|| (!$testExample && $solution->hasData())
) {
$expectedResult = $solution::${'part'.$part.($testExample ? 'Example' : null).'Result'};
if ($expectedResult) {
$result = $solution->{'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.'.');
}
}
}