xref: /dokuwiki/_test/tests/Parsing/ParserMode/GfmEmphasisStrongTest.php (revision 2bb62bca317961d66fa2908b40f183af01402a91)
1*2bb62bcaSAndreas Gohr<?php
2*2bb62bcaSAndreas Gohr
3*2bb62bcaSAndreas Gohrnamespace dokuwiki\test\Parsing\ParserMode;
4*2bb62bcaSAndreas Gohr
5*2bb62bcaSAndreas Gohruse dokuwiki\Parsing\ModeRegistry;
6*2bb62bcaSAndreas Gohruse dokuwiki\Parsing\ParserMode\GfmEmphasisStrong;
7*2bb62bcaSAndreas Gohr
8*2bb62bcaSAndreas Gohr/**
9*2bb62bcaSAndreas Gohr * Tests for the GFM em-wrapping-strong mode (`***text***`).
10*2bb62bcaSAndreas Gohr *
11*2bb62bcaSAndreas Gohr * Only the exact 3+3 symmetric variant is handled. Longer symmetric runs
12*2bb62bcaSAndreas Gohr * (4+4, 6+6, ...) and asymmetric variants fall through to other modes or
13*2bb62bcaSAndreas Gohr * stay literal.
14*2bb62bcaSAndreas Gohr */
15*2bb62bcaSAndreas Gohrclass GfmEmphasisStrongTest extends ParserTestBase
16*2bb62bcaSAndreas Gohr{
17*2bb62bcaSAndreas Gohr    public function setUp(): void
18*2bb62bcaSAndreas Gohr    {
19*2bb62bcaSAndreas Gohr        parent::setUp();
20*2bb62bcaSAndreas Gohr        global $conf;
21*2bb62bcaSAndreas Gohr        $conf['syntax'] = 'markdown';
22*2bb62bcaSAndreas Gohr        ModeRegistry::reset();
23*2bb62bcaSAndreas Gohr    }
24*2bb62bcaSAndreas Gohr
25*2bb62bcaSAndreas Gohr    public function tearDown(): void
26*2bb62bcaSAndreas Gohr    {
27*2bb62bcaSAndreas Gohr        ModeRegistry::reset();
28*2bb62bcaSAndreas Gohr        parent::tearDown();
29*2bb62bcaSAndreas Gohr    }
30*2bb62bcaSAndreas Gohr
31*2bb62bcaSAndreas Gohr    function testBasic()
32*2bb62bcaSAndreas Gohr    {
33*2bb62bcaSAndreas Gohr        $this->P->addMode('gfm_emphasis_strong', new GfmEmphasisStrong());
34*2bb62bcaSAndreas Gohr        $this->P->parse('Foo ***Bar*** Baz');
35*2bb62bcaSAndreas Gohr        $calls = [
36*2bb62bcaSAndreas Gohr            ['document_start', []],
37*2bb62bcaSAndreas Gohr            ['p_open', []],
38*2bb62bcaSAndreas Gohr            ['cdata', ["\nFoo "]],
39*2bb62bcaSAndreas Gohr            ['emphasis_open', []],
40*2bb62bcaSAndreas Gohr            ['strong_open', []],
41*2bb62bcaSAndreas Gohr            ['cdata', ['Bar']],
42*2bb62bcaSAndreas Gohr            ['strong_close', []],
43*2bb62bcaSAndreas Gohr            ['emphasis_close', []],
44*2bb62bcaSAndreas Gohr            ['cdata', [' Baz']],
45*2bb62bcaSAndreas Gohr            ['p_close', []],
46*2bb62bcaSAndreas Gohr            ['document_end', []],
47*2bb62bcaSAndreas Gohr        ];
48*2bb62bcaSAndreas Gohr        $this->assertCalls($calls, $this->H->calls);
49*2bb62bcaSAndreas Gohr    }
50*2bb62bcaSAndreas Gohr
51*2bb62bcaSAndreas Gohr    function testSingleCharacter()
52*2bb62bcaSAndreas Gohr    {
53*2bb62bcaSAndreas Gohr        $this->P->addMode('gfm_emphasis_strong', new GfmEmphasisStrong());
54*2bb62bcaSAndreas Gohr        $this->P->parse('***a***');
55*2bb62bcaSAndreas Gohr        $modes = array_column($this->H->calls, 0);
56*2bb62bcaSAndreas Gohr        $this->assertContains('emphasis_open', $modes);
57*2bb62bcaSAndreas Gohr        $this->assertContains('strong_open', $modes);
58*2bb62bcaSAndreas Gohr        $this->assertContains('strong_close', $modes);
59*2bb62bcaSAndreas Gohr        $this->assertContains('emphasis_close', $modes);
60*2bb62bcaSAndreas Gohr    }
61*2bb62bcaSAndreas Gohr
62*2bb62bcaSAndreas Gohr    function testLeadingWhitespaceDoesNotMatch()
63*2bb62bcaSAndreas Gohr    {
64*2bb62bcaSAndreas Gohr        $this->P->addMode('gfm_emphasis_strong', new GfmEmphasisStrong());
65*2bb62bcaSAndreas Gohr        $this->P->parse('*** foo***');
66*2bb62bcaSAndreas Gohr        $this->assertNotContains('emphasis_open', array_column($this->H->calls, 0));
67*2bb62bcaSAndreas Gohr    }
68*2bb62bcaSAndreas Gohr
69*2bb62bcaSAndreas Gohr    function testTrailingWhitespaceDoesNotMatch()
70*2bb62bcaSAndreas Gohr    {
71*2bb62bcaSAndreas Gohr        $this->P->addMode('gfm_emphasis_strong', new GfmEmphasisStrong());
72*2bb62bcaSAndreas Gohr        $this->P->parse('***foo ***');
73*2bb62bcaSAndreas Gohr        $this->assertNotContains('emphasis_open', array_column($this->H->calls, 0));
74*2bb62bcaSAndreas Gohr    }
75*2bb62bcaSAndreas Gohr
76*2bb62bcaSAndreas Gohr    function testDoesNotSpanParagraphBoundary()
77*2bb62bcaSAndreas Gohr    {
78*2bb62bcaSAndreas Gohr        $this->P->addMode('gfm_emphasis_strong', new GfmEmphasisStrong());
79*2bb62bcaSAndreas Gohr        $this->P->parse("***foo\n\nbar***");
80*2bb62bcaSAndreas Gohr        $this->assertNotContains('emphasis_open', array_column($this->H->calls, 0));
81*2bb62bcaSAndreas Gohr    }
82*2bb62bcaSAndreas Gohr
83*2bb62bcaSAndreas Gohr    function testLongerSymmetricRunDoesNotMatch()
84*2bb62bcaSAndreas Gohr    {
85*2bb62bcaSAndreas Gohr        // `****foo****` has 4 asterisks each side. The entry pattern requires
86*2bb62bcaSAndreas Gohr        // the opener run to be exactly 3 (via `(?<!\*)` and `(?!\*)` on the
87*2bb62bcaSAndreas Gohr        // closer), so this mode doesn't fire. It falls through to other
88*2bb62bcaSAndreas Gohr        // modes (which is tested via the full spec suite).
89*2bb62bcaSAndreas Gohr        $this->P->addMode('gfm_emphasis_strong', new GfmEmphasisStrong());
90*2bb62bcaSAndreas Gohr        $this->P->parse('****foo****');
91*2bb62bcaSAndreas Gohr        $this->assertNotContains('emphasis_open', array_column($this->H->calls, 0));
92*2bb62bcaSAndreas Gohr    }
93*2bb62bcaSAndreas Gohr
94*2bb62bcaSAndreas Gohr    function testAsymmetricDoesNotMatch()
95*2bb62bcaSAndreas Gohr    {
96*2bb62bcaSAndreas Gohr        // `***foo**` has 3 asterisks on the left but only 2 on the right.
97*2bb62bcaSAndreas Gohr        // The entry's closing-delimiter lookahead requires exactly 3 `*`s
98*2bb62bcaSAndreas Gohr        // not followed by another `*`, so there's no valid closer.
99*2bb62bcaSAndreas Gohr        $this->P->addMode('gfm_emphasis_strong', new GfmEmphasisStrong());
100*2bb62bcaSAndreas Gohr        $this->P->parse('***foo**');
101*2bb62bcaSAndreas Gohr        $this->assertNotContains('emphasis_open', array_column($this->H->calls, 0));
102*2bb62bcaSAndreas Gohr    }
103*2bb62bcaSAndreas Gohr
104*2bb62bcaSAndreas Gohr    function testSortValue()
105*2bb62bcaSAndreas Gohr    {
106*2bb62bcaSAndreas Gohr        $this->assertSame(65, (new GfmEmphasisStrong())->getSort());
107*2bb62bcaSAndreas Gohr    }
108*2bb62bcaSAndreas Gohr}
109