xref: /dokuwiki/_test/tests/Parsing/ParserMode/GfmHrTest.php (revision 13a62f810fbd091d15ab734b467eaec0a6bf829a)
13e6baeffSAndreas Gohr<?php
23e6baeffSAndreas Gohr
33e6baeffSAndreas Gohrnamespace dokuwiki\test\Parsing\ParserMode;
43e6baeffSAndreas Gohr
53e6baeffSAndreas Gohruse dokuwiki\Parsing\ModeRegistry;
63e6baeffSAndreas Gohruse dokuwiki\Parsing\ParserMode\Eol;
73e6baeffSAndreas Gohruse dokuwiki\Parsing\ParserMode\GfmHr;
83e6baeffSAndreas Gohr
93e6baeffSAndreas Gohr/**
103e6baeffSAndreas Gohr * Tests for the unified horizontal-rule mode.
113e6baeffSAndreas Gohr *
12*13a62f81SAndreas Gohr * Covers both pattern flavors: pure `dw` (4-or-more dashes only)
13*13a62f81SAndreas Gohr * and the wider GFM flavor that loads in `md`, `dw+md`, `md+dw`
143e6baeffSAndreas Gohr * (3-or-more of `-` / `*` / `_`). No leading, trailing, or internal
153e6baeffSAndreas Gohr * whitespace tolerance in either flavor.
163e6baeffSAndreas Gohr */
173e6baeffSAndreas Gohrclass GfmHrTest extends ParserTestBase
183e6baeffSAndreas Gohr{
193e6baeffSAndreas Gohr    public function setUp(): void
203e6baeffSAndreas Gohr    {
213e6baeffSAndreas Gohr        parent::setUp();
223e6baeffSAndreas Gohr        ModeRegistry::reset();
233e6baeffSAndreas Gohr    }
243e6baeffSAndreas Gohr
253e6baeffSAndreas Gohr    public function tearDown(): void
263e6baeffSAndreas Gohr    {
273e6baeffSAndreas Gohr        ModeRegistry::reset();
283e6baeffSAndreas Gohr        parent::tearDown();
293e6baeffSAndreas Gohr    }
303e6baeffSAndreas Gohr
313e6baeffSAndreas Gohr    protected function setSyntax(string $syntax): void
323e6baeffSAndreas Gohr    {
333e6baeffSAndreas Gohr        global $conf;
343e6baeffSAndreas Gohr        $conf['syntax'] = $syntax;
353e6baeffSAndreas Gohr    }
363e6baeffSAndreas Gohr
373e6baeffSAndreas Gohr    /**
383e6baeffSAndreas Gohr     * Whether at least one `hr` call was emitted for the given input
393e6baeffSAndreas Gohr     * under the given syntax. Returns the call count for richer
403e6baeffSAndreas Gohr     * assertion messages.
413e6baeffSAndreas Gohr     */
423e6baeffSAndreas Gohr    protected function countHrCalls(string $syntax, string $input): int
433e6baeffSAndreas Gohr    {
443e6baeffSAndreas Gohr        $this->setUp();
453e6baeffSAndreas Gohr        $this->setSyntax($syntax);
463e6baeffSAndreas Gohr        $this->P->addMode('gfm_hr', new GfmHr());
473e6baeffSAndreas Gohr        $this->P->parse($input);
483e6baeffSAndreas Gohr        return count(array_filter($this->H->calls, static fn($c) => $c[0] === 'hr'));
493e6baeffSAndreas Gohr    }
503e6baeffSAndreas Gohr
513e6baeffSAndreas Gohr    // ------------------------------------------------------------------
52*13a62f81SAndreas Gohr    // DW flavor (`$conf['syntax'] = 'dw'`)
533e6baeffSAndreas Gohr    // ------------------------------------------------------------------
543e6baeffSAndreas Gohr
553e6baeffSAndreas Gohr    public function testDwFourDashes()
563e6baeffSAndreas Gohr    {
57*13a62f81SAndreas Gohr        $this->assertSame(1, $this->countHrCalls('dw', "\n----\n"));
583e6baeffSAndreas Gohr    }
593e6baeffSAndreas Gohr
603e6baeffSAndreas Gohr    public function testDwManyDashes()
613e6baeffSAndreas Gohr    {
62*13a62f81SAndreas Gohr        $this->assertSame(1, $this->countHrCalls('dw', "\n--------\n"));
633e6baeffSAndreas Gohr    }
643e6baeffSAndreas Gohr
653e6baeffSAndreas Gohr    public function testDwThreeDashesNotHr()
663e6baeffSAndreas Gohr    {
67*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('dw', "\n---\n"));
683e6baeffSAndreas Gohr    }
693e6baeffSAndreas Gohr
703e6baeffSAndreas Gohr    public function testDwAsterisksNotHr()
713e6baeffSAndreas Gohr    {
72*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('dw', "\n***\n"));
73*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('dw', "\n********\n"));
743e6baeffSAndreas Gohr    }
753e6baeffSAndreas Gohr
763e6baeffSAndreas Gohr    public function testDwUnderscoresNotHr()
773e6baeffSAndreas Gohr    {
78*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('dw', "\n___\n"));
79*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('dw', "\n_____\n"));
803e6baeffSAndreas Gohr    }
813e6baeffSAndreas Gohr
823e6baeffSAndreas Gohr    public function testDwLeadingSpaceNotHr()
833e6baeffSAndreas Gohr    {
84*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('dw', "\n ----\n"));
853e6baeffSAndreas Gohr    }
863e6baeffSAndreas Gohr
873e6baeffSAndreas Gohr    public function testDwTrailingSpaceNotHr()
883e6baeffSAndreas Gohr    {
89*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('dw', "\n---- \n"));
903e6baeffSAndreas Gohr    }
913e6baeffSAndreas Gohr
923e6baeffSAndreas Gohr    public function testDwInterruptsParagraph()
933e6baeffSAndreas Gohr    {
94*13a62f81SAndreas Gohr        $this->setSyntax('dw');
953e6baeffSAndreas Gohr        $this->P->addMode('gfm_hr', new GfmHr());
963e6baeffSAndreas Gohr        $this->P->parse("Foo\n----\nBar");
973e6baeffSAndreas Gohr        $calls = [
983e6baeffSAndreas Gohr            ['document_start', []],
993e6baeffSAndreas Gohr            ['p_open', []],
1003e6baeffSAndreas Gohr            ['cdata', ["\nFoo"]],
1013e6baeffSAndreas Gohr            ['p_close', []],
1023e6baeffSAndreas Gohr            ['hr', []],
1033e6baeffSAndreas Gohr            ['p_open', []],
1043e6baeffSAndreas Gohr            ['cdata', ["\nBar"]],
1053e6baeffSAndreas Gohr            ['p_close', []],
1063e6baeffSAndreas Gohr            ['document_end', []],
1073e6baeffSAndreas Gohr        ];
1083e6baeffSAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
1093e6baeffSAndreas Gohr    }
1103e6baeffSAndreas Gohr
1113e6baeffSAndreas Gohr    // ------------------------------------------------------------------
112*13a62f81SAndreas Gohr    // GFM flavor (any non-`dw` syntax setting)
1133e6baeffSAndreas Gohr    // ------------------------------------------------------------------
1143e6baeffSAndreas Gohr
1153e6baeffSAndreas Gohr    public function testMdDashes()
1163e6baeffSAndreas Gohr    {
117*13a62f81SAndreas Gohr        foreach (['md', 'dw+md', 'md+dw'] as $syntax) {
1183e6baeffSAndreas Gohr            $this->assertSame(1, $this->countHrCalls($syntax, "\n---\n"),
1193e6baeffSAndreas Gohr                "syntax=$syntax: bare `---` must produce hr");
1203e6baeffSAndreas Gohr        }
1213e6baeffSAndreas Gohr    }
1223e6baeffSAndreas Gohr
1233e6baeffSAndreas Gohr    public function testMdAsterisks()
1243e6baeffSAndreas Gohr    {
125*13a62f81SAndreas Gohr        foreach (['md', 'dw+md', 'md+dw'] as $syntax) {
1263e6baeffSAndreas Gohr            $this->assertSame(1, $this->countHrCalls($syntax, "\n***\n"),
1273e6baeffSAndreas Gohr                "syntax=$syntax: bare `***` must produce hr");
1283e6baeffSAndreas Gohr        }
1293e6baeffSAndreas Gohr    }
1303e6baeffSAndreas Gohr
1313e6baeffSAndreas Gohr    public function testMdUnderscores()
1323e6baeffSAndreas Gohr    {
133*13a62f81SAndreas Gohr        foreach (['md', 'dw+md', 'md+dw'] as $syntax) {
1343e6baeffSAndreas Gohr            $this->assertSame(1, $this->countHrCalls($syntax, "\n___\n"),
1353e6baeffSAndreas Gohr                "syntax=$syntax: bare `___` must produce hr");
1363e6baeffSAndreas Gohr        }
1373e6baeffSAndreas Gohr    }
1383e6baeffSAndreas Gohr
1393e6baeffSAndreas Gohr    public function testMdManyChars()
1403e6baeffSAndreas Gohr    {
141*13a62f81SAndreas Gohr        $this->assertSame(1, $this->countHrCalls('md', "\n--------\n"));
142*13a62f81SAndreas Gohr        $this->assertSame(1, $this->countHrCalls('md', "\n********\n"));
143*13a62f81SAndreas Gohr        $this->assertSame(1, $this->countHrCalls('md', "\n________\n"));
1443e6baeffSAndreas Gohr    }
1453e6baeffSAndreas Gohr
1463e6baeffSAndreas Gohr    public function testMdTooFew()
1473e6baeffSAndreas Gohr    {
148*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('md', "\n--\n"));
149*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('md', "\n**\n"));
150*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('md', "\n__\n"));
1513e6baeffSAndreas Gohr    }
1523e6baeffSAndreas Gohr
1533e6baeffSAndreas Gohr    public function testMdInternalSpacesNotSupported()
1543e6baeffSAndreas Gohr    {
155*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('md', "\n- - -\n"));
156*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('md', "\n* * *\n"));
157*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('md', "\n_ _ _\n"));
1583e6baeffSAndreas Gohr    }
1593e6baeffSAndreas Gohr
1603e6baeffSAndreas Gohr    public function testMdLeadingSpaceNotSupported()
1613e6baeffSAndreas Gohr    {
162*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('md', "\n ***\n"));
163*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('md', "\n   ---\n"));
1643e6baeffSAndreas Gohr    }
1653e6baeffSAndreas Gohr
1663e6baeffSAndreas Gohr    public function testMdTrailingSpaceNotSupported()
1673e6baeffSAndreas Gohr    {
168*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('md', "\n--- \n"));
1693e6baeffSAndreas Gohr    }
1703e6baeffSAndreas Gohr
1713e6baeffSAndreas Gohr    public function testMdMixedChars()
1723e6baeffSAndreas Gohr    {
173*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('md', "\n-*-\n"));
174*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('md', "\n***---\n"));
1753e6baeffSAndreas Gohr    }
1763e6baeffSAndreas Gohr
1773e6baeffSAndreas Gohr    public function testMdLetterMixed()
1783e6baeffSAndreas Gohr    {
179*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('md', "\n---a\n"));
180*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('md', "\na---\n"));
181*13a62f81SAndreas Gohr        $this->assertSame(0, $this->countHrCalls('md', "\n---a---\n"));
1823e6baeffSAndreas Gohr    }
1833e6baeffSAndreas Gohr
1843e6baeffSAndreas Gohr    public function testMdInterruptsParagraph()
1853e6baeffSAndreas Gohr    {
186*13a62f81SAndreas Gohr        $this->setSyntax('md');
1873e6baeffSAndreas Gohr        $this->P->addMode('gfm_hr', new GfmHr());
1883e6baeffSAndreas Gohr        $this->P->addMode('eol', new Eol());
1893e6baeffSAndreas Gohr        $this->P->parse("Foo\n***\nbar");
1903e6baeffSAndreas Gohr        $modes = array_column($this->H->calls, 0);
1913e6baeffSAndreas Gohr        $this->assertContains('hr', $modes,
1923e6baeffSAndreas Gohr            'thematic break interrupts paragraph without blank line (spec 28)');
1933e6baeffSAndreas Gohr    }
1943e6baeffSAndreas Gohr
1953e6baeffSAndreas Gohr    // ------------------------------------------------------------------
1963e6baeffSAndreas Gohr    // Common
1973e6baeffSAndreas Gohr    // ------------------------------------------------------------------
1983e6baeffSAndreas Gohr
1993e6baeffSAndreas Gohr    public function testSortValue()
2003e6baeffSAndreas Gohr    {
2013e6baeffSAndreas Gohr        $mode = new GfmHr();
2023e6baeffSAndreas Gohr        $this->assertSame(160, $mode->getSort());
2033e6baeffSAndreas Gohr    }
2043e6baeffSAndreas Gohr}
205