forked from regen-network/gocuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatable.go
104 lines (83 loc) · 2.36 KB
/
datatable.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package gocuke
import (
"math/big"
"reflect"
"github.com/cockroachdb/apd/v3"
messages "github.com/cucumber/messages/go/v22"
)
// DataTable wraps a data table step argument
type DataTable struct {
t TestingT
table *messages.PickleTable
}
// NumRows returns the number of rows in the data table.
func (d DataTable) NumRows() int {
return len(d.table.Rows)
}
// NumCols returns the number of columns in the data table.
func (d DataTable) NumCols() int {
if len(d.table.Rows) == 0 {
d.t.Fatalf("no table rows")
}
return len(d.table.Rows[0].Cells)
}
// Cell returns the cell at the provided 0-based row and col offset.
func (d DataTable) Cell(row, col int) *Cell {
if row >= len(d.table.Rows) {
d.t.Fatalf("table row %d out of range", row)
}
r := d.table.Rows[row]
if col >= len(r.Cells) {
d.t.Fatalf("table column %d out of range", col)
}
return &Cell{
t: d.t,
value: r.Cells[col].Value,
}
}
// Cell represents a data table cell.
type Cell struct {
t TestingT
value string
}
// String returns the cell value as a string.
func (c Cell) String() string {
return c.value
}
// Int64 returns the cell as an int64.
func (c Cell) Int64() int64 {
return toInt64(c.t, c.value)
}
// BigInt returns the cell as a *big.Int.
func (c Cell) BigInt() *big.Int {
return toBigInt(c.t, c.value)
}
// Decimal returns the cell value as an *apd.Decimal.
func (c Cell) Decimal() *apd.Decimal {
return toDecimal(c.t, c.value)
}
// HeaderTable returns the data table as a header table which is a wrapper
// around the table which assumes that the first row is the table header.
func (d DataTable) HeaderTable() *HeaderTable {
headers := map[string]int{}
for i := 0; i < d.NumCols(); i++ {
headers[d.Cell(0, i).String()] = i
}
return &HeaderTable{headers: headers, DataTable: d}
}
// HeaderTable is a wrapper around a table which assumes that the first row is \
// the table header.
type HeaderTable struct {
DataTable
headers map[string]int
}
// Get returns the cell at the provided row offset (skipping the header row)
// and column name (as indicated in the header).
func (h *HeaderTable) Get(row int, col string) *Cell {
return h.DataTable.Cell(row+1, h.headers[col])
}
// NumRows returns the number of rows in the table (excluding the header row).
func (h *HeaderTable) NumRows() int {
return h.DataTable.NumRows() - 1
}
var dataTableType = reflect.TypeOf(DataTable{})