UtilsTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests;
  12. use PhpCsFixer\Tokenizer\Token;
  13. use PhpCsFixer\Utils;
  14. use PHPUnit\Framework\TestCase;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. * @author Graham Campbell <graham@alt-three.com>
  18. * @author Odín del Río <odin.drp@gmail.com>
  19. *
  20. * @internal
  21. *
  22. * @covers \PhpCsFixer\Utils
  23. */
  24. final class UtilsTest extends TestCase
  25. {
  26. /**
  27. * @param string $expected Camel case string
  28. * @param string $input Input string
  29. *
  30. * @dataProvider provideCamelCaseToUnderscoreCases
  31. */
  32. public function testCamelCaseToUnderscore($expected, $input = null)
  33. {
  34. if (null !== $input) {
  35. $this->assertSame($expected, Utils::camelCaseToUnderscore($input));
  36. }
  37. $this->assertSame($expected, Utils::camelCaseToUnderscore($expected));
  38. }
  39. /**
  40. * @return array
  41. */
  42. public function provideCamelCaseToUnderscoreCases()
  43. {
  44. return [
  45. [
  46. 'dollar_close_curly_braces',
  47. 'DollarCloseCurlyBraces',
  48. ],
  49. [
  50. 'utf8_encoder_fixer',
  51. 'utf8EncoderFixer',
  52. ],
  53. [
  54. 'terminated_with_number10',
  55. 'TerminatedWithNumber10',
  56. ],
  57. [
  58. 'utf8_encoder_fixer',
  59. ],
  60. ];
  61. }
  62. /**
  63. * @param int $expected
  64. * @param int $left
  65. * @param int $right
  66. *
  67. * @dataProvider provideCmpIntCases
  68. */
  69. public function testCmpInt($expected, $left, $right)
  70. {
  71. $this->assertSame($expected, Utils::cmpInt($left, $right));
  72. }
  73. public function provideCmpIntCases()
  74. {
  75. return [
  76. [0, 1, 1],
  77. [0, -1, -1],
  78. [-1, 10, 20],
  79. [-1, -20, -10],
  80. [1, 20, 10],
  81. [1, -10, -20],
  82. ];
  83. }
  84. /**
  85. * @param array $expected
  86. * @param string $input
  87. *
  88. * @dataProvider provideSplitLinesCases
  89. */
  90. public function testSplitLines(array $expected, $input)
  91. {
  92. $this->assertSame($expected, Utils::splitLines($input));
  93. }
  94. public function provideSplitLinesCases()
  95. {
  96. return [
  97. [
  98. ["\t aaa\n", " bbb\n", "\t"],
  99. "\t aaa\n bbb\n\t",
  100. ],
  101. [
  102. ["aaa\r\n", " bbb\r\n"],
  103. "aaa\r\n bbb\r\n",
  104. ],
  105. [
  106. ["aaa\r\n", " bbb\n"],
  107. "aaa\r\n bbb\n",
  108. ],
  109. [
  110. ["aaa\r\n\n\n\r\n", " bbb\n"],
  111. "aaa\r\n\n\n\r\n bbb\n",
  112. ],
  113. ];
  114. }
  115. /**
  116. * @param string $spaces
  117. * @param string $input
  118. *
  119. * @dataProvider provideCalculateTrailingWhitespaceIndentCases
  120. */
  121. public function testCalculateTrailingWhitespaceIndent($spaces, $input)
  122. {
  123. $token = new Token([T_WHITESPACE, $input]);
  124. $this->assertSame($spaces, Utils::calculateTrailingWhitespaceIndent($token));
  125. }
  126. public function provideCalculateTrailingWhitespaceIndentCases()
  127. {
  128. return [
  129. [' ', "\n\n "],
  130. [' ', "\r\n\r\r\r "],
  131. ["\t", "\r\n\t"],
  132. ['', "\t\n\r"],
  133. ['', "\n"],
  134. ['', ''],
  135. ];
  136. }
  137. public function testCalculateTrailingWhitespaceIndentFail()
  138. {
  139. $this->setExpectedException(
  140. \InvalidArgumentException::class,
  141. 'The given token must be whitespace, got "T_STRING".'
  142. );
  143. $token = new Token([T_STRING, 'foo']);
  144. Utils::calculateTrailingWhitespaceIndent($token);
  145. }
  146. }