|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the \PHP_CodeSniffer\Util\Sniffs\Common::stripColors() method. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <[email protected]> |
| 6 | + * @copyright 2024 Juliette Reinders Folmer. All rights reserved. |
| 7 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core\Util; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Util\Common; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +/** |
| 16 | + * Tests for the \PHP_CodeSniffer\Util\Sniffs\Common::stripColors() method. |
| 17 | + * |
| 18 | + * @covers \PHP_CodeSniffer\Util\Common::stripColors |
| 19 | + */ |
| 20 | +final class StripColorsTest extends TestCase |
| 21 | +{ |
| 22 | + |
| 23 | + |
| 24 | + /** |
| 25 | + * Test stripping color codes from a text. |
| 26 | + * |
| 27 | + * @param string $text The text provided. |
| 28 | + * @param string $expected Expected function output. |
| 29 | + * |
| 30 | + * @dataProvider dataStripColors |
| 31 | + * |
| 32 | + * @return void |
| 33 | + */ |
| 34 | + public function testStripColors($text, $expected) |
| 35 | + { |
| 36 | + $this->assertSame($expected, Common::stripColors($text)); |
| 37 | + |
| 38 | + }//end testStripColors() |
| 39 | + |
| 40 | + |
| 41 | + /** |
| 42 | + * Data provider. |
| 43 | + * |
| 44 | + * @see testStripColors() |
| 45 | + * |
| 46 | + * @return array<string, array<string, string>> |
| 47 | + */ |
| 48 | + public static function dataStripColors() |
| 49 | + { |
| 50 | + return [ |
| 51 | + 'Text is empty' => [ |
| 52 | + 'text' => '', |
| 53 | + 'expected' => '', |
| 54 | + ], |
| 55 | + 'Text enclosed in color code' => [ |
| 56 | + 'text' => "\033[36mSome text\033[0m", |
| 57 | + 'expected' => 'Some text', |
| 58 | + ], |
| 59 | + 'Text containing color code' => [ |
| 60 | + 'text' => "Some text \033[33mSome other text", |
| 61 | + 'expected' => 'Some text Some other text', |
| 62 | + ], |
| 63 | + 'Text enclosed in color code, bold' => [ |
| 64 | + 'text' => "\033[1;32mSome text\033[0m", |
| 65 | + 'expected' => 'Some text', |
| 66 | + ], |
| 67 | + 'Text enclosed in color code, with escaped text' => [ |
| 68 | + 'text' => "\033[30;1m\\n\033[0m", |
| 69 | + 'expected' => '\n', |
| 70 | + ], |
| 71 | + 'Text enclosed in color code, bold, dark, italic' => [ |
| 72 | + 'text' => "\033[1;2;3mtext\033[0m", |
| 73 | + 'expected' => 'text', |
| 74 | + ], |
| 75 | + 'Text enclosed in color code, foreground color' => [ |
| 76 | + 'text' => "\033[38;5;255mtext\033[0m", |
| 77 | + 'expected' => 'text', |
| 78 | + ], |
| 79 | + 'Text enclosed in color code, foreground color and background color' => [ |
| 80 | + 'text' => "\033[38;5;200;48;5;255mtext\033[0m", |
| 81 | + 'expected' => 'text', |
| 82 | + ], |
| 83 | + 'Multiline text containing multiple color codes' => [ |
| 84 | + 'text' => "First \033[36mSecond\033[0m |
| 85 | +Third \033[1;2;3mFourth |
| 86 | +Next line\033[0m Last", |
| 87 | + 'expected' => 'First Second |
| 88 | +Third Fourth |
| 89 | +Next line Last', |
| 90 | + ], |
| 91 | + ]; |
| 92 | + |
| 93 | + }//end dataStripColors() |
| 94 | + |
| 95 | + |
| 96 | +}//end class |
0 commit comments