forked from ggicci/httpin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder_test.go
More file actions
64 lines (53 loc) · 2.27 KB
/
decoder_test.go
File metadata and controls
64 lines (53 loc) · 2.27 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
package httpin
import (
"reflect"
"strconv"
"strings"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
// decodeCustomBool additionally parses "yes/no".
func decodeCustomBool(value string) (interface{}, error) {
sdata := strings.ToLower(value)
if sdata == "yes" {
return true, nil
}
if sdata == "no" {
return false, nil
}
return strconv.ParseBool(sdata)
}
func TestDecoders(t *testing.T) {
boolType := reflect.TypeOf(bool(true))
Convey("Register nil decoder", t, func() {
So(func() { RegisterTypeDecoder(boolType, nil) }, ShouldPanic)
So(func() { RegisterNamedDecoder("myBool", nil) }, ShouldPanic)
})
delete(decoders, boolType) // remove the custom decoder
delete(namedDecoders, "mybool") // remove the custom decoder
var invalidDecoder = func(string) error {
return nil
}
Convey("Register invalid decoder", t, func() {
So(func() { RegisterTypeDecoder(boolType, invalidDecoder) }, ShouldPanic)
So(func() { RegisterNamedDecoder("myBool", invalidDecoder) }, ShouldPanic)
})
delete(decoders, boolType) // remove the custom decoder
delete(namedDecoders, "mybool") // remove the custom decoder
Convey("Register duplicate decoder", t, func() {
So(func() { RegisterTypeDecoder(boolType, ValueTypeDecoderFunc(decodeCustomBool)) }, ShouldNotPanic)
So(func() { RegisterTypeDecoder(boolType, ValueTypeDecoderFunc(decodeCustomBool)) }, ShouldPanic)
So(func() { RegisterNamedDecoder("mybool", ValueTypeDecoderFunc(decodeCustomBool)) }, ShouldNotPanic)
So(func() { RegisterNamedDecoder("mybool", ValueTypeDecoderFunc(decodeCustomBool)) }, ShouldPanic)
})
delete(decoders, boolType) // remove the custom decoder
delete(namedDecoders, "mybool") // remove the custom decoder
Convey("Replace a decoder", t, func() {
So(func() { ReplaceTypeDecoder(boolType, ValueTypeDecoderFunc(decodeCustomBool)) }, ShouldNotPanic)
So(func() { ReplaceTypeDecoder(boolType, ValueTypeDecoderFunc(decodeCustomBool)) }, ShouldNotPanic)
So(func() { ReplaceNamedDecoder("mybool", ValueTypeDecoderFunc(decodeCustomBool)) }, ShouldNotPanic)
So(func() { ReplaceNamedDecoder("mybool", ValueTypeDecoderFunc(decodeCustomBool)) }, ShouldNotPanic)
})
delete(decoders, boolType) // remove the custom decoder
delete(namedDecoders, "mybool") // remove the custom decoder
}