43 lines
1.9 KiB
PHP
43 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace JumPHP\Ide\Tests;
|
|
|
|
use JumPHP\Ide\Workspace\TextFormat;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class TextFormatTest extends TestCase
|
|
{
|
|
public function testAUtf8FileWithABomAndCrlfIsReadAsPlainTextAndWrittenBackTheSame(): void
|
|
{
|
|
$bytes = "\xEF\xBB\xBFligne é\r\ndeux\r\n";
|
|
$read = TextFormat::decode($bytes);
|
|
self::assertSame(['content' => "ligne é\ndeux\n", 'encoding' => 'UTF-8 BOM', 'eol' => 'CRLF'], $read);
|
|
self::assertSame($bytes, TextFormat::encode($read['content'], $read['encoding'], $read['eol']));
|
|
}
|
|
|
|
public function testAnOldMacFileAndAWindows1252FileAreRecognised(): void
|
|
{
|
|
self::assertSame('CR', TextFormat::decode("a\rb\r")['eol']);
|
|
self::assertSame("a\rb\r", TextFormat::encode("a\nb\n", 'UTF-8', 'CR'));
|
|
$latin = TextFormat::decode("caf\xE9\n");
|
|
self::assertSame(['content' => "café\n", 'encoding' => 'Windows-1252', 'eol' => 'LF'], $latin);
|
|
self::assertSame("caf\xE9\n", TextFormat::encode("café\n", 'Windows-1252'));
|
|
self::assertSame('mixed', TextFormat::decode("a\r\nb\nc")['eol']);
|
|
}
|
|
|
|
public function testUtf16AndConversionsBetweenEncodings(): void
|
|
{
|
|
$utf16 = TextFormat::encode("été\n", 'UTF-16LE');
|
|
self::assertStringStartsWith("\xFF\xFE", $utf16);
|
|
self::assertSame(['content' => "été\n", 'encoding' => 'UTF-16LE', 'eol' => 'LF'], TextFormat::decode($utf16));
|
|
self::assertSame("été\n", TextFormat::encode("été\n", 'UTF-8'), 'the BOM taken away');
|
|
}
|
|
|
|
public function testCharactersAn8BitEncodingCannotWriteAreCounted(): void
|
|
{
|
|
self::assertSame(0, TextFormat::lost('café €', 'Windows-1252'));
|
|
self::assertSame(2, TextFormat::lost('café 日本', 'Windows-1252'));
|
|
self::assertSame(1, TextFormat::lost('€', 'ISO-8859-1'));
|
|
self::assertNull(TextFormat::decode("\x00\x01\x02\xFF"), 'binary');
|
|
}
|
|
}
|