PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/Template/tests/whitespace_removal_test.php

https://github.com/Yannix/zetacomponents
PHP | 189 lines | 110 code | 25 blank | 54 comment | 1 complexity | 9552cee7e3ef79767dce36bd5148d496 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  22. * @version //autogentag//
  23. * @filesource
  24. * @package Template
  25. * @subpackage Tests
  26. */
  27. /**
  28. * @package Template
  29. * @subpackage Tests
  30. */
  31. class ezcTemplateWhitespaceRemovalTest extends ezcTestCase
  32. {
  33. public static function suite()
  34. {
  35. return new PHPUnit_Framework_TestSuite( "ezcTemplateWhitespaceRemovalTest" );
  36. }
  37. /**
  38. * Test if trimming a string containing only whitespace returns an empty string
  39. * for both leading and trailing trimming.
  40. */
  41. public function testWhitespaceOnlyStringIsTrimmed()
  42. {
  43. $ws = new ezcTemplateWhitespaceRemoval();
  44. // Contains all whitespace characters
  45. $lines = array( array( " ", "\n" ),
  46. array( "\t", "\r" ),
  47. array( "\x0B", false ) );
  48. $leading = array( array( "", false ),
  49. array( "", false ),
  50. array( "", false ) );
  51. $trailing = array( array( "", "\n" ),
  52. array( "", false ),
  53. array( "", false ) );
  54. self::assertSame( $leading, $ws->trimLeading( $lines ), 'Trimming leading WS does not return an empty string.' );
  55. self::assertSame( $trailing, $ws->trimTrailing( $lines ), 'Trimming trailing WS does not return an empty string.' );
  56. }
  57. /**
  58. * Test if trimming an empty stringreturns an empty string
  59. * for both leading and trailing trimming.
  60. */
  61. public function testEmptyStringIsTrimmed()
  62. {
  63. $ws = new ezcTemplateWhitespaceRemoval();
  64. // Empty string
  65. $lines = array( array( "", false ) );
  66. $empty = array( array( "", false ) );
  67. self::assertSame( $empty, $ws->trimLeading( $lines ), 'Trimming leading WS does not return an empty string.' );
  68. self::assertSame( $empty, $ws->trimTrailing( $lines ), 'Trimming trailing WS does not return an empty string.' );
  69. }
  70. /**
  71. * Test if trimming a string containing trailing whitespace is only
  72. * trimmed at the trailing lines and not the leading ones
  73. */
  74. public function testTextWithTrailingWhitespaceIsTrimmed()
  75. {
  76. $ws = new ezcTemplateWhitespaceRemoval();
  77. // Contains text with all whitespace characters at the end
  78. $lines = array( array( "a simple line", "\n" ),
  79. array( "and a second one with whitespace to keep ", "\n" ),
  80. array( "\t", "\r" ),
  81. array( "\x0B", false ) );
  82. // No lines are changed so it should return false
  83. $leading = false;
  84. $trailing = array( array( "a simple line", "\n" ),
  85. array( "and a second one with whitespace to keep ", "\n" ),
  86. array( "", false ),
  87. array( "", false ) );
  88. self::assertSame( $leading, $ws->trimLeading( $lines ), 'String without leading WS should not be trimmed.' );
  89. self::assertSame( $trailing, $ws->trimTrailing( $lines ), 'String with trailing WS should be trimmed.' );
  90. }
  91. /**
  92. * Test if trimming a string containing leading whitespace is only
  93. * trimmed at the leading lines and not the trailing ones
  94. */
  95. public function testTextWithLeadingWhitespaceIsTrimmed()
  96. {
  97. $ws = new ezcTemplateWhitespaceRemoval();
  98. // Contains text with all whitespace characters at the start
  99. $lines = array( array( "", "\n" ),
  100. array( "\t", "\r" ),
  101. array( "\x0B", "\n" ),
  102. array( "a simple line", "\n" ),
  103. array( "and a second one with whitespace to keep ", false ) );
  104. $leading = array( array( "", false ),
  105. array( "", false ),
  106. array( "", false ),
  107. array( "a simple line", "\n" ),
  108. array( "and a second one with whitespace to keep ", false ) );
  109. // No lines are changed so it should return false
  110. $trailing = false;
  111. self::assertSame( $leading, $ws->trimLeading( $lines ), 'String with leading WS should be trimmed.' );
  112. self::assertSame( $trailing, $ws->trimTrailing( $lines ), 'String without trailing WS should not be trimmed.' );
  113. }
  114. /**
  115. * Test if trimming indentations with tabs are handled properly.
  116. */
  117. public function testLineWithTabsIsTrimmedCorrectly()
  118. {
  119. $ws = new ezcTemplateWhitespaceRemoval();
  120. $ws->tabSize = 8;
  121. for ( $i = 0; $i <= 8; ++$i )
  122. {
  123. self::assertSame( "", $ws->trimIndentationLine( "", $i ),
  124. "Empty string trimmed at indentation {$i} should be empty." );
  125. }
  126. self::assertSame( "\t", $ws->trimIndentationLine( "\t", 0 ),
  127. "String with tab trimmed at indentation 0 should have the tab marker." );
  128. self::assertSame( "", $ws->trimIndentationLine( "\t", 1 ),
  129. "String with tab trimmed at indentation 1 should be empty." );
  130. self::assertSame( "", $ws->trimIndentationLine( "\t", 7 ),
  131. "String with tab trimmed at indentation 7 should be empty." );
  132. self::assertSame( "", $ws->trimIndentationLine( "\t", 8 ),
  133. "String with tab trimmed at indentation 8 should be empty." );
  134. self::assertSame( " \t", $ws->trimIndentationLine( " \t", 0 ),
  135. "String with space+tab trimmed at indentation 0 should have the space+tab marker." );
  136. self::assertSame( "\t", $ws->trimIndentationLine( " \t", 1 ),
  137. "String with space+tab trimmed at indentation 1 should have the tab marker." );
  138. self::assertSame( "", $ws->trimIndentationLine( " \t", 2 ),
  139. "String with space+tab trimmed at indentation 2 should be empty." );
  140. self::assertSame( "", $ws->trimIndentationLine( " \t", 7 ),
  141. "String with space+tab trimmed at indentation 7 should be empty." );
  142. self::assertSame( "", $ws->trimIndentationLine( " \t", 8 ),
  143. "String with space+tab trimmed at indentation 8 should be empty." );
  144. self::assertSame( " \t", $ws->trimIndentationLine( " \t", 0 ),
  145. "String with 7*space+tab trimmed at indentation 0 should have the 7*space+tab marker." );
  146. self::assertSame( " \t", $ws->trimIndentationLine( " \t", 1 ),
  147. "String with 7*space+tab trimmed at indentation 1 should have the 6*space+tab marker." );
  148. self::assertSame( " \t", $ws->trimIndentationLine( " \t", 2 ),
  149. "String with 7*space+tab trimmed at indentation 2 should have the 5*space+tab marker." );
  150. self::assertSame( " \t", $ws->trimIndentationLine( " \t", 3 ),
  151. "String with 7*space+tab trimmed at indentation 3 should have the 4*space+tab marker." );
  152. self::assertSame( " \t", $ws->trimIndentationLine( " \t", 4 ),
  153. "String with 7*space+tab trimmed at indentation 4 should have the 3*space+tab marker." );
  154. self::assertSame( " \t", $ws->trimIndentationLine( " \t", 5 ),
  155. "String with 7*space+tab trimmed at indentation 5 should have the 2*space+tab marker." );
  156. self::assertSame( " \t", $ws->trimIndentationLine( " \t", 6 ),
  157. "String with 7*space+tab trimmed at indentation 6 should have the 1*space+tab marker." );
  158. self::assertSame( "\t", $ws->trimIndentationLine( " \t", 7 ),
  159. "String with 7*space+tab trimmed at indentation 7 should have the tab marker." );
  160. self::assertSame( "", $ws->trimIndentationLine( " \t", 8 ),
  161. "String with 7*space+tab trimmed at indentation 8 should be empty." );
  162. }
  163. }
  164. ?>