-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommon_test.go
70 lines (56 loc) · 1.82 KB
/
common_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
package lasgo
import (
"context"
"reflect"
"testing"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/google/go-cmp/cmp"
"github.com/mitchellh/mapstructure"
)
func TestChunk(t *testing.T) {
testData := []string{"1", "2", "3", "4", "5", "6", "7"}
wanted := [][]string{{"1", "2"}, {"3", "4"}, {"5", "6"}, {"7"}}
got := chunk(testData, 2)
if !reflect.DeepEqual(got, wanted) {
t.Errorf("chunk(%q) == %q, want %q", testData, got, wanted)
}
}
func TestRemoveComment(t *testing.T) {
testData := []string{"#ignore\nDon't Ignore\n#\n # also ignore", "Don't ignore\n#\nBe there"}
wanted := [][]string{{"Don't Ignore"}, {"Don't ignore", "Be there"}}
for i := 0; i < len(testData); i++ {
got := removeComment(testData[i])
if !reflect.DeepEqual(got, wanted[i]) {
t.Errorf("chunk(%q) == %q, want %q", testData[i], got, wanted[i])
}
}
}
func TestStructConvert(t *testing.T) {
type list struct {
Index int `las:"index"`
Item string `las:"item"`
}
testData := []string{"1", "books", "2", "bicycles", "3", "cars", "4", "computers"}
header := []string{"index", "item"}
chnk := chunk(testData, len(header))
expected := []interface{}{
&list{Index: int(1), Item: "books"},
&list{Index: int(2), Item: "bicycles"},
&list{Index: int(3), Item: "cars"},
&list{Index: int(4), Item: "computers"},
}
opts := &DataOptions{ConcreteStruct: list{}, DecoderConfig: &StructorConfig{
DecodeHook: mapstructure.StringToTimeHookFunc(time.RFC3339),
WeaklyTypedInput: true}}
// opts := &DataOptions{ConcreteStruct: list{}}
ctx := context.Background()
actual, err := structConvert(ctx, &chnk, header, opts)
if err != nil {
t.Errorf("Error encountered: %s\n", err)
}
spew.Dump(actual)
if !cmp.Equal(expected, actual) {
t.Errorf("wrong val: expected: %T %v actual: %T %v\n", expected, expected, actual, actual)
}
}