xref: /dokuwiki/_test/tests/Parsing/Lexer/ParallelRegexTest.php (revision 504c13e8df88563c11b3720b317991bc38835a35)
1*504c13e8SAndreas Gohr<?php
2*504c13e8SAndreas Gohr
3*504c13e8SAndreas Gohrnamespace dokuwiki\test\Parsing\Lexer;
4*504c13e8SAndreas Gohr
5*504c13e8SAndreas Gohruse dokuwiki\Parsing\Lexer\ParallelRegex;
6*504c13e8SAndreas Gohr
7*504c13e8SAndreas Gohrclass ParallelRegexTest extends \DokuWikiTest
8*504c13e8SAndreas Gohr{
9*504c13e8SAndreas Gohr    function testNoPatterns()
10*504c13e8SAndreas Gohr    {
11*504c13e8SAndreas Gohr        $regex = new ParallelRegex(false);
12*504c13e8SAndreas Gohr        $this->assertFalse($regex->split("Hello", $split));
13*504c13e8SAndreas Gohr    }
14*504c13e8SAndreas Gohr
15*504c13e8SAndreas Gohr    function testNoSubject()
16*504c13e8SAndreas Gohr    {
17*504c13e8SAndreas Gohr        $regex = new ParallelRegex(false);
18*504c13e8SAndreas Gohr        $regex->addPattern(".*");
19*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("", $split));
20*504c13e8SAndreas Gohr        $this->assertEquals("", $split[1]);
21*504c13e8SAndreas Gohr    }
22*504c13e8SAndreas Gohr
23*504c13e8SAndreas Gohr    function testMatchAll()
24*504c13e8SAndreas Gohr    {
25*504c13e8SAndreas Gohr        $regex = new ParallelRegex(false);
26*504c13e8SAndreas Gohr        $regex->addPattern(".*");
27*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("Hello", $split));
28*504c13e8SAndreas Gohr        $this->assertEquals("Hello", $split[1]);
29*504c13e8SAndreas Gohr    }
30*504c13e8SAndreas Gohr
31*504c13e8SAndreas Gohr    function testCaseSensitive()
32*504c13e8SAndreas Gohr    {
33*504c13e8SAndreas Gohr        $regex = new ParallelRegex(true);
34*504c13e8SAndreas Gohr        $regex->addPattern("abc");
35*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("abcdef", $split));
36*504c13e8SAndreas Gohr        $this->assertEquals("abc", $split[1]);
37*504c13e8SAndreas Gohr        $regex = new ParallelRegex(true);
38*504c13e8SAndreas Gohr        $regex->addPattern("abc");
39*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("AAABCabcdef", $split));
40*504c13e8SAndreas Gohr        $this->assertEquals("abc", $split[1]);
41*504c13e8SAndreas Gohr    }
42*504c13e8SAndreas Gohr
43*504c13e8SAndreas Gohr    function testCaseInsensitive()
44*504c13e8SAndreas Gohr    {
45*504c13e8SAndreas Gohr        $regex = new ParallelRegex(false);
46*504c13e8SAndreas Gohr        $regex->addPattern("abc");
47*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("abcdef", $split));
48*504c13e8SAndreas Gohr        $this->assertEquals("abc", $split[1]);
49*504c13e8SAndreas Gohr        $regex = new ParallelRegex(false);
50*504c13e8SAndreas Gohr        $regex->addPattern("abc");
51*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("AAABCabcdef", $split));
52*504c13e8SAndreas Gohr        $this->assertEquals("ABC", $split[1]);
53*504c13e8SAndreas Gohr    }
54*504c13e8SAndreas Gohr
55*504c13e8SAndreas Gohr    function testMatchMultiple()
56*504c13e8SAndreas Gohr    {
57*504c13e8SAndreas Gohr        $regex = new ParallelRegex(true);
58*504c13e8SAndreas Gohr        $regex->addPattern("abc");
59*504c13e8SAndreas Gohr        $regex->addPattern("ABC");
60*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("abcdef", $split));
61*504c13e8SAndreas Gohr        $this->assertEquals("abc", $split[1]);
62*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("AAABCabcdef", $split));
63*504c13e8SAndreas Gohr        $this->assertEquals("ABC", $split[1]);
64*504c13e8SAndreas Gohr        $this->assertFalse($regex->split("Hello", $split));
65*504c13e8SAndreas Gohr    }
66*504c13e8SAndreas Gohr
67*504c13e8SAndreas Gohr    function testPatternLabels()
68*504c13e8SAndreas Gohr    {
69*504c13e8SAndreas Gohr        $regex = new ParallelRegex(false);
70*504c13e8SAndreas Gohr        $regex->addPattern("abc", "letter");
71*504c13e8SAndreas Gohr        $regex->addPattern("123", "number");
72*504c13e8SAndreas Gohr        $this->assertEquals("letter", $regex->split("abcdef", $split));
73*504c13e8SAndreas Gohr        $this->assertEquals("abc", $split[1]);
74*504c13e8SAndreas Gohr        $this->assertEquals("number", $regex->split("0123456789", $split));
75*504c13e8SAndreas Gohr        $this->assertEquals("123", $split[1]);
76*504c13e8SAndreas Gohr    }
77*504c13e8SAndreas Gohr
78*504c13e8SAndreas Gohr    function testMatchMultipleWithLookaheadNot()
79*504c13e8SAndreas Gohr    {
80*504c13e8SAndreas Gohr        $regex = new ParallelRegex(true);
81*504c13e8SAndreas Gohr        $regex->addPattern("abc");
82*504c13e8SAndreas Gohr        $regex->addPattern("ABC");
83*504c13e8SAndreas Gohr        $regex->addPattern("a(?!\n).{1}");
84*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("abcdef", $split));
85*504c13e8SAndreas Gohr        $this->assertEquals("abc", $split[1]);
86*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("AAABCabcdef", $split));
87*504c13e8SAndreas Gohr        $this->assertEquals("ABC", $split[1]);
88*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("a\nab", $split));
89*504c13e8SAndreas Gohr        $this->assertEquals("ab", $split[1]);
90*504c13e8SAndreas Gohr        $this->assertFalse($regex->split("Hello", $split));
91*504c13e8SAndreas Gohr    }
92*504c13e8SAndreas Gohr
93*504c13e8SAndreas Gohr    function testMatchSetOptionCaseless()
94*504c13e8SAndreas Gohr    {
95*504c13e8SAndreas Gohr        $regex = new ParallelRegex(true);
96*504c13e8SAndreas Gohr        $regex->addPattern("a(?i)b(?i)c");
97*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("aBc", $split));
98*504c13e8SAndreas Gohr        $this->assertEquals("aBc", $split[1]);
99*504c13e8SAndreas Gohr    }
100*504c13e8SAndreas Gohr
101*504c13e8SAndreas Gohr    function testMatchSetOptionUngreedy()
102*504c13e8SAndreas Gohr    {
103*504c13e8SAndreas Gohr        $regex = new ParallelRegex(true);
104*504c13e8SAndreas Gohr        $regex->addPattern("(?U)\w+");
105*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("aaaaaa", $split));
106*504c13e8SAndreas Gohr        $this->assertEquals("a", $split[1]);
107*504c13e8SAndreas Gohr    }
108*504c13e8SAndreas Gohr
109*504c13e8SAndreas Gohr    function testMatchLookaheadEqual()
110*504c13e8SAndreas Gohr    {
111*504c13e8SAndreas Gohr        $regex = new ParallelRegex(true);
112*504c13e8SAndreas Gohr        $regex->addPattern("\w(?=c)");
113*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("xbyczd", $split));
114*504c13e8SAndreas Gohr        $this->assertEquals("y", $split[1]);
115*504c13e8SAndreas Gohr    }
116*504c13e8SAndreas Gohr
117*504c13e8SAndreas Gohr    function testMatchLookaheadNot()
118*504c13e8SAndreas Gohr    {
119*504c13e8SAndreas Gohr        $regex = new ParallelRegex(true);
120*504c13e8SAndreas Gohr        $regex->addPattern("\w(?!b|c)");
121*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("xbyczd", $split));
122*504c13e8SAndreas Gohr        $this->assertEquals("b", $split[1]);
123*504c13e8SAndreas Gohr    }
124*504c13e8SAndreas Gohr
125*504c13e8SAndreas Gohr    function testMatchLookbehindEqual()
126*504c13e8SAndreas Gohr    {
127*504c13e8SAndreas Gohr        $regex = new ParallelRegex(true);
128*504c13e8SAndreas Gohr        $regex->addPattern("(?<=c)\w");
129*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("xbyczd", $split));
130*504c13e8SAndreas Gohr        $this->assertEquals("z", $split[1]);
131*504c13e8SAndreas Gohr    }
132*504c13e8SAndreas Gohr
133*504c13e8SAndreas Gohr    function testMatchLookbehindNot()
134*504c13e8SAndreas Gohr    {
135*504c13e8SAndreas Gohr        $regex = new ParallelRegex(true);
136*504c13e8SAndreas Gohr        $regex->addPattern("(?<!\A|x|b)\w");
137*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("xbyczd", $split));
138*504c13e8SAndreas Gohr        $this->assertEquals("c", $split[1]);
139*504c13e8SAndreas Gohr    }
140*504c13e8SAndreas Gohr
141*504c13e8SAndreas Gohr    function testSplitReturnsPreAndPostMatch()
142*504c13e8SAndreas Gohr    {
143*504c13e8SAndreas Gohr        $regex = new ParallelRegex(true);
144*504c13e8SAndreas Gohr        $regex->addPattern("abc");
145*504c13e8SAndreas Gohr        $this->assertTrue($regex->split("xxxabcyyy", $split));
146*504c13e8SAndreas Gohr        $this->assertEquals("xxx", $split[0]);
147*504c13e8SAndreas Gohr        $this->assertEquals("abc", $split[1]);
148*504c13e8SAndreas Gohr        $this->assertEquals("yyy", $split[2]);
149*504c13e8SAndreas Gohr    }
150*504c13e8SAndreas Gohr}
151