This repository has been archived by the owner on Sep 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathreflector_test.go
136 lines (126 loc) · 3.23 KB
/
reflector_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 reflector_test
import (
. "."
"testing"
)
type T struct {
Int int
Uint8 uint8
Uintptr uintptr
Float32 float32 `json:"f32"`
String string
Pstring *string
foo int
}
func TestStructToMapBad1(t *testing.T) {
defer func() {
r := recover()
e, ok := r.(error)
if !ok || e.Error() != "StructToMap: expected pointer to struct as first argument, got struct" {
t.Error(r)
}
}()
m := make(map[string]interface{})
StructToMap(T{}, m, "")
t.Fatal("should panic")
}
func TestStructToMapBad2(t *testing.T) {
defer func() {
r := recover()
e, ok := r.(error)
if !ok || e.Error() != "StructToMap: expected pointer to struct as first argument, got pointer to int" {
t.Error(r)
}
}()
var i int
m := make(map[string]interface{})
StructToMap(&i, m, "")
t.Fatal("should panic")
}
func TestMapToStructBad1(t *testing.T) {
defer func() {
r := recover()
e, ok := r.(error)
if !ok || e.Error() != "MapToStruct: expected pointer to struct as second argument, got struct" {
t.Error(r)
}
}()
MapToStruct(map[string]interface{}{}, T{}, NoConvert, "")
t.Fatal("should panic")
}
func TestMapToStructBad2(t *testing.T) {
defer func() {
r := recover()
e, ok := r.(error)
if !ok || e.Error() != "MapToStruct: expected pointer to struct as second argument, got pointer to int" {
t.Error(r)
}
}()
var i int
MapToStruct(map[string]interface{}{}, &i, NoConvert, "")
t.Fatal("should panic")
}
func TestMapToStructWrongType(t *testing.T) {
defer func() {
r := recover()
e, ok := r.(error)
if !ok || e.Error() != "MapToStruct, field Uint8: interface conversion: interface is int, not uint8" {
t.Error(r)
}
}()
type T struct {
Uint8 uint8
}
var s T
m := map[string]interface{}{"Uint8": 8}
MapToStruct(m, &s, NoConvert, "")
t.Fatal("should panic")
}
func TestMapsToStructsBad1(t *testing.T) {
defer func() {
r := recover()
e, ok := r.(error)
if !ok || e.Error() != "MapsToStructs: expected pointer to slice of structs as second argument, got slice" {
t.Error(r)
}
}()
var s []T
m := map[string]interface{}{
"Int": 42, "Uint8": uint8(8), "Uintptr": uintptr(0xbadcafe),
"f32": float32(3.14), "String": "str", "foo": 13,
}
MapsToStructs([]map[string]interface{}{m}, s, NoConvert, "json")
t.Fatal("should panic")
}
func TestMapsToStructsBad2(t *testing.T) {
defer func() {
r := recover()
e, ok := r.(error)
if !ok || e.Error() != "MapsToStructs: expected pointer to slice of structs as second argument, got pointer to int" {
t.Error(r)
}
}()
var s *int
m := map[string]interface{}{
"Int": 42, "Uint8": uint8(8), "Uintptr": uintptr(0xbadcafe),
"f32": float32(3.14), "String": "str", "foo": 13,
}
MapsToStructs([]map[string]interface{}{m}, s, NoConvert, "json")
t.Fatal("should panic")
}
func TestMapsToStructsBad3(t *testing.T) {
defer func() {
r := recover()
e, ok := r.(error)
if !ok || e.Error() != "MapsToStructs: expected pointer to slice of structs as second argument, got pointer to slice of int" {
t.Error(r)
}
}()
var s *[]int
m := map[string]interface{}{
"Int": 42, "Uint8": uint8(8), "Uintptr": uintptr(0xbadcafe),
"f32": float32(3.14), "String": "str", "foo": 13,
}
MapsToStructs([]map[string]interface{}{m}, s, NoConvert, "json")
t.Fatal("should panic")
}