From dbfa0df17fb1ad817528b1b54acee663263e2003 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Fri, 21 Feb 2025 10:31:21 +0100 Subject: [PATCH] Begin restructuring NodaTimeSplitterTest to use data providers --- tests/NodaTimeSplitterTest.php | 450 +++++++++++++++++---------------- 1 file changed, 232 insertions(+), 218 deletions(-) diff --git a/tests/NodaTimeSplitterTest.php b/tests/NodaTimeSplitterTest.php index 261746b..1379d32 100644 --- a/tests/NodaTimeSplitterTest.php +++ b/tests/NodaTimeSplitterTest.php @@ -9,6 +9,7 @@ declare(strict_types = 1); use PHPUnit\Framework\TestCase; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Small; +use PHPUnit\Framework\Attributes\DataProvider; /** * This script contains tests for the time name splitter. @@ -1067,6 +1068,181 @@ final class NodaTimeSplitterTest extends TestCase { } + /** + * Data provider for time names that the time splitter should not + * be able to split. + * + * @return array + */ + public static function timeNamesToSplitFromTillProvider():array { + + $output = []; + + foreach ([ + "2004.01.-2004.02." => [ + 'start_name' => "Januar 2004", + 'end_name' => "Februar 2004", + "start_year" => '2004', + "end_year" => '2004', + 'start_date' => '2004-01-01', + 'end_date' => '2004-02-31', + "counting_time_year" => "2004", + "counting_time_month" => "02", + "counting_time_day" => "01", + "counting_time_bcce" => "+", + ], + "2004.01.-02." => [ + 'start_name' => "Januar 2004", + 'end_name' => "Februar 2004", + "start_year" => '2004', + "end_year" => '2004', + 'start_date' => '2004-01-01', + 'end_date' => '2004-02-31', + "counting_time_year" => "2004", + "counting_time_month" => "02", + "counting_time_day" => "01", + "counting_time_bcce" => "+", + ], + "2003.04-05." => [ + 'start_name' => "April 2003", + 'end_name' => "Mai 2003", + "start_year" => '2003', + "end_year" => '2003', + 'start_date' => '2003-04-01', + 'end_date' => '2003-05-31', + "counting_time_year" => "2003", + "counting_time_month" => "05", + "counting_time_day" => "01", + "counting_time_bcce" => "+", + ], + "1981. július-augusztus" => [ + 'start_name' => "Juli 1981", + 'end_name' => "August 1981", + "start_year" => '1981', + "end_year" => '1981', + 'start_date' => '1981-07-01', + 'end_date' => '1981-08-31', + "counting_time_year" => "1981", + "counting_time_month" => "08", + "counting_time_day" => "01", + "counting_time_bcce" => "+", + ], + "2019.03.14.,04.15." => [ + 'start_name' => "14.03.2019", + 'end_name' => "15.04.2019", + "start_year" => '2019', + "end_year" => '2019', + 'start_date' => '2019-03-14', + 'end_date' => '2019-04-15', + "counting_time_year" => "2019", + "counting_time_month" => "03", + "counting_time_day" => "30", + "counting_time_bcce" => "+", + ], + "14.-15.03.2019" => [ + 'start_name' => "14.03.2019", + 'end_name' => "15.03.2019", + "start_year" => '2019', + "end_year" => '2019', + 'start_date' => '2019-03-14', + 'end_date' => '2019-03-15', + "counting_time_year" => "2019", + "counting_time_month" => "03", + "counting_time_day" => "15", + "counting_time_bcce" => "+", + ], + "3.-7.9.1819" => [ + 'start_name' => "03.09.1819", + 'end_name' => "07.09.1819", + "start_year" => '1819', + "end_year" => '1819', + 'start_date' => '1819-09-03', + 'end_date' => '1819-09-07', + "counting_time_year" => "1819", + "counting_time_month" => "09", + "counting_time_day" => "05", + "counting_time_bcce" => "+", + ], + "3.-15.9.1819" => [ + 'start_name' => "03.09.1819", + 'end_name' => "15.09.1819", + "start_year" => '1819', + "end_year" => '1819', + 'start_date' => '1819-09-03', + 'end_date' => '1819-09-15', + "counting_time_year" => "1819", + "counting_time_month" => "09", + "counting_time_day" => "09", + "counting_time_bcce" => "+", + ], + "14.-15.9.1819" => [ + 'start_name' => "14.09.1819", + 'end_name' => "15.09.1819", + "start_year" => '1819', + "end_year" => '1819', + 'start_date' => '1819-09-14', + 'end_date' => '1819-09-15', + "counting_time_year" => "1819", + "counting_time_month" => "09", + "counting_time_day" => "15", + "counting_time_bcce" => "+", + ], + "14.-15.11.1819" => [ + 'start_name' => "14.11.1819", + 'end_name' => "15.11.1819", + "start_year" => '1819', + "end_year" => '1819', + 'start_date' => '1819-11-14', + 'end_date' => '1819-11-15', + "counting_time_year" => "1819", + "counting_time_month" => "11", + "counting_time_day" => "15", + "counting_time_bcce" => "+", + ], + "2019.03.14-15" => [ + 'start_name' => "14.03.2019", + 'end_name' => "15.03.2019", + "start_year" => '2019', + "end_year" => '2019', + 'start_date' => '2019-03-14', + 'end_date' => '2019-03-15', + "counting_time_year" => "2019", + "counting_time_month" => "03", + "counting_time_day" => "15", + "counting_time_bcce" => "+", + ], + "17-19.század" => [ + 'start_name' => "1601", + 'end_name' => "1900", + "start_year" => '1601', + "end_year" => '1900', + 'start_date' => '1601-01-01', + 'end_date' => '1900-12-31', + "counting_time_year" => "1751", + "counting_time_month" => "01", + "counting_time_day" => "01", + "counting_time_bcce" => "+", + ], + "1950-60-as évek" => [ + 'start_name' => "1950", + 'end_name' => "1969", + "start_year" => '1950', + "end_year" => '1969', + 'start_date' => '1950-01-01', + 'end_date' => '1969-12-31', + "counting_time_year" => "1960", + "counting_time_month" => "01", + "counting_time_day" => "01", + "counting_time_bcce" => "+", + ], + ] as $in => $out) { + $output[$in] = [$in, $out]; + } + + return $output; + + } + /** * Test to check special cases can be parsed. * @@ -1074,186 +1250,27 @@ final class NodaTimeSplitterTest extends TestCase { * @group ValidOutput * @small * + * @param string $time_name Input time name. + * @param array{start_name: string, end_name: string, start_year: string, end_year: string, start_date: string, end_date: string, counting_time_year: string, counting_time_month: string, counting_time_day: string, counting_time_bcce: string} $expected Expected values. + * * @return void */ - public function testSplittingFromTill():void { + #[DataProvider('timeNamesToSplitFromTillProvider')] + public function testSplittingFromTill(string $time_name, array $expected):void { // Regular - $output = NodaTimeSplitter::attempt_splitting_from_till("2004.01.-2004.02."); + $output = NodaTimeSplitter::attempt_splitting_from_till($time_name); self::assertNotEmpty($output); - self::assertEquals($output, [ - 'start_name' => "Januar 2004", - 'end_name' => "Februar 2004", - "start_year" => '2004', - "end_year" => '2004', - 'start_date' => '2004-01-01', - 'end_date' => '2004-02-31', - "counting_time_year" => "2004", - "counting_time_month" => "02", - "counting_time_day" => "01", - "counting_time_bcce" => "+", - ]); + self::assertEquals($output, $expected); - // Rewritten / Hungarian YYYY.MM.-MM. - $output = NodaTimeSplitter::attempt_splitting_from_till("2004.01.-02."); - self::assertNotEmpty($output); - self::assertEquals($output, [ - 'start_name' => "Januar 2004", - 'end_name' => "Februar 2004", - "start_year" => '2004', - "end_year" => '2004', - 'start_date' => '2004-01-01', - 'end_date' => '2004-02-31', - "counting_time_year" => "2004", - "counting_time_month" => "02", - "counting_time_day" => "01", - "counting_time_bcce" => "+", - ]); + } - // Rewritten / Hungarian YYYY.MM.-MM. - $output = NodaTimeSplitter::attempt_splitting_from_till("2003.04-05."); - self::assertNotEmpty($output); - - // Rewritten / Hungarian YYYY.MM.-MM. - $output = NodaTimeSplitter::attempt_splitting_from_till("1981. július-augusztus"); - self::assertNotEmpty($output); - - $output = NodaTimeSplitter::attempt_splitting_from_till("2019.03.14.,04.15."); - self::assertNotEmpty($output); - self::assertEquals($output, [ - 'start_name' => "14.03.2019", - 'end_name' => "15.04.2019", - "start_year" => '2019', - "end_year" => '2019', - 'start_date' => '2019-03-14', - 'end_date' => '2019-04-15', - "counting_time_year" => "2019", - "counting_time_month" => "03", - "counting_time_day" => "30", - "counting_time_bcce" => "+", - ]); - - $output = NodaTimeSplitter::attempt_splitting_from_till("14.-15.03.2019"); - self::assertNotEmpty($output); - self::assertEquals($output, [ - 'start_name' => "14.03.2019", - 'end_name' => "15.03.2019", - "start_year" => '2019', - "end_year" => '2019', - 'start_date' => '2019-03-14', - 'end_date' => '2019-03-15', - "counting_time_year" => "2019", - "counting_time_month" => "03", - "counting_time_day" => "15", - "counting_time_bcce" => "+", - ]); - - $output = NodaTimeSplitter::attempt_splitting_from_till("3.-7.9.1819"); - self::assertNotEmpty($output); - self::assertEquals($output, [ - 'start_name' => "03.09.1819", - 'end_name' => "07.09.1819", - "start_year" => '1819', - "end_year" => '1819', - 'start_date' => '1819-09-03', - 'end_date' => '1819-09-07', - "counting_time_year" => "1819", - "counting_time_month" => "09", - "counting_time_day" => "05", - "counting_time_bcce" => "+", - ]); - - $output = NodaTimeSplitter::attempt_splitting_from_till("3.-15.9.1819"); - self::assertNotEmpty($output); - self::assertEquals($output, [ - 'start_name' => "03.09.1819", - 'end_name' => "15.09.1819", - "start_year" => '1819', - "end_year" => '1819', - 'start_date' => '1819-09-03', - 'end_date' => '1819-09-15', - "counting_time_year" => "1819", - "counting_time_month" => "09", - "counting_time_day" => "09", - "counting_time_bcce" => "+", - ]); - - $output = NodaTimeSplitter::attempt_splitting_from_till("14.-15.9.1819"); - self::assertNotEmpty($output); - self::assertEquals($output, [ - 'start_name' => "14.09.1819", - 'end_name' => "15.09.1819", - "start_year" => '1819', - "end_year" => '1819', - 'start_date' => '1819-09-14', - 'end_date' => '1819-09-15', - "counting_time_year" => "1819", - "counting_time_month" => "09", - "counting_time_day" => "15", - "counting_time_bcce" => "+", - ]); - - $output = NodaTimeSplitter::attempt_splitting_from_till("14.-15.11.1819"); - self::assertNotEmpty($output); - self::assertEquals($output, [ - 'start_name' => "14.11.1819", - 'end_name' => "15.11.1819", - "start_year" => '1819', - "end_year" => '1819', - 'start_date' => '1819-11-14', - 'end_date' => '1819-11-15', - "counting_time_year" => "1819", - "counting_time_month" => "11", - "counting_time_day" => "15", - "counting_time_bcce" => "+", - ]); - - $output = NodaTimeSplitter::attempt_splitting_from_till("2019.03.14-15"); - self::assertNotEmpty($output); - self::assertEquals($output, [ - 'start_name' => "14.03.2019", - 'end_name' => "15.03.2019", - "start_year" => '2019', - "end_year" => '2019', - 'start_date' => '2019-03-14', - 'end_date' => '2019-03-15', - "counting_time_year" => "2019", - "counting_time_month" => "03", - "counting_time_day" => "15", - "counting_time_bcce" => "+", - ]); - - // Rewritten / Hungarian YYYY.MM.-MM. - $output = NodaTimeSplitter::attempt_splitting_from_till("17-19.század"); - self::assertNotEmpty($output); - self::assertEquals($output, [ - 'start_name' => "1601", - 'end_name' => "1900", - "start_year" => '1601', - "end_year" => '1900', - 'start_date' => '1601-01-01', - 'end_date' => '1900-12-31', - "counting_time_year" => "1751", - "counting_time_month" => "01", - "counting_time_day" => "01", - "counting_time_bcce" => "+", - ]); - - // Rewritten / 1950-60-as évek - $output = NodaTimeSplitter::attempt_splitting_from_till("1950-60-as évek"); - self::assertNotEmpty($output); - self::assertEquals($output, [ - 'start_name' => "1950", - 'end_name' => "1969", - "start_year" => '1950', - "end_year" => '1969', - 'start_date' => '1950-01-01', - 'end_date' => '1969-12-31', - "counting_time_year" => "1960", - "counting_time_month" => "01", - "counting_time_day" => "01", - "counting_time_bcce" => "+", - ]); + /** + * Special cases for timespans. + * + * @return void + */ + public function testSpecialCasesForTimespans():void { // Rewritten / 1950-60-as évek $output = NodaTimeSplitter::attempt_splitting("1400-1100 v. Chr."); @@ -1271,6 +1288,41 @@ final class NodaTimeSplitterTest extends TestCase { } + /** + * Data provider for time names that the time splitter should not + * be able to split. + * + * @return array + */ + public static function nonSplittableNamesProvider():array { + + $output = [ + 'empty string' => [''], + ]; + + foreach ([ + "1.2.2020-2.2.2020", + "2020 Januar 2-2020 Februar 2", + "2020 Januar-2020 Februar", + "Januar-Februar", + "13.13.2022", + "2022-13-13", + "40.10.2022", + "2022-10-40", + "6;November 1978", + "65497028c51eb", + "6552cf08b0196 test tag", + "1978. július7", + "1978. július 7 elött", + "Anfang September 1903", + ] as $entry) { + $output[$entry] = [$entry]; + } + + return $output; + + } + /** * Test to check whether the HTML page is correctly generated. * @@ -1278,54 +1330,16 @@ final class NodaTimeSplitterTest extends TestCase { * @group ValidOutput * @small * + * @param string $unsplittable_name Name that the time splitter should not + * be able to split. + * * @return void */ - public function testSplitDoesNotWorkWhenItShouldNot():void { + #[DataProvider('nonSplittableNamesProvider')] + public function testSplitDoesNotWorkWhenItShouldNot(string $unsplittable_name):void { - $output = NodaTimeSplitter::attempt_splitting(""); - self::assertEmpty($output); - - $output = NodaTimeSplitter::attempt_splitting("1.2.2020-2.2.2020"); - self::assertEmpty($output); - - $output = NodaTimeSplitter::attempt_splitting("2020 Januar 2-2020 Februar 2"); - self::assertEmpty($output); - - $output = NodaTimeSplitter::attempt_splitting("2020 Januar-2020 Februar"); - self::assertEmpty($output); - - $output = NodaTimeSplitter::attempt_splitting("Januar-Februar"); - self::assertEmpty($output); - - $output = NodaTimeSplitter::attempt_splitting("13.13.2022"); - self::assertEmpty($output); - $output = NodaTimeSplitter::attempt_splitting("2022-13-13"); - self::assertEmpty($output); - - $output = NodaTimeSplitter::attempt_splitting("40.10.2022"); - self::assertEmpty($output); - $output = NodaTimeSplitter::attempt_splitting("2022-10-40"); - self::assertEmpty($output); - - $output = NodaTimeSplitter::attempt_splitting("6;November 1978"); - self::assertEmpty($output); - - $output = NodaTimeSplitter::attempt_splitting("65497028c51eb"); - self::assertEmpty($output); - $output = NodaTimeSplitter::attempt_splitting("6552cf08b0196 test tag"); - self::assertEmpty($output); - - $output = NodaTimeSplitter::attempt_splitting("1978. július7"); - self::assertEmpty($output); - - $output = NodaTimeSplitter::attempt_splitting("1978. július 7 elött"); - self::assertEmpty($output); - - $output = NodaTimeSplitter::attempt_splitting("Anfang September 1903"); - self::assertEmpty($output); - - # $output = NodaTimeSplitter::attempt_splitting("Nach 1944-1964"); - # self::assertEmpty($output); + $output = NodaTimeSplitter::attempt_splitting($unsplittable_name); + self::assertEmpty($output, "Time name $unsplittable_name should not be split. It was split into:" . PHP_EOL . PHP_EOL . var_export($output, true)); } }