-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag_test.go
61 lines (50 loc) · 1.7 KB
/
tag_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
package csvlib
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_parseTag(t *testing.T) {
type Item struct {
Col0 bool
Col1 int `csv:"col1,optional"`
Col2 *int `csv:"col2,omitempty"`
Col3 string `csv:"-"`
Col4 string `csv:""`
Col5 string `csv:",unsupported"`
Col6 InlineColumn[int] `csv:"col6,inline,prefix=xyz"`
col7 int32 `csv:"col7"` // nolint: unused
}
structType := reflect.TypeOf(Item{})
col0, _ := structType.FieldByName("Col0")
tag0, err := parseTag(DefaultTagName, col0)
assert.Nil(t, err)
assert.Nil(t, tag0)
col1, _ := structType.FieldByName("Col1")
tag1, err := parseTag(DefaultTagName, col1)
assert.Nil(t, err)
assert.True(t, tag1.name == "col1" && tag1.optional)
col2, _ := structType.FieldByName("Col2")
tag2, err := parseTag(DefaultTagName, col2)
assert.Nil(t, err)
assert.True(t, tag2.name == "col2" && tag2.omitEmpty)
col3, _ := structType.FieldByName("Col3")
tag3, err := parseTag(DefaultTagName, col3)
assert.Nil(t, err)
assert.True(t, tag3.ignored)
col4, _ := structType.FieldByName("Col4")
tag4, err := parseTag(DefaultTagName, col4)
assert.Nil(t, err)
assert.True(t, tag4.name == "Col4" && tag4.empty)
col5, _ := structType.FieldByName("Col5")
tag5, err := parseTag(DefaultTagName, col5)
assert.Nil(t, err)
assert.True(t, tag5.name == "Col5")
col6, _ := structType.FieldByName("Col6")
tag6, err := parseTag(DefaultTagName, col6)
assert.Nil(t, err)
assert.True(t, tag6.name == "col6" && tag6.inline && tag6.prefix == "xyz")
col7, _ := structType.FieldByName("col7")
_, err = parseTag(DefaultTagName, col7)
assert.ErrorIs(t, err, ErrTagOptionInvalid)
}