Skip to content

Commit 6f50a69

Browse files
feat: implement getter functions for table
1 parent 90d97ef commit 6f50a69

File tree

2 files changed

+104
-2
lines changed

2 files changed

+104
-2
lines changed

table.go

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

28+
func (t *Table) Row(index int) Row {
29+
if len(t.Rows) > index {
30+
return t.Rows[index]
31+
}
32+
return Row{}
33+
}
34+
2835
func (h *Header) append(cell HeaderCell) {
2936
h.Cells = append(h.Cells, cell)
3037
}
3138

32-
func (h *Row) append(cell RowCell) {
33-
h.Cells = append(h.Cells, cell)
39+
func (r *Row) append(cell RowCell) {
40+
r.Cells = append(r.Cells, cell)
41+
}
42+
43+
func (r *Row) Cell(index int) RowCell {
44+
if len(r.Cells) > index {
45+
return r.Cells[index]
46+
}
47+
return RowCell{}
48+
}
49+
50+
func (r *Row) CellByName(s string) RowCell {
51+
for _, cell := range r.Cells {
52+
if cell.Relation == s {
53+
return cell
54+
}
55+
}
56+
return RowCell{}
3457
}

table_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package table
2+
3+
import (
4+
"runtime"
5+
"testing"
6+
)
7+
8+
const input = `
9+
NAME ROLES OS IMAGE KERNEL-VERSION CONTAINER RUNTIME
10+
master-1 control-plane,master Ubuntu 20.04.1 LTS 5.13.0-39-generic containerd://1.4.1
11+
monitor-1 monitor Ubuntu 20.04.2 LTS 5.13.0-39-generic containerd://1.4.2
12+
worker-1 worker Ubuntu 20.04.3 LTS 5.13.0-39-generic containerd://1.4.3
13+
`
14+
15+
func TestTable_Row(t *testing.T) {
16+
tx := ReadAll(input)
17+
18+
tests := []struct {
19+
row Row
20+
want string
21+
}{
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), ""},
27+
}
28+
for _, tt := range tests {
29+
if tt.row.Text != tt.want {
30+
t.Errorf("Row: got = %v, want %v", tt.row.Text, tt.want)
31+
}
32+
}
33+
}
34+
35+
func TestRow_Cell(t *testing.T) {
36+
tx := ReadAll(input)
37+
row := tx.Row(0)
38+
39+
tests := []struct {
40+
cell RowCell
41+
want string
42+
}{
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), ""},
50+
}
51+
for _, tt := range tests {
52+
if tt.cell.Value != tt.want {
53+
t.Errorf("Cell: got = %v, want %v", tt.cell.Value, tt.want)
54+
}
55+
}
56+
}
57+
58+
func TestRow_CellByName(t *testing.T) {
59+
tx := ReadAll(input)
60+
row := tx.Row(0)
61+
62+
tests := []struct {
63+
cell RowCell
64+
want string
65+
}{
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), ""},
73+
}
74+
for _, tt := range tests {
75+
if tt.cell.Value != tt.want {
76+
t.Errorf("CellByName: got = %v, want %v", tt.cell.Value, tt.want)
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)