-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathTableFormatterTest.php
More file actions
141 lines (111 loc) · 3.16 KB
/
TableFormatterTest.php
File metadata and controls
141 lines (111 loc) · 3.16 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
<?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([20, '*'], [$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([20, '*'], [$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([5, '*'], [$col1, $col2]);
$this->assertEquals($expect, $result);
}
}