forked from splitbrain/php-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableFormatterTest.php
More file actions
188 lines (154 loc) · 4.7 KB
/
TableFormatterTest.php
File metadata and controls
188 lines (154 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace splitbrain\phpcli\tests;
use splitbrain\phpcli\Colors;
class TableFormatter extends \splitbrain\phpcli\TableFormatter
{
public function calculateColLengths($columns)
{
return parent::calculateColLengths($columns);
}
public function strlen($string)
{
return parent::strlen($string);
}
public function wordwrap($str, $width = 75, $break = "\n", $cut = false)
{
return parent::wordwrap($str, $width, $break, $cut);
}
}
class TableFormatterTest extends \PHPUnit\Framework\TestCase
{
/**
* Provide test data for column width calculations
*
* @return array
*/
public function calcProvider()
{
return array(
array(
array(5, 5, 5),
array(5, 5, 88)
),
array(
array('*', 5, 5),
array(88, 5, 5)
),
array(
array(5, '50%', '50%'),
array(5, 46, 47)
),
array(
array(5, '*', '50%'),
array(5, 47, 46)
),
);
}
/**
* Test calculation of column sizes
*
* @dataProvider calcProvider
* @param array $input
* @param array $expect
* @param int $max
* @param string $border
*/
public function test_calc($input, $expect, $max = 100, $border = ' ')
{
$tf = new TableFormatter();
$tf->setMaxWidth($max);
$tf->setBorder($border);
$result = $tf->calculateColLengths($input);
$this->assertEquals($max, array_sum($result) + (strlen($border) * (count($input) - 1)));
$this->assertEquals($expect, $result);
}
/**
* Check wrapping
*/
public function test_wrap()
{
$text = "this is a long string something\n" .
"123456789012345678901234567890";
$expt = "this is a long\n" .
"string\n" .
"something\n" .
"123456789012345\n" .
"678901234567890";
$tf = new TableFormatter();
$this->assertEquals($expt, $tf->wordwrap($text, 15, "\n", true));
}
public function test_length()
{
$text = "this is häppy ☺";
$expect = "$text |test";
$tf = new TableFormatter();
$tf->setBorder('|');
$result = $tf->format(array(20, '*'), array($text, 'test'));
$this->assertEquals($expect, trim($result));
}
public function test_colorlength()
{
$color = new Colors();
$text = 'this is ' . $color->wrap('green', Colors::C_GREEN);
$expect = "$text |test";
$tf = new TableFormatter();
$tf->setBorder('|');
$result = $tf->format(array(20, '*'), array($text, 'test'));
$this->assertEquals($expect, trim($result));
}
public function test_onewrap()
{
$col1 = "test\nwrap";
$col2 = "test";
$expect = "test |test \n" .
"wrap | \n";
$tf = new TableFormatter();
$tf->setMaxWidth(11);
$tf->setBorder('|');
$result = $tf->format(array(5, '*'), array($col1, $col2));
$this->assertEquals($expect, $result);
}
/**
* Test that colors are correctly applied when text is wrapping across lines.
*
* @dataProvider colorwrapProvider
*/
public function test_colorwrap($text, $expect)
{
$tf = new TableFormatter();
$tf->setMaxWidth(15);
$this->assertEquals($expect, $tf->format(array('*'), array($text)));
}
/**
* Data provider for test_colorwrap.
*
* @return array[]
*/
public function colorwrapProvider()
{
$color = new Colors();
$cyan = $color->getColorCode(Colors::C_CYAN);
$reset = $color->getColorCode(Colors::C_RESET);
$wrap = function ($str) use ($color) {
return $color->wrap($str, Colors::C_CYAN);
};
return array(
'color word line 1' => array(
"This is ". $wrap("cyan") . " text wrapping",
"This is {$cyan}cyan{$reset} \ntext wrapping \n",
),
'color word line 2' => array(
"This is text ". $wrap("cyan") . " wrapping",
"This is text \n{$cyan}cyan{$reset} wrapping \n",
),
'color across lines' => array(
"This is ". $wrap("cyan text") . " wrapping",
"This is {$cyan}cyan \ntext{$reset} wrapping \n",
),
'color across lines until end' => array(
"This is ". $wrap("cyan text wrapping"),
"This is {$cyan}cyan \n{$cyan}text wrapping{$reset} \n",
),
);
}
}