The current tests in testGetPluralFormat()
do test the plural evaluator. But since the code is fairly complex, it can use some additional test. Below is the code I have used to manually test the code during development. It can be used to create additional test. Take a few complex formula's and compare the evaluatePlural() result with the computed result. Of course instead of eval() the test should use hardcoded formula's
<?php
Drupal::state()->delete('locale.translation.plurals');
$plurals[0] = 'nplurals=1; plural=0;';
$eval_plural[0] = '(0)';
$plurals[1] = 'nplurals=2; plural=(n > 1);';
$eval_plural[1] = '(n > 1)';
$plurals[2] = 'nplurals=2; plural=(n!=1);';
$eval_plural[2] = 'n!=1';
$plurals[3] = 'nplurals=2; plural=(((n==1)||((n%10)==1))?(0):1);';
$eval_plural[3] = '(((n==1)||((n%10)==1))?(0):1)';
$plurals[4] = 'nplurals=3; plural=((((n%10)==1)&&((n%100)!=11))?(0):(((((n%10)>=2)&&((n%10)<=4))&&(((n%100)<10)||((n%100)>=20)))?(1):2));';
$eval_plural[4] = '((((n%10)==1)&&((n%100)!=11))?(0):(((((n%10)>=2)&&((n%10)<=4))&&(((n%100)<10)||((n%100)>=20)))?(1):2))';
$plurals[5] = 'nplurals=3; plural=((n==1)?(0):(((n>=2)&&(n<=4))?(1):2));';
$eval_plural[5] = '((n==1)?(0):(((n>=2)&&(n<=4))?(1):2))';
$plurals[6] = 'nplurals=3; plural=((n==1)?(0):(((n==0)||(((n%100)>0)&&((n%100)<20)))?(1):2));';
$eval_plural[6] = '((n==1)?(0):(((n==0)||(((n%100)>0)&&((n%100)<20)))?(1):2))';
$plurals[7] = 'nplurals=3; plural=((n==1)?(0):(((((n%10)>=2)&&((n%10)<=4))&&(((n%100)<10)||((n%100)>=20)))?(1):2));';
$eval_plural[7] = '((n==1)?(0):(((((n%10)>=2)&&((n%10)<=4))&&(((n%100)<10)||((n%100)>=20)))?(1):2))';
$plurals[8] = 'nplurals=4; plural=(((n==1)||(n==11))?(0):(((n==2)||(n==12))?(1):(((n>2)&&(n<20))?(2):3)));';
$eval_plural[8] = '(((n==1)||(n==11))?(0):(((n==2)||(n==12))?(1):(((n>2)&&(n<20))?(2):3)))';
$plurals[9] = 'nplurals=4; plural=(((n%100)==1)?(0):(((n%100)==2)?(1):((((n%100)==3)||((n%100)==4))?(2):3)));';
$eval_plural[9] = '(((n%100)==1)?(0):(((n%100)==2)?(1):((((n%100)==3)||((n%100)==4))?(2):3)))';
$plurals[10] = 'nplurals=5; plural=((n==1)?(0):((n==2)?(1):((n<7)?(2):((n<11)?(3):4))));';
$eval_plural[10] = '((n==1)?(0):((n==2)?(1):((n<7)?(2):((n<11)?(3):4))))';
$plurals[11] = 'nplurals=6; plural=((n==1)?(0):((n==0)?(1):((n==2)?(2):((((n%100)>=3)&&((n%100)<=10))?(3):((((n%100)>=11)&&((n%100)<=99))?(4):5)))));';
$eval_plural[11] = '((n==1)?(0):((n==0)?(1):((n==2)?(2):((((n%100)>=3)&&((n%100)<=10))?(3):((((n%100)>=11)&&((n%100)<=99))?(4):5)))))';
$fault = FALSE;
for ($i=0; $i < count($eval_plural); $i++) {
if (!isset($plurals[$i])) continue;
$p = new Drupal\Component\Gettext\PoHeader;
$parsed = $p->parsePluralForms($plurals[$i]);
list($nplurals, $new_plural) = $parsed;
for ($n = 0; $n <= 199; $n++) {
$new_plural_n = isset($new_plural[$n]) ? $new_plural[$n] : $new_plural['default'];
$formula = strtr($eval_plural[$i], array('n' => $n));
$old_plural[$n] = @eval('return intval(' . $formula . ');');
if ((int)$old_plural[$n] != (int)$new_plural_n) {
debug('Difference found at ' . $n . ': ' . $old_plural[$n] . ' versus ' . $new_plural_n);
$fault = TRUE;
}
}
}
if (!$fault) {
debug ('All is well that ends well!');
}