/
MatchPatternTest.php
205 lines (173 loc) · 5.53 KB
/
MatchPatternTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace Test\Unit\TRegx\CleanRegex\Match\MatchPattern\forFirst;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use TRegx\CleanRegex\Exception\CleanRegex\Messages\Subject\FirstMatchMessage;
use TRegx\CleanRegex\Exception\CleanRegex\SubjectNotMatchedException;
use TRegx\CleanRegex\Internal\InternalPattern as Pattern;
use TRegx\CleanRegex\Match\Details\Match;
use TRegx\CleanRegex\Match\Details\NotMatched;
use TRegx\CleanRegex\Match\MatchPattern;
class MatchPatternTest extends TestCase
{
/**
* @test
*/
public function shouldGetMatch_withDetails()
{
// given
$pattern = $this->getMatchPattern("Nice matching pattern");
// when
$pattern
->forFirst(function (Match $match) {
// then
$this->assertEquals(0, $match->index());
$this->assertEquals("Nice matching pattern", $match->subject());
$this->assertEquals(['Nice', 'matching', 'pattern'], $match->all());
$this->assertEquals(['N'], $match->groups()->texts());
})
->orThrow();
}
/**
* @test
*/
public function shouldGetMatch_withoutCollapsingOrMethod()
{
// given
$pattern = $this->getMatchPattern("Nice matching pattern");
// when
$pattern
->forFirst(function (Match $match) {
// then
$this->assertEquals("Nice matching pattern", $match->subject());
});
// ->orThrow();
}
/**
* @test
*/
public function shouldGetFirst()
{
// given
$pattern = $this->getMatchPattern("Nice matching pattern");
// when
$first1 = $pattern->forFirst('strtoupper')->orReturn(null);
$first2 = $pattern->forFirst('strtoupper')->orThrow();
$first3 = $pattern->forFirst('strtoupper')->orElse(function () {
});
// then
$this->assertEquals('NICE', $first1);
$this->assertEquals('NICE', $first2);
$this->assertEquals('NICE', $first3);
}
/**
* @test
*/
public function shouldNotInvokeFirst_onNotMatchingSubject()
{
// given
$pattern = $this->getMatchPattern('NOT MATCHING');
// when
$pattern->forFirst($this->assertFalseCallback())->orReturn(null);
$pattern->forFirst($this->assertFalseCallback())->orElse(function () {
});
try {
@$pattern->forFirst($this->assertFalseCallback())->orThrow();
} catch (SubjectNotMatchedException $ignored) {
}
// then
$this->assertTrue(true);
}
/**
* @test
*/
public function should_onNotMatchingSubject_throw()
{
// given
$pattern = $this->getMatchPattern('NOT MATCHING');
// then
$this->expectException(SubjectNotMatchedException::class);
// when
$pattern->forFirst('strrev')->orThrow();
}
/**
* @test
*/
public function should_onNotMatchingSubject_throw_userGivenException()
{
// given
$pattern = $this->getMatchPattern('NOT MATCHING');
// then
$this->expectException(InvalidArgumentException::class);
// when
$pattern->forFirst('strrev')->orThrow(InvalidArgumentException::class);
}
/**
* @test
*/
public function should_onNotMatchingSubject_throw_withMessage()
{
// given
$pattern = $this->getMatchPattern('NOT MATCHING');
// then
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Expected to get first match, but subject was not matched');
// when
$pattern->forFirst('strrev')->orThrow(InvalidArgumentException::class);
}
/**
* @test
*/
public function should_onNotMatchingSubject_getDefault()
{
// given
$pattern = $this->getMatchPattern('NOT MATCHING');
// when
$value = $pattern->forFirst('strrev')->orReturn('def');
// then
$this->assertEquals('def', $value);
}
/**
* @test
*/
public function should_onNotMatchingSubject_call()
{
// given
$pattern = $this->getMatchPattern('NOT MATCHING');
// when
$value = $pattern->forFirst('strrev')->orElse(function () {
return 'new value';
});
// then
$this->assertEquals('new value', $value);
}
/**
* @test
*/
public function should_onNotMatchingSubject_call_withDetails()
{
// given
$pattern = new MatchPattern(new Pattern("(?:[A-Z])?[a-z']+ (?<group>.)"), 'NOT MATCHING');
// when
$pattern->forFirst('strrev')->orElse(function (NotMatched $details) {
// then
$this->assertEquals('NOT MATCHING', $details->subject());
$this->assertEquals(['group'], $details->groupNames());
$this->assertTrue($details->hasGroup('group'));
$this->assertTrue($details->hasGroup(0));
$this->assertTrue($details->hasGroup(1));
$this->assertFalse($details->hasGroup('other'));
$this->assertFalse($details->hasGroup(2));
});
}
private function getMatchPattern($subject): MatchPattern
{
return new MatchPattern(new Pattern("([A-Z])?[a-z']+"), $subject);
}
private function assertFalseCallback(): callable
{
return function () {
$this->assertTrue(false, "Failed asserting that forFirst() is not invoked for not matching subject");
};
}
}