Commit 00a3fadc authored by Anton Smirnov's avatar Anton Smirnov
Browse files

Test nested comment replacement

parent c0d9707e
Loading
Loading
Loading
Loading
Loading
+1 −4
Original line number Diff line number Diff line
@@ -382,10 +382,7 @@ final class Encoder
            return;
        }

        if (str_contains($comment, '*/')) {
            $comment = str_replace('*/', '* /', $comment);
            trigger_error("Multiline comment can't contain '*/' sequence, replacing with '* /'");
        }
        $comment = str_replace('*/', "*\u{200b}/", $comment);

        fwrite($this->resource, $prefix);
        fwrite($this->resource, '/* ');
+44 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Arokettu\Json5\Tests\Formatting;

use Arokettu\Json5\Json5Encoder;
use Arokettu\Json5\Values\Comment;
use Arokettu\Json5\Values\CommentDecorator;
use Arokettu\Json5\Values\InlineObject;
use PHPUnit\Framework\TestCase;

class NestedCommentTest extends TestCase
{
    public function testNoReplacementInFullSize(): void
    {
        $obj = [
            'k1' => new CommentDecorator('v1', '/* begin */', '/* end */'),
            new Comment('/* standalone */'),
        ];

        self::assertEquals(<<<JSON5
            {
                // /* begin */
                k1: "v1", // /* end */
                // /* standalone */
            }

            JSON5, Json5Encoder::encode($obj));
    }

    public function testReplacementInFullSize(): void
    {
        $obj = [
            'k1' => new CommentDecorator('v1', '/* begin */', '/* end */'),
            new Comment('/* standalone */'),
        ];

        self::assertEquals(<<<JSON5
            { /* /* begin *\u{200b}/ */ k1: "v1" /* /* end *\u{200b}/ */, /* /* standalone *\u{200b}/ */ }

            JSON5, Json5Encoder::encode(new InlineObject($obj)));
    }
}