Begin restructuring NodaTimeSplitterTest to use data providers

This commit is contained in:
Joshua Ramon Enslin 2025-02-21 10:31:21 +01:00
parent 3409ec7afe
commit dbfa0df17f
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

View File

@ -9,6 +9,7 @@ declare(strict_types = 1);
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small; use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\DataProvider;
/** /**
* This script contains tests for the time name splitter. * This script contains tests for the time name splitter.
@ -1068,20 +1069,17 @@ final class NodaTimeSplitterTest extends TestCase {
} }
/** /**
* Test to check special cases can be parsed. * Data provider for time names that the time splitter should not
* be able to split.
* *
* @author Joshua Ramon Enslin <joshua@museum-digital.de> * @return array<array{0: string, 1: 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}}>
* @group ValidOutput
* @small
*
* @return void
*/ */
public function testSplittingFromTill():void { public static function timeNamesToSplitFromTillProvider():array {
// Regular $output = [];
$output = NodaTimeSplitter::attempt_splitting_from_till("2004.01.-2004.02.");
self::assertNotEmpty($output); foreach ([
self::assertEquals($output, [ "2004.01.-2004.02." => [
'start_name' => "Januar 2004", 'start_name' => "Januar 2004",
'end_name' => "Februar 2004", 'end_name' => "Februar 2004",
"start_year" => '2004', "start_year" => '2004',
@ -1092,12 +1090,8 @@ final class NodaTimeSplitterTest extends TestCase {
"counting_time_month" => "02", "counting_time_month" => "02",
"counting_time_day" => "01", "counting_time_day" => "01",
"counting_time_bcce" => "+", "counting_time_bcce" => "+",
]); ],
"2004.01.-02." => [
// Rewritten / Hungarian YYYY.MM.-MM.
$output = NodaTimeSplitter::attempt_splitting_from_till("2004.01.-02.");
self::assertNotEmpty($output);
self::assertEquals($output, [
'start_name' => "Januar 2004", 'start_name' => "Januar 2004",
'end_name' => "Februar 2004", 'end_name' => "Februar 2004",
"start_year" => '2004', "start_year" => '2004',
@ -1108,19 +1102,32 @@ final class NodaTimeSplitterTest extends TestCase {
"counting_time_month" => "02", "counting_time_month" => "02",
"counting_time_day" => "01", "counting_time_day" => "01",
"counting_time_bcce" => "+", "counting_time_bcce" => "+",
]); ],
"2003.04-05." => [
// Rewritten / Hungarian YYYY.MM.-MM. 'start_name' => "April 2003",
$output = NodaTimeSplitter::attempt_splitting_from_till("2003.04-05."); 'end_name' => "Mai 2003",
self::assertNotEmpty($output); "start_year" => '2003',
"end_year" => '2003',
// Rewritten / Hungarian YYYY.MM.-MM. 'start_date' => '2003-04-01',
$output = NodaTimeSplitter::attempt_splitting_from_till("1981. július-augusztus"); 'end_date' => '2003-05-31',
self::assertNotEmpty($output); "counting_time_year" => "2003",
"counting_time_month" => "05",
$output = NodaTimeSplitter::attempt_splitting_from_till("2019.03.14.,04.15."); "counting_time_day" => "01",
self::assertNotEmpty($output); "counting_time_bcce" => "+",
self::assertEquals($output, [ ],
"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", 'start_name' => "14.03.2019",
'end_name' => "15.04.2019", 'end_name' => "15.04.2019",
"start_year" => '2019', "start_year" => '2019',
@ -1131,11 +1138,8 @@ final class NodaTimeSplitterTest extends TestCase {
"counting_time_month" => "03", "counting_time_month" => "03",
"counting_time_day" => "30", "counting_time_day" => "30",
"counting_time_bcce" => "+", "counting_time_bcce" => "+",
]); ],
"14.-15.03.2019" => [
$output = NodaTimeSplitter::attempt_splitting_from_till("14.-15.03.2019");
self::assertNotEmpty($output);
self::assertEquals($output, [
'start_name' => "14.03.2019", 'start_name' => "14.03.2019",
'end_name' => "15.03.2019", 'end_name' => "15.03.2019",
"start_year" => '2019', "start_year" => '2019',
@ -1146,11 +1150,8 @@ final class NodaTimeSplitterTest extends TestCase {
"counting_time_month" => "03", "counting_time_month" => "03",
"counting_time_day" => "15", "counting_time_day" => "15",
"counting_time_bcce" => "+", "counting_time_bcce" => "+",
]); ],
"3.-7.9.1819" => [
$output = NodaTimeSplitter::attempt_splitting_from_till("3.-7.9.1819");
self::assertNotEmpty($output);
self::assertEquals($output, [
'start_name' => "03.09.1819", 'start_name' => "03.09.1819",
'end_name' => "07.09.1819", 'end_name' => "07.09.1819",
"start_year" => '1819', "start_year" => '1819',
@ -1161,11 +1162,8 @@ final class NodaTimeSplitterTest extends TestCase {
"counting_time_month" => "09", "counting_time_month" => "09",
"counting_time_day" => "05", "counting_time_day" => "05",
"counting_time_bcce" => "+", "counting_time_bcce" => "+",
]); ],
"3.-15.9.1819" => [
$output = NodaTimeSplitter::attempt_splitting_from_till("3.-15.9.1819");
self::assertNotEmpty($output);
self::assertEquals($output, [
'start_name' => "03.09.1819", 'start_name' => "03.09.1819",
'end_name' => "15.09.1819", 'end_name' => "15.09.1819",
"start_year" => '1819', "start_year" => '1819',
@ -1176,11 +1174,8 @@ final class NodaTimeSplitterTest extends TestCase {
"counting_time_month" => "09", "counting_time_month" => "09",
"counting_time_day" => "09", "counting_time_day" => "09",
"counting_time_bcce" => "+", "counting_time_bcce" => "+",
]); ],
"14.-15.9.1819" => [
$output = NodaTimeSplitter::attempt_splitting_from_till("14.-15.9.1819");
self::assertNotEmpty($output);
self::assertEquals($output, [
'start_name' => "14.09.1819", 'start_name' => "14.09.1819",
'end_name' => "15.09.1819", 'end_name' => "15.09.1819",
"start_year" => '1819', "start_year" => '1819',
@ -1191,11 +1186,8 @@ final class NodaTimeSplitterTest extends TestCase {
"counting_time_month" => "09", "counting_time_month" => "09",
"counting_time_day" => "15", "counting_time_day" => "15",
"counting_time_bcce" => "+", "counting_time_bcce" => "+",
]); ],
"14.-15.11.1819" => [
$output = NodaTimeSplitter::attempt_splitting_from_till("14.-15.11.1819");
self::assertNotEmpty($output);
self::assertEquals($output, [
'start_name' => "14.11.1819", 'start_name' => "14.11.1819",
'end_name' => "15.11.1819", 'end_name' => "15.11.1819",
"start_year" => '1819', "start_year" => '1819',
@ -1206,11 +1198,8 @@ final class NodaTimeSplitterTest extends TestCase {
"counting_time_month" => "11", "counting_time_month" => "11",
"counting_time_day" => "15", "counting_time_day" => "15",
"counting_time_bcce" => "+", "counting_time_bcce" => "+",
]); ],
"2019.03.14-15" => [
$output = NodaTimeSplitter::attempt_splitting_from_till("2019.03.14-15");
self::assertNotEmpty($output);
self::assertEquals($output, [
'start_name' => "14.03.2019", 'start_name' => "14.03.2019",
'end_name' => "15.03.2019", 'end_name' => "15.03.2019",
"start_year" => '2019', "start_year" => '2019',
@ -1221,12 +1210,8 @@ final class NodaTimeSplitterTest extends TestCase {
"counting_time_month" => "03", "counting_time_month" => "03",
"counting_time_day" => "15", "counting_time_day" => "15",
"counting_time_bcce" => "+", "counting_time_bcce" => "+",
]); ],
"17-19.század" => [
// Rewritten / Hungarian YYYY.MM.-MM.
$output = NodaTimeSplitter::attempt_splitting_from_till("17-19.század");
self::assertNotEmpty($output);
self::assertEquals($output, [
'start_name' => "1601", 'start_name' => "1601",
'end_name' => "1900", 'end_name' => "1900",
"start_year" => '1601', "start_year" => '1601',
@ -1237,12 +1222,8 @@ final class NodaTimeSplitterTest extends TestCase {
"counting_time_month" => "01", "counting_time_month" => "01",
"counting_time_day" => "01", "counting_time_day" => "01",
"counting_time_bcce" => "+", "counting_time_bcce" => "+",
]); ],
"1950-60-as évek" => [
// Rewritten / 1950-60-as évek
$output = NodaTimeSplitter::attempt_splitting_from_till("1950-60-as évek");
self::assertNotEmpty($output);
self::assertEquals($output, [
'start_name' => "1950", 'start_name' => "1950",
'end_name' => "1969", 'end_name' => "1969",
"start_year" => '1950', "start_year" => '1950',
@ -1253,7 +1234,43 @@ final class NodaTimeSplitterTest extends TestCase {
"counting_time_month" => "01", "counting_time_month" => "01",
"counting_time_day" => "01", "counting_time_day" => "01",
"counting_time_bcce" => "+", "counting_time_bcce" => "+",
]); ],
] as $in => $out) {
$output[$in] = [$in, $out];
}
return $output;
}
/**
* Test to check special cases can be parsed.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
* @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
*/
#[DataProvider('timeNamesToSplitFromTillProvider')]
public function testSplittingFromTill(string $time_name, array $expected):void {
// Regular
$output = NodaTimeSplitter::attempt_splitting_from_till($time_name);
self::assertNotEmpty($output);
self::assertEquals($output, $expected);
}
/**
* Special cases for timespans.
*
* @return void
*/
public function testSpecialCasesForTimespans():void {
// Rewritten / 1950-60-as évek // Rewritten / 1950-60-as évek
$output = NodaTimeSplitter::attempt_splitting("1400-1100 v. Chr."); $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<array{0: string}>
*/
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. * Test to check whether the HTML page is correctly generated.
* *
@ -1278,54 +1330,16 @@ final class NodaTimeSplitterTest extends TestCase {
* @group ValidOutput * @group ValidOutput
* @small * @small
* *
* @param string $unsplittable_name Name that the time splitter should not
* be able to split.
*
* @return void * @return void
*/ */
public function testSplitDoesNotWorkWhenItShouldNot():void { #[DataProvider('nonSplittableNamesProvider')]
public function testSplitDoesNotWorkWhenItShouldNot(string $unsplittable_name):void {
$output = NodaTimeSplitter::attempt_splitting(""); $output = NodaTimeSplitter::attempt_splitting($unsplittable_name);
self::assertEmpty($output); self::assertEmpty($output, "Time name $unsplittable_name should not be split. It was split into:" . PHP_EOL . PHP_EOL . var_export($output, true));
$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);
} }
} }