-
Notifications
You must be signed in to change notification settings - Fork 1
/
tag_test.go
136 lines (111 loc) · 3.74 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package structmapper
import (
"errors"
"testing"
"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestOptionTagName(t *testing.T) {
t.Run("OK", func(t *testing.T) {
sm, err := NewMapper(OptionTagName("test"))
require.NoError(t, err)
require.NotNil(t, sm)
// Check if the supplied tag name was set
require.EqualValues(t, "test", sm.tagName)
})
t.Run("EmptyTag", func(t *testing.T) {
// Try setting an empty tag name
sm, err := NewMapper(OptionTagName(""))
require.Error(t, err)
require.Nil(t, sm)
require.IsType(t, &multierror.Error{}, err)
multiErr := err.(*multierror.Error)
assert.Len(t, multiErr.WrappedErrors(), 1)
assert.EqualError(t, multiErr.WrappedErrors()[0], ErrTagNameEmpty.Error())
})
}
func TestParseTag(t *testing.T) {
t.Run("Dash", func(t *testing.T) {
// Check if the special-case ignore-me tag ("-") gives the correct result
name, omitEmpty, err := parseTag("-")
assert.EqualValues(t, "-", name)
assert.EqualValues(t, false, omitEmpty)
assert.NoError(t, err)
})
t.Run("OmitEmptyNoTagName", func(t *testing.T) {
// Check if ",omitEmpty" alone works
name, omitEmpty, err := parseTag(",omitempty")
assert.EqualValues(t, "", name)
assert.EqualValues(t, true, omitEmpty)
assert.NoError(t, err)
})
t.Run("OmitEmpty", func(t *testing.T) {
// Check if "name,omitEmpty" returns the correct tag name
name, omitEmpty, err := parseTag("test,omitempty")
assert.EqualValues(t, "test", name)
assert.EqualValues(t, true, omitEmpty)
assert.NoError(t, err)
})
t.Run("Puncation", func(t *testing.T) {
// Check if a punctation inside the tag name gives an error
name, omitEmpty, err := parseTag("test.,omitempty")
assert.EqualValues(t, "test.", name)
assert.EqualValues(t, true, omitEmpty)
assert.Error(t, err)
require.IsType(t, &InvalidTag{}, err)
invalidTagErr := err.(*InvalidTag)
assert.EqualValues(t, "test.,omitempty", invalidTagErr.Tag())
})
t.Run("Whitespace", func(t *testing.T) {
// Check if whitespace inside the tag name gives an error
name, omitEmpty, err := parseTag("test ,omitempty")
assert.EqualValues(t, "test ", name)
assert.EqualValues(t, true, omitEmpty)
assert.Error(t, err)
require.IsType(t, &InvalidTag{}, err)
invalidTagErr := err.(*InvalidTag)
assert.EqualValues(t, "test ,omitempty", invalidTagErr.Tag())
})
t.Run("Underscores", func(t *testing.T) {
// Check if underscores are allowed
name, omitEmpty, err := parseTag("test_tag")
assert.NoError(t, err)
assert.EqualValues(t, "test_tag", name)
assert.EqualValues(t, false, omitEmpty)
})
}
func TestIsInvalidTag(t *testing.T) {
// Test if IsInvalidTag works correctly for an invalid tag
err := newErrorInvalidTag("test")
require.NotNil(t, err)
t.Run("Yes", func(t *testing.T) {
it, ok := IsInvalidTag(err)
require.EqualValues(t, true, ok, "IsInvalidTag should return true")
require.NotNil(t, it)
require.EqualValues(t, err, it)
})
t.Run("No", func(t *testing.T) {
// Test if IsInvalidTag works correctly for non-invalid tag error
err = errors.New("test")
it, ok := IsInvalidTag(err)
require.EqualValues(t, false, ok, "IsInvalidTag should return false")
require.Nil(t, it)
})
}
func TestInvalidTag_Tag(t *testing.T) {
// Check if the tag is preserved
err := newErrorInvalidTag("test tag")
require.NotNil(t, err)
it, ok := IsInvalidTag(err)
require.EqualValues(t, true, ok, "IsInvalidTag should return true")
require.NotNil(t, it)
require.EqualValues(t, err, it)
require.EqualValues(t, "test tag", it.Tag())
}
func TestInvalidTag_Error(t *testing.T) {
// Check if the error message is correct
err := newErrorInvalidTag("test tag")
require.NotNil(t, err)
require.EqualError(t, err, "Invalid tag: 'test tag'")
}