|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* (c) Steve Nebes <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace SN\RangeDifferencer; |
|
12
|
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
use SN\RangeDifferencer\Tag\TagComparator; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* RangeComparatorLCS Tests |
|
18
|
|
|
*/ |
|
19
|
|
|
class RangeComparatorLCSTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
public function testDifferencesIterator(): void |
|
22
|
|
|
{ |
|
23
|
|
|
$oldText = '<p> This is a blue book</p>'; |
|
24
|
|
|
$newText = '<p> This is a <b>big</b> blue book</p>'; |
|
25
|
|
|
|
|
26
|
|
|
$left = new TagComparator($oldText); |
|
27
|
|
|
$right = new TagComparator($newText); |
|
28
|
|
|
$rangeDifferences = RangeComparatorLCS::findDifferences($left, $right); |
|
29
|
|
|
|
|
30
|
|
|
$this->assertSame('RangeDifference {CHANGE/RIGHT, Left: (8, 0) Right: (8, 4)}', $rangeDifferences[0]->__toString()); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testLength(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$oldText = '<p> This is a blue book</p>'; |
|
36
|
|
|
$newText = '<p> This is a <b>big</b> blue book</p>'; |
|
37
|
|
|
$left = new TagComparator($oldText); |
|
38
|
|
|
$right = new TagComparator($newText); |
|
39
|
|
|
|
|
40
|
|
|
$comp = new RangeComparatorLCS($left, $right); |
|
41
|
|
|
$this->assertSame(0, $comp->getLength()); |
|
42
|
|
|
|
|
43
|
|
|
$refMethod = new \ReflectionMethod($comp, 'getLength1'); |
|
44
|
|
|
$refMethod->setAccessible(true); |
|
45
|
|
|
$this->assertSame(12, $refMethod->invoke($comp)); |
|
46
|
|
|
|
|
47
|
|
|
$refMethod = new \ReflectionMethod($comp, 'getLength2'); |
|
48
|
|
|
$refMethod->setAccessible(true); |
|
49
|
|
|
$this->assertSame(16, $refMethod->invoke($comp)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testLengthEmpty1(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$oldText = ''; |
|
55
|
|
|
$newText = '<p> This is a <b>big</b> blue book</p>'; |
|
56
|
|
|
$left = new TagComparator($oldText); |
|
57
|
|
|
$right = new TagComparator($newText); |
|
58
|
|
|
|
|
59
|
|
|
$comp = new RangeComparatorLCS($left, $right); |
|
60
|
|
|
$this->assertSame(0, $comp->getLength()); |
|
61
|
|
|
|
|
62
|
|
|
$refMethod = new \ReflectionMethod($comp, 'getLength1'); |
|
63
|
|
|
$refMethod->setAccessible(true); |
|
64
|
|
|
$this->assertSame(0, $refMethod->invoke($comp)); |
|
65
|
|
|
|
|
66
|
|
|
$refMethod = new \ReflectionMethod($comp, 'getLength2'); |
|
67
|
|
|
$refMethod->setAccessible(true); |
|
68
|
|
|
$this->assertSame(16, $refMethod->invoke($comp)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testLengthEmpty2(): void |
|
72
|
|
|
{ |
|
73
|
|
|
$oldText = '<p> This is a blue book</p>'; |
|
74
|
|
|
$newText = ''; |
|
75
|
|
|
$left = new TagComparator($oldText); |
|
76
|
|
|
$right = new TagComparator($newText); |
|
77
|
|
|
|
|
78
|
|
|
$comp = new RangeComparatorLCS($left, $right); |
|
79
|
|
|
$this->assertSame(0, $comp->getLength()); |
|
80
|
|
|
|
|
81
|
|
|
$refMethod = new \ReflectionMethod($comp, 'getLength1'); |
|
82
|
|
|
$refMethod->setAccessible(true); |
|
83
|
|
|
$this->assertSame(12, $refMethod->invoke($comp)); |
|
84
|
|
|
|
|
85
|
|
|
$refMethod = new \ReflectionMethod($comp, 'getLength2'); |
|
86
|
|
|
$refMethod->setAccessible(true); |
|
87
|
|
|
$this->assertSame(0, $refMethod->invoke($comp)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testLengthEmpty3(): void |
|
91
|
|
|
{ |
|
92
|
|
|
$oldText = ''; |
|
93
|
|
|
$newText = ''; |
|
94
|
|
|
$left = new TagComparator($oldText); |
|
95
|
|
|
$right = new TagComparator($newText); |
|
96
|
|
|
|
|
97
|
|
|
$comp = new RangeComparatorLCS($left, $right); |
|
98
|
|
|
$this->assertSame(0, $comp->getLength()); |
|
99
|
|
|
|
|
100
|
|
|
$refMethod = new \ReflectionMethod($comp, 'getLength1'); |
|
101
|
|
|
$refMethod->setAccessible(true); |
|
102
|
|
|
$this->assertSame(0, $refMethod->invoke($comp)); |
|
103
|
|
|
|
|
104
|
|
|
$refMethod = new \ReflectionMethod($comp, 'getLength2'); |
|
105
|
|
|
$refMethod->setAccessible(true); |
|
106
|
|
|
$this->assertSame(0, $refMethod->invoke($comp)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function testInitializeLCS(): void |
|
110
|
|
|
{ |
|
111
|
|
|
$oldText = '<p> This is a blue book</p>'; |
|
112
|
|
|
$newText = '<p> This is a <b>big</b> blue book</p>'; |
|
113
|
|
|
$left = new TagComparator($oldText); |
|
114
|
|
|
$right = new TagComparator($newText); |
|
115
|
|
|
$comp = new RangeComparatorLCS($left, $right); |
|
116
|
|
|
|
|
117
|
|
|
$refMethod = new \ReflectionMethod($comp, 'initializeLcs'); |
|
118
|
|
|
$refMethod->setAccessible(true); |
|
119
|
|
|
$refMethod->invoke($comp, 20); |
|
120
|
|
|
|
|
121
|
|
|
$refProp = new \ReflectionProperty($comp, 'lcs'); |
|
122
|
|
|
$refProp->setAccessible(true); |
|
123
|
|
|
$lcs = $refProp->getValue($comp); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertSame(2, \count($lcs)); |
|
126
|
|
|
$this->assertSame(20, \count($lcs[0])); |
|
127
|
|
|
$this->assertSame(20, \count($lcs[1])); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function testInitializeLCSZero(): void |
|
131
|
|
|
{ |
|
132
|
|
|
$oldText = '<p> This is a blue book</p>'; |
|
133
|
|
|
$newText = '<p> This is a <b>big</b> blue book</p>'; |
|
134
|
|
|
$left = new TagComparator($oldText); |
|
135
|
|
|
$right = new TagComparator($newText); |
|
136
|
|
|
$comp = new RangeComparatorLCS($left, $right); |
|
137
|
|
|
|
|
138
|
|
|
$refMethod = new \ReflectionMethod($comp, 'initializeLcs'); |
|
139
|
|
|
$refMethod->setAccessible(true); |
|
140
|
|
|
$refMethod->invoke($comp, 0); |
|
141
|
|
|
|
|
142
|
|
|
$refProp = new \ReflectionProperty($comp, 'lcs'); |
|
143
|
|
|
$refProp->setAccessible(true); |
|
144
|
|
|
$lcs = $refProp->getValue($comp); |
|
145
|
|
|
|
|
146
|
|
|
$this->assertSame(2, \count($lcs)); |
|
147
|
|
|
$this->assertSame(0, \count($lcs[0])); |
|
148
|
|
|
$this->assertSame(0, \count($lcs[1])); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function testIsRangeEqual(): void |
|
152
|
|
|
{ |
|
153
|
|
|
$oldText = '<p> This is a blue book</p>'; |
|
154
|
|
|
$newText = '<p> This is a <b>big</b> blue book</p>'; |
|
155
|
|
|
$left = new TagComparator($oldText); |
|
156
|
|
|
$right = new TagComparator($newText); |
|
157
|
|
|
$comp = new RangeComparatorLCS($left, $right); |
|
158
|
|
|
|
|
159
|
|
|
$refMethod = new \ReflectionMethod($comp, 'isRangeEqual'); |
|
160
|
|
|
$refMethod->setAccessible(true); |
|
161
|
|
|
|
|
162
|
|
|
$this->assertTrue($refMethod->invoke($comp, 0, 0)); |
|
163
|
|
|
$this->assertFalse($refMethod->invoke($comp, 0, 3)); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function testGetDifferences1(): void |
|
167
|
|
|
{ |
|
168
|
|
|
$oldText = "<p> This is a blue book</p> \n <div style=\"example\">This book is about food</div>"; |
|
169
|
|
|
$newText = '<p> This is a <b>big</b> blue book</p>'; |
|
170
|
|
|
$left = new TagComparator($oldText); |
|
171
|
|
|
$right = new TagComparator($newText); |
|
172
|
|
|
$comp = new RangeComparatorLCS($left, $right); |
|
173
|
|
|
|
|
174
|
|
|
$diffs = $comp->getDifferences(); |
|
175
|
|
|
$this->assertSame(1, \count($diffs)); |
|
176
|
|
|
$this->assertSame('RangeDifference {CHANGE/RIGHT, Left: (0, 26) Right: (0, 16)}', $diffs[0]->__toString()); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function testGetDifferences2(): void |
|
180
|
|
|
{ |
|
181
|
|
|
$oldText = "<div style=\"example\">This book is about food</div>"; |
|
182
|
|
|
$newText = '<p> This is a <b>big</b> blue book</p>'; |
|
183
|
|
|
$left = new TagComparator($oldText); |
|
184
|
|
|
$right = new TagComparator($newText); |
|
185
|
|
|
$comp = new RangeComparatorLCS($left, $right); |
|
186
|
|
|
|
|
187
|
|
|
$diffs = $comp->getDifferences(); |
|
188
|
|
|
$this->assertSame(1, \count($diffs)); |
|
189
|
|
|
$this->assertSame('RangeDifference {CHANGE/RIGHT, Left: (0, 11) Right: (0, 16)}', $diffs[0]->__toString()); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function testGetDifferences3(): void |
|
193
|
|
|
{ |
|
194
|
|
|
$newText = "<p> This is a <b>big</b> blue book</p>"; |
|
195
|
|
|
$right = new TagComparator($newText); |
|
196
|
|
|
$comp = new RangeComparatorLCS($right, $right); |
|
197
|
|
|
|
|
198
|
|
|
$diffs = $comp->getDifferences(); |
|
199
|
|
|
$this->assertSame(1, \count($diffs)); |
|
200
|
|
|
$this->assertSame('RangeDifference {CHANGE/RIGHT, Left: (0, 16) Right: (0, 16)}', $diffs[0]->__toString()); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function testGetDifferences4(): void |
|
204
|
|
|
{ |
|
205
|
|
|
$newText = ''; |
|
206
|
|
|
$right = new TagComparator($newText); |
|
207
|
|
|
$comp = new RangeComparatorLCS($right, $right); |
|
208
|
|
|
|
|
209
|
|
|
$diffs = $comp->getDifferences(); |
|
210
|
|
|
$this->assertSame(1, \count($diffs)); |
|
211
|
|
|
$this->assertSame('RangeDifference {CHANGE/RIGHT, Left: (0, 0) Right: (0, 0)}', $diffs[0]->__toString()); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function testGetDifferences5(): void |
|
215
|
|
|
{ |
|
216
|
|
|
$oldText = "<div style=\"example\">This book is about food</div>"; |
|
217
|
|
|
$newText = ''; |
|
218
|
|
|
$left = new TagComparator($oldText); |
|
219
|
|
|
$right = new TagComparator($newText); |
|
220
|
|
|
$comp = new RangeComparatorLCS($left, $right); |
|
221
|
|
|
|
|
222
|
|
|
$diffs = $comp->getDifferences(); |
|
223
|
|
|
$this->assertSame(1, \count($diffs)); |
|
224
|
|
|
$this->assertSame('RangeDifference {CHANGE/RIGHT, Left: (0, 11) Right: (0, 0)}', $diffs[0]->__toString()); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
public function testGetDifferences6(): void |
|
228
|
|
|
{ |
|
229
|
|
|
$oldText = "<div style=\"example\">This book is about food</div>"; |
|
230
|
|
|
$newText = ''; |
|
231
|
|
|
$left = new TagComparator($oldText); |
|
232
|
|
|
$right = new TagComparator($newText); |
|
233
|
|
|
$comp = new RangeComparatorLCS($right, $left); |
|
234
|
|
|
|
|
235
|
|
|
$diffs = $comp->getDifferences(); |
|
236
|
|
|
$this->assertSame(1, \count($diffs)); |
|
237
|
|
|
$this->assertSame('RangeDifference {CHANGE/RIGHT, Left: (0, 0) Right: (0, 11)}', $diffs[0]->__toString()); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
public function testFindMostProgress(): void |
|
241
|
|
|
{ |
|
242
|
|
|
$lcs = $this->createMock(AbstractLCS::class); |
|
243
|
|
|
|
|
244
|
|
|
$M = 7; |
|
245
|
|
|
$N = 2; |
|
246
|
|
|
$limit = 3; |
|
247
|
|
|
$V = [ |
|
248
|
|
|
[0,0,1,1,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
|
249
|
|
|
[0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
|
250
|
|
|
]; |
|
251
|
|
|
|
|
252
|
|
|
$refMethod = new \ReflectionMethod($lcs, 'findMostProgress'); |
|
253
|
|
|
$refMethod->setAccessible(true); |
|
254
|
|
|
|
|
255
|
|
|
$actual = $refMethod->invokeArgs($lcs, [$M, $N, $limit, $V]); |
|
256
|
|
|
$this->assertSame([0, 4, 5], $actual); |
|
257
|
|
|
|
|
258
|
|
|
$actual = $refMethod->invokeArgs($lcs, [$M, $N, 4, $V]); |
|
259
|
|
|
$this->assertSame([0, 1, 8], $actual); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|