-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader_test.go
More file actions
187 lines (177 loc) · 5.87 KB
/
reader_test.go
File metadata and controls
187 lines (177 loc) · 5.87 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
package table
import (
"reflect"
"strings"
"testing"
)
var tests = []struct {
name string
text string
want *Table
}{
{
name: "test full table",
text: `
NAME ROLES OS IMAGE KERNEL-VERSION CONTAINER RUNTIME
master-1 control-plane,master Ubuntu 20.04.1 LTS 5.13.0-39-generic containerd://1.4.1
monitor-1 monitor Ubuntu 20.04.2 LTS 5.13.0-39-generic containerd://1.4.2
worker-1 worker Ubuntu 20.04.3 LTS 5.13.0-39-generic containerd://1.4.3
`,
want: &Table{
Header: Header{
Text: "NAME ROLES OS IMAGE KERNEL-VERSION CONTAINER RUNTIME",
Cells: []HeaderCell{
{Index: 0, Key: "NAME"},
{Index: 20, Key: "ROLES"},
{Index: 47, Key: "OS IMAGE"},
{Index: 68, Key: "KERNEL-VERSION"},
{Index: 88, Key: "CONTAINER RUNTIME"},
},
},
Rows: []Row{
{
Text: "master-1 control-plane,master Ubuntu 20.04.1 LTS 5.13.0-39-generic containerd://1.4.1",
Cells: []RowCell{
{Relation: "NAME", Value: "master-1"},
{Relation: "ROLES", Value: "control-plane,master"},
{Relation: "OS IMAGE", Value: "Ubuntu 20.04.1 LTS"},
{Relation: "KERNEL-VERSION", Value: "5.13.0-39-generic"},
{Relation: "CONTAINER RUNTIME", Value: "containerd://1.4.1"},
},
},
{
Text: "monitor-1 monitor Ubuntu 20.04.2 LTS 5.13.0-39-generic containerd://1.4.2",
Cells: []RowCell{
{Relation: "NAME", Value: "monitor-1"},
{Relation: "ROLES", Value: "monitor"},
{Relation: "OS IMAGE", Value: "Ubuntu 20.04.2 LTS"},
{Relation: "KERNEL-VERSION", Value: "5.13.0-39-generic"},
{Relation: "CONTAINER RUNTIME", Value: "containerd://1.4.2"},
},
},
{
Text: "worker-1 worker Ubuntu 20.04.3 LTS 5.13.0-39-generic containerd://1.4.3",
Cells: []RowCell{
{Relation: "NAME", Value: "worker-1"},
{Relation: "ROLES", Value: "worker"},
{Relation: "OS IMAGE", Value: "Ubuntu 20.04.3 LTS"},
{Relation: "KERNEL-VERSION", Value: "5.13.0-39-generic"},
{Relation: "CONTAINER RUNTIME", Value: "containerd://1.4.3"},
},
},
},
},
},
{
name: "test slipped table",
text: `
NAME ROLES OS IMAGE KERNEL-VERSION CONTAINER RUNTIME
master-1 control-plane,master Ubuntu 20.04.1 LTS 5.13.0-39-generic
monitor-1 monitor Ubuntu 20.04.2 LTS 5.13.0-39-generic containerd://1.4.2
worker 5.13.0-39-generic containerd://1.4.3
`,
want: &Table{
Header: Header{
Text: "NAME ROLES OS IMAGE KERNEL-VERSION CONTAINER RUNTIME",
Cells: []HeaderCell{
{Index: 0, Key: "NAME"},
{Index: 20, Key: "ROLES"},
{Index: 47, Key: "OS IMAGE"},
{Index: 68, Key: "KERNEL-VERSION"},
{Index: 88, Key: "CONTAINER RUNTIME"},
},
},
Rows: []Row{
{
Text: "master-1 control-plane,master Ubuntu 20.04.1 LTS 5.13.0-39-generic",
Cells: []RowCell{
{Relation: "NAME", Value: "master-1"},
{Relation: "ROLES", Value: "control-plane,master"},
{Relation: "OS IMAGE", Value: "Ubuntu 20.04.1 LTS"},
{Relation: "KERNEL-VERSION", Value: "5.13.0-39-generic"},
{Relation: "CONTAINER RUNTIME"},
},
},
{
Text: "monitor-1 monitor Ubuntu 20.04.2 LTS 5.13.0-39-generic containerd://1.4.2",
Cells: []RowCell{
{Relation: "NAME", Value: "monitor-1"},
{Relation: "ROLES", Value: "monitor"},
{Relation: "OS IMAGE", Value: "Ubuntu 20.04.2 LTS"},
{Relation: "KERNEL-VERSION", Value: "5.13.0-39-generic"},
{Relation: "CONTAINER RUNTIME", Value: "containerd://1.4.2"},
},
},
{
Text: "worker 5.13.0-39-generic containerd://1.4.3",
Cells: []RowCell{
{Relation: "NAME"},
{Relation: "ROLES", Value: "worker"},
{Relation: "OS IMAGE"},
{Relation: "KERNEL-VERSION", Value: "5.13.0-39-generic"},
{Relation: "CONTAINER RUNTIME", Value: "containerd://1.4.3"},
},
},
},
},
},
{
name: "test empty table",
text: `NAME ROLES OS IMAGE KERNEL-VERSION CONTAINER RUNTIME`,
want: &Table{
Header: Header{
Text: "NAME ROLES OS IMAGE KERNEL-VERSION CONTAINER RUNTIME",
Cells: []HeaderCell{
{Index: 0, Key: "NAME"},
{Index: 20, Key: "ROLES"},
{Index: 47, Key: "OS IMAGE"},
{Index: 68, Key: "KERNEL-VERSION"},
{Index: 88, Key: "CONTAINER RUNTIME"},
},
},
Rows: make([]Row, 0),
},
},
{
name: "test empty input",
text: "",
want: &Table{
Header: Header{},
Rows: make([]Row, 0),
},
},
}
func TestNewReader(t *testing.T) {
r := NewReader(strings.NewReader("A\tB\tC"))
if r == nil {
t.Error("reader must be not nil")
}
r = NewReader(strings.NewReader(" "))
if r == nil {
t.Errorf("reader must not be nil")
}
}
func TestReader(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := NewReader(strings.NewReader(tt.text))
rows := make([]Row, 0)
for r.Next() {
rows = append(rows, r.Row())
}
got := &Table{Header: r.Header(), Rows: rows}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewReader() failed\n got: %v\nwant: %v", got, tt.want)
}
})
}
}
func TestReadAll(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ReadAll(tt.text); !reflect.DeepEqual(*got, *tt.want) {
t.Errorf("ReadAll() failed\n got: %v\nwant: %v", got, tt.want)
}
})
}
}