xref: /dokuwiki/_test/tests/Parsing/ParserMode/GfmFileTest.php (revision 47a02a102092be9e1e6f1ddaf158bdfffdb13d4f)
1b1c59bedSAndreas Gohr<?php
2b1c59bedSAndreas Gohr
3b1c59bedSAndreas Gohrnamespace dokuwiki\test\Parsing\ParserMode;
4b1c59bedSAndreas Gohr
5b1c59bedSAndreas Gohruse dokuwiki\Parsing\ParserMode\Eol;
6b1c59bedSAndreas Gohruse dokuwiki\Parsing\ParserMode\GfmFile;
7b1c59bedSAndreas Gohr
8b1c59bedSAndreas Gohr/**
9b1c59bedSAndreas Gohr * Tests for GFM tilde-fenced code blocks (`GfmFile`).
10b1c59bedSAndreas Gohr */
11b1c59bedSAndreas Gohrclass GfmFileTest extends ParserTestBase
12b1c59bedSAndreas Gohr{
13b1c59bedSAndreas Gohr    public function setUp(): void
14b1c59bedSAndreas Gohr    {
15b1c59bedSAndreas Gohr        parent::setUp();
16*47a02a10SAndreas Gohr        $this->setSyntax('md');
17b1c59bedSAndreas Gohr    }
18b1c59bedSAndreas Gohr
19b1c59bedSAndreas Gohr    private function addModes(): void
20b1c59bedSAndreas Gohr    {
21b1c59bedSAndreas Gohr        $this->P->addMode('gfm_file', new GfmFile());
22b1c59bedSAndreas Gohr        $this->P->addMode('eol', new Eol());
23b1c59bedSAndreas Gohr    }
24b1c59bedSAndreas Gohr
25b1c59bedSAndreas Gohr    function testBasicTildeFence()
26b1c59bedSAndreas Gohr    {
27b1c59bedSAndreas Gohr        $this->addModes();
28b1c59bedSAndreas Gohr        $this->P->parse("~~~\nhello\n~~~");
29b1c59bedSAndreas Gohr        $fileCalls = array_values(array_filter(
30b1c59bedSAndreas Gohr            $this->H->calls,
31b1c59bedSAndreas Gohr            static fn($c) => $c[0] === 'file'
32b1c59bedSAndreas Gohr        ));
33b1c59bedSAndreas Gohr        $this->assertCount(1, $fileCalls);
34b1c59bedSAndreas Gohr        $this->assertSame("hello\n", $fileCalls[0][1][0]);
35b1c59bedSAndreas Gohr        $this->assertNull($fileCalls[0][1][1]);
36b1c59bedSAndreas Gohr        $this->assertNull($fileCalls[0][1][2]);
37b1c59bedSAndreas Gohr    }
38b1c59bedSAndreas Gohr
39b1c59bedSAndreas Gohr    function testLanguageFromInfoString()
40b1c59bedSAndreas Gohr    {
41b1c59bedSAndreas Gohr        $this->addModes();
42b1c59bedSAndreas Gohr        $this->P->parse("~~~ruby\nx\n~~~");
43b1c59bedSAndreas Gohr        $fileCalls = array_values(array_filter(
44b1c59bedSAndreas Gohr            $this->H->calls,
45b1c59bedSAndreas Gohr            static fn($c) => $c[0] === 'file'
46b1c59bedSAndreas Gohr        ));
47b1c59bedSAndreas Gohr        $this->assertCount(1, $fileCalls);
48b1c59bedSAndreas Gohr        $this->assertSame('ruby', $fileCalls[0][1][1]);
49b1c59bedSAndreas Gohr    }
50b1c59bedSAndreas Gohr
51b1c59bedSAndreas Gohr    function testTildeInfoAcceptsBackticks()
52b1c59bedSAndreas Gohr    {
53b1c59bedSAndreas Gohr        // GFM spec example 116: tilde fences allow backticks in the info
54b1c59bedSAndreas Gohr        // string; first word is the language.
55b1c59bedSAndreas Gohr        $this->addModes();
56b1c59bedSAndreas Gohr        $this->P->parse("~~~ aa ``` ~~~\nfoo\n~~~");
57b1c59bedSAndreas Gohr        $fileCalls = array_values(array_filter(
58b1c59bedSAndreas Gohr            $this->H->calls,
59b1c59bedSAndreas Gohr            static fn($c) => $c[0] === 'file'
60b1c59bedSAndreas Gohr        ));
61b1c59bedSAndreas Gohr        $this->assertCount(1, $fileCalls);
62b1c59bedSAndreas Gohr        $this->assertSame('aa', $fileCalls[0][1][1]);
63b1c59bedSAndreas Gohr    }
64b1c59bedSAndreas Gohr
65b1c59bedSAndreas Gohr    function testBacktickLineDoesNotCloseTildeFence()
66b1c59bedSAndreas Gohr    {
67b1c59bedSAndreas Gohr        $this->addModes();
68b1c59bedSAndreas Gohr        $this->P->parse("~~~\naaa\n```\nbbb\n~~~");
69b1c59bedSAndreas Gohr        $fileCalls = array_values(array_filter(
70b1c59bedSAndreas Gohr            $this->H->calls,
71b1c59bedSAndreas Gohr            static fn($c) => $c[0] === 'file'
72b1c59bedSAndreas Gohr        ));
73b1c59bedSAndreas Gohr        $this->assertCount(1, $fileCalls);
74b1c59bedSAndreas Gohr        $this->assertSame("aaa\n```\nbbb\n", $fileCalls[0][1][0]);
75b1c59bedSAndreas Gohr    }
76b1c59bedSAndreas Gohr
77b1c59bedSAndreas Gohr    function testUnclosedFenceStaysLiteral()
78b1c59bedSAndreas Gohr    {
79b1c59bedSAndreas Gohr        // Unclosed fences stay literal — same rule as GfmCode. See
80b1c59bedSAndreas Gohr        // GfmCode class docblock for the rationale.
81b1c59bedSAndreas Gohr        $this->addModes();
82b1c59bedSAndreas Gohr        $this->P->parse("~~~\nabc\ndef");
83b1c59bedSAndreas Gohr        $modes = array_column($this->H->calls, 0);
84b1c59bedSAndreas Gohr        $this->assertNotContains('file', $modes,
85b1c59bedSAndreas Gohr            'Unclosed tilde fences must stay literal, not emit file');
86b1c59bedSAndreas Gohr    }
87b1c59bedSAndreas Gohr
88b1c59bedSAndreas Gohr    function testEmptyBody()
89b1c59bedSAndreas Gohr    {
90b1c59bedSAndreas Gohr        $this->addModes();
91b1c59bedSAndreas Gohr        $this->P->parse("~~~\n~~~");
92b1c59bedSAndreas Gohr        $fileCalls = array_values(array_filter(
93b1c59bedSAndreas Gohr            $this->H->calls,
94b1c59bedSAndreas Gohr            static fn($c) => $c[0] === 'file'
95b1c59bedSAndreas Gohr        ));
96b1c59bedSAndreas Gohr        $this->assertCount(1, $fileCalls);
97b1c59bedSAndreas Gohr        $this->assertSame('', $fileCalls[0][1][0]);
98b1c59bedSAndreas Gohr    }
99b1c59bedSAndreas Gohr
100b1c59bedSAndreas Gohr    function testSortValue()
101b1c59bedSAndreas Gohr    {
102b1c59bedSAndreas Gohr        $this->assertSame(210, (new GfmFile())->getSort());
103b1c59bedSAndreas Gohr    }
104b1c59bedSAndreas Gohr}
105