Skip to content

Commit 72c2315

Browse files
refactor: cover edge cases (empty table e.g.)
1 parent 6f50a69 commit 72c2315

File tree

4 files changed

+147
-50
lines changed

4 files changed

+147
-50
lines changed

reader.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func NewReader(rd io.Reader) *Reader {
2121
}
2222

2323
if h == "" {
24-
return nil
24+
return &Reader{s: s}
2525
}
2626

2727
return &Reader{
@@ -58,13 +58,19 @@ func ReadAll(s string) *Table {
5858
}
5959
}
6060

61-
if len(rows) <= 1 {
62-
return nil
61+
rl := len(rows)
62+
if rl <= 1 {
63+
t := &Table{Rows: make([]Row, 0)}
64+
if rl == 1 {
65+
t.Header = parseHeader(rows[0])
66+
}
67+
68+
return t
6369
}
6470

6571
table := &Table{
6672
Header: parseHeader(rows[0]),
67-
Rows: make([]Row, 0, len(rows)-1),
73+
Rows: make([]Row, 0, rl-1),
6874
}
6975

7076
for _, row := range rows[1:] {

reader_test.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,31 @@ master-1 control-plane,master Ubuntu 20.04.1 LTS 5.13.0-39-ge
119119
},
120120
},
121121
},
122+
{
123+
name: "test empty table",
124+
text: `NAME ROLES OS IMAGE KERNEL-VERSION CONTAINER RUNTIME`,
125+
want: &Table{
126+
Header: Header{
127+
Text: "NAME ROLES OS IMAGE KERNEL-VERSION CONTAINER RUNTIME",
128+
Cells: []HeaderCell{
129+
{Index: 0, Key: "NAME"},
130+
{Index: 20, Key: "ROLES"},
131+
{Index: 47, Key: "OS IMAGE"},
132+
{Index: 68, Key: "KERNEL-VERSION"},
133+
{Index: 88, Key: "CONTAINER RUNTIME"},
134+
},
135+
},
136+
Rows: make([]Row, 0),
137+
},
138+
},
139+
{
140+
name: "test empty input",
141+
text: "",
142+
want: &Table{
143+
Header: Header{},
144+
Rows: make([]Row, 0),
145+
},
146+
},
122147
}
123148

124149
func TestNewReader(t *testing.T) {
@@ -128,8 +153,8 @@ func TestNewReader(t *testing.T) {
128153
}
129154

130155
r = NewReader(strings.NewReader(" "))
131-
if r != nil {
132-
t.Errorf("reader must be nil")
156+
if r == nil {
157+
t.Errorf("reader must not be nil")
133158
}
134159
}
135160

@@ -138,14 +163,14 @@ func TestReader(t *testing.T) {
138163
t.Run(tt.name, func(t *testing.T) {
139164
r := NewReader(strings.NewReader(tt.text))
140165

141-
var rows []Row
166+
rows := make([]Row, 0)
142167
for r.Next() {
143168
rows = append(rows, r.Row())
144169
}
145170

146171
got := &Table{Header: r.Header(), Rows: rows}
147172
if !reflect.DeepEqual(got, tt.want) {
148-
t.Errorf("NewReader() failed\n got:%v \nwant %v", got, tt.want)
173+
t.Errorf("NewReader() failed\n got: %v\nwant: %v", got, tt.want)
149174
}
150175
})
151176
}
@@ -154,8 +179,8 @@ func TestReader(t *testing.T) {
154179
func TestReadAll(t *testing.T) {
155180
for _, tt := range tests {
156181
t.Run(tt.name, func(t *testing.T) {
157-
if got := ReadAll(tt.text); !reflect.DeepEqual(got, tt.want) {
158-
t.Errorf("ReadAll() failed\n got:%v \nwant %v", got, tt.want)
182+
if got := ReadAll(tt.text); !reflect.DeepEqual(*got, *tt.want) {
183+
t.Errorf("ReadAll() failed\n got: %v\nwant: %v", got, tt.want)
159184
}
160185
})
161186
}

table.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ type RowCell struct {
2525
Relation string
2626
}
2727

28-
func (t *Table) Row(index int) Row {
28+
func (t *Table) Row(index int) *Row {
2929
if len(t.Rows) > index {
30-
return t.Rows[index]
30+
return &t.Rows[index]
3131
}
32-
return Row{}
32+
return nil
3333
}
3434

3535
func (h *Header) append(cell HeaderCell) {
@@ -40,18 +40,18 @@ func (r *Row) append(cell RowCell) {
4040
r.Cells = append(r.Cells, cell)
4141
}
4242

43-
func (r *Row) Cell(index int) RowCell {
43+
func (r *Row) Cell(index int) *RowCell {
4444
if len(r.Cells) > index {
45-
return r.Cells[index]
45+
return &r.Cells[index]
4646
}
47-
return RowCell{}
47+
return nil
4848
}
4949

50-
func (r *Row) CellByName(s string) RowCell {
50+
func (r *Row) CellByName(s string) *RowCell {
5151
for _, cell := range r.Cells {
5252
if cell.Relation == s {
53-
return cell
53+
return &cell
5454
}
5555
}
56-
return RowCell{}
56+
return nil
5757
}

table_test.go

Lines changed: 97 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package table
22

33
import (
4+
"reflect"
45
"runtime"
56
"testing"
67
)
@@ -16,18 +17,53 @@ func TestTable_Row(t *testing.T) {
1617
tx := ReadAll(input)
1718

1819
tests := []struct {
19-
row Row
20-
want string
20+
row *Row
21+
want *Row
2122
}{
22-
{tx.Row(0), "master-1 control-plane,master Ubuntu 20.04.1 LTS 5.13.0-39-generic containerd://1.4.1"},
23-
{tx.Row(1), "monitor-1 monitor Ubuntu 20.04.2 LTS 5.13.0-39-generic containerd://1.4.2"},
24-
{tx.Row(2), "worker-1 worker Ubuntu 20.04.3 LTS 5.13.0-39-generic containerd://1.4.3"},
25-
{tx.Row(3), ""},
26-
{tx.Row(99), ""},
23+
{
24+
tx.Row(0),
25+
&Row{
26+
Text: "master-1 control-plane,master Ubuntu 20.04.1 LTS 5.13.0-39-generic containerd://1.4.1",
27+
Cells: []RowCell{
28+
{Relation: "NAME", Value: "master-1"},
29+
{Relation: "ROLES", Value: "control-plane,master"},
30+
{Relation: "OS IMAGE", Value: "Ubuntu 20.04.1 LTS"},
31+
{Relation: "KERNEL-VERSION", Value: "5.13.0-39-generic"},
32+
{Relation: "CONTAINER RUNTIME", Value: "containerd://1.4.1"},
33+
},
34+
}},
35+
{
36+
tx.Row(1),
37+
&Row{
38+
Text: "monitor-1 monitor Ubuntu 20.04.2 LTS 5.13.0-39-generic containerd://1.4.2",
39+
Cells: []RowCell{
40+
{Relation: "NAME", Value: "monitor-1"},
41+
{Relation: "ROLES", Value: "monitor"},
42+
{Relation: "OS IMAGE", Value: "Ubuntu 20.04.2 LTS"},
43+
{Relation: "KERNEL-VERSION", Value: "5.13.0-39-generic"},
44+
{Relation: "CONTAINER RUNTIME", Value: "containerd://1.4.2"},
45+
},
46+
},
47+
},
48+
{
49+
tx.Row(2),
50+
&Row{
51+
Text: "worker-1 worker Ubuntu 20.04.3 LTS 5.13.0-39-generic containerd://1.4.3",
52+
Cells: []RowCell{
53+
{Relation: "NAME", Value: "worker-1"},
54+
{Relation: "ROLES", Value: "worker"},
55+
{Relation: "OS IMAGE", Value: "Ubuntu 20.04.3 LTS"},
56+
{Relation: "KERNEL-VERSION", Value: "5.13.0-39-generic"},
57+
{Relation: "CONTAINER RUNTIME", Value: "containerd://1.4.3"},
58+
},
59+
},
60+
},
61+
{tx.Row(3), nil},
62+
{tx.Row(99), nil},
2763
}
2864
for _, tt := range tests {
29-
if tt.row.Text != tt.want {
30-
t.Errorf("Row: got = %v, want %v", tt.row.Text, tt.want)
65+
if !reflect.DeepEqual(tt.row, tt.want) {
66+
t.Errorf("Row failed\n got: %v\nwant: %v", tt.row.Text, tt.want)
3167
}
3268
}
3369
}
@@ -37,20 +73,35 @@ func TestRow_Cell(t *testing.T) {
3773
row := tx.Row(0)
3874

3975
tests := []struct {
40-
cell RowCell
41-
want string
76+
cell *RowCell
77+
want *RowCell
4278
}{
43-
{row.Cell(0), "master-1"},
44-
{row.Cell(1), "control-plane,master"},
45-
{row.Cell(2), "Ubuntu 20.04.1 LTS"},
46-
{row.Cell(3), "5.13.0-39-generic"},
47-
{row.Cell(4), "containerd://1.4.1"},
48-
{row.Cell(5), ""},
49-
{row.Cell(99), ""},
79+
{row.Cell(0), &RowCell{
80+
Value: "master-1",
81+
Relation: "NAME",
82+
}},
83+
{row.Cell(1), &RowCell{
84+
Value: "control-plane,master",
85+
Relation: "ROLES",
86+
}},
87+
{row.Cell(2), &RowCell{
88+
Value: "Ubuntu 20.04.1 LTS",
89+
Relation: "OS IMAGE",
90+
}},
91+
{row.Cell(3), &RowCell{
92+
Value: "5.13.0-39-generic",
93+
Relation: "KERNEL-VERSION",
94+
}},
95+
{row.Cell(4), &RowCell{
96+
Value: "containerd://1.4.1",
97+
Relation: "CONTAINER RUNTIME",
98+
}},
99+
{row.Cell(5), nil},
100+
{row.Cell(99), nil},
50101
}
51102
for _, tt := range tests {
52-
if tt.cell.Value != tt.want {
53-
t.Errorf("Cell: got = %v, want %v", tt.cell.Value, tt.want)
103+
if !reflect.DeepEqual(tt.cell, tt.want) {
104+
t.Errorf("Cell failed\n got: %v\nwant: %v", tt.cell, tt.want)
54105
}
55106
}
56107
}
@@ -60,20 +111,35 @@ func TestRow_CellByName(t *testing.T) {
60111
row := tx.Row(0)
61112

62113
tests := []struct {
63-
cell RowCell
64-
want string
114+
cell *RowCell
115+
want *RowCell
65116
}{
66-
{row.CellByName("NAME"), "master-1"},
67-
{row.CellByName("ROLES"), "control-plane,master"},
68-
{row.CellByName("OS IMAGE"), "Ubuntu 20.04.1 LTS"},
69-
{row.CellByName("KERNEL-VERSION"), "5.13.0-39-generic"},
70-
{row.CellByName("CONTAINER RUNTIME"), "containerd://1.4.1"},
71-
{row.CellByName("NOTHING"), ""},
72-
{row.CellByName(runtime.GOOS), ""},
117+
{row.CellByName("NAME"), &RowCell{
118+
Value: "master-1",
119+
Relation: "NAME",
120+
}},
121+
{row.CellByName("ROLES"), &RowCell{
122+
Value: "control-plane,master",
123+
Relation: "ROLES",
124+
}},
125+
{row.CellByName("OS IMAGE"), &RowCell{
126+
Value: "Ubuntu 20.04.1 LTS",
127+
Relation: "OS IMAGE",
128+
}},
129+
{row.CellByName("KERNEL-VERSION"), &RowCell{
130+
Value: "5.13.0-39-generic",
131+
Relation: "KERNEL-VERSION",
132+
}},
133+
{row.CellByName("CONTAINER RUNTIME"), &RowCell{
134+
Value: "containerd://1.4.1",
135+
Relation: "CONTAINER RUNTIME",
136+
}},
137+
{row.CellByName("NOTHING"), nil},
138+
{row.CellByName(runtime.GOOS), nil},
73139
}
74140
for _, tt := range tests {
75-
if tt.cell.Value != tt.want {
76-
t.Errorf("CellByName: got = %v, want %v", tt.cell.Value, tt.want)
141+
if !reflect.DeepEqual(tt.cell, tt.want) {
142+
t.Errorf("CellByName failed\n got: %v\nwant: %v", tt.cell, tt.want)
77143
}
78144
}
79145
}

0 commit comments

Comments
 (0)