-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhybrid_test.go
244 lines (196 loc) · 6.65 KB
/
hybrid_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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package strconvx
import (
"errors"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHybridCoder_TestMarshalerOnly(t *testing.T) {
apple := &TextMarshalerApple{}
rv := reflect.ValueOf(apple)
sb := createHybridStringConverter(rv)
assert.NotNil(t, sb)
text, err := sb.ToString()
assert.NoError(t, err)
assert.Equal(t, "apple", text)
assert.ErrorIs(t, sb.FromString("red apple"), ErrCannotFromString)
}
func TestHybridCoder_TextUnmarshalerOnly(t *testing.T) {
banana := &TextUnmarshalerBanana{}
rv := reflect.ValueOf(banana)
sb := createHybridStringConverter(rv)
assert.NotNil(t, sb)
text, err := sb.ToString()
assert.ErrorIs(t, err, ErrCannotToString)
assert.Empty(t, text)
err = sb.FromString("yellow banana")
assert.NoError(t, err)
assert.Equal(t, "yellow banana", banana.Content)
}
func TestHybridCoder_TestMarshaler_and_TextUnmarshaler(t *testing.T) {
orange := &TextMarshalerAndUnmarshalerOrange{Content: "orange"}
rv := reflect.ValueOf(orange)
sb := createHybridStringConverter(rv)
assert.NotNil(t, sb)
text, err := sb.ToString()
assert.NoError(t, err)
assert.Equal(t, "orange", text)
err = sb.FromString("red orange")
assert.NoError(t, err)
assert.Equal(t, "red orange", orange.Content)
}
func TestHybridCoder_StringMarshaler_TakesPrecedence(t *testing.T) {
peach := &StringMarshalerAndTextMarshalerPeach{Content: "peach"}
rv := reflect.ValueOf(peach)
sb := createHybridStringConverter(rv)
assert.NotNil(t, sb)
text, err := sb.ToString()
assert.NoError(t, err)
assert.Equal(t, "ToString:peach", text)
err = sb.FromString("red peach")
assert.ErrorIs(t, err, ErrCannotFromString)
}
func TestHybridCoder_StringUnmarshaler_TakesPrecedence(t *testing.T) {
peach := &StringUnmarshalerAndTextUnmarshalerPeach{Content: "peach"}
rv := reflect.ValueOf(peach)
sb := createHybridStringConverter(rv)
assert.NotNil(t, sb)
text, err := sb.ToString()
assert.ErrorIs(t, err, ErrCannotToString)
assert.Empty(t, text)
err = sb.FromString("red peach")
assert.NoError(t, err)
assert.Equal(t, "FromString:red peach", peach.Content)
}
func TestHybridCoder_StringMarshaler_and_TextUnmarshaler(t *testing.T) {
pineapple := &StringMarshalerAndTextUnmarshalerPineapple{Content: "pineapple"}
rv := reflect.ValueOf(pineapple)
sb := createHybridStringConverter(rv)
assert.NotNil(t, sb)
text, err := sb.ToString()
assert.NoError(t, err)
assert.Equal(t, "ToString:pineapple", text)
err = sb.FromString("red pineapple")
assert.NoError(t, err)
assert.Equal(t, "UnmarshalText:red pineapple", pineapple.Content)
}
func TestHybridCoder_MarshalText_Error(t *testing.T) {
watermelon := &TextMarshalerSpoiledWatermelon{}
rv := reflect.ValueOf(watermelon)
sb := createHybridStringConverter(rv)
assert.NotNil(t, sb)
text, err := sb.ToString()
assert.ErrorContains(t, err, "spoiled")
assert.Empty(t, text)
}
func TestHybridCoder_ErrCannotInterface(t *testing.T) {
type mystruct struct {
unexportedName string
}
v := mystruct{unexportedName: "mystruct"}
rv := reflect.ValueOf(v)
sb := createHybridStringConverter(rv.Field(0))
assert.Nil(t, sb)
}
func TestHybridCoder_NilOnNoInterfacesDetected(t *testing.T) {
var zero zeroInterface
rv := reflect.ValueOf(zero)
sb := createHybridStringConverter(rv)
assert.Nil(t, sb)
}
// TextMarshalerApple implements:
// - CanToString - no
// - CanFromString - no
// - encoding.TextMarshaler - yes
// - encoding.TextUnmarshaler - no
type TextMarshalerApple struct{}
func (t *TextMarshalerApple) MarshalText() ([]byte, error) {
return []byte("apple"), nil
}
// TextUnmarshalerBanana implements:
// - CanToString - no
// - CanFromString - no
// - encoding.TextMarshaler - no
// - encoding.TextUnmarshaler - yes
type TextUnmarshalerBanana struct{ Content string }
func (t *TextUnmarshalerBanana) UnmarshalText(text []byte) error {
t.Content = string(text)
return nil
}
// TextMarshalerAndUnmarshalerOrange implements:
// - CanToString - no
// - CanFromString - no
// - encoding.TextMarshaler - yes
// - encoding.TextUnmarshaler - yes
type TextMarshalerAndUnmarshalerOrange struct{ Content string }
func (t *TextMarshalerAndUnmarshalerOrange) MarshalText() ([]byte, error) {
return []byte(t.Content), nil
}
func (t *TextMarshalerAndUnmarshalerOrange) UnmarshalText(text []byte) error {
t.Content = string(text)
return nil
}
// StringMarshalerAndTextMarshalerPeach implements:
// - CanToString - yes
// - CanFromString - no
// - encoding.TextMarshaler - yes
// - encoding.TextUnmarshaler - no
type StringMarshalerAndTextMarshalerPeach struct{ Content string }
func (s *StringMarshalerAndTextMarshalerPeach) ToString() (string, error) {
return "ToString:" + s.Content, nil
}
func (s *StringMarshalerAndTextMarshalerPeach) MarshalText() ([]byte, error) {
return []byte("MarshalText:" + s.Content), nil
}
// StringUnmarshalerAndTextUnmarshalerPeach implements:
// - CanToString - no
// - CanFromString - yes
// - encoding.TextMarshaler - no
// - encoding.TextUnmarshaler - yes
type StringUnmarshalerAndTextUnmarshalerPeach struct{ Content string }
func (s *StringUnmarshalerAndTextUnmarshalerPeach) FromString(text string) error {
s.Content = "FromString:" + text
return nil
}
func (s *StringUnmarshalerAndTextUnmarshalerPeach) UnmarshalText(text []byte) error {
s.Content = "UnmarshalText:" + string(text)
return nil
}
// StringMarshalerAndTextUnmarshalerPineapple implements:
// - CanToString - yes
// - CanFromString - no
// - encoding.TextMarshaler - no
// - encoding.TextUnmarshaler - yes
type StringMarshalerAndTextUnmarshalerPineapple struct{ Content string }
func (s *StringMarshalerAndTextUnmarshalerPineapple) ToString() (string, error) {
return "ToString:" + s.Content, nil
}
func (s *StringMarshalerAndTextUnmarshalerPineapple) UnmarshalText(text []byte) error {
s.Content = "UnmarshalText:" + string(text)
return nil
}
// StringMarshalerAndStringUnmarshalerCherry implements:
// - CanToString - yes
// - CanFromString - yes
// - encoding.TextMarshaler - no
// - encoding.TextUnmarshaler - no
type StringMarshalerAndStringUnmarshalerCherry struct {
Content string
}
func (s *StringMarshalerAndStringUnmarshalerCherry) FromString(text string) error {
s.Content = "FromString:" + text
return nil
}
func (s *StringMarshalerAndStringUnmarshalerCherry) ToString() (string, error) {
return "ToString:" + s.Content, nil
}
// TextMarshalerSpoiledWatermelon implements:
// - CanToString - no
// - CanFromString - no
// - encoding.TextMarshaler - yes (but returns error)
// - encoding.TextUnmarshaler - no
type TextMarshalerSpoiledWatermelon struct{}
func (t *TextMarshalerSpoiledWatermelon) MarshalText() ([]byte, error) {
return nil, errors.New("spoiled")
}
type zeroInterface struct{}