ExtractTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. require_once dirname( dirname( __FILE__ ) ) . '/extract.php';
  3. class ExtractTest extends PHPUnit_Framework_TestCase {
  4. function setUp() {
  5. $this->extractor = new StringExtractor;
  6. $this->extractor->rules = array(
  7. '__' => array('string'),
  8. );
  9. }
  10. function test_with_just_a_string() {
  11. $expected = new Translation_Entry( array( 'singular' => 'baba', 'references' => array('baba.php:1') ) );
  12. $result = $this->extractor->extract_from_code('<?php __("baba"); ?>', 'baba.php' );
  13. $this->assertEquals( $expected, $result->entries['baba'] );
  14. }
  15. function test_entry_from_call_simple() {
  16. $entry = $this->extractor->entry_from_call( array( 'name' => '__', 'args' => array('baba') ), 'baba.php' );
  17. $this->assertEquals( $entry, new Translation_Entry( array( 'singular' => 'baba' ) ) );
  18. }
  19. function test_entry_from_call_nonexisting_function() {
  20. $entry = $this->extractor->entry_from_call( array( 'name' => 'f', 'args' => array('baba') ), 'baba.php' );
  21. $this->assertEquals( $entry, null );
  22. }
  23. function test_entry_from_call_too_few_args() {
  24. $entry = $this->extractor->entry_from_call( array( 'name' => '__', 'args' => array() ), 'baba.php' );
  25. $this->assertEquals( $entry, null );
  26. }
  27. function test_entry_from_call_non_expected_null_arg() {
  28. $this->extractor->rules = array( '_nx' => array( 'singular', 'plural', 'context' ) );
  29. $entry = $this->extractor->entry_from_call( array( 'name' => '_nx', 'args' => array('%s baba', null, 'noun') ), 'baba.php' );
  30. $this->assertEquals( $entry, null );
  31. }
  32. function test_entry_from_call_more_args_should_be_ok() {
  33. $this->extractor->rules = array( '__' => array('string') );
  34. $entry = $this->extractor->entry_from_call( array( 'name' => '__', 'args' => array('baba', 5, 'pijo', null) ), 'baba.php' );
  35. $this->assertEquals( $entry, new Translation_Entry( array( 'singular' => 'baba' ) ) );
  36. }
  37. function test_entry_from_call_context() {
  38. $this->extractor->rules = array( '_x' => array( 'string', 'context' ) );
  39. $entry = $this->extractor->entry_from_call( array( 'name' => '_x', 'args' => array('baba', 'noun') ), 'baba.php' );
  40. $this->assertEquals( $entry, new Translation_Entry( array( 'singular' => 'baba', 'context' => 'noun' ) ) );
  41. }
  42. function test_entry_from_call_plural() {
  43. $this->extractor->rules = array( '_n' => array( 'singular', 'plural' ) );
  44. $entry = $this->extractor->entry_from_call( array( 'name' => '_n', 'args' => array('%s baba', '%s babas') ), 'baba.php' );
  45. $this->assertEquals( $entry, new Translation_Entry( array( 'singular' => '%s baba', 'plural' => '%s babas' ) ) );
  46. }
  47. function test_entry_from_call_plural_and_context() {
  48. $this->extractor->rules = array( '_nx' => array( 'singular', 'plural', 'context' ) );
  49. $entry = $this->extractor->entry_from_call( array( 'name' => '_nx', 'args' => array('%s baba', '%s babas', 'noun') ), 'baba.php' );
  50. $this->assertEquals( $entry, new Translation_Entry( array( 'singular' => '%s baba', 'plural' => '%s babas', 'context' => 'noun' ) ) );
  51. }
  52. function test_entry_from_call_extracted_comment() {
  53. $entry = $this->extractor->entry_from_call( array( 'name' => '__', 'args' => array('baba'), 'comment' => 'translators: give me back my pants!' ), 'baba.php' );
  54. $this->assertEquals( $entry, new Translation_Entry( array( 'singular' => 'baba', 'extracted_comments' => "translators: give me back my pants!\n" ) ) );
  55. }
  56. function test_entry_from_call_line_number() {
  57. $entry = $this->extractor->entry_from_call( array( 'name' => '__', 'args' => array('baba'), 'line' => 10 ), 'baba.php' );
  58. $this->assertEquals( $entry, new Translation_Entry( array( 'singular' => 'baba', 'references' => array('baba.php:10') ) ) );
  59. }
  60. function test_entry_from_call_zero() {
  61. $entry = $this->extractor->entry_from_call( array( 'name' => '__', 'args' => array('0') ), 'baba.php' );
  62. $this->assertEquals( $entry, new Translation_Entry( array( 'singular' => '0' ) ) );
  63. }
  64. function test_entry_from_call_multiple() {
  65. $this->extractor->rules = array( 'c' => array( 'string', 'singular', 'plural' ) );
  66. $entries = $this->extractor->entry_from_call( array( 'name' => 'c', 'args' => array('baba', 'dyado', 'dyados') ), 'baba.php' );
  67. $this->assertEquals( array(
  68. new Translation_Entry( array( 'singular' => 'baba' ) ), new Translation_Entry( array( 'singular' => 'dyado', 'plural' => 'dyados' ) ) ), $entries );
  69. }
  70. function test_entry_from_call_multiple_first_plural_then_two_strings() {
  71. $this->extractor->rules = array( 'c' => array( 'singular', 'plural', null, 'string', 'string' ) );
  72. $entries = $this->extractor->entry_from_call( array( 'name' => 'c', 'args' => array('dyado', 'dyados', 'baba', 'foo', 'bar') ), 'baba.php' );
  73. $this->assertEquals( array(
  74. new Translation_Entry( array( 'singular' => 'dyado', 'plural' => 'dyados' ) ),
  75. new Translation_Entry( array( 'singular' => 'foo' ) ),
  76. new Translation_Entry( array( 'singular' => 'bar' ) ) ), $entries );
  77. }
  78. function test_find_function_calls_one_arg_literal() {
  79. $this->assertEquals( array( array( 'name' => '__', 'args' => array( 'baba' ), 'line' => 1 ) ), $this->extractor->find_function_calls( array('__'), '<?php __("baba"); ?>' ) );
  80. }
  81. function test_find_function_calls_one_arg_zero() {
  82. $this->assertEquals( array( array( 'name' => '__', 'args' => array( '0' ), 'line' => 1 ) ), $this->extractor->find_function_calls( array('__'), '<?php __("0"); ?>' ) );
  83. }
  84. function test_find_function_calls_one_arg_non_literal() {
  85. $this->assertEquals( array( array( 'name' => '__', 'args' => array( null ), 'line' => 1 ) ), $this->extractor->find_function_calls( array('__'), '<?php __("baba" . "dudu"); ?>' ) );
  86. }
  87. function test_find_function_calls_shouldnt_be_mistaken_by_a_class() {
  88. $this->assertEquals( array(), $this->extractor->find_function_calls( array('__'), '<?php class __ { }; ("dyado");' ) );
  89. }
  90. function test_find_function_calls_2_args_bad_literal() {
  91. $this->assertEquals( array( array( 'name' => 'f', 'args' => array( null, "baba" ), 'line' => 1 ) ), $this->extractor->find_function_calls( array('f'), '<?php f(5, "baba" ); ' ) );
  92. }
  93. function test_find_function_calls_2_args_bad_literal_bad() {
  94. $this->assertEquals( array( array( 'name' => 'f', 'args' => array( null, "baba", null ), 'line' => 1 ) ), $this->extractor->find_function_calls( array('f'), '<?php f(5, "baba", 5 ); ' ) );
  95. }
  96. function test_find_function_calls_1_arg_bad_concat() {
  97. $this->assertEquals( array( array( 'name' => 'f', 'args' => array( null ), 'line' => 1 ) ), $this->extractor->find_function_calls( array('f'), '<?php f( "baba" . "baba" ); ' ) );
  98. }
  99. function test_find_function_calls_1_arg_bad_function_call() {
  100. $this->assertEquals( array( array( 'name' => 'f', 'args' => array( null ), 'line' => 1 ) ), $this->extractor->find_function_calls( array('f'), '<?php f( g( "baba" ) ); ' ) );
  101. }
  102. function test_find_function_calls_2_arg_literal_bad() {
  103. $this->assertEquals( array( array( 'name' => 'f', 'args' => array( "baba", null ), 'line' => 1 ) ), $this->extractor->find_function_calls( array('f'), '<?php f( "baba", null ); ' ) );
  104. }
  105. function test_find_function_calls_2_arg_bad_with_parens_literal() {
  106. $this->assertEquals( array( array( 'name' => 'f', 'args' => array( null, "baba" ), 'line' => 1 ) ), $this->extractor->find_function_calls( array('f'), '<?php f( g( "dyado", "chicho", "lelya "), "baba" ); ' ) );
  107. }
  108. function test_find_function_calls_with_comment() {
  109. $this->assertEquals(
  110. array( array( 'name' => 'f', 'args' => array( 'baba' ), 'line' => 1, 'comment' => 'translators: let your ears fly!' ) ),
  111. $this->extractor->find_function_calls( array('f'), '<?php /* translators: let your ears fly! */ f( "baba" ); ' ) );
  112. }
  113. function test_find_function_calls_with_not_immediate_comment() {
  114. $this->assertEquals(
  115. array( array( 'name' => 'f', 'args' => array( 'baba' ), 'line' => 1, 'comment' => 'translators: let your ears fly!' ) ),
  116. $this->extractor->find_function_calls( array('f'), '<?php /* translators: let your ears fly! */ $foo = g ( f( "baba" ) ); ' ) );
  117. }
  118. function test_find_function_calls_with_not_immediate_comment_include_only_latest() {
  119. $this->assertEquals(
  120. array( array( 'name' => 'f', 'args' => array( 'baba' ), 'line' => 1, 'comment' => 'translators: let your ears fly!' ) ),
  121. $this->extractor->find_function_calls( array('f'), '<?php /* translators: boo */ /* translators: let your ears fly! */ /* baba */ $foo = g ( f( "baba" ) ); ' ) );
  122. }
  123. function test_comment_prefix_should_be_case_insensitive() {
  124. $this->assertEquals(
  125. array( array( 'name' => 'f', 'args' => array( 'baba' ), 'line' => 1, 'comment' => 'Translators: let your ears fly!' ) ),
  126. $this->extractor->find_function_calls( array('f'), '<?php /* Translators: let your ears fly! */ f( "baba" ); ' ) );
  127. }
  128. }