-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnullify_test.go
More file actions
173 lines (143 loc) · 4.7 KB
/
nullify_test.go
File metadata and controls
173 lines (143 loc) · 4.7 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
170
171
172
173
package nullify
import (
"github.com/stretchr/testify/assert"
"reflect"
"testing"
"time"
"unsafe"
)
func TestNullify_Nil(t *testing.T) {
// Arrange
var inf interface{} // the nil interface
// Act
p := Nullify(inf)
// Assert
assert.Nil(t, p)
}
func TestNullify_Struct(t *testing.T) {
// Arrange
type Person struct {
Name string `json:"name"`
}
person := Person{}
// Act
p := Nullify(person)
// Assert
assert.Equal(t, reflect.Pointer, reflect.TypeOf(p).Kind())
assert.Equal(t, reflect.Struct, reflect.TypeOf(p).Elem().Kind())
assert.Equal(t, reflect.Pointer, reflect.TypeOf(p).Elem().Field(0).Type.Kind())
assert.Equal(t, reflect.String, reflect.TypeOf(p).Elem().Field(0).Type.Elem().Kind())
assert.Equal(t, reflect.StructTag(`json:"name"`), reflect.TypeOf(p).Elem().Field(0).Tag)
}
func TestNullify_Array(t *testing.T) {
// Arrange
var input [1]string
input[0] = "test"
// Act
i := Nullify(input)
// Assert
assert.Equal(t, reflect.Pointer, reflect.TypeOf(i).Kind())
assert.Equal(t, reflect.Array, reflect.TypeOf(i).Elem().Kind())
assert.Equal(t, reflect.Pointer, reflect.TypeOf(i).Elem().Elem().Kind())
assert.Equal(t, reflect.String, reflect.TypeOf(i).Elem().Elem().Elem().Kind())
}
func TestNullify_Slice(t *testing.T) {
// Arrange
input := []string{"1"}
// Act
i := Nullify(input)
// Assert
assert.Equal(t, reflect.Pointer, reflect.TypeOf(i).Kind())
assert.Equal(t, reflect.Slice, reflect.TypeOf(i).Elem().Kind())
assert.Equal(t, reflect.Pointer, reflect.TypeOf(i).Elem().Elem().Kind())
assert.Equal(t, reflect.String, reflect.TypeOf(i).Elem().Elem().Elem().Kind())
}
func TestNullify_Map(t *testing.T) {
// Arrange
input := map[string]int{}
// Act
i := Nullify(input)
// Assert
assert.Equal(t, reflect.Pointer, reflect.TypeOf(i).Kind())
assert.Equal(t, reflect.Map, reflect.TypeOf(i).Elem().Kind())
assert.Equal(t, reflect.Pointer, reflect.TypeOf(i).Elem().Key().Kind())
assert.Equal(t, reflect.String, reflect.TypeOf(i).Elem().Key().Elem().Kind())
assert.Equal(t, reflect.Pointer, reflect.TypeOf(i).Elem().Elem().Kind())
assert.Equal(t, reflect.Int, reflect.TypeOf(i).Elem().Elem().Elem().Kind())
}
func TestNullify_Primitive(t *testing.T) {
tests := map[string]struct {
Input any
Output reflect.Kind
}{
"Bool": {Input: true, Output: reflect.Bool},
"Int": {Input: 1, Output: reflect.Int},
"Int8": {Input: int8(1), Output: reflect.Int8},
"Int16": {Input: int16(2), Output: reflect.Int16},
"Int32": {Input: int32(3), Output: reflect.Int32},
"Int64": {Input: int64(4), Output: reflect.Int64},
"Uint": {Input: uint(5), Output: reflect.Uint},
"Uint8": {Input: uint8(6), Output: reflect.Uint8},
"Uint16": {Input: uint16(7), Output: reflect.Uint16},
"Uint32": {Input: uint32(8), Output: reflect.Uint32},
"Uint64": {Input: uint64(9), Output: reflect.Uint64},
"Float32": {Input: float32(10.0), Output: reflect.Float32},
"Float64": {Input: float64(11.0), Output: reflect.Float64},
"Complex64": {Input: complex64(12.0), Output: reflect.Complex64},
"Complex128": {Input: complex128(13.0), Output: reflect.Complex128},
"String": {Input: "String", Output: reflect.String},
}
for name, testData := range tests {
testData := testData
t.Run(name, func(t *testing.T) {
// Arrange
input := testData.Input
// Act
output := Nullify(input)
// Assert
assert.Equal(t, reflect.Pointer, reflect.TypeOf(output).Kind())
assert.Equal(t, testData.Output, reflect.TypeOf(output).Elem().Kind())
})
}
}
func TestNullify_Pointer(t *testing.T) {
// Arrange
var input *string
// Act
i := Nullify(input)
// Assert
assert.Equal(t, reflect.Pointer, reflect.TypeOf(i).Kind())
assert.Equal(t, reflect.String, reflect.TypeOf(i).Elem().Kind())
}
func TestNullify_Time(t *testing.T) {
// Arrange
var input time.Time
// Act
i := Nullify(input, NullifyMarshalJson{Value: false}, NullifyUnmarshalJson{Value: false})
// Assert
assert.Equal(t, reflect.Pointer, reflect.TypeOf(i).Kind())
assert.Equal(t, reflect.TypeOf(input), reflect.TypeOf(i).Elem())
}
func TestNullify_Default(t *testing.T) {
str := "test"
tests := map[string]struct {
Input any
Kind reflect.Kind
}{
"Chan": {Input: make(chan int), Kind: reflect.Chan},
"UnsafePointer": {Input: unsafe.Pointer(&str), Kind: reflect.UnsafePointer},
"Func": {Input: func() {}, Kind: reflect.Func},
}
for name, testData := range tests {
testData := testData
t.Run(name, func(t *testing.T) {
// Arrange
input := testData.Input
// Act
output := Nullify(input)
// Assert
assert.Equal(t, reflect.Pointer, reflect.TypeOf(output).Kind())
assert.Equal(t, testData.Kind, reflect.TypeOf(output).Elem().Kind())
})
}
}