|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the \PHP_CodeSniffer\Util\Common::prepareForOutput() method. |
| 4 | + * |
| 5 | + * @copyright 2024 PHPCSStandards and contributors |
| 6 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 7 | + */ |
| 8 | + |
| 9 | +namespace PHP_CodeSniffer\Tests\Core\Util\Common; |
| 10 | + |
| 11 | +use PHP_CodeSniffer\Util\Common; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +/** |
| 15 | + * Tests for the \PHP_CodeSniffer\Util\Common::prepareForOutput() method. |
| 16 | + * |
| 17 | + * @covers \PHP_CodeSniffer\Util\Common::prepareForOutput |
| 18 | + * @group Windows |
| 19 | + */ |
| 20 | +final class PrepareForOutputTest extends TestCase |
| 21 | +{ |
| 22 | + |
| 23 | + |
| 24 | + /** |
| 25 | + * Test formatting whitespace characters, on anything other than Windows. |
| 26 | + * |
| 27 | + * @param string $content The content to prepare. |
| 28 | + * @param string[] $exclude A list of characters to leave invisible. |
| 29 | + * @param string $expected Expected function output. |
| 30 | + * @param string $expectedWin Expected function output on Windows (unused in this test). |
| 31 | + * |
| 32 | + * @requires OS ^(?!WIN).* |
| 33 | + * @dataProvider dataPrepareForOutput |
| 34 | + * |
| 35 | + * @return void |
| 36 | + */ |
| 37 | + public function testPrepareForOutput($content, $exclude, $expected, $expectedWin) |
| 38 | + { |
| 39 | + $this->assertSame($expected, Common::prepareForOutput($content, $exclude)); |
| 40 | + |
| 41 | + }//end testPrepareForOutput() |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * Test formatting whitespace characters, on Windows. |
| 46 | + * |
| 47 | + * @param string $content The content to prepare. |
| 48 | + * @param string[] $exclude A list of characters to leave invisible. |
| 49 | + * @param string $expected Expected function output (unused in this test). |
| 50 | + * @param string $expectedWin Expected function output on Windows. |
| 51 | + * |
| 52 | + * @requires OS ^WIN.*. |
| 53 | + * @dataProvider dataPrepareForOutput |
| 54 | + * |
| 55 | + * @return void |
| 56 | + */ |
| 57 | + public function testPrepareForOutputWindows($content, $exclude, $expected, $expectedWin) |
| 58 | + { |
| 59 | + $this->assertSame($expectedWin, Common::prepareForOutput($content, $exclude)); |
| 60 | + |
| 61 | + }//end testPrepareForOutputWindows() |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * Data provider. |
| 66 | + * |
| 67 | + * @see testPrepareForOutput() |
| 68 | + * @see testPrepareForOutputWindows() |
| 69 | + * |
| 70 | + * @return array<string, array<string, mixed>> |
| 71 | + */ |
| 72 | + public static function dataPrepareForOutput() |
| 73 | + { |
| 74 | + return [ |
| 75 | + 'Special characters are replaced with their escapes' => [ |
| 76 | + 'content' => "\r\n\t", |
| 77 | + 'exclude' => [], |
| 78 | + 'expected' => "\033[30;1m\\r\033[0m\033[30;1m\\n\033[0m\033[30;1m\\t\033[0m", |
| 79 | + 'expectedWin' => "\\r\\n\\t", |
| 80 | + ], |
| 81 | + 'Spaces are replaced with a unique mark' => [ |
| 82 | + 'content' => ' ', |
| 83 | + 'exclude' => [], |
| 84 | + 'expected' => "\033[30;1m·\033[0m\033[30;1m·\033[0m\033[30;1m·\033[0m\033[30;1m·\033[0m", |
| 85 | + 'expectedWin' => ' ', |
| 86 | + ], |
| 87 | + 'Other characters are unaffected' => [ |
| 88 | + 'content' => '{echo 1;}', |
| 89 | + 'exclude' => [], |
| 90 | + 'expected' => "{echo\033[30;1m·\033[0m1;}", |
| 91 | + 'expectedWin' => '{echo 1;}', |
| 92 | + ], |
| 93 | + 'No replacements' => [ |
| 94 | + 'content' => 'nothing-should-be-replaced', |
| 95 | + 'exclude' => [], |
| 96 | + 'expected' => 'nothing-should-be-replaced', |
| 97 | + 'expectedWin' => 'nothing-should-be-replaced', |
| 98 | + ], |
| 99 | + 'Excluded whitespace characters are unaffected' => [ |
| 100 | + 'content' => "\r\n\t ", |
| 101 | + 'exclude' => [ |
| 102 | + "\r", |
| 103 | + "\n", |
| 104 | + ], |
| 105 | + 'expected' => "\r\n\033[30;1m\\t\033[0m\033[30;1m·\033[0m", |
| 106 | + 'expectedWin' => "\r\n\\t ", |
| 107 | + ], |
| 108 | + ]; |
| 109 | + |
| 110 | + }//end dataPrepareForOutput() |
| 111 | + |
| 112 | + |
| 113 | +}//end class |
0 commit comments