NumericLiteralSeparatorFixerTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\Basic;
  13. use PhpCsFixer\Fixer\Basic\NumericLiteralSeparatorFixer;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Marvin Heilemann <marvin.heilemann+github@googlemail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\Basic\NumericLiteralSeparatorFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Basic\NumericLiteralSeparatorFixer>
  23. *
  24. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Basic\NumericLiteralSeparatorFixer
  25. */
  26. final class NumericLiteralSeparatorFixerTest extends AbstractFixerTestCase
  27. {
  28. /**
  29. * @param _AutogeneratedInputConfiguration $config
  30. *
  31. * @dataProvider provideFixCases
  32. */
  33. public function testFix(string $expected, ?string $input = null, ?array $config = []): void
  34. {
  35. $this->fixer->configure($config);
  36. $this->doTest($expected, $input);
  37. }
  38. /**
  39. * @return iterable<string, array{0: string, 1?: null|string, 2?: array<string, mixed>}>
  40. */
  41. public static function provideFixCases(): iterable
  42. {
  43. yield 'do not override existing separator' => [
  44. <<<'PHP'
  45. <?php
  46. echo 0B01010100_01101000;
  47. echo 70_10_00;
  48. PHP,
  49. null,
  50. [
  51. 'override_existing' => false,
  52. 'strategy' => NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR,
  53. ],
  54. ];
  55. yield 'override existing separator' => [
  56. <<<'PHP'
  57. <?php
  58. echo 1_234.5;
  59. echo 701_000;
  60. PHP,
  61. <<<'PHP'
  62. <?php
  63. echo 123_4.5;
  64. echo 70_10_00;
  65. PHP,
  66. [
  67. 'override_existing' => true,
  68. 'strategy' => NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR,
  69. ],
  70. ];
  71. yield from self::yieldCases([
  72. 'decimal' => [
  73. '1234' => '1_234',
  74. '-1234' => '-1_234',
  75. '12345' => '12_345',
  76. '123456' => '123_456',
  77. ],
  78. 'binary' => [
  79. '0b0101010001101000' => '0b01010100_01101000',
  80. '0b01010100011010000110010101101111' => '0b01010100_01101000_01100101_01101111',
  81. '0b110001000' => '0b1_10001000',
  82. ],
  83. 'float' => [
  84. '.001' => null,
  85. '.1001' => '.100_1',
  86. '0.0001' => '0.000_1',
  87. '0.001' => null,
  88. '1234.5' => '1_234.5',
  89. '1.2345' => '1.234_5',
  90. '1234e5' => '1_234e5',
  91. '1234E5' => '1_234E5',
  92. '1e2345' => '1e2_345',
  93. '1234.5678e1234' => '1_234.567_8e1_234',
  94. '.5678e1234' => '.567_8e1_234',
  95. '1.1e-1234' => '1.1e-1_234',
  96. '1.1e-12345' => '1.1e-12_345',
  97. '1.1e-123456' => '1.1e-123_456',
  98. '.1e-12345' => '.1e-12_345',
  99. ],
  100. 'hexadecimal' => [
  101. '0x42726F776E' => '0x42_72_6F_77_6E',
  102. '0X42726F776E' => '0X42_72_6F_77_6E',
  103. '0x2726F776E' => '0x2_72_6F_77_6E',
  104. '0x1234567890abcdef' => '0x12_34_56_78_90_ab_cd_ef',
  105. '0X1234567890ABCDEF' => '0X12_34_56_78_90_AB_CD_EF',
  106. '0x1234e5' => '0x12_34_e5',
  107. ],
  108. 'octal' => [
  109. '012345' => '012_345',
  110. '0123456' => '0123_456',
  111. '01234567' => '01_234_567',
  112. ],
  113. ]);
  114. yield 'do not change float to int when there is nothing after the dot' => ['<?php $x = 100.;'];
  115. }
  116. /**
  117. * @param _AutogeneratedInputConfiguration $config
  118. *
  119. * @requires PHP 8.1
  120. *
  121. * @dataProvider provideFix81Cases
  122. */
  123. public function testFix81(string $expected, ?string $input = null, ?array $config = []): void
  124. {
  125. $this->fixer->configure($config);
  126. $this->doTest($expected, $input);
  127. }
  128. /**
  129. * @return iterable<string, array{0: string, 1?: null|string, 2?: array<string, mixed>}>
  130. */
  131. public static function provideFix81Cases(): iterable
  132. {
  133. yield 'do not override existing separator' => [
  134. '<?php echo 0o123_45;',
  135. null,
  136. [
  137. 'override_existing' => false,
  138. 'strategy' => NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR,
  139. ],
  140. ];
  141. yield 'override existing separator' => [
  142. '<?php echo 1_234.5;',
  143. '<?php echo 123_4.5;',
  144. [
  145. 'override_existing' => true,
  146. 'strategy' => NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR,
  147. ],
  148. ];
  149. yield from self::yieldCases([
  150. 'octal' => [
  151. '0o12345' => '0o12_345',
  152. '0o123456' => '0o123_456',
  153. ],
  154. ]);
  155. }
  156. /**
  157. * @param array<string, array<mixed, mixed>> $cases
  158. *
  159. * @return iterable<string, array{0: string, 1?: null|string, 2?: array<string, mixed>}>
  160. */
  161. private static function yieldCases(array $cases): iterable
  162. {
  163. foreach ($cases as $pairsType => $pairs) {
  164. foreach ($pairs as $withoutSeparator => $withSeparator) {
  165. if (null === $withSeparator) {
  166. yield "do not modify valid {$pairsType} {$withoutSeparator}" => [
  167. \sprintf('<?php echo %s;', $withoutSeparator),
  168. null,
  169. ['strategy' => NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR],
  170. ];
  171. } else {
  172. yield "add separator to {$pairsType} {$withoutSeparator}" => [
  173. \sprintf('<?php echo %s;', $withSeparator),
  174. \sprintf('<?php echo %s;', $withoutSeparator),
  175. ['strategy' => NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR],
  176. ];
  177. }
  178. }
  179. foreach ($pairs as $withoutSeparator => $withSeparator) {
  180. if (null === $withSeparator) {
  181. continue;
  182. }
  183. yield "remove separator from {$pairsType} {$withoutSeparator}" => [
  184. \sprintf('<?php echo %s;', $withoutSeparator),
  185. \sprintf('<?php echo %s;', $withSeparator),
  186. ['strategy' => NumericLiteralSeparatorFixer::STRATEGY_NO_SEPARATOR],
  187. ];
  188. }
  189. }
  190. }
  191. }