Completed
Push — master ( 96cbf3...9bcdfc )
by Steve
03:03 queued 01:23
created

DiffTest::testLineAppendStart()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 0
dl 0
loc 24
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * (c) Steve Nebes <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace SN\RangeDifferencer\Core;
13
14
use PHPUnit\Framework\TestCase;
15
16
class DiffTest extends TestCase
17
{
18
    public function testLineAdditions(): void
19
    {
20
        $s1 = 'abc' . PHP_EOL . 'def' . PHP_EOL . 'xyz';
21
        $s2 = 'abc' . PHP_EOL . 'def' . PHP_EOL . '123' . PHP_EOL . 'xyz';
22
23
        $l1 = TextLineLCS::getTextLines($s1);
24
        $l2 = TextLineLCS::getTextLines($s2);
25
        $lcs = new TextLineLCS($l1, $l2);
26
        $lcs->longestCommonSubsequence();
27
28
        /** @var TextLine[][] $result */
29
        $result = $lcs->getResult();
30
31
        $this->assertSame(0, $result[0][0]->getLineNumber());
32
        $this->assertSame(0, $result[1][0]->getLineNumber());
33
        $this->assertSame(1, $result[0][1]->getLineNumber());
34
        $this->assertSame(1, $result[1][1]->getLineNumber());
35
        $this->assertSame(2, $result[0][2]->getLineNumber());
36
        $this->assertSame(3, $result[1][2]->getLineNumber());
37
    }
38
39
    public function testLineDeletion(): void
40
    {
41
        $s1 = 'abc' . PHP_EOL . 'def' . PHP_EOL . '123' . PHP_EOL . 'xyz';
42
        $s2 = 'abc' . PHP_EOL . 'def' . PHP_EOL . 'xyz';
43
44
        $l1 = TextLineLCS::getTextLines($s1);
45
        $l2 = TextLineLCS::getTextLines($s2);
46
        $lcs = new TextLineLCS($l1, $l2);
47
        $lcs->longestCommonSubsequence();
48
49
        /** @var TextLine[][] $result */
50
        $result = $lcs->getResult();
51
52
        $this->assertTrue(\count($result[0]) === \count($result[1]));
53
54
        for ($i = 0; $i < \count($result[0]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
55
            $this->assertTrue($result[0][$i]->isSameText($result[1][$i]));
56
        }
57
58
        $this->assertSame(0, $result[0][0]->getLineNumber());
59
        $this->assertSame(1, $result[0][1]->getLineNumber());
60
        $this->assertSame(3, $result[0][2]->getLineNumber());
61
        $this->assertSame(0, $result[1][0]->getLineNumber());
62
        $this->assertSame(1, $result[1][1]->getLineNumber());
63
        $this->assertSame(2, $result[1][2]->getLineNumber());
64
    }
65
66
    public function testLineAppendEnd(): void
67
    {
68
        $s1 = 'abc' . PHP_EOL . 'def';
69
        $s2 = 'abc' . PHP_EOL . 'def' . PHP_EOL . '123';
70
71
        $l1 = TextLineLCS::getTextLines($s1);
72
        $l2 = TextLineLCS::getTextLines($s2);
73
        $lcs = new TextLineLCS($l1, $l2);
74
        $lcs->longestCommonSubsequence();
75
76
        /** @var TextLine[][] $result */
77
        $result = $lcs->getResult();
78
79
        $this->assertSame(2, \count($result[0]));
80
        $this->assertSame(2, \count($result[1]));
81
82
        for ($i = 0; $i < \count($result[0]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
83
            $this->assertTrue($result[0][$i]->isSameText($result[1][$i]));
84
        }
85
86
        $this->assertSame(0, $result[0][0]->getLineNumber());
87
        $this->assertSame(0, $result[1][0]->getLineNumber());
88
        $this->assertSame(1, $result[0][1]->getLineNumber());
89
        $this->assertSame(1, $result[1][1]->getLineNumber());
90
    }
91
92
    public function testLineDeleteEnd(): void
93
    {
94
        $s1 = 'abc' . PHP_EOL . 'def' . PHP_EOL . '123';
95
        $s2 = 'abc' . PHP_EOL . 'def';
96
97
        $l1 = TextLineLCS::getTextLines($s1);
98
        $l2 = TextLineLCS::getTextLines($s2);
99
        $lcs = new TextLineLCS($l1, $l2);
100
        $lcs->longestCommonSubsequence();
101
102
        /** @var TextLine[][] $result */
103
        $result = $lcs->getResult();
104
105
        $this->assertSame(2, \count($result[0]));
106
        $this->assertSame(2, \count($result[1]));
107
108
        for ($i = 0; $i < \count($result[0]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
109
            $this->assertTrue($result[0][$i]->isSameText($result[1][$i]));
110
        }
111
112
        $this->assertSame(0, $result[0][0]->getLineNumber());
113
        $this->assertSame(0, $result[1][0]->getLineNumber());
114
        $this->assertSame(1, $result[0][1]->getLineNumber());
115
        $this->assertSame(1, $result[1][1]->getLineNumber());
116
    }
117
118
    public function testLineAppendStart(): void
119
    {
120
        $s1 = 'abc' . PHP_EOL . 'def';
121
        $s2 = '123' . PHP_EOL . 'abc' . PHP_EOL . 'def';
122
123
        $l1 = TextLineLCS::getTextLines($s1);
124
        $l2 = TextLineLCS::getTextLines($s2);
125
        $lcs = new TextLineLCS($l1, $l2);
126
        $lcs->longestCommonSubsequence();
127
128
        /** @var TextLine[][] $result */
129
        $result = $lcs->getResult();
130
131
        $this->assertSame(2, \count($result[0]));
132
        $this->assertSame(2, \count($result[1]));
133
134
        for ($i = 0; $i < \count($result[0]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
135
            $this->assertTrue($result[0][$i]->isSameText($result[1][$i]));
136
        }
137
138
        $this->assertSame(0, $result[0][0]->getLineNumber());
139
        $this->assertSame(1, $result[1][0]->getLineNumber());
140
        $this->assertSame(1, $result[0][1]->getLineNumber());
141
        $this->assertSame(2, $result[1][1]->getLineNumber());
142
    }
143
144
    public function testLineDeleteStart(): void
145
    {
146
        $s1 = '123' . PHP_EOL . 'abc' . PHP_EOL . 'def';
147
        $s2 = 'abc' . PHP_EOL . 'def';
148
149
        $l1 = TextLineLCS::getTextLines($s1);
150
        $l2 = TextLineLCS::getTextLines($s2);
151
        $lcs = new TextLineLCS($l1, $l2);
152
        $lcs->longestCommonSubsequence();
153
154
        /** @var TextLine[][] $result */
155
        $result = $lcs->getResult();
156
157
        $this->assertSame(2, \count($result[0]));
158
        $this->assertSame(2, \count($result[1]));
159
160
        for ($i = 0; $i < \count($result[0]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
161
            $this->assertTrue($result[0][$i]->isSameText($result[1][$i]));
162
        }
163
164
        $this->assertSame(1, $result[0][0]->getLineNumber());
165
        $this->assertSame(0, $result[1][0]->getLineNumber());
166
        $this->assertSame(2, $result[0][1]->getLineNumber());
167
        $this->assertSame(1, $result[1][1]->getLineNumber());
168
    }
169
}
170