1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
<?php
namespace Gettext\Languages\Test;
use Exception;
use Gettext\Languages\Category;
use PHPUnit\Framework\Attributes\DataProvider;
class RulesTest extends TestCase
{
public static function providerTestRules()
{
$testData = array();
foreach (array('php', 'json') as $format) {
foreach (self::readData($format) as $locale => $info) {
foreach ($info['examples'] as $rule => $numbers) {
$testData[] = array(
$format,
$locale,
$info['formula'],
$info['cases'],
$numbers,
$rule,
);
}
}
}
return $testData;
}
#[DataProvider('providerTestRules')]
public function testRules($format, $locale, $formula, $allCases, $numbers, $expectedCase)
{
$expectedCaseIndex = in_array($expectedCase, $allCases);
foreach (Category::expandExamples($numbers) as $number) {
$numericFormula = preg_replace('/\bn\b/', (string) $number, $formula);
$extraneousChars = preg_replace('/^[\d %!=<>&\|()?:]+$/', '', $numericFormula);
$this->assertSame('', $extraneousChars, "The formula '{$numericFormula}' contains extraneous characters: '{$extraneousChars}' (format: {$format})");
$caseIndex = @eval("return (({$numericFormula}) === true) ? 1 : ((({$numericFormula}) === false) ? 0 : ({$numericFormula}));");
$caseIndexType = gettype($caseIndex);
$this->assertSame('integer', $caseIndexType, "Error evaluating the numeric formula '{$numericFormula}' (format: {$format})");
$this->assertArrayHasKey($caseIndex, $allCases, "The formula '{$formula}' evaluated for {$number} gave an out-of-range case index ({$caseIndex}) (format: {$format})");
$case = $allCases[$caseIndex];
$this->assertSame($expectedCase, $case, "The formula '{$formula}' evaluated for {$number} resulted in '{$case}' ({$caseIndex}) instead of '{$expectedCase}' ({$expectedCaseIndex}) (format: {$format})");
}
}
public static function providerTestExamplesExist()
{
$testData = array();
foreach (array('php', 'json') as $format) {
foreach (self::readData($format) as $locale => $info) {
foreach ($info['cases'] as $case) {
$testData[] = array(
$format,
$locale,
$case,
$info['examples'],
);
}
}
}
return $testData;
}
#[DataProvider('providerTestExamplesExist')]
public function testExamplesExist($format, $locale, $case, $examples)
{
$this->assertArrayHasKey($case, $examples, "The language '{$locale}' does not have tests for the case '{$case}' (format: {$format})");
}
private static function readData($format)
{
static $data = array();
if (!isset($data[$format])) {
$filename = GETTEXT_LANGUAGES_TESTDIR . '/data.' . $format;
switch ($format) {
case 'php':
$data[$format] = require $filename;
break;
case 'json':
$data[$format] = json_decode(file_get_contents($filename), true);
break;
default:
throw new Exception("Unhandled format: {$format}");
}
}
return $data[$format];
}
}
|