-
Notifications
You must be signed in to change notification settings - Fork 2
/
elastictable_test.go
86 lines (74 loc) · 2.04 KB
/
elastictable_test.go
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
package elastictable
import (
"bytes"
"fmt"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var dummyHeaders = []string{"h1", "h2", "h3"}
var widthTests = []struct {
term int
given []int
expected []int
}{
{20, []int{5, 10, 15}, []int{3, 3, 3}},
{40, []int{10, 10, 10}, []int{10, 10, 10}},
{50, []int{10, 10, 20}, []int{10, 10, 20}},
{60, []int{10, 25, 70}, []int{10, 14, 26}},
{60, []int{10, 70, 25}, []int{10, 26, 14}},
{60, []int{70, 25, 10}, []int{26, 14, 10}},
{100, []int{50, 50, 50}, []int{30, 30, 30}},
}
func TestOptimizedWidths(t *testing.T) {
for _, tt := range widthTests {
os.Setenv(envTermWidth, fmt.Sprintf("%v", tt.term))
et := NewElasticTable(dummyHeaders)
et.AddRow(dummyRow(tt.given...))
actual := et.optimizedWidths()
assert.Equal(t, tt.expected, actual)
}
os.Setenv(envTermWidth, "")
}
func TestTableOutput(t *testing.T) {
given := []string{"column1-10", "column2-10", "column3-10"}
expected := []string{
" h1 | h2 | h3 ",
"------------+------------+------------",
" column1-10 | column2-10 | column3-10 ",
"",
}
os.Setenv(envTermWidth, "40")
buf := new(bytes.Buffer)
et := NewElasticTable(dummyHeaders)
et.AddRow(given)
et.Render(buf)
actual := strings.Split(buf.String(), "\n")
assert.Equal(t, expected, actual)
}
func TestWrapping(t *testing.T) {
given := []string{"column1-10", "column2-10column2-20", "column3-10column3-20column3-30"}
expected := []string{
" h1 | h2 | h3 ",
"------------+------------+------------",
" column1-10 | column2-10 | column3-10 ",
" | column2-20 | column3-20 ",
" | | column3-30 ",
"",
}
os.Setenv(envTermWidth, "40")
buf := new(bytes.Buffer)
et := NewElasticTable(dummyHeaders)
et.AddRow(given)
et.Render(buf)
actual := strings.Split(buf.String(), "\n")
assert.Equal(t, expected, actual)
}
func dummyRow(widths ...int) []string {
row := make([]string, len(widths))
for i, w := range widths {
row[i] = strings.Repeat("z", w)
}
return row
}