1 | <?php declare(strict_types=1); |
||
2 | |||
3 | namespace s9e\RegexpBuilder\Tests; |
||
4 | |||
5 | use PHPUnit\Framework\TestCase; |
||
6 | use s9e\RegexpBuilder\Input\Utf8; |
||
7 | use s9e\RegexpBuilder\MetaCharacters; |
||
8 | |||
9 | /** |
||
10 | * @covers s9e\RegexpBuilder\MetaCharacters |
||
11 | */ |
||
12 | class MetaCharactersTest extends TestCase |
||
13 | { |
||
14 | protected function getMeta(array $map = []) |
||
15 | { |
||
16 | $meta = new MetaCharacters(new Utf8, $map); |
||
17 | foreach ($map as $char => $expr) |
||
18 | { |
||
19 | $meta->add($char, $expr); |
||
20 | } |
||
21 | |||
22 | return $meta; |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * @testdox Using multiple chars as meta-character throws an exception |
||
27 | */ |
||
28 | public function testMultipleCharsException() |
||
29 | { |
||
30 | $this->expectException('InvalidArgumentException', 'Meta-characters must be represented by exactly one character'); |
||
31 | $this->getMeta(['xx' => 'x']); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @testdox Invalid expressions throw an exception |
||
36 | */ |
||
37 | public function testInvalidExceptionException() |
||
38 | { |
||
39 | $this->expectException('InvalidArgumentException', "Invalid expression '+++'"); |
||
40 | $this->getMeta(['x' => '+++']); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @testdox getExpression() returns the original expression that matches the given meta value |
||
45 | */ |
||
46 | public function testGetExpression() |
||
47 | { |
||
48 | $meta = $this->getMeta(["\0" => 'foo', "\1" => 'bar']); |
||
49 | $strings = $meta->replaceMeta([[0, 1]]); |
||
50 | |||
51 | $this->assertEquals('foo', $meta->getExpression($strings[0][0])); |
||
52 | $this->assertEquals('bar', $meta->getExpression($strings[0][1])); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @testdox getExpression() throws an exception on unknown meta values |
||
57 | */ |
||
58 | public function testGetExpressionException() |
||
59 | { |
||
60 | $this->expectException('InvalidArgumentException', "Invalid meta value -1"); |
||
0 ignored issues
–
show
|
|||
61 | $this->getMeta([])->getExpression(-1); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @testdox Meta-characters properties |
||
66 | * @dataProvider getPropertiesTests |
||
67 | */ |
||
68 | public function testProperties($properties, $expr) |
||
69 | { |
||
70 | $meta = $this->getMeta(["\0" => $expr]); |
||
71 | $strings = $meta->replaceMeta([[0]]); |
||
72 | |||
73 | $map = [ |
||
74 | 'c' => 'isChar', |
||
75 | 'q' => 'isQuantifiable' |
||
76 | ]; |
||
77 | foreach ($map as $c => $methodName) |
||
78 | { |
||
79 | $assertMethod = (strpos($properties, $c) === false) ? 'assertFalse' : 'assertTrue'; |
||
80 | $msg = $methodName . '(' . var_export($expr, true) . ')'; |
||
81 | |||
82 | $this->$assertMethod(MetaCharacters::$methodName($strings[0][0]), $msg); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | public function getPropertiesTests() |
||
87 | { |
||
88 | return [ |
||
89 | ['cq', '\\w' ], |
||
90 | ['cq', '\\d' ], |
||
91 | ['cq', '\\x{2600}'], |
||
92 | ['cq', '\\pL' ], |
||
93 | ['cq', '\\p{^L}' ], |
||
94 | ['q', '.' ], |
||
95 | ['q', '\\R' ], |
||
96 | ['q', '[0-9]' ], |
||
97 | ['', '[0-9]+' ], |
||
98 | ['', '.*' ], |
||
99 | ['', 'xx' ], |
||
100 | ['', '^' ], |
||
101 | ['', '$' ], |
||
102 | ]; |
||
103 | } |
||
104 | } |
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.