Skip to content

Commit 1cf2193

Browse files
committed
Add: More tests
1 parent b794e3d commit 1cf2193

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

tests/DiffTest.php

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use InvalidArgumentException;
6+
use jblond\Diff;
7+
use jblond\Diff\Renderer\Html\SideBySide;
8+
use OutOfRangeException;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/**
12+
*
13+
*/
14+
class DiffTest extends TestCase
15+
{
16+
/**
17+
* @var Diff
18+
*/
19+
protected $diff;
20+
21+
/**
22+
* @param string|null $name
23+
* @param array $data
24+
* @param $dataName
25+
*/
26+
public function __construct(?string $name = null, array $data = [], $dataName = '')
27+
{
28+
parent::__construct($name, $data, $dataName);
29+
$this->diff = new Diff(
30+
file_get_contents('tests/resources/a.txt'),
31+
file_get_contents('tests/resources/b.txt')
32+
);
33+
}
34+
35+
/**
36+
* @return void
37+
*/
38+
public function testGetGroupedOpCodes()
39+
{
40+
$this->assertEquals(
41+
[
42+
[
43+
['equal', 0, 3, 0, 3],
44+
['replace', 3, 4, 3, 4],
45+
['equal', 4, 7, 4, 7],
46+
['delete', 7, 8, 7, 7],
47+
['equal', 8, 9, 7, 8],
48+
['replace', 9, 10, 8, 9],
49+
['equal', 10, 11, 9, 10],
50+
['replace', 11, 12, 10, 11],
51+
['equal', 12,13, 11, 12],
52+
['insert', 13, 13, 12, 13],
53+
['equal', 13, 16, 13, 16],
54+
['replace', 16, 19, 16, 19],
55+
['equal', 19, 22, 19, 22],
56+
],
57+
[
58+
['outOfContext', 22, 24, 22, 24],
59+
],
60+
[
61+
['equal', 24, 27, 24, 27],
62+
['replace', 27, 28, 27, 28],
63+
['equal', 28, 29, 28, 29],
64+
['replace', 29, 30, 29, 30],
65+
['equal', 30, 33, 30, 33],
66+
]
67+
],
68+
$this->diff->getGroupedOpCodes()
69+
);
70+
}
71+
72+
public function testGetVersion1()
73+
{
74+
$this->assertEquals(
75+
file('tests/resources/a.txt'),
76+
$this->diff->getVersion1()
77+
);
78+
}
79+
80+
public function testGetVersion2()
81+
{
82+
$this->assertEquals(
83+
file('tests/resources/b.txt'),
84+
$this->diff->getVersion2()
85+
);
86+
}
87+
88+
public function testGetArgumentType()
89+
{
90+
$this->assertEquals(
91+
[
92+
0,
93+
1
94+
],
95+
[
96+
$this->diff->getArgumentType([]),
97+
$this->diff->getArgumentType('ABC')
98+
]
99+
);
100+
$this->expectException(InvalidArgumentException::class);
101+
$this->diff->getArgumentType(123);
102+
}
103+
104+
public function testRender()
105+
{
106+
$renderer = new SideBySide();
107+
$this->assertEquals(
108+
file_get_contents('tests/resources/htmlSideBySide.txt'),
109+
$this->diff->render($renderer)
110+
);
111+
}
112+
113+
public function testGetSimilarity()
114+
{
115+
$this->assertEquals(
116+
0.7272727272727273,
117+
$this->diff->getSimilarity()
118+
);
119+
}
120+
121+
public function testIsIdentical()
122+
{
123+
$this->assertEquals(
124+
false,
125+
$this->diff->isIdentical()
126+
);
127+
}
128+
129+
public function testGetStatistics()
130+
{
131+
$this->assertEquals(
132+
[
133+
'inserted' => 1,
134+
'deleted' => 1,
135+
'replaced' => 8,
136+
'equal' => 24
137+
],
138+
$this->diff->getStatistics()
139+
);
140+
}
141+
142+
public function testGetArrayRange()
143+
{
144+
$this->assertEquals(
145+
[5,6],
146+
$this->diff->getArrayRange([2,5,6],1, 3)
147+
);
148+
149+
$this->expectException(OutOfRangeException::class);
150+
$this->diff->getArrayRange([1,5],5);
151+
}
152+
}

0 commit comments

Comments
 (0)