-
Notifications
You must be signed in to change notification settings - Fork 1
/
valley_test.go
77 lines (63 loc) · 2.3 KB
/
valley_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
package valley
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestContext_Clone(t *testing.T) {
t.Run("should return a copy of the given context", func(t *testing.T) {
context := Context{}
cloned := context.Clone()
cloned.TypeName = "test"
assert.NotEqual(t, context, cloned)
})
}
func TestGetFieldAliasFromTag(t *testing.T) {
t.Run("should return the field name if the tag is empty", func(t *testing.T) {
alias, err := GetFieldAliasFromTag("testField", "valley", "")
require.NoError(t, err)
assert.Equal(t, "testField", alias)
})
t.Run("should error if the struct tag is invalid", func(t *testing.T) {
_, err := GetFieldAliasFromTag("testField", "valley", "this is not a valid tag")
assert.Error(t, err)
})
t.Run("should return the field name if there is no matching tag", func(t *testing.T) {
alias, err := GetFieldAliasFromTag("testField", "valley", `json:"test"`)
require.NoError(t, err)
assert.Equal(t, "testField", alias)
})
t.Run("should return the field name if there is an empty matching tag", func(t *testing.T) {
alias, err := GetFieldAliasFromTag("testField", "valley", `valley:""`)
require.NoError(t, err)
assert.Equal(t, "testField", alias)
})
t.Run("should return the alias if there is one", func(t *testing.T) {
alias, err := GetFieldAliasFromTag("testField", "valley", `valley:"test_field"`)
require.NoError(t, err)
assert.Equal(t, "test_field", alias)
})
t.Run("should return only the section of the tag's value preceding the first comma", func(t *testing.T) {
alias, err := GetFieldAliasFromTag("testField", "json", `json:"test_field,omitempty"`)
require.NoError(t, err)
assert.Equal(t, "test_field", alias)
})
t.Run("should remove any excess space from an alias", func(t *testing.T) {
alias, err := GetFieldAliasFromTag("testField", "valley", `valley:" test_field "`)
require.NoError(t, err)
assert.Equal(t, "test_field", alias)
})
}
func TestTimeMustParse(t *testing.T) {
t.Run("should not panic if the given error is nil", func(t *testing.T) {
assert.NotPanics(t, func() {
TimeMustParse(time.Now(), nil)
})
})
t.Run("should panic if the given error is not nil", func(t *testing.T) {
assert.Panics(t, func() {
TimeMustParse(time.Parse("2006-01-02", "hello world"))
})
})
}