Debian Codegraph @

autorest/to/convert_test.go

raw
1 package to
2
3 // Copyright 2017 Microsoft Corporation
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 import (
18 "reflect"
19 "testing"
20 )
21
22 func TestString(t *testing.T) {
23 v := ""
24 if String(&v) != v {
25 t.Fatalf("to: String failed to return the correct string -- expected %v, received %v",
26 v, String(&v))
27 }
28 }
29
30 func TestStringHandlesNil(t *testing.T) {
31 if String(nil) != "" {
32 t.Fatalf("to: String failed to correctly convert nil -- expected %v, received %v",
33 "", String(nil))
34 }
35 }
36
37 func TestStringPtr(t *testing.T) {
38 v := ""
39 if *StringPtr(v) != v {
40 t.Fatalf("to: StringPtr failed to return the correct string -- expected %v, received %v",
41 v, *StringPtr(v))
42 }
43 }
44
45 func TestStringSlice(t *testing.T) {
46 v := []string{}
47 if out := StringSlice(&v); !reflect.DeepEqual(out, v) {
48 t.Fatalf("to: StringSlice failed to return the correct slice -- expected %v, received %v",
49 v, out)
50 }
51 }
52
53 func TestStringSliceHandlesNil(t *testing.T) {
54 if out := StringSlice(nil); out != nil {
55 t.Fatalf("to: StringSlice failed to correctly convert nil -- expected %v, received %v",
56 nil, out)
57 }
58 }
59
60 func TestStringSlicePtr(t *testing.T) {
61 v := []string{"a", "b"}
62 if out := StringSlicePtr(v); !reflect.DeepEqual(*out, v) {
63 t.Fatalf("to: StringSlicePtr failed to return the correct slice -- expected %v, received %v",
64 v, *out)
65 }
66 }
67
68 func TestStringMap(t *testing.T) {
69 msp := map[string]*string{"foo": StringPtr("foo"), "bar": StringPtr("bar"), "baz": StringPtr("baz")}
70 for k, v := range StringMap(msp) {
71 if *msp[k] != v {
72 t.Fatalf("to: StringMap incorrectly converted an entry -- expected [%s]%v, received[%s]%v",
73 k, v, k, *msp[k])
74 }
75 }
76 }
77
78 func TestStringMapHandlesNil(t *testing.T) {
79 msp := map[string]*string{"foo": StringPtr("foo"), "bar": nil, "baz": StringPtr("baz")}
80 for k, v := range StringMap(msp) {
81 if msp[k] == nil && v != "" {
82 t.Fatalf("to: StringMap incorrectly converted a nil entry -- expected [%s]%v, received[%s]%v",
83 k, v, k, *msp[k])
84 }
85 }
86 }
87
88 func TestStringMapPtr(t *testing.T) {
89 ms := map[string]string{"foo": "foo", "bar": "bar", "baz": "baz"}
90 for k, msp := range *StringMapPtr(ms) {
91 if ms[k] != *msp {
92 t.Fatalf("to: StringMapPtr incorrectly converted an entry -- expected [%s]%v, received[%s]%v",
93 k, ms[k], k, *msp)
94 }
95 }
96 }
97
98 func TestBool(t *testing.T) {
99 v := false
100 if Bool(&v) != v {
101 t.Fatalf("to: Bool failed to return the correct string -- expected %v, received %v",
102 v, Bool(&v))
103 }
104 }
105
106 func TestBoolHandlesNil(t *testing.T) {
107 if Bool(nil) != false {
108 t.Fatalf("to: Bool failed to correctly convert nil -- expected %v, received %v",
109 false, Bool(nil))
110 }
111 }
112
113 func TestBoolPtr(t *testing.T) {
114 v := false
115 if *BoolPtr(v) != v {
116 t.Fatalf("to: BoolPtr failed to return the correct string -- expected %v, received %v",
117 v, *BoolPtr(v))
118 }
119 }
120
121 func TestInt(t *testing.T) {
122 v := 0
123 if Int(&v) != v {
124 t.Fatalf("to: Int failed to return the correct string -- expected %v, received %v",
125 v, Int(&v))
126 }
127 }
128
129 func TestIntHandlesNil(t *testing.T) {
130 if Int(nil) != 0 {
131 t.Fatalf("to: Int failed to correctly convert nil -- expected %v, received %v",
132 0, Int(nil))
133 }
134 }
135
136 func TestIntPtr(t *testing.T) {
137 v := 0
138 if *IntPtr(v) != v {
139 t.Fatalf("to: IntPtr failed to return the correct string -- expected %v, received %v",
140 v, *IntPtr(v))
141 }
142 }
143
144 func TestInt32(t *testing.T) {
145 v := int32(0)
146 if Int32(&v) != v {
147 t.Fatalf("to: Int32 failed to return the correct string -- expected %v, received %v",
148 v, Int32(&v))
149 }
150 }
151
152 func TestInt32HandlesNil(t *testing.T) {
153 if Int32(nil) != int32(0) {
154 t.Fatalf("to: Int32 failed to correctly convert nil -- expected %v, received %v",
155 0, Int32(nil))
156 }
157 }
158
159 func TestInt32Ptr(t *testing.T) {
160 v := int32(0)
161 if *Int32Ptr(v) != v {
162 t.Fatalf("to: Int32Ptr failed to return the correct string -- expected %v, received %v",
163 v, *Int32Ptr(v))
164 }
165 }
166
167 func TestInt64(t *testing.T) {
168 v := int64(0)
169 if Int64(&v) != v {
170 t.Fatalf("to: Int64 failed to return the correct string -- expected %v, received %v",
171 v, Int64(&v))
172 }
173 }
174
175 func TestInt64HandlesNil(t *testing.T) {
176 if Int64(nil) != int64(0) {
177 t.Fatalf("to: Int64 failed to correctly convert nil -- expected %v, received %v",
178 0, Int64(nil))
179 }
180 }
181
182 func TestInt64Ptr(t *testing.T) {
183 v := int64(0)
184 if *Int64Ptr(v) != v {
185 t.Fatalf("to: Int64Ptr failed to return the correct string -- expected %v, received %v",
186 v, *Int64Ptr(v))
187 }
188 }
189
190 func TestFloat32(t *testing.T) {
191 v := float32(0)
192 if Float32(&v) != v {
193 t.Fatalf("to: Float32 failed to return the correct string -- expected %v, received %v",
194 v, Float32(&v))
195 }
196 }
197
198 func TestFloat32HandlesNil(t *testing.T) {
199 if Float32(nil) != float32(0) {
200 t.Fatalf("to: Float32 failed to correctly convert nil -- expected %v, received %v",
201 0, Float32(nil))
202 }
203 }
204
205 func TestFloat32Ptr(t *testing.T) {
206 v := float32(0)
207 if *Float32Ptr(v) != v {
208 t.Fatalf("to: Float32Ptr failed to return the correct string -- expected %v, received %v",
209 v, *Float32Ptr(v))
210 }
211 }
212
213 func TestFloat64(t *testing.T) {
214 v := float64(0)
215 if Float64(&v) != v {
216 t.Fatalf("to: Float64 failed to return the correct string -- expected %v, received %v",
217 v, Float64(&v))
218 }
219 }
220
221 func TestFloat64HandlesNil(t *testing.T) {
222 if Float64(nil) != float64(0) {
223 t.Fatalf("to: Float64 failed to correctly convert nil -- expected %v, received %v",
224 0, Float64(nil))
225 }
226 }
227
228 func TestFloat64Ptr(t *testing.T) {
229 v := float64(0)
230 if *Float64Ptr(v) != v {
231 t.Fatalf("to: Float64Ptr failed to return the correct string -- expected %v, received %v",
232 v, *Float64Ptr(v))
233 }
234 }
235
236 func TestByteSlicePtr(t *testing.T) {
237 v := []byte("bytes")
238 if out := ByteSlicePtr(v); !reflect.DeepEqual(*out, v) {
239 t.Fatalf("to: ByteSlicePtr failed to return the correct slice -- expected %v, received %v",
240 v, *out)
241 }
242 }