xref: /dokuwiki/_test/tests/Parsing/ParserMode/ReplacementsTest.php (revision 504c13e8df88563c11b3720b317991bc38835a35)
1<?php
2
3namespace dokuwiki\test\Parsing\ParserMode;
4
5use dokuwiki\Parsing\ParserMode\Acronym;
6use dokuwiki\Parsing\ParserMode\Entity;
7use dokuwiki\Parsing\ParserMode\Hr;
8use dokuwiki\Parsing\ParserMode\Multiplyentity;
9use dokuwiki\Parsing\ParserMode\Smiley;
10use dokuwiki\Parsing\ParserMode\Wordblock;
11
12class ReplacementsTest extends ParserTestBase
13{
14
15    function testSingleAcronym() {
16        $this->P->addMode('acronym',new Acronym(['FOOBAR']));
17        $this->P->parse('abc FOOBAR xyz');
18        $calls = [
19            ['document_start',[]],
20            ['p_open',[]],
21            ['cdata',["\n".'abc ']],
22            ['acronym',['FOOBAR']],
23            ['cdata',[' xyz']],
24            ['p_close',[]],
25            ['document_end',[]],
26        ];
27        $this->assertCalls($calls, $this->H->calls);
28    }
29
30    function testAlmostAnAcronym() {
31        $this->P->addMode('acronym',new Acronym(['FOOBAR']));
32        $this->P->parse('abcFOOBARxyz');
33        $calls = [
34            ['document_start',[]],
35            ['p_open',[]],
36            ['cdata',["\n".'abcFOOBARxyz']],
37            ['p_close',[]],
38            ['document_end',[]],
39        ];
40        $this->assertCalls($calls, $this->H->calls);
41    }
42
43    function testPickAcronymCorrectly() {
44        $this->P->addMode('acronym',new Acronym(['FOO']));
45        $this->P->parse('FOOBAR FOO');
46        $calls = [
47            ['document_start',[]],
48            ['p_open',[]],
49            ['cdata',["\n".'FOOBAR ']],
50            ['acronym',['FOO']],
51            ['cdata',['']],
52            ['p_close',[]],
53            ['document_end',[]],
54        ];
55        $this->assertCalls($calls, $this->H->calls);
56    }
57
58    function testMultipleAcronyms() {
59        $this->P->addMode('acronym',new Acronym(['FOO','BAR']));
60        $this->P->parse('abc FOO def BAR xyz');
61        $calls = [
62            ['document_start',[]],
63            ['p_open',[]],
64            ['cdata',["\n".'abc ']],
65            ['acronym',['FOO']],
66            ['cdata',[' def ']],
67            ['acronym',['BAR']],
68            ['cdata',[' xyz']],
69            ['p_close',[]],
70            ['document_end',[]],
71        ];
72        $this->assertCalls($calls, $this->H->calls);
73    }
74
75    function testMultipleAcronymsWithSubset1() {
76        $this->P->addMode('acronym',new Acronym(['FOO','A.FOO','FOO.1','A.FOO.1']));
77        $this->P->parse('FOO A.FOO FOO.1 A.FOO.1');
78        $calls = [
79            ['document_start',[]],
80            ['p_open',[]],
81            ['cdata',["\n"]],
82            ['acronym',['FOO']],
83            ['cdata',[" "]],
84            ['acronym',['A.FOO']],
85            ['cdata',[" "]],
86            ['acronym',['FOO.1']],
87            ['cdata',[" "]],
88            ['acronym',['A.FOO.1']],
89            ['cdata',['']],
90            ['p_close',[]],
91            ['document_end',[]],
92        ];
93        $this->assertCalls($calls, $this->H->calls);
94    }
95
96    function testMultipleAcronymsWithSubset2() {
97        $this->P->addMode('acronym',new Acronym(['A.FOO.1','FOO.1','A.FOO','FOO']));
98        $this->P->parse('FOO A.FOO FOO.1 A.FOO.1');
99        $calls = [
100            ['document_start',[]],
101            ['p_open',[]],
102            ['cdata',["\n"]],
103            ['acronym',['FOO']],
104            ['cdata',[" "]],
105            ['acronym',['A.FOO']],
106            ['cdata',[" "]],
107            ['acronym',['FOO.1']],
108            ['cdata',[" "]],
109            ['acronym',['A.FOO.1']],
110            ['cdata',['']],
111            ['p_close',[]],
112            ['document_end',[]],
113        ];
114        $this->assertCalls($calls, $this->H->calls);
115    }
116
117    function testSingleSmileyFail() {
118        $this->P->addMode('smiley',new Smiley([':-)']));
119        $this->P->parse('abc:-)xyz');
120        $calls = [
121            ['document_start',[]],
122            ['p_open',[]],
123            ['cdata',["\nabc:-)xyz"]],
124            ['p_close',[]],
125            ['document_end',[]],
126        ];
127        $this->assertCalls($calls, $this->H->calls);
128    }
129
130    function testSingleSmiley() {
131        $this->P->addMode('smiley',new Smiley([':-)']));
132        $this->P->parse('abc :-) xyz');
133        $calls = [
134            ['document_start',[]],
135            ['p_open',[]],
136            ['cdata',["\n".'abc ']],
137            ['smiley',[':-)']],
138            ['cdata',[' xyz']],
139            ['p_close',[]],
140            ['document_end',[]],
141        ];
142        $this->assertCalls($calls, $this->H->calls);
143    }
144
145    function testMultipleSmileysFail() {
146        $this->P->addMode('smiley',new Smiley([':-)','^_^']));
147        $this->P->parse('abc:-)x^_^yz');
148        $calls = [
149            ['document_start',[]],
150            ['p_open',[]],
151            ['cdata',["\nabc:-)x^_^yz"]],
152            ['p_close',[]],
153            ['document_end',[]],
154        ];
155        $this->assertCalls($calls, $this->H->calls);
156    }
157
158    function testMultipleSmileys() {
159        $this->P->addMode('smiley',new Smiley([':-)','^_^']));
160        $this->P->parse('abc :-) x ^_^ yz');
161        $calls = [
162            ['document_start',[]],
163            ['p_open',[]],
164            ['cdata',["\n".'abc ']],
165            ['smiley',[':-)']],
166            ['cdata',[' x ']],
167            ['smiley',['^_^']],
168            ['cdata',[' yz']],
169            ['p_close',[]],
170            ['document_end',[]],
171        ];
172        $this->assertCalls($calls, $this->H->calls);
173    }
174
175    function testBackslashSmileyFail() {
176        $this->P->addMode('smiley',new Smiley([':-\\\\']));
177        $this->P->parse('abc:-\\\xyz');
178        $calls = [
179            ['document_start',[]],
180            ['p_open',[]],
181            ['cdata',["\nabc".':-\\\\'."xyz"]],
182            ['p_close',[]],
183            ['document_end',[]],
184        ];
185        $this->assertCalls($calls, $this->H->calls);
186    }
187
188    function testBackslashSmiley() {
189        $this->P->addMode('smiley',new Smiley([':-\\\\']));
190        $this->P->parse('abc :-\\\ xyz');
191        $calls = [
192            ['document_start',[]],
193            ['p_open',[]],
194            ['cdata',["\n".'abc ']],
195            ['smiley',[':-\\\\']],
196            ['cdata',[' xyz']],
197            ['p_close',[]],
198            ['document_end',[]],
199        ];
200        $this->assertCalls($calls, $this->H->calls);
201    }
202
203    function testSingleWordblock() {
204        $this->P->addMode('wordblock',new Wordblock(['CAT']));
205        $this->P->parse('abc CAT xyz');
206        $calls = [
207            ['document_start',[]],
208            ['p_open',[]],
209            ['cdata',["\n".'abc ']],
210            ['wordblock',['CAT']],
211            ['cdata',[' xyz']],
212            ['p_close',[]],
213            ['document_end',[]],
214        ];
215        $this->assertCalls($calls, $this->H->calls);
216    }
217
218    function testWordblockCase() {
219        $this->P->addMode('wordblock',new Wordblock(['CAT']));
220        $this->P->parse('abc cat xyz');
221        $calls = [
222            ['document_start',[]],
223            ['p_open',[]],
224            ['cdata',["\n".'abc ']],
225            ['wordblock',['cat']],
226            ['cdata',[' xyz']],
227            ['p_close',[]],
228            ['document_end',[]],
229        ];
230        $this->assertCalls($calls, $this->H->calls);
231    }
232
233    function testMultipleWordblock() {
234        $this->P->addMode('wordblock',new Wordblock(['CAT','dog']));
235        $this->P->parse('abc cat x DOG yz');
236        $calls = [
237            ['document_start',[]],
238            ['p_open',[]],
239            ['cdata',["\n".'abc ']],
240            ['wordblock',['cat']],
241            ['cdata',[' x ']],
242            ['wordblock',['DOG']],
243            ['cdata',[' yz']],
244            ['p_close',[]],
245            ['document_end',[]],
246        ];
247        $this->assertCalls($calls, $this->H->calls);
248    }
249
250    function testSingleEntity() {
251        $this->P->addMode('entity',new Entity(['->']));
252        $this->P->parse('x -> y');
253        $calls = [
254            ['document_start',[]],
255            ['p_open',[]],
256            ['cdata',["\n".'x ']],
257            ['entity',['->']],
258            ['cdata',[' y']],
259            ['p_close',[]],
260            ['document_end',[]],
261        ];
262        $this->assertCalls($calls, $this->H->calls);
263    }
264
265    function testMultipleEntities() {
266        $this->P->addMode('entity',new Entity(['->','<-']));
267        $this->P->parse('x -> y <- z');
268        $calls = [
269            ['document_start',[]],
270            ['p_open',[]],
271            ['cdata',["\n".'x ']],
272            ['entity',['->']],
273            ['cdata',[' y ']],
274            ['entity',['<-']],
275            ['cdata',[' z']],
276            ['p_close',[]],
277            ['document_end',[]],
278        ];
279        $this->assertCalls($calls, $this->H->calls);
280    }
281
282    function testMultiplyEntity() {
283        $this->P->addMode('multiplyentity',new Multiplyentity());
284        $this->P->parse('Foo 10x20 Bar');
285        $calls = [
286            ['document_start',[]],
287            ['p_open',[]],
288            ['cdata',["\n".'Foo ']],
289            ['multiplyentity',[10,20]],
290            ['cdata',[' Bar']],
291            ['p_close',[]],
292            ['document_end',[]],
293        ];
294        $this->assertCalls($calls, $this->H->calls);
295    }
296
297    function testMultiplyEntityHex() {
298        $this->P->addMode('multiplyentity',new Multiplyentity());
299        $this->P->parse('Foo 0x123 Bar');
300        $calls = [
301            ['document_start',[]],
302            ['p_open',[]],
303            ['cdata',["\n".'Foo 0x123 Bar']],
304            ['p_close',[]],
305            ['document_end',[]],
306        ];
307        $this->assertCalls($calls, $this->H->calls);
308    }
309
310    function testHR() {
311        $this->P->addMode('hr',new Hr());
312        $this->P->parse("Foo \n ---- \n Bar");
313        $calls = [
314            ['document_start',[]],
315            ['p_open',[]],
316            ['cdata',["\n".'Foo ']],
317            ['p_close',[]],
318            ['hr',[]],
319            ['p_open',[]],
320            ['cdata',["\n Bar"]],
321            ['p_close',[]],
322            ['document_end',[]],
323        ];
324        $this->assertCalls($calls, $this->H->calls);
325    }
326
327    function testHREol() {
328        $this->P->addMode('hr',new Hr());
329        $this->P->parse("Foo \n----\n Bar");
330        $calls = [
331            ['document_start',[]],
332            ['p_open',[]],
333            ['cdata',["\n".'Foo ']],
334            ['p_close',[]],
335            ['hr',[]],
336            ['p_open',[]],
337            ['cdata',["\n Bar"]],
338            ['p_close',[]],
339            ['document_end',[]],
340        ];
341        $this->assertCalls($calls, $this->H->calls);
342    }
343}
344