Browse Source

improved unit tests for ICU message formatter

tags/2.0.0-beta
Carsten Brandt 11 years ago
parent
commit
2eb5abbfcb
  1. 5
      framework/yii/i18n/MessageFormatter.php
  2. 87
      tests/unit/framework/i18n/MessageFormatterTest.php

5
framework/yii/i18n/MessageFormatter.php

@ -15,6 +15,7 @@ namespace yii\i18n;
* substituted. * substituted.
* *
* @author Alexander Makarov <sam@rmcreative.ru> * @author Alexander Makarov <sam@rmcreative.ru>
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0 * @since 2.0
*/ */
class MessageFormatter extends \MessageFormatter class MessageFormatter extends \MessageFormatter
@ -55,7 +56,7 @@ class MessageFormatter extends \MessageFormatter
} }
/** /**
* Replace named placeholders with numeric placeholders. * Replace named placeholders with numeric placeholders and quote unused.
* *
* @param string $pattern The pattern string to replace things into. * @param string $pattern The pattern string to replace things into.
* @param array $args The array of values to insert into the format string. * @param array $args The array of values to insert into the format string.
@ -65,7 +66,7 @@ class MessageFormatter extends \MessageFormatter
{ {
$map = array_flip(array_keys($args)); $map = array_flip(array_keys($args));
// parsing pattern base on ICU grammar: // parsing pattern based on ICU grammar:
// http://icu-project.org/apiref/icu4c/classMessageFormat.html#details // http://icu-project.org/apiref/icu4c/classMessageFormat.html#details
$parts = explode('{', $pattern); $parts = explode('{', $pattern);
$c = count($parts); $c = count($parts);

87
tests/unit/framework/i18n/MessageFormatterTest.php

@ -22,18 +22,19 @@ class MessageFormatterTest extends TestCase
const SUBJECT = 'сабж'; const SUBJECT = 'сабж';
const SUBJECT_VALUE = 'Answer to the Ultimate Question of Life, the Universe, and Everything'; const SUBJECT_VALUE = 'Answer to the Ultimate Question of Life, the Universe, and Everything';
public function testNamedArguments() public function patterns()
{ {
$expected = self::SUBJECT_VALUE.' is '.self::N_VALUE; return array(
array(
$result = MessageFormatter::formatMessage('en_US', '{'.self::SUBJECT.'} is {'.self::N.', number}', array( '{'.self::SUBJECT.'} is {'.self::N.', number}', // pattern
self::SUBJECT_VALUE.' is '.self::N_VALUE, // expected
array( // params
self::N => self::N_VALUE, self::N => self::N_VALUE,
self::SUBJECT => self::SUBJECT_VALUE, self::SUBJECT => self::SUBJECT_VALUE,
)); )
),
$this->assertEquals($expected, $result); array(<<<_MSG_
$pattern = <<<_MSG_
{gender_of_host, select, {gender_of_host, select,
female {{num_guests, plural, offset:1 female {{num_guests, plural, offset:1
=0 {{host} does not give a party.} =0 {{host} does not give a party.}
@ -50,53 +51,83 @@ class MessageFormatterTest extends TestCase
=1 {{host} invites {guest} to their party.} =1 {{host} invites {guest} to their party.}
=2 {{host} invites {guest} and one other person to their party.} =2 {{host} invites {guest} and one other person to their party.}
other {{host} invites {guest} and # other people to their party.}}}} other {{host} invites {guest} and # other people to their party.}}}}
_MSG_; _MSG_
$result = MessageFormatter::formatMessage('en_US', $pattern, array( ,
'ralph invites beep and 3 other people to his party.',
array(
'gender_of_host' => 'male', 'gender_of_host' => 'male',
'num_guests' => 4, 'num_guests' => 4,
'host' => 'ralph', 'host' => 'ralph',
'guest' => 'beep' 'guest' => 'beep'
)); )
$this->assertEquals('ralph invites beep and 3 other people to his party.', $result); ),
$pattern = '{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!'; array(
$result = MessageFormatter::formatMessage('en_US', $pattern, array( '{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!',
'Alexander is male and he loves Yii!',
array(
'name' => 'Alexander', 'name' => 'Alexander',
'gender' => 'male', 'gender' => 'male',
)); ),
$this->assertEquals('Alexander is male and he loves Yii!', $result); ),
// verify pattern in select does not get replaced // verify pattern in select does not get replaced
$pattern = '{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!'; array(
$result = MessageFormatter::formatMessage('en_US', $pattern, array( '{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!',
'Alexander is male and he loves Yii!',
array(
'name' => 'Alexander', 'name' => 'Alexander',
'gender' => 'male', 'gender' => 'male',
// following should not be replaced // following should not be replaced
'he' => 'wtf', 'he' => 'wtf',
'she' => 'wtf', 'she' => 'wtf',
'it' => 'wtf', 'it' => 'wtf',
)); )
$this->assertEquals('Alexander is male and he loves Yii!', $result); ),
// verify pattern in select message gets replaced // verify pattern in select message gets replaced
$pattern = '{name} is {gender} and {gender, select, female{she} male{{he}} other{it}} loves Yii!'; array(
$result = MessageFormatter::formatMessage('en_US', $pattern, array( '{name} is {gender} and {gender, select, female{she} male{{he}} other{it}} loves Yii!',
'Alexander is male and wtf loves Yii!',
array(
'name' => 'Alexander', 'name' => 'Alexander',
'gender' => 'male', 'gender' => 'male',
'he' => 'wtf', 'he' => 'wtf',
'she' => 'wtf', 'she' => 'wtf',
)); ),
$this->assertEquals('Alexander is male and wtf loves Yii!', $result); ),
// some parser specific verifications // some parser specific verifications
$pattern = '{gender} and {gender, select, female{she} male{{he}} other{it}} loves {nr, number} is {gender}!'; array(
$result = MessageFormatter::formatMessage('en_US', $pattern, array( '{gender} and {gender, select, female{she} male{{he}} other{it}} loves {nr, number} is {gender}!',
'male and wtf loves 42 is male!',
array(
'nr' => 42, 'nr' => 42,
'gender' => 'male', 'gender' => 'male',
'he' => 'wtf', 'he' => 'wtf',
'she' => 'wtf', 'she' => 'wtf',
)); ),
$this->assertEquals('male and wtf loves 42 is male!', $result); ),
);
}
/**
* @dataProvider patterns
*/
public function testNamedArgumentsStatic($pattern, $expected, $args)
{
$result = MessageFormatter::formatMessage('en_US', $pattern, $args);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider patterns
*/
public function testNamedArgumentsObject($pattern, $expected, $args)
{
$formatter = new MessageFormatter('en_US', $pattern);
$result = $formatter->format($args);
$this->assertEquals($expected, $result);
} }
public function testInsufficientArguments() public function testInsufficientArguments()

Loading…
Cancel
Save