[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/t/ -> test_mo.php (source)

   1  <?php
   2  require_once ('init.php');
   3  
   4  class GP_Test_MO extends GP_UnitTestCase {
   5  
   6  	function test_mo_simple() {
   7          $mo = new MO();
   8          $mo->import_from_file('data/simple.mo');
   9          $this->assertEquals(array('Project-Id-Version' => 'WordPress 2.6-bleeding', 'Report-Msgid-Bugs-To' => 'wp-polyglots@lists.automattic.com'), $mo->headers);
  10          $this->assertEquals(2, count($mo->entries));
  11          $this->assertEquals(array('dyado'), $mo->entries['baba']->translations);
  12          $this->assertEquals(array('yes'), $mo->entries["kuku\nruku"]->translations);
  13      }
  14  
  15  	function test_mo_plural() {
  16          $mo = new MO();
  17          $mo->import_from_file('data/plural.mo');
  18          $this->assertEquals(1, count($mo->entries));
  19          $this->assertEquals(array("oney dragoney", "twoey dragoney", "manyey dragoney", "manyeyey dragoney", "manyeyeyey dragoney"), $mo->entries["one dragon"]->translations);
  20  
  21          $this->assertEquals('oney dragoney', $mo->translate_plural('one dragon', '%d dragons', 1));
  22          $this->assertEquals('twoey dragoney', $mo->translate_plural('one dragon', '%d dragons', 2));
  23          $this->assertEquals('twoey dragoney', $mo->translate_plural('one dragon', '%d dragons', -8));
  24  
  25  
  26          $mo->set_header('Plural-Forms', 'nplurals=5; plural=0');
  27          $this->assertEquals('oney dragoney', $mo->translate_plural('one dragon', '%d dragons', 1));
  28          $this->assertEquals('oney dragoney', $mo->translate_plural('one dragon', '%d dragons', 2));
  29          $this->assertEquals('oney dragoney', $mo->translate_plural('one dragon', '%d dragons', -8));
  30  
  31          $mo->set_header('Plural-Forms', 'nplurals=5; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;');
  32          $this->assertEquals('oney dragoney', $mo->translate_plural('one dragon', '%d dragons', 1));
  33          $this->assertEquals('manyey dragoney', $mo->translate_plural('one dragon', '%d dragons', 11));
  34          $this->assertEquals('twoey dragoney', $mo->translate_plural('one dragon', '%d dragons', 3));
  35  
  36          $mo->set_header('Plural-Forms', 'nplurals=2; plural=n !=1;');
  37          $this->assertEquals('oney dragoney', $mo->translate_plural('one dragon', '%d dragons', 1));
  38          $this->assertEquals('twoey dragoney', $mo->translate_plural('one dragon', '%d dragons', 2));
  39          $this->assertEquals('twoey dragoney', $mo->translate_plural('one dragon', '%d dragons', -8));
  40      }
  41  
  42  	function test_mo_context() {
  43          $mo = new MO();
  44          $mo->import_from_file('data/context.mo');
  45          $this->assertEquals(2, count($mo->entries));
  46          $plural_entry = new Translation_Entry(array('singular' => 'one dragon', 'plural' => '%d dragons', 'translations' => array("oney dragoney", "twoey dragoney","manyey dragoney"), 'context' => 'dragonland'));
  47          $this->assertEquals($plural_entry, $mo->entries[$plural_entry->key()]);
  48          $this->assertEquals("dragonland", $mo->entries[$plural_entry->key()]->context);
  49  
  50          $single_entry = new Translation_Entry(array('singular' => 'one dragon', 'translations' => array("oney dragoney"), 'context' => 'not so dragon'));
  51          $this->assertEquals($single_entry, $mo->entries[$single_entry->key()]);
  52          $this->assertEquals("not so dragon", $mo->entries[$single_entry->key()]->context);
  53  
  54      }
  55  
  56  	function test_translations_merge() {
  57          $host = new Translations();
  58          $host->add_entry(new Translation_Entry(array('singular' => 'pink',)));
  59          $host->add_entry(new Translation_Entry(array('singular' => 'green',)));
  60          $guest = new Translations();
  61          $guest->add_entry(new Translation_Entry(array('singular' => 'green',)));
  62          $guest->add_entry(new Translation_Entry(array('singular' => 'red',)));
  63          $host->merge_with($guest);
  64          $this->assertEquals(3, count($host->entries));
  65          $this->assertEquals(array(), array_diff(array('pink', 'green', 'red'), array_keys($host->entries)));
  66      }
  67  
  68  	function test_export_mo_file() {
  69          $entries = array();
  70          $entries[] = new Translation_Entry(array('singular' => 'pink',
  71              'translations' => array('розов')));
  72          $no_translation_entry = new Translation_Entry(array('singular' => 'grey'));
  73          $entries[] = new Translation_Entry(array('singular' => 'green', 'plural' => 'greens',
  74              'translations' => array('зелен', 'зелени')));
  75          $entries[] = new Translation_Entry(array('singular' => 'red', 'context' => 'color',
  76              'translations' => array('червен')));
  77          $entries[] = new Translation_Entry(array('singular' => 'red', 'context' => 'bull',
  78              'translations' => array('бик')));
  79          $entries[] = new Translation_Entry(array('singular' => 'maroon', 'plural' => 'maroons', 'context' => 'context',
  80              'translations' => array('пурпурен', 'пурпурни')));
  81  
  82          $mo = new MO();
  83          $mo->set_header('Project-Id-Version', 'Baba Project 1.0');
  84          foreach($entries as $entry) {
  85              $mo->add_entry($entry);
  86          }
  87          $mo->add_entry($no_translation_entry);
  88  
  89          $temp_fn = $this->temp_filename();
  90          $mo->export_to_file($temp_fn);
  91  
  92          $again = new MO();
  93          $again->import_from_file($temp_fn);
  94  
  95          $this->assertEquals(count($entries), count($again->entries));
  96          foreach($entries as $entry) {
  97              $this->assertEquals($entry, $again->entries[$entry->key()]);
  98          }
  99      }
 100  
 101      function test_export_should_not_include_empty_translations() {
 102          $entries = array(  );
 103          $mo = new MO;
 104          $mo->add_entry( array( 'singular' => 'baba', 'translations' => array( '', '' ) ) );
 105  
 106          $temp_fn = $this->temp_filename();
 107          $mo->export_to_file( $temp_fn );
 108  
 109          $again = new MO();
 110          $again->import_from_file($temp_fn);
 111  
 112          $this->assertEquals( 0, count( $again->entries ) );
 113      }
 114  
 115  	function test_nplurals_with_backslashn() {
 116          $mo = new MO();
 117          $mo->import_from_file('data/bad_nplurals.mo');
 118          $this->assertEquals('%d foro', $mo->translate_plural('%d forum', '%d forums', 1));
 119          $this->assertEquals('%d foros', $mo->translate_plural('%d forum', '%d forums', 2));
 120          $this->assertEquals('%d foros', $mo->translate_plural('%d forum', '%d forums', -1));
 121      }
 122  
 123  	function disabled_test_performance() {
 124          $start = microtime(true);
 125          $mo = new MO();
 126          $mo->import_from_file('data/de_DE-2.8.mo');
 127          // echo "\nPerformance: ".(microtime(true) - $start)."\n";
 128      }
 129  
 130  	function test_overloaded_mb_functions() {
 131          // skipIf() method skips the whole test case, not only this method
 132          // that's why we are skipping it the stupid way
 133          if ((ini_get("mbstring.func_overload") & 2) != 0) {
 134              $mo = new MO();
 135              $mo->import_from_file('data/overload.mo');
 136              $this->assertEquals(array('Табло'), $mo->entries['Dashboard']->translations);
 137          }
 138      }
 139  
 140  	function test_load_pot_file() {
 141          $mo = new MO();
 142          $this->assertEquals( false, $mo->import_from_file('data/mo.pot') );
 143      }
 144  }


Generated: Sat May 25 03:59:55 2013 Hosted by follow the white rabbit.