ANSITest::setUp()   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 0
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\Terminal\ANSI;
15
16
use Graze\DiffRenderer\Terminal\ANSI;
17
use Graze\DiffRenderer\Terminal\CursorInterface;
18
use Graze\DiffRenderer\Test\TestCase;
19
20
class ANSITest extends TestCase
21
{
22
    /** @var ANSI */
23
    private $cursor;
24
25
    public function setUp()
26
    {
27
        parent::setUp();
28
29
        $this->cursor = new ANSI();
30
    }
31
32
    public function testImplements()
33
    {
34
        $this->assertInstanceOf(CursorInterface::class, $this->cursor);
35
    }
36
37
    public function testMove()
38
    {
39
        $this->assertEquals("\e[12;13H", $this->cursor->move(12, 13));
40
        $this->assertEquals("\e[2;8H", $this->cursor->move(2, 8));
41
    }
42
43
    public function testMoveUp()
44
    {
45
        $this->assertEquals("\e[1A", $this->cursor->moveUp(1));
46
        $this->assertEquals("\e[8A", $this->cursor->moveUp(8));
47
    }
48
49
    public function testMoveDown()
50
    {
51
        $this->assertEquals("\e[2B", $this->cursor->moveDown(2));
52
        $this->assertEquals("\e[7B", $this->cursor->moveDown(7));
53
    }
54
55
    public function testMoveLeft()
56
    {
57
        $this->assertEquals("\e[3D", $this->cursor->moveLeft(3));
58
        $this->assertEquals("\e[12D", $this->cursor->moveLeft(12));
59
    }
60
61
    public function testMoveRight()
62
    {
63
        $this->assertEquals("\e[6C", $this->cursor->moveRight(6));
64
        $this->assertEquals("\e[13C", $this->cursor->moveRight(13));
65
    }
66
67
    public function testEraseToEnd()
68
    {
69
        $this->assertEquals("\e[K", $this->cursor->eraseToEnd());
70
    }
71
72
    public function testEraseToStart()
73
    {
74
        $this->assertEquals("\e[1K", $this->cursor->eraseToStart());
75
    }
76
77
    public function testEraseDown()
78
    {
79
        $this->assertEquals("\e[J", $this->cursor->eraseDown());
80
    }
81
82
    public function testEraseUp()
83
    {
84
        $this->assertEquals("\e[1J", $this->cursor->eraseUp());
85
    }
86
87
    public function testEraseScreen()
88
    {
89
        $this->assertEquals("\e[2J", $this->cursor->eraseScreen());
90
    }
91
92
    public function testShowCursor()
93
    {
94
        $this->assertEquals("\e[?25h", $this->cursor->showCursor());
95
    }
96
97
    public function testHideCursor()
98
    {
99
        $this->assertEquals("\e[?25l", $this->cursor->hideCursor());
100
    }
101
102
    /**
103
     * @dataProvider filterData
104
     *
105
     * @param string $string
106
     * @param string $replacement
107
     * @param string $expected
108
     */
109
    public function testFilter($string, $replacement, $expected)
110
    {
111
        $this->assertEquals($expected, $this->cursor->filter($string, $replacement));
112
    }
113
114
    /**
115
     * @return array
116
     */
117
    public function filterData()
118
    {
119
        return [
120
            ["bla\e[12;13Hbla", '', 'blabla'],
121
            ["bla\e[1Abla", '', 'blabla'],
122
            ["bla\e[2Bbla", '', 'blabla'],
123
            ["bla\e[3Dbla", '', 'blabla'],
124
            ["bla\e[6Cbla", '', 'blabla'],
125
            ["bla\e[Kbla", '', 'blabla'],
126
            ["bla\e[1Kbla", '', 'blabla'],
127
            ["bla\e[Jbla", '', 'blabla'],
128
            ["bla\e[1Jbla", '', 'blabla'],
129
            ["bla\e[2Jbla", '', 'blabla'],
130
            ["bla\rbla", '', 'blabla'],
131
            [
132
                "bla\e[12;13H" .
133
                "bla\e[1A" .
134
                "bla\e[2B" .
135
                "bla\e[12;33;54m",
136
                '',
137
                'blablablabla',
138
            ],
139
            ["bla\e(bla\e)bla\eI", '', "blablabla\eI"],
140
            ["bla\e[1Abla", '<>', 'bla<><><><>bla'], // applies the replacement once per character being replaced
141
            ["bla\e[1A1bla", '', 'bla1bla'],
142
            ["bla\e[1A2bla", '', 'bla2bla'],
143
            ["bla\e[1A3bla", '', 'bla3bla'],
144
            ["bla\e[1A4bla", '', 'bla4bla'],
145
            ["bla\e[1A5bla", '', 'bla5bla'],
146
            ["bla\e[1A6bla", '', 'bla6bla'],
147
            ["bla\e[1A7bla", '', 'bla7bla'],
148
            ["bla\e[1A8bla", '', 'bla8bla'],
149
            ["bla\e[1A9bla", '', 'bla9bla'],
150
            ["bla\e[1A0bla", '', 'bla0bla'],
151
            [
152
                "\e[13m1234567890\e[42m1234567890\e[15m12345678901234567890\e[42m",
153
                '',
154
                "1234567890123456789012345678901234567890",
155
            ],
156
        ];
157
    }
158
159
    /**
160
     * @dataProvider getCurrentFormattingData
161
     *
162
     * @param string $string
163
     * @param string $expected
164
     */
165
    public function testGetCurrentFormatting($string, $expected)
166
    {
167
        $actual = $this->cursor->getCurrentFormatting($string);
168
        $this->assertEquals(bin2hex($expected), bin2hex($actual));
169
    }
170
171
    /**
172
     * return array
173
     */
174
    public function getCurrentFormattingData()
175
    {
176
        return [
177
            ["bla\e[4mcake", "\e[4m"],
178
            ["bla\e[4;3mcake\e[2mfish", "\e[4;3;2m"],
179
            ["bla\e[4;0;3mcake", "\e[3m"],
180
            ["bla\e[38;2;123;123;123;5;2mcake", "\e[38;2;123;123;123;5;2m"],
181
            ["bla\e[32;43mcake\e[39mfish", "\e[43m"],
182
            ["bla\e[32mcake\e[39m", ''],
183
            ["bla\e[32,42mcake\e[mboo", ''],
184
            ["bla\e[1;2;3;4;5mcake\e[22m", "\e[3;4;5m"],
185
            ["bla\e[32;43mcake\e[38;5;123mboo\e[49m", "\e[32;38;5;123m"] // this could be \e[38;5;123m but still works
186
        ];
187
    }
188
}
189