ConsoleDiffTest::testDifferenceWithANSI()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of graze/console-diff-renderer.
4
 *
5
 * Copyright (c) 2017 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/console-diff-renderer/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/console-diff-renderer
12
 */
13
14
namespace Graze\DiffRenderer\Test\Unit\Diff;
15
16
use Graze\DiffRenderer\Diff\ConsoleDiff;
17
use Graze\DiffRenderer\Test\TestCase;
18
19
class ConsoleDiffTest extends TestCase
20
{
21
    /**
22
     * @dataProvider differenceWithANSIData
23
     *
24
     * @param string[] $old
25
     * @param string[] $new
26
     * @param string[] $expected
27
     */
28
    public function testDifferenceWithANSI(array $old, array $new, array $expected)
29
    {
30
        $diff = new ConsoleDiff();
31
32
        $this->assertEquals($expected, $diff->lines($old, $new));
33
    }
34
35
    /**
36
     * @return array
37
     */
38
    public function differenceWithANSIData()
39
    {
40
        return [
41
            [ // col now equals the strip_tags version (hence 6)
42
                ["\e[32mfirst\e[39m", "\e[32msecond\e[39m"],
43
                ["\e[32mnew\e[39m", "\e[32msecond cake\e[39m"],
44
                [
45
                    ["col" => 0, "str" => "\e[32mnew\e[39m"],
46
                    ["col" => 6, "str" => "\e[32m cake\e[39m"],
47
                ],
48
            ],
49
            [ // multiple tags should represent all tags
50
                ["\e[32m\e[37;41mfirst\e[39;49m\e[39m", "\e[32msecond\e[39m"],
51
                ["\e[32m\e[37;41mnew\e[39;49m\e[39m", "\e[32msecond cake\e[39m"],
52
                [
53
                    ["col" => 0, "str" => "\e[32m\e[37;41mnew\e[39;49m\e[39m"],
54
                    ["col" => 6, "str" => "\e[32m cake\e[39m"],
55
                ],
56
            ],
57
            [
58
                ["\e[32m\e[37;41mfirst\e[39;49m\e[39m", "\e[32msecond\e[39m"],
59
                ["\e[32m\e[37;41mnew\e[39;49m\e[39m", "\e[32msecond cake\e[39m"],
60
                [
61
                    ["col" => 0, "str" => "\e[32m\e[37;41mnew\e[39;49m\e[39m"],
62
                    ["col" => 6, "str" => "\e[32m cake\e[39m"],
63
                ],
64
            ],
65
            [ // col now equals the strip_tags version (hence 6)
66
                ["\e[32mfirst\e[39m", "\e[32msecond\e[39m"],
67
                ["\e[37;41mnew\e[39m", "\e[32msecond cake\e[39m"],
68
                [
69
                    ["col" => 0, "str" => "\e[37;41mnew\e[39m"],
70
                    ["col" => 6, "str" => "\e[32m cake\e[39m"],
71
                ],
72
            ],
73
        ];
74
    }
75
76
    /**
77
     * @dataProvider firstDifferenceData
78
     *
79
     * @param string $left
80
     * @param string $right
81
     * @param int    $pos
82
     */
83
    public function testFirstDifference($left, $right, $pos)
84
    {
85
        $diff = new ConsoleDiff();
86
        $this->assertEquals($pos, $diff->firstDifference($left, $right));
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    public function firstDifferenceData()
93
    {
94
        return [
95
            ['abcdef', 'abcdef', -1],
96
            ['abcdef', 'abcde', 5],
97
            ['abcdef', 'bcdef', 0],
98
            ["\e[32mfish", "\e[32mnew", 0],
99
            ["new\e[32mfish", "new\e[32mcake", 3],
100
            ["\e[32mfish", "\e[32;49mfish", 0],
101
            ["new\e[32mcash", "nes\e[32mcash", 2]
102
        ];
103
    }
104
}
105