ParsingTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. class ParsingTest extends PHPUnit_Framework_TestCase
  3. {
  4. public function test_extract_plural_forms_header_from_po_header()
  5. {
  6. $parser = new gettext_reader(null);
  7. // It defaults to a "Western-style" plural header.
  8. $this->assertEquals(
  9. 'nplurals=2; plural=n == 1 ? 0 : 1;',
  10. $parser->extract_plural_forms_header_from_po_header("")
  11. );
  12. // Extracting it from the middle of the header works.
  13. $this->assertEquals(
  14. 'nplurals=1; plural=0;',
  15. $parser->extract_plural_forms_header_from_po_header(
  16. "Content-type: text/html; charset=UTF-8\n"
  17. ."Plural-Forms: nplurals=1; plural=0;\n"
  18. ."Last-Translator: nobody\n"
  19. )
  20. );
  21. // It's also case-insensitive.
  22. $this->assertEquals(
  23. 'nplurals=1; plural=0;',
  24. $parser->extract_plural_forms_header_from_po_header(
  25. "PLURAL-forms: nplurals=1; plural=0;\n"
  26. )
  27. );
  28. // It falls back to default if it's not on a separate line.
  29. $this->assertEquals(
  30. 'nplurals=2; plural=n == 1 ? 0 : 1;',
  31. $parser->extract_plural_forms_header_from_po_header(
  32. "Content-type: text/html; charset=UTF-8" // note the missing \n here
  33. ."Plural-Forms: nplurals=1; plural=0;\n"
  34. ."Last-Translator: nobody\n"
  35. )
  36. );
  37. }
  38. /**
  39. * @expectedException InvalidArgumentException
  40. */
  41. public function test_select_string_disallows_nonint_numbers()
  42. {
  43. $pofile_data = ''
  44. ."msgid \"\"\n"
  45. ."msgstr \"\"\n"
  46. ."\"Content-Type: text/plain; charset=utf-8\\n\"\n"
  47. ."\"Plural-Forms: nplurals=2; plural= n == 1 ? 0 : 1;\\n\"\n";
  48. $mofile = tempnam(sys_get_temp_dir(), "pg");
  49. $msgfmt = popen("msgfmt -o $mofile -", "w");
  50. fwrite($msgfmt, $pofile_data);
  51. pclose($msgfmt);
  52. $modata = new CachedFileReader($mofile);
  53. unlink($mofile);
  54. $parser = new gettext_reader($modata);
  55. // It defaults to a "Western-style" plural header.
  56. $this->assertEquals(
  57. 'nplurals=2; plural=n == 1 ? 0 : 1;',
  58. $parser->extract_plural_forms_header_from_po_header("")
  59. );
  60. $new_tempfile = tempnam(sys_get_temp_dir(), "pg");
  61. $parser->select_string(
  62. "(file_put_contents('$new_tempfile', 'boom'))"
  63. );
  64. $this->assertEquals("", file_get_contents($new_tempfile));
  65. unlink($new_tempfile);
  66. }
  67. /**
  68. * @dataProvider data_provider_test_npgettext
  69. */
  70. public function test_npgettext($number, $expected)
  71. {
  72. $parser = new gettext_reader(null);
  73. $result = $parser->npgettext(
  74. "context",
  75. "%d pig went to the market\n",
  76. "%d pigs went to the market\n",
  77. $number
  78. );
  79. $this->assertSame($expected, $result);
  80. }
  81. public static function data_provider_test_npgettext()
  82. {
  83. return array(
  84. array(1, "%d pig went to the market\n"),
  85. array(2, "%d pigs went to the market\n"),
  86. );
  87. }
  88. }