Skip to content

Commit 1d8aa22

Browse files
committed
Tests: Add unit tests for Common::prepareForOutput()
This function has different behavior on Windows and on other systems, therefore add two tests, only one of which runs.
1 parent 7f5d178 commit 1d8aa22

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Util\Common::prepareForOutput() method.
4+
*
5+
* @author Bartosz Dziewoński <[email protected]>
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+
*/
19+
final class PrepareForOutputTest extends TestCase
20+
{
21+
22+
23+
/**
24+
* Test formatting whitespace characters, on anything other than Windows.
25+
*
26+
* @param string $content The content to prepare.
27+
* @param string[] $exclude A list of characters to leave invisible.
28+
* @param string $expected Expected function output.
29+
* @param string $expectedWin Expected function output on Windows (unused in this test).
30+
*
31+
* @dataProvider dataPrepareForOutput
32+
*
33+
* @return void
34+
*/
35+
public function testPrepareForOutput($content, $exclude, $expected, $expectedWin)
36+
{
37+
if (stripos(PHP_OS, 'WIN') === 0) {
38+
$this->markTestSkipped('Non Windows only');
39+
}
40+
41+
$this->assertSame($expected, Common::prepareForOutput($content, $exclude));
42+
43+
}//end testPrepareForOutput()
44+
45+
46+
/**
47+
* Test formatting whitespace characters, on Windows.
48+
*
49+
* @param string $content The content to prepare.
50+
* @param string[] $exclude A list of characters to leave invisible.
51+
* @param string $expected Expected function output (unused in this test).
52+
* @param string $expectedWin Expected function output on Windows.
53+
*
54+
* @dataProvider dataPrepareForOutput
55+
*
56+
* @return void
57+
*/
58+
public function testPrepareForOutputWindows($content, $exclude, $expected, $expectedWin)
59+
{
60+
if (stripos(PHP_OS, 'WIN') !== 0) {
61+
$this->markTestSkipped('Windows only');
62+
}
63+
64+
$this->assertSame($expectedWin, Common::prepareForOutput($content, $exclude));
65+
66+
}//end testPrepareForOutputWindows()
67+
68+
69+
/**
70+
* Data provider.
71+
*
72+
* @see testPrepareForOutput()
73+
* @see testPrepareForOutputWindows()
74+
*
75+
* @return array<string, array<string, mixed>>
76+
*/
77+
public static function dataPrepareForOutput()
78+
{
79+
return [
80+
'Special characters are replaced with their escapes' => [
81+
'content' => "\r\n\t",
82+
'exclude' => [],
83+
'expected' => "\033[30;1m\\r\033[0m\033[30;1m\\n\033[0m\033[30;1m\\t\033[0m",
84+
'expectedWin' => "\\r\\n\\t",
85+
],
86+
'Spaces are replaced with a unique mark' => [
87+
'content' => " ",
88+
'exclude' => [],
89+
'expected' => "\033[30;1m·\033[0m\033[30;1m·\033[0m\033[30;1m·\033[0m\033[30;1m·\033[0m",
90+
'expectedWin' => " ",
91+
],
92+
'Other characters are unaffected' => [
93+
'content' => "{echo 1;}",
94+
'exclude' => [],
95+
'expected' => "{echo\033[30;1m·\033[0m1;}",
96+
'expectedWin' => "{echo 1;}",
97+
],
98+
'Excluded whitespace characters are unaffected' => [
99+
'content' => "\r\n\t ",
100+
'exclude' => [
101+
"\r",
102+
"\n",
103+
],
104+
'expected' => "\r\n\033[30;1m\\t\033[0m\033[30;1m·\033[0m",
105+
'expectedWin' => "\r\n\\t ",
106+
],
107+
];
108+
109+
}//end dataPrepareForOutput()
110+
111+
112+
}//end class

0 commit comments

Comments
 (0)