-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvalues_test.go
102 lines (82 loc) · 1.99 KB
/
values_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package sheets
import (
"testing"
)
type testRow struct {
Name string
Other string `sheets:"-"`
Age int
}
func BenchmarkDecodeValueRange(b *testing.B) {
lenValues := 420
values := make([][]interface{}, lenValues)
for i := 0; i < lenValues; i++ {
values[i] = []interface{}{"makis", 27}
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var result []testRow
if err := DecodeValueRange(&result, ValueRange{Values: values}); err != nil {
b.Fatal(err)
}
}
}
func TestDecodeValueRange(t *testing.T) {
var singleResult testRow
DecodeValueRange(&singleResult, ValueRange{
Values: [][]interface{}{
{
"makis",
27,
},
},
})
// t.Logf("%#+v", singleResult)
// empty value ranges (should not panic).
DecodeValueRange(&singleResult)
// empty value range (should not panic).
DecodeValueRange(&singleResult, []ValueRange{{Values: [][]interface{}{}}}...)
}
type testRowFieldDecoder struct {
Name string
}
func (t *testRowFieldDecoder) DecodeField(h *Header, value interface{}) error {
t.Name = value.(string) + " custom value"
return nil
}
func TestFieldDecoder(t *testing.T) {
var names = []string{"makis", "giwrgos", "efi"}
expectedNames := make([]string, len(names))
values := make([][]interface{}, len(names))
for i, name := range names {
expectedNames[i] = name + " custom value"
values[i] = []interface{}{name}
}
// test with ptr
var ptrRows []*testRowFieldDecoder
err := DecodeValueRange(&ptrRows, ValueRange{
Values: values,
})
if err != nil {
t.Fatal(err)
}
for i, row := range ptrRows {
if expected, got := expectedNames[i], row.Name; expected != got {
t.Fatalf("[%d] expected %s but got %s", i, expected, got)
}
}
// test without ptr.
var rows []testRowFieldDecoder
err = DecodeValueRange(&rows, ValueRange{
Values: values,
})
if err != nil {
t.Fatal(err)
}
for i, row := range rows {
if expected, got := expectedNames[i], row.Name; expected != got {
t.Fatalf("[%d] expected %s but got %s", i, expected, got)
}
}
}