Passed
Pull Request — master (#8)
by
unknown
06:36
created

MyersDiffTest::testIdenticalThree()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
1
<?php
2
3
namespace Fisharebest\Algorithm;
4
5
/**
6
 * @author    Greg Roach <[email protected]>
7
 * @copyright (c) 2015 Greg Roach <[email protected]>
8
 * @license   GPL-3.0+
9
 *s
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU General Public License for more details.
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
class MyersDiffTest extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * Test empty sequences.
25
     *
26
     * @return void
27
     */
28
    public function testBothEmpty()
29
    {
30
        $algorithm = new MyersDiff();
31
        $x = array();
32
        $y = array();
33
        $diff = array();
34
35
        $this->assertSame($diff, $algorithm->calculate($x, $y));
36
    }
37
38
    /**
39
     * Test one empty sequence.
40
     *
41
     * @return void
42
     */
43
    public function testFirstEmpty()
44
    {
45
        $algorithm = new MyersDiff();
46
        $x = array();
47
        $y = array('a', 'b', 'c');
48
        $diff = array(
49
            array('a', MyersDiff::INSERT),
50
            array('b', MyersDiff::INSERT),
51
            array('c', MyersDiff::INSERT),
52
        );
53
54
        $this->assertSame($diff, $algorithm->calculate($x, $y));
55
    }
56
57
    /**
58
     * Test one empty sequence.
59
     *
60
     * @return void
61
     */
62
    public function testSecondEmpty()
63
    {
64
        $algorithm = new MyersDiff();
65
        $x = array('a', 'b', 'c');
66
        $y = array();
67
        $diff = array(
68
            array('a', MyersDiff::DELETE),
69
            array('b', MyersDiff::DELETE),
70
            array('c', MyersDiff::DELETE),
71
        );
72
73
        $this->assertSame($diff, $algorithm->calculate($x, $y));
74
    }
75
76
    /**
77
     * Test identical sequences containing one token.
78
     *
79
     * @return void
80
     */
81
    public function testIdenticalOne()
82
    {
83
        $algorithm = new MyersDiff();
84
        $x = array('a');
85
        $y = array('a');
86
        $diff = array(
87
            array('a', MyersDiff::KEEP),
88
        );
89
90
        $this->assertSame($diff, $algorithm->calculate($x, $y));
91
    }
92
93
    /**
94
     * Test identical sequences containing two tokens.
95
     *
96
     * @return void
97
     */
98
    public function testIdenticalTwo()
99
    {
100
        $algorithm = new MyersDiff();
101
        $x = array('a', 'b');
102
        $y = array('a', 'b');
103
        $diff = array(
104
            array('a', MyersDiff::KEEP),
105
            array('b', MyersDiff::KEEP),
106
        );
107
108
        $this->assertSame($diff, $algorithm->calculate($x, $y));
109
    }
110
111
    /**
112
     * Test identical sequences containing three tokens.
113
     *
114
     * @return void
115
     */
116
    public function testIdenticalThree()
117
    {
118
        $algorithm = new MyersDiff();
119
        $x = array('a', 'b', 'c');
120
        $y = array('a', 'b', 'c');
121
        $diff = array(
122
            array('a', MyersDiff::KEEP),
123
            array('b', MyersDiff::KEEP),
124
            array('c', MyersDiff::KEEP),
125
        );
126
127
        $this->assertSame($diff, $algorithm->calculate($x, $y));
128
    }
129
130
    /**
131
     * Test different strings containing one token.
132
     *
133
     * @return void
134
     */
135
    public function testSingleDifferentONe()
136
    {
137
        $algorithm = new MyersDiff();
138
        $x = array('a');
139
        $y = array('x');
140
        $diff = array(
141
            array('a', MyersDiff::DELETE),
142
            array('x', MyersDiff::INSERT),
143
        );
144
145
        $this->assertSame($diff, $algorithm->calculate($x, $y));
146
    }
147
148
    /**
149
     * Test different strings containing two token.
150
     *
151
     * @return void
152
     */
153
    public function testSingleDifferentTwo()
154
    {
155
        $algorithm = new MyersDiff();
156
        $x = array('a', 'b');
157
        $y = array('x', 'y');
158
        $diff = array(
159
            array('a', MyersDiff::DELETE),
160
            array('b', MyersDiff::DELETE),
161
            array('x', MyersDiff::INSERT),
162
            array('y', MyersDiff::INSERT),
163
        );
164
165
        $this->assertSame($diff, $algorithm->calculate($x, $y));
166
    }
167
168
    /**
169
     * Test different strings containing three token.
170
     *
171
     * @return void
172
     */
173
    public function testSingleDifferentThree()
174
    {
175
        $algorithm = new MyersDiff();
176
        $x = array('a', 'b', 'c');
177
        $y = array('x', 'y', 'z');
178
        $diff = array(
179
            array('a', MyersDiff::DELETE),
180
            array('b', MyersDiff::DELETE),
181
            array('c', MyersDiff::DELETE),
182
            array('x', MyersDiff::INSERT),
183
            array('y', MyersDiff::INSERT),
184
            array('z', MyersDiff::INSERT),
185
        );
186
187
        $this->assertSame($diff, $algorithm->calculate($x, $y));
188
    }
189
190
    /**
191
     * Test two non-empty sequences.
192
     *
193
     * @return void
194
     */
195
    public function testBothNonEmpty()
196
    {
197
        $algorithm = new MyersDiff();
198
        $x = array('a', 'b', 'c', 'a', 'b', 'b', 'a');
199
        $y = array('c', 'b', 'a', 'b', 'a', 'c');
200
        $diff = array(
201
            array('a', MyersDiff::DELETE),
202
            array('b', MyersDiff::DELETE),
203
            array('c', MyersDiff::KEEP),
204
            array('b', MyersDiff::INSERT),
205
            array('a', MyersDiff::KEEP),
206
            array('b', MyersDiff::KEEP),
207
            array('b', MyersDiff::DELETE),
208
            array('a', MyersDiff::KEEP),
209
            array('c', MyersDiff::INSERT),
210
        );
211
212
        $this->assertSame($diff, $algorithm->calculate($x, $y));
213
    }
214
215
    /**
216
     * Test delete-before-insert.  Delete/insert gives the same
217
     * result as insert/delete.  Our algorithm consistently
218
     * deletes first.
219
     *
220
     * void
221
     */
222
    public function testDeleteBeforeInsert()
223
    {
224
        $algorithm = new MyersDiff();
225
        $x = array('a', 'b', 'c');
226
        $y = array('a', 'd', 'c');
227
        $diff = array(
228
            array('a', MyersDiff::KEEP),
229
            array('b', MyersDiff::DELETE),
230
            array('d', MyersDiff::INSERT),
231
            array('c', MyersDiff::KEEP),
232
        );
233
234
        $this->assertSame($diff, $algorithm->calculate($x, $y));
235
    }
236
237
    /**
238
     * Test custom token comparison.
239
     *
240
     * @return void
241
     */
242
    public function testCustomCompare()
243
    {
244
        $algorithm = new MyersDiff();
245
        $ignorecase = static function ($x, $y) { return strtolower($x) === strtolower($y); };
246
        $x = array('a', 'b', 'c');
247
        $y = array('A', 'B', 'C');
248
        $diff = array(
249
            array('a', MyersDiff::KEEP),
250
            array('b', MyersDiff::KEEP),
251
            array('c', MyersDiff::KEEP),
252
        );
253
254
        $this->assertSame($diff, $algorithm->calculate($x, $y, $ignorecase));
0 ignored issues
show
Bug introduced by
$ignorecase of type callable is incompatible with the type Fisharebest\Algorithm\function expected by parameter $compare of Fisharebest\Algorithm\MyersDiff::calculate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

254
        $this->assertSame($diff, $algorithm->calculate($x, $y, /** @scrutinizer ignore-type */ $ignorecase));
Loading history...
255
    }
256
}
257