PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/postie/util/wp-i18n/extract/ExtractTest.php

https://bitbucket.org/rchlmrtn/chiari
PHP | 154 lines | 123 code | 31 blank | 0 comment | 0 complexity | de2b69d0b47f2490795be5bc6734f31a MD5 | raw file
  1. <?php
  2. require_once 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_entries('<?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. }