| 1 |
/* |
| 2 |
Copyright 2021 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package dump |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"k8s.io/utils/ptr" |
| 24 |
) |
| 25 |
|
| 26 |
// custom type to test Stringer interface on non-pointer receiver. |
| 27 |
type customString string |
| 28 |
|
| 29 |
// String implements the Stringer interface for testing invocation |
| 30 |
func (s customString) String() string { |
| 31 |
return "custom string " + string(s) |
| 32 |
} |
| 33 |
|
| 34 |
// custom type to test error interface on non-pointer receiver. |
| 35 |
type customError int |
| 36 |
|
| 37 |
// Error implements the error interface for testing invocation |
| 38 |
func (e customError) Error() string { |
| 39 |
return fmt.Sprintf("custom error: %d", int(e)) |
| 40 |
} |
| 41 |
|
| 42 |
// custom type to test Stringer interface on pointer receiver. |
| 43 |
type pCustomString string |
| 44 |
|
| 45 |
// String implements the Stringer interface for testing invocation |
| 46 |
func (s *pCustomString) String() string { |
| 47 |
return "custom string " + string(*s) |
| 48 |
} |
| 49 |
|
| 50 |
// custom type to test error interface on pointer receiver. |
| 51 |
type pCustomError int |
| 52 |
|
| 53 |
// Error implements the error interface for testing invocation |
| 54 |
func (e *pCustomError) Error() string { |
| 55 |
return fmt.Sprintf("custom error: %d", int(*e)) |
| 56 |
} |
| 57 |
|
| 58 |
// embed is used to test embedded structures. |
| 59 |
type embed struct { |
| 60 |
s string |
| 61 |
} |
| 62 |
|
| 63 |
// embedwrap is used to test embedded structures. |
| 64 |
type embedwrap struct { |
| 65 |
embed |
| 66 |
e *embed |
| 67 |
} |
| 68 |
|
| 69 |
func TestPretty(t *testing.T) { |
| 70 |
tcs := customString("test") |
| 71 |
tpcs := pCustomString("&test") |
| 72 |
|
| 73 |
tce := customError(0) |
| 74 |
tpce := pCustomError(0) |
| 75 |
|
| 76 |
teb := embed{"test"} |
| 77 |
tebw := embedwrap{teb, &teb} |
| 78 |
|
| 79 |
testCases := []struct { |
| 80 |
a any |
| 81 |
want string |
| 82 |
}{ |
| 83 |
{int8(93), "(int8) 93\n"}, |
| 84 |
{int16(93), "(int16) 93\n"}, |
| 85 |
{int32(93), "(int32) 93\n"}, |
| 86 |
{int64(93), "(int64) 93\n"}, |
| 87 |
{int(-93), "(int) -93\n"}, |
| 88 |
{int8(-93), "(int8) -93\n"}, |
| 89 |
{int16(-93), "(int16) -93\n"}, |
| 90 |
{int32(-93), "(int32) -93\n"}, |
| 91 |
{int64(-93), "(int64) -93\n"}, |
| 92 |
{uint(93), "(uint) 93\n"}, |
| 93 |
{uint8(93), "(uint8) 93\n"}, |
| 94 |
{uint16(93), "(uint16) 93\n"}, |
| 95 |
{uint32(93), "(uint32) 93\n"}, |
| 96 |
{uint64(93), "(uint64) 93\n"}, |
| 97 |
{uintptr(93), "(uintptr) 0x5d\n"}, |
| 98 |
{ptr.To(93), "(*int)(93)\n"}, |
| 99 |
{float32(93.76), "(float32) 93.76\n"}, |
| 100 |
{float64(93.76), "(float64) 93.76\n"}, |
| 101 |
{complex64(93i), "(complex64) (0+93i)\n"}, |
| 102 |
{complex128(93i), "(complex128) (0+93i)\n"}, |
| 103 |
{bool(true), "(bool) true\n"}, |
| 104 |
{bool(false), "(bool) false\n"}, |
| 105 |
{string("test"), "(string) (len=4) \"test\"\n"}, |
| 106 |
{ptr.To("test"), "(*string)((len=4) \"test\")\n"}, |
| 107 |
{[1]string{"arr"}, "([1]string) (len=1) {\n (string) (len=3) \"arr\"\n}\n"}, |
| 108 |
{[]string{"slice"}, "([]string) (len=1) {\n (string) (len=5) \"slice\"\n}\n"}, |
| 109 |
{tcs, "(dump.customString) (len=4) \"test\"\n"}, |
| 110 |
{&tcs, "(*dump.customString)((len=4) \"test\")\n"}, |
| 111 |
{tpcs, "(dump.pCustomString) (len=5) \"&test\"\n"}, |
| 112 |
{&tpcs, "(*dump.pCustomString)((len=5) \"&test\")\n"}, |
| 113 |
{tce, "(dump.customError) 0\n"}, |
| 114 |
{&tce, "(*dump.customError)(0)\n"}, |
| 115 |
{tpce, "(dump.pCustomError) 0\n"}, |
| 116 |
{&tpce, "(*dump.pCustomError)(0)\n"}, |
| 117 |
{ |
| 118 |
struct { |
| 119 |
arr [1]string |
| 120 |
slice []string |
| 121 |
m map[string]int |
| 122 |
}{ |
| 123 |
[1]string{"arr"}, |
| 124 |
[]string{"slice"}, |
| 125 |
map[string]int{"one": 1}, |
| 126 |
}, |
| 127 |
"(struct { arr [1]string; slice []string; m map[string]int }) {\n arr: ([1]string) (len=1) {\n (string) (len=3) \"arr\"\n },\n slice: ([]string) (len=1) {\n (string) (len=5) \"slice\"\n },\n m: (map[string]int) (len=1) {\n (string) (len=3) \"one\": (int) 1\n }\n}\n", |
| 128 |
}, |
| 129 |
{teb, "(dump.embed) {\n s: (string) (len=4) \"test\"\n}\n"}, |
| 130 |
{tebw, "(dump.embedwrap) {\n embed: (dump.embed) {\n s: (string) (len=4) \"test\"\n },\n e: (*dump.embed)({\n s: (string) (len=4) \"test\"\n })\n}\n"}, |
| 131 |
{map[string]int{}, "(map[string]int) {\n}\n"}, |
| 132 |
{map[string]int{"one": 1}, "(map[string]int) (len=1) {\n (string) (len=3) \"one\": (int) 1\n}\n"}, |
| 133 |
{map[string]any{"one": 1}, "(map[string]interface {}) (len=1) {\n (string) (len=3) \"one\": (int) 1\n}\n"}, |
| 134 |
{map[string]customString{"key": tcs}, "(map[string]dump.customString) (len=1) {\n (string) (len=3) \"key\": (dump.customString) (len=4) \"test\"\n}\n"}, |
| 135 |
{map[string]customError{"key": tce}, "(map[string]dump.customError) (len=1) {\n (string) (len=3) \"key\": (dump.customError) 0\n}\n"}, |
| 136 |
{map[string]embed{"key": teb}, "(map[string]dump.embed) (len=1) {\n (string) (len=3) \"key\": (dump.embed) {\n s: (string) (len=4) \"test\"\n }\n}\n"}, |
| 137 |
{map[string]embedwrap{"key": tebw}, "(map[string]dump.embedwrap) (len=1) {\n (string) (len=3) \"key\": (dump.embedwrap) {\n embed: (dump.embed) {\n s: (string) (len=4) \"test\"\n },\n e: (*dump.embed)({\n s: (string) (len=4) \"test\"\n })\n }\n}\n"}, |
| 138 |
} |
| 139 |
|
| 140 |
for i, tc := range testCases { |
| 141 |
s := Pretty(tc.a) |
| 142 |
if tc.want != s { |
| 143 |
t.Errorf("[%d]:\n\texpected %q\n\tgot %q", i, tc.want, s) |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
func TestForHash(t *testing.T) { |
| 149 |
tcs := customString("test") |
| 150 |
tpcs := pCustomString("&test") |
| 151 |
|
| 152 |
tce := customError(0) |
| 153 |
tpce := pCustomError(0) |
| 154 |
|
| 155 |
teb := embed{"test"} |
| 156 |
tebw := embedwrap{teb, &teb} |
| 157 |
|
| 158 |
testCases := []struct { |
| 159 |
a any |
| 160 |
want string |
| 161 |
}{ |
| 162 |
{int8(93), "(int8)93"}, |
| 163 |
{int16(93), "(int16)93"}, |
| 164 |
{int32(93), "(int32)93"}, |
| 165 |
{int64(93), "(int64)93"}, |
| 166 |
{int(-93), "(int)-93"}, |
| 167 |
{int8(-93), "(int8)-93"}, |
| 168 |
{int16(-93), "(int16)-93"}, |
| 169 |
{int32(-93), "(int32)-93"}, |
| 170 |
{int64(-93), "(int64)-93"}, |
| 171 |
{uint(93), "(uint)93"}, |
| 172 |
{uint8(93), "(uint8)93"}, |
| 173 |
{uint16(93), "(uint16)93"}, |
| 174 |
{uint32(93), "(uint32)93"}, |
| 175 |
{uint64(93), "(uint64)93"}, |
| 176 |
{uintptr(93), "(uintptr)0x5d"}, |
| 177 |
{ptr.To(93), "(*int)93"}, |
| 178 |
{float32(93.76), "(float32)93.76"}, |
| 179 |
{float64(93.76), "(float64)93.76"}, |
| 180 |
{complex64(93i), "(complex64)(0+93i)"}, |
| 181 |
{complex128(93i), "(complex128)(0+93i)"}, |
| 182 |
{bool(true), "(bool)true"}, |
| 183 |
{bool(false), "(bool)false"}, |
| 184 |
{string("test"), "(string)test"}, |
| 185 |
{ptr.To("test"), "(*string)test"}, |
| 186 |
{[1]string{"arr"}, "([1]string)[arr]"}, |
| 187 |
{[]string{"slice"}, "([]string)[slice]"}, |
| 188 |
{tcs, "(dump.customString)test"}, |
| 189 |
{&tcs, "(*dump.customString)test"}, |
| 190 |
{tpcs, "(dump.pCustomString)&test"}, |
| 191 |
{&tpcs, "(*dump.pCustomString)&test"}, |
| 192 |
{tce, "(dump.customError)0"}, |
| 193 |
{&tce, "(*dump.customError)0"}, |
| 194 |
{tpce, "(dump.pCustomError)0"}, |
| 195 |
{&tpce, "(*dump.pCustomError)0"}, |
| 196 |
{ |
| 197 |
struct { |
| 198 |
arr [1]string |
| 199 |
slice []string |
| 200 |
m map[string]int |
| 201 |
}{ |
| 202 |
[1]string{"arr"}, |
| 203 |
[]string{"slice"}, |
| 204 |
map[string]int{"one": 1}, |
| 205 |
}, |
| 206 |
"(struct { arr [1]string; slice []string; m map[string]int }){arr:([1]string)[arr] slice:([]string)[slice] m:(map[string]int)map[one:1]}", |
| 207 |
}, |
| 208 |
{teb, "(dump.embed){s:(string)test}"}, |
| 209 |
{tebw, "(dump.embedwrap){embed:(dump.embed){s:(string)test} e:(*dump.embed){s:(string)test}}"}, |
| 210 |
{map[string]int{}, "(map[string]int)map[]"}, |
| 211 |
{map[string]int{"one": 1, "two": 2}, "(map[string]int)map[one:1 two:2]"}, |
| 212 |
{map[string]any{"one": 1}, "(map[string]interface {})map[one:(int)1]"}, |
| 213 |
{map[string]customString{"key": tcs}, "(map[string]dump.customString)map[key:test]"}, |
| 214 |
{map[string]customError{"key": tce}, "(map[string]dump.customError)map[key:0]"}, |
| 215 |
{map[string]embed{"key": teb}, "(map[string]dump.embed)map[key:{s:(string)test}]"}, |
| 216 |
{map[string]embedwrap{"key": tebw}, "(map[string]dump.embedwrap)map[key:{embed:(dump.embed){s:(string)test} e:(*dump.embed){s:(string)test}}]"}, |
| 217 |
} |
| 218 |
|
| 219 |
for i, tc := range testCases { |
| 220 |
s := ForHash(tc.a) |
| 221 |
if tc.want != s { |
| 222 |
t.Errorf("[%d]:\n\texpected %q\n\tgot %q", i, tc.want, s) |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
func TestOneLine(t *testing.T) { |
| 228 |
tcs := customString("test") |
| 229 |
tpcs := pCustomString("&test") |
| 230 |
|
| 231 |
tce := customError(0) |
| 232 |
tpce := pCustomError(0) |
| 233 |
|
| 234 |
teb := embed{"test"} |
| 235 |
tebw := embedwrap{teb, &teb} |
| 236 |
|
| 237 |
testCases := []struct { |
| 238 |
a any |
| 239 |
want string |
| 240 |
}{ |
| 241 |
{int8(93), "(int8)93"}, |
| 242 |
{int16(93), "(int16)93"}, |
| 243 |
{int32(93), "(int32)93"}, |
| 244 |
{int64(93), "(int64)93"}, |
| 245 |
{int(-93), "(int)-93"}, |
| 246 |
{int8(-93), "(int8)-93"}, |
| 247 |
{int16(-93), "(int16)-93"}, |
| 248 |
{int32(-93), "(int32)-93"}, |
| 249 |
{int64(-93), "(int64)-93"}, |
| 250 |
{uint(93), "(uint)93"}, |
| 251 |
{uint8(93), "(uint8)93"}, |
| 252 |
{uint16(93), "(uint16)93"}, |
| 253 |
{uint32(93), "(uint32)93"}, |
| 254 |
{uint64(93), "(uint64)93"}, |
| 255 |
{uintptr(93), "(uintptr)0x5d"}, |
| 256 |
{ptr.To(93), "(*int)93"}, |
| 257 |
{float32(93.76), "(float32)93.76"}, |
| 258 |
{float64(93.76), "(float64)93.76"}, |
| 259 |
{complex64(93i), "(complex64)(0+93i)"}, |
| 260 |
{complex128(93i), "(complex128)(0+93i)"}, |
| 261 |
{bool(true), "(bool)true"}, |
| 262 |
{bool(false), "(bool)false"}, |
| 263 |
{string("test"), "(string)test"}, |
| 264 |
{ptr.To("test"), "(*string)test"}, |
| 265 |
{[1]string{"arr"}, "([1]string)[arr]"}, |
| 266 |
{[]string{"slice"}, "([]string)[slice]"}, |
| 267 |
{tcs, "(dump.customString)test"}, |
| 268 |
{&tcs, "(*dump.customString)test"}, |
| 269 |
{tpcs, "(dump.pCustomString)&test"}, |
| 270 |
{&tpcs, "(*dump.pCustomString)&test"}, |
| 271 |
{tce, "(dump.customError)0"}, |
| 272 |
{&tce, "(*dump.customError)0"}, |
| 273 |
{tpce, "(dump.pCustomError)0"}, |
| 274 |
{&tpce, "(*dump.pCustomError)0"}, |
| 275 |
{ |
| 276 |
struct { |
| 277 |
arr [1]string |
| 278 |
slice []string |
| 279 |
m map[string]int |
| 280 |
}{ |
| 281 |
[1]string{"arr"}, |
| 282 |
[]string{"slice"}, |
| 283 |
map[string]int{"one": 1}, |
| 284 |
}, |
| 285 |
"(struct { arr [1]string; slice []string; m map[string]int }){arr:([1]string)[arr] slice:([]string)[slice] m:(map[string]int)map[one:1]}", |
| 286 |
}, |
| 287 |
{teb, "(dump.embed){s:(string)test}"}, |
| 288 |
{tebw, "(dump.embedwrap){embed:(dump.embed){s:(string)test} e:(*dump.embed){s:(string)test}}"}, |
| 289 |
{map[string]int{}, "(map[string]int)map[]"}, |
| 290 |
{map[string]int{"one": 1}, "(map[string]int)map[one:1]"}, |
| 291 |
{map[string]any{"one": 1}, "(map[string]interface {})map[one:(int)1]"}, |
| 292 |
{map[string]customString{"key": tcs}, "(map[string]dump.customString)map[key:test]"}, |
| 293 |
{map[string]customError{"key": tce}, "(map[string]dump.customError)map[key:0]"}, |
| 294 |
{map[string]embed{"key": teb}, "(map[string]dump.embed)map[key:{s:(string)test}]"}, |
| 295 |
{map[string]embedwrap{"key": tebw}, "(map[string]dump.embedwrap)map[key:{embed:(dump.embed){s:(string)test} e:(*dump.embed){s:(string)test}}]"}, |
| 296 |
} |
| 297 |
|
| 298 |
for i, tc := range testCases { |
| 299 |
s := OneLine(tc.a) |
| 300 |
if tc.want != s { |
| 301 |
t.Errorf("[%d]:\n\texpected %q\n\tgot %q", i, tc.want, s) |
| 302 |
} |
| 303 |
} |
| 304 |
} |