-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrimmer_test.go
More file actions
169 lines (140 loc) · 3.49 KB
/
trimmer_test.go
File metadata and controls
169 lines (140 loc) · 3.49 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package trimmer_test
import (
"testing"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/soundtrackyourbrand/trimmer"
)
func TestMail(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Trimmer Suite")
}
var _ = Describe("trimmer", func() {
Context(".TrimStrings(..)", func() {
It("should trim string fields", func() {
type stringalias string
type test struct {
Field1 string
Field2 *string
Field3 **string
Field4 stringalias
}
s1 := " ptr field "
s2 := " ptr field "
ptr1 := &s1
ptr2 := &s2
t := test{
Field1: " field1 ",
Field2: ptr1,
Field3: &ptr2,
Field4: " alias ",
}
err := TrimStrings(&t)
Expect(err).ShouldNot(HaveOccurred())
Expect(t.Field1).To(Equal("field1"))
Expect(*t.Field2).To(Equal("ptr field"))
Expect(**t.Field3).To(Equal("ptr field"))
Expect(t.Field4).To(Equal(stringalias("alias")))
})
It("should not trim anything to other field types", func() {
type test struct {
Field1 []byte
}
t := test{
Field1: []byte(" byte! "),
}
err := TrimStrings(&t)
Expect(err).ShouldNot(HaveOccurred())
Expect(t.Field1).To(Equal([]byte(" byte! ")))
})
It("should return error if not a pointer", func() {
type test struct{}
t := test{}
err := TrimStrings(t)
Expect(err).To(Equal(ErrInvalidType))
})
It("should return an error if pointer to a non-struct", func() {
t := []string{" a", " b", " c"}
err := TrimStrings(&t)
Expect(err).To(Equal(ErrInvalidType))
})
It("shoud not trim strings with trim tag set to -", func() {
type test struct {
Field1 string `trim:"-"`
Field2 *string `trim:"-"`
}
s := " ptr "
t := test{
Field1: " not trimmed ",
Field2: &s,
}
err := TrimStrings(&t)
Expect(err).ShouldNot(HaveOccurred())
Expect(t.Field1).To(Equal(" not trimmed "))
Expect(*t.Field2).To(Equal(" ptr "))
})
It("should trim string fields in nested objects", func() {
type inner struct {
Field1 string
Field2 string
}
type middle struct {
Field1 string
Field2 string
Inner1 *inner
Inner2 *inner
Inner3 inner
}
type outer struct {
Field string
Middle1 middle
Middle2 middle
}
t := outer{
Field: " first ",
Middle1: middle{
Field1: " field1 ",
},
Middle2: middle{
Field1: " field1 ",
Field2: " field2 ",
Inner1: &inner{
Field1: " field1 ",
Field2: " field2 ",
},
Inner2: &inner{
Field1: " field1 ",
Field2: " field2 ",
},
Inner3: inner{
Field1: " field1 ",
Field2: " field2 ",
},
},
}
err := TrimStrings(&t)
Expect(err).ShouldNot(HaveOccurred())
Expect(t.Field).To(Equal("first"))
Expect(t.Middle1.Field1).To(Equal("field1"))
Expect(t.Middle2.Field1).To(Equal("field1"))
Expect(t.Middle2.Field2).To(Equal("field2"))
Expect(t.Middle2.Inner1.Field1).To(Equal("field1"))
Expect(t.Middle2.Inner1.Field2).To(Equal("field2"))
Expect(t.Middle2.Inner2.Field1).To(Equal("field1"))
Expect(t.Middle2.Inner2.Field2).To(Equal("field2"))
Expect(t.Middle2.Inner3.Field1).To(Equal("field1"))
Expect(t.Middle2.Inner3.Field2).To(Equal("field2"))
})
It("should not fail when there are wierd time types on the struct", func() {
type specialTime struct{ time.Time }
type test struct {
Time specialTime
}
t := test{
Time: specialTime{time.Now()},
}
Expect(TrimStrings(&t)).To(Succeed())
})
})
})