| 1 |
<?php |
| 2 |
|
| 3 |
namespace Test\Parsoid\Logger; |
| 4 |
|
| 5 |
use Wikimedia\Parsoid\Logger\LintLogger; |
| 6 |
use Wikimedia\Parsoid\Mocks\MockEnv; |
| 7 |
|
| 8 |
/** |
| 9 |
* @coversDefaultClass \Wikimedia\Parsoid\Logger\LintLogger |
| 10 |
*/ |
| 11 |
class LintLoggerTest extends \PHPUnit\Framework\TestCase { |
| 12 |
/** |
| 13 |
* @covers ::convertDSROffsets() |
| 14 |
* @dataProvider provideConvertDSROffsets |
| 15 |
*/ |
| 16 |
public function testConvertDSROffsets( |
| 17 |
string $str, array $byteStrs, array $byteDSR, array $ucs2DSR |
| 18 |
) { |
| 19 |
// Verify that the test is well-formed |
| 20 |
$testStrs = [ |
| 21 |
substr( $str, $byteDSR[0], $byteDSR[1] - $byteDSR[0] ), |
| 22 |
$byteDSR[2] > 0 ? substr( $str, $byteDSR[0], $byteDSR[2] ) : "", |
| 23 |
$byteDSR[3] > 0 ? substr( $str, $byteDSR[1] - $byteDSR[3], $byteDSR[3] ) : "" |
| 24 |
]; |
| 25 |
$this->assertSame( $testStrs, $byteStrs, "Double check" ); |
| 26 |
|
| 27 |
// Now, verify that offset conversion is correct |
| 28 |
$env = new MockEnv( [ "pageContent" => $str ] ); |
| 29 |
$lints = [ [ "dsr" => $byteDSR ] ]; |
| 30 |
LintLogger::convertDSROffsets( $env, $lints ); |
| 31 |
$this->assertSame( $lints[0]["dsr"], $ucs2DSR, "byte → ucs2" ); |
| 32 |
} |
| 33 |
|
| 34 |
public static function provideConvertDSROffsets(): iterable { |
| 35 |
# This string and the offsets are borrowed from Utils\TokenUtilsTest.php |
| 36 |
# Ensure that we have char from each UTF-8 class here. |
| 37 |
# |
| 38 |
# "foo bár 💩💩 baz AԱ人💩" |
| 39 |
# char 012345678 9 01234567 8 |
| 40 |
# ucs 012345678 0 23456789 0 |
| 41 |
# byte 012345789 3 78901235 8 |
| 42 |
# |
| 43 |
$str = "foo bár \u{1F4A9}\u{1F4A9} baz A\u{0531}\u{4EBA}\u{1F4A9}"; |
| 44 |
|
| 45 |
# These are arbitrary DSRs. All we care in this test is that |
| 46 |
# dsr offsts are properly converted. They needn't correspond |
| 47 |
# to actual lint errors. |
| 48 |
$lints = [ |
| 49 |
[ // Entire string |
| 50 |
"byteStrs" => [ $str, "", "" ], |
| 51 |
"byte" => [ 0, 32, 0, 0 ], |
| 52 |
"ucs2" => [ 0, 22, 0, 0 ] |
| 53 |
], |
| 54 |
[ // foo |
| 55 |
"byteStrs" => [ "foo", "f", "o" ], |
| 56 |
"byte" => [ 0, 3, 1/*f*/, 1/*o*/ ], |
| 57 |
"ucs2" => [ 0, 3, 1/*f*/, 1/*f*/ ] |
| 58 |
], |
| 59 |
[ // bár |
| 60 |
"byteStrs" => [ "bár", "", "ár" ], |
| 61 |
"byte" => [ 4, 8, 0, 3/*ár*/ ], |
| 62 |
"ucs2" => [ 4, 7, 0, 2/*ár*/ ] |
| 63 |
], |
| 64 |
[ // \u{1F4A9} baz A\u{0531} |
| 65 |
"byteStrs" => [ "\u{1F4A9} baz A\u{0531}", "\u{1F4A9}", "A\u{0531}" ], |
| 66 |
"byte" => [ 13, 25, 4/*\u{1F4A9}*/, 3/*A\u{0531}*/ ], |
| 67 |
"ucs2" => [ 10, 19, 2/*\u{1F4A9}*/, 2/*A\u{0531}*/ ] |
| 68 |
] |
| 69 |
]; |
| 70 |
|
| 71 |
foreach ( $lints as $l ) { |
| 72 |
yield [ |
| 73 |
$str, |
| 74 |
$l['byteStrs'], |
| 75 |
$l['byte'], |
| 76 |
$l['ucs2'], |
| 77 |
]; |
| 78 |
} |
| 79 |
} |
| 80 |
} |