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
|
|
|
* DifferencesIterator Tests |
18
|
|
|
*/ |
19
|
|
|
class DifferencesIteratorTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @return RangeDifference[] |
23
|
|
|
*/ |
24
|
|
|
private function getDifferences(): array |
25
|
|
|
{ |
26
|
|
|
$oldText = '<p> This is a blue book</p>'; |
27
|
|
|
$newText = '<p> This is a <b>big</b> blue book</p>'; |
28
|
|
|
$oldComp = new TagComparator($oldText); |
29
|
|
|
$newComp = new TagComparator($newText); |
30
|
|
|
|
31
|
|
|
return RangeDifferencer::findDifferences($oldComp, $newComp); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testGetIndex(): void |
35
|
|
|
{ |
36
|
|
|
$diff = $this->getDifferences(); |
37
|
|
|
$iter = new DifferencesIterator($diff); |
38
|
|
|
|
39
|
|
|
$this->assertSame('RangeDifference {CHANGE/RIGHT, Left: (8, 0) Right: (8, 4)}', $diff[0]->__toString()); |
40
|
|
|
$this->assertSame(1, $iter->getIndex()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testEmpty(): void |
44
|
|
|
{ |
45
|
|
|
$iter = new DifferencesIterator([]); |
46
|
|
|
|
47
|
|
|
$this->assertSame(0, $iter->getIndex()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testGetCount(): void |
51
|
|
|
{ |
52
|
|
|
$iter = new DifferencesIterator($this->getDifferences()); |
53
|
|
|
|
54
|
|
|
$this->assertSame(0, $iter->getCount()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testGetCountEmpty(): void |
58
|
|
|
{ |
59
|
|
|
$iter = new DifferencesIterator([]); |
60
|
|
|
|
61
|
|
|
$this->assertSame(0, $iter->getCount()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testNext(): void |
65
|
|
|
{ |
66
|
|
|
$diff = $this->getDifferences(); |
67
|
|
|
$iter = new DifferencesIterator($diff); |
68
|
|
|
|
69
|
|
|
$this->assertSame('RangeDifference {CHANGE/RIGHT, Left: (8, 0) Right: (8, 4)}', $diff[0]->__toString()); |
70
|
|
|
|
71
|
|
|
$oldText = '<p> This is a green book about food</p>'; |
72
|
|
|
$newText = '<p> This is a <b>big</b> blue book</p>'; |
73
|
|
|
$oldComp = new TagComparator($oldText); |
74
|
|
|
$newComp = new TagComparator($newText); |
75
|
|
|
|
76
|
|
|
$iter->next(); |
77
|
|
|
$diff = RangeDifferencer::findDifferences($oldComp, $newComp); |
78
|
|
|
$iter = new DifferencesIterator($diff); |
79
|
|
|
$iter->next(); |
80
|
|
|
|
81
|
|
|
$this->assertSame('RangeDifference {CHANGE/RIGHT, Left: (8, 1) Right: (8, 5)}', $diff[0]->__toString()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testNextNull(): void |
85
|
|
|
{ |
86
|
|
|
$diff = $this->getDifferences(); |
87
|
|
|
$iter = new DifferencesIterator($diff); |
88
|
|
|
|
89
|
|
|
$this->assertSame('RangeDifference {CHANGE/RIGHT, Left: (8, 0) Right: (8, 4)}', $diff[0]->__toString()); |
90
|
|
|
|
91
|
|
|
$iter->next(); |
92
|
|
|
$iter = new DifferencesIterator([]); |
93
|
|
|
$iter->next(); |
94
|
|
|
|
95
|
|
|
$this->assertSame(1, $iter->getCount()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testOther(): void |
99
|
|
|
{ |
100
|
|
|
$diff = $this->getDifferences(); |
101
|
|
|
$left = new DifferencesIterator($diff); |
102
|
|
|
$right = new DifferencesIterator($diff); |
103
|
|
|
|
104
|
|
|
$this->assertSame($right, $left->other($right, $left)); |
105
|
|
|
$this->assertSame($left, $right->other($right, $left)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testRemoveAll(): void |
109
|
|
|
{ |
110
|
|
|
$iter = new DifferencesIterator($this->getDifferences()); |
111
|
|
|
$iter->removeAll(); |
112
|
|
|
|
113
|
|
|
$this->assertSame(0, $iter->getCount()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testRemoveAllEmpty(): void |
117
|
|
|
{ |
118
|
|
|
$iter = new DifferencesIterator([]); |
119
|
|
|
$iter->removeAll(); |
120
|
|
|
|
121
|
|
|
$this->assertSame(0, $iter->getCount()); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|