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