xref: /dokuwiki/_test/tests/Parsing/Helpers/CodeTest.php (revision 1e28e406b358f79221c515b2a56520d5dbbfb6c8)
1*1e28e406SAndreas Gohr<?php
2*1e28e406SAndreas Gohr
3*1e28e406SAndreas Gohrnamespace dokuwiki\test\Parsing\Helpers;
4*1e28e406SAndreas Gohr
5*1e28e406SAndreas Gohruse dokuwiki\Parsing\Helpers\Code;
6*1e28e406SAndreas Gohr
7*1e28e406SAndreas Gohr/**
8*1e28e406SAndreas Gohr * Tests for code-block attribute parsing shared between Code / File
9*1e28e406SAndreas Gohr * (DokuWiki) and GfmCode / GfmFile.
10*1e28e406SAndreas Gohr */
11*1e28e406SAndreas Gohrclass CodeTest extends \DokuWikiTest
12*1e28e406SAndreas Gohr{
13*1e28e406SAndreas Gohr    // ----- parseHighlightOptions ----------------------------------------
14*1e28e406SAndreas Gohr
15*1e28e406SAndreas Gohr    public static function highlightOptionsProvider(): array
16*1e28e406SAndreas Gohr    {
17*1e28e406SAndreas Gohr        return [
18*1e28e406SAndreas Gohr            ['', null],
19*1e28e406SAndreas Gohr            ['something weird', null],
20*1e28e406SAndreas Gohr            ['enable_line_numbers', ['enable_line_numbers' => true]],
21*1e28e406SAndreas Gohr            ['enable_line_numbers=1', ['enable_line_numbers' => true]],
22*1e28e406SAndreas Gohr            ['enable_line_numbers="1"', ['enable_line_numbers' => true]],
23*1e28e406SAndreas Gohr            ['enable_line_numbers=0', ['enable_line_numbers' => false]],
24*1e28e406SAndreas Gohr            ['enable_line_numbers="0"', ['enable_line_numbers' => false]],
25*1e28e406SAndreas Gohr            ['enable_line_numbers=false', ['enable_line_numbers' => false]],
26*1e28e406SAndreas Gohr            ['enable_line_numbers="false"', ['enable_line_numbers' => false]],
27*1e28e406SAndreas Gohr            ['highlight_lines_extra', ['highlight_lines_extra' => [1]]],
28*1e28e406SAndreas Gohr            ['highlight_lines_extra=17', ['highlight_lines_extra' => [17]]],
29*1e28e406SAndreas Gohr            ['highlight_lines_extra=17,19', ['highlight_lines_extra' => [17, 19]]],
30*1e28e406SAndreas Gohr            ['highlight_lines_extra="17,19"', ['highlight_lines_extra' => [17, 19]]],
31*1e28e406SAndreas Gohr            ['highlight_lines_extra="17,19,17"', ['highlight_lines_extra' => [17, 19]]],
32*1e28e406SAndreas Gohr            ['start_line_numbers_at', ['start_line_numbers_at' => 1]],
33*1e28e406SAndreas Gohr            ['start_line_numbers_at=12', ['start_line_numbers_at' => 12]],
34*1e28e406SAndreas Gohr            ['start_line_numbers_at="12"', ['start_line_numbers_at' => 12]],
35*1e28e406SAndreas Gohr            ['enable_keyword_links', ['enable_keyword_links' => true]],
36*1e28e406SAndreas Gohr            ['enable_keyword_links=1', ['enable_keyword_links' => true]],
37*1e28e406SAndreas Gohr            ['enable_keyword_links="1"', ['enable_keyword_links' => true]],
38*1e28e406SAndreas Gohr            ['enable_keyword_links=0', ['enable_keyword_links' => false]],
39*1e28e406SAndreas Gohr            ['enable_keyword_links="0"', ['enable_keyword_links' => false]],
40*1e28e406SAndreas Gohr            ['enable_keyword_links=false', ['enable_keyword_links' => false]],
41*1e28e406SAndreas Gohr            ['enable_keyword_links="false"', ['enable_keyword_links' => false]],
42*1e28e406SAndreas Gohr            [
43*1e28e406SAndreas Gohr                'enable_line_numbers weird nothing highlight_lines_extra=17,19 start_line_numbers_at="12" enable_keyword_links=false',
44*1e28e406SAndreas Gohr                [
45*1e28e406SAndreas Gohr                    'enable_line_numbers' => true,
46*1e28e406SAndreas Gohr                    'highlight_lines_extra' => [17, 19],
47*1e28e406SAndreas Gohr                    'start_line_numbers_at' => 12,
48*1e28e406SAndreas Gohr                    'enable_keyword_links' => false
49*1e28e406SAndreas Gohr                ]
50*1e28e406SAndreas Gohr            ],
51*1e28e406SAndreas Gohr        ];
52*1e28e406SAndreas Gohr    }
53*1e28e406SAndreas Gohr
54*1e28e406SAndreas Gohr    /**
55*1e28e406SAndreas Gohr     * @dataProvider highlightOptionsProvider
56*1e28e406SAndreas Gohr     */
57*1e28e406SAndreas Gohr    function testParseHighlightOptions(string $input, ?array $expect): void
58*1e28e406SAndreas Gohr    {
59*1e28e406SAndreas Gohr        $this->assertEquals($expect, Code::parseHighlightOptions($input));
60*1e28e406SAndreas Gohr    }
61*1e28e406SAndreas Gohr
62*1e28e406SAndreas Gohr    // ----- parseAttributes ------------------------------------------
63*1e28e406SAndreas Gohr
64*1e28e406SAndreas Gohr    function testParseCodeAttributesEmpty()
65*1e28e406SAndreas Gohr    {
66*1e28e406SAndreas Gohr        $this->assertSame([null, null, null], Code::parseAttributes(''));
67*1e28e406SAndreas Gohr    }
68*1e28e406SAndreas Gohr
69*1e28e406SAndreas Gohr    function testParseCodeAttributesLanguageOnly()
70*1e28e406SAndreas Gohr    {
71*1e28e406SAndreas Gohr        $this->assertSame(['php', null, null], Code::parseAttributes('php'));
72*1e28e406SAndreas Gohr    }
73*1e28e406SAndreas Gohr
74*1e28e406SAndreas Gohr    function testParseCodeAttributesLanguageAndFilename()
75*1e28e406SAndreas Gohr    {
76*1e28e406SAndreas Gohr        $this->assertSame(
77*1e28e406SAndreas Gohr            ['php', 'myfile.php', null],
78*1e28e406SAndreas Gohr            Code::parseAttributes('php myfile.php')
79*1e28e406SAndreas Gohr        );
80*1e28e406SAndreas Gohr    }
81*1e28e406SAndreas Gohr
82*1e28e406SAndreas Gohr    function testParseCodeAttributesDashMeansNoLanguage()
83*1e28e406SAndreas Gohr    {
84*1e28e406SAndreas Gohr        // `-` is DokuWiki's explicit "no language" marker — lets a
85*1e28e406SAndreas Gohr        // filename follow without a language argument first.
86*1e28e406SAndreas Gohr        $this->assertSame(
87*1e28e406SAndreas Gohr            [null, 'myfile.txt', null],
88*1e28e406SAndreas Gohr            Code::parseAttributes('- myfile.txt')
89*1e28e406SAndreas Gohr        );
90*1e28e406SAndreas Gohr    }
91*1e28e406SAndreas Gohr
92*1e28e406SAndreas Gohr    function testParseCodeAttributesHtmlAliased()
93*1e28e406SAndreas Gohr    {
94*1e28e406SAndreas Gohr        // GeSHi's identifier for HTML is `html4strict`; DokuWiki
95*1e28e406SAndreas Gohr        // normalises `html` for author convenience.
96*1e28e406SAndreas Gohr        $this->assertSame(
97*1e28e406SAndreas Gohr            ['html4strict', null, null],
98*1e28e406SAndreas Gohr            Code::parseAttributes('html')
99*1e28e406SAndreas Gohr        );
100*1e28e406SAndreas Gohr    }
101*1e28e406SAndreas Gohr
102*1e28e406SAndreas Gohr    function testParseCodeAttributesOptionsOnly()
103*1e28e406SAndreas Gohr    {
104*1e28e406SAndreas Gohr        $this->assertSame(
105*1e28e406SAndreas Gohr            [null, null, ['enable_line_numbers' => true]],
106*1e28e406SAndreas Gohr            Code::parseAttributes('[enable_line_numbers]')
107*1e28e406SAndreas Gohr        );
108*1e28e406SAndreas Gohr    }
109*1e28e406SAndreas Gohr
110*1e28e406SAndreas Gohr    function testParseCodeAttributesLanguageAndOptions()
111*1e28e406SAndreas Gohr    {
112*1e28e406SAndreas Gohr        $this->assertSame(
113*1e28e406SAndreas Gohr            ['php', null, ['enable_line_numbers' => true]],
114*1e28e406SAndreas Gohr            Code::parseAttributes('php [enable_line_numbers]')
115*1e28e406SAndreas Gohr        );
116*1e28e406SAndreas Gohr    }
117*1e28e406SAndreas Gohr
118*1e28e406SAndreas Gohr    function testParseCodeAttributesLanguageFilenameAndOptions()
119*1e28e406SAndreas Gohr    {
120*1e28e406SAndreas Gohr        $this->assertSame(
121*1e28e406SAndreas Gohr            ['php', 'myfile.php', ['enable_line_numbers' => true]],
122*1e28e406SAndreas Gohr            Code::parseAttributes('php myfile.php [enable_line_numbers]')
123*1e28e406SAndreas Gohr        );
124*1e28e406SAndreas Gohr    }
125*1e28e406SAndreas Gohr
126*1e28e406SAndreas Gohr    function testParseCodeAttributesUnknownOptionsReturnsNull()
127*1e28e406SAndreas Gohr    {
128*1e28e406SAndreas Gohr        // Unknown keys in `[...]` are filtered out; the options slot
129*1e28e406SAndreas Gohr        // ends up null, same as if no `[...]` block had been present.
130*1e28e406SAndreas Gohr        $this->assertSame(
131*1e28e406SAndreas Gohr            ['C', null, null],
132*1e28e406SAndreas Gohr            Code::parseAttributes('C [unknown="ignored"]')
133*1e28e406SAndreas Gohr        );
134*1e28e406SAndreas Gohr    }
135*1e28e406SAndreas Gohr}
136