github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/test/go-spew/spew/format_test.go (about) 1 /* 2 * Copyright (c) 2013-2016 Dave Collins <dave@davec.name> 3 * 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 /* 18 Test Summary: 19 NOTE: For each test, a nil pointer, a single pointer and double pointer to the 20 base test element are also tested to ensure proper indirection across all types. 21 22 - Max int8, int16, int32, int64, int 23 - Max uint8, uint16, uint32, uint64, uint 24 - Boolean true and false 25 - Standard complex64 and complex128 26 - Array containing standard ints 27 - Array containing type with custom formatter on pointer receiver only 28 - Array containing interfaces 29 - Slice containing standard float32 values 30 - Slice containing type with custom formatter on pointer receiver only 31 - Slice containing interfaces 32 - Nil slice 33 - Standard string 34 - Nil interface 35 - Sub-interface 36 - Map with string keys and int vals 37 - Map with custom formatter type on pointer receiver only keys and vals 38 - Map with interface keys and values 39 - Map with nil interface value 40 - Struct with primitives 41 - Struct that contains another struct 42 - Struct that contains custom type with Stringer pointer interface via both 43 exported and unexported fields 44 - Struct that contains embedded struct and field to same struct 45 - Uintptr to 0 (null pointer) 46 - Uintptr address of real variable 47 - Unsafe.Pointer to 0 (null pointer) 48 - Unsafe.Pointer to address of real variable 49 - Nil channel 50 - Standard int channel 51 - Function with no params and no returns 52 - Function with param and no returns 53 - Function with multiple params and multiple returns 54 - Struct that is circular through self referencing 55 - Structs that are circular through cross referencing 56 - Structs that are indirectly circular 57 - Type that panics in its Stringer interface 58 - Type that has a custom Error interface 59 - %x passthrough with uint 60 - %#x passthrough with uint 61 - %f passthrough with precision 62 - %f passthrough with width and precision 63 - %d passthrough with width 64 - %q passthrough with string 65 */ 66 67 package spew_test 68 69 import ( 70 "bytes" 71 "fmt" 72 "testing" 73 "unsafe" 74 75 "github.com/davecgh/go-spew/spew" 76 ) 77 78 // formatterTest is used to describe a test to be performed against NewFormatter. 79 type formatterTest struct { 80 format string 81 in interface{} 82 wants []string 83 } 84 85 // formatterTests houses all of the tests to be performed against NewFormatter. 86 var formatterTests = make([]formatterTest, 0) 87 88 // addFormatterTest is a helper method to append the passed input and desired 89 // result to formatterTests. 90 func addFormatterTest(format string, in interface{}, wants ...string) { 91 test := formatterTest{format, in, wants} 92 formatterTests = append(formatterTests, test) 93 } 94 95 func addIntFormatterTests() { 96 // Max int8. 97 v := int8(127) 98 nv := (*int8)(nil) 99 pv := &v 100 vAddr := fmt.Sprintf("%p", pv) 101 pvAddr := fmt.Sprintf("%p", &pv) 102 vt := "int8" 103 vs := "127" 104 addFormatterTest("%v", v, vs) 105 addFormatterTest("%v", pv, "<*>"+vs) 106 addFormatterTest("%v", &pv, "<**>"+vs) 107 addFormatterTest("%v", nv, "<nil>") 108 addFormatterTest("%+v", v, vs) 109 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) 110 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) 111 addFormatterTest("%+v", nv, "<nil>") 112 addFormatterTest("%#v", v, "("+vt+")"+vs) 113 addFormatterTest("%#v", pv, "(*"+vt+")"+vs) 114 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) 115 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 116 addFormatterTest("%#+v", v, "("+vt+")"+vs) 117 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) 118 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) 119 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 120 121 // Max int16. 122 v2 := int16(32767) 123 nv2 := (*int16)(nil) 124 pv2 := &v2 125 v2Addr := fmt.Sprintf("%p", pv2) 126 pv2Addr := fmt.Sprintf("%p", &pv2) 127 v2t := "int16" 128 v2s := "32767" 129 addFormatterTest("%v", v2, v2s) 130 addFormatterTest("%v", pv2, "<*>"+v2s) 131 addFormatterTest("%v", &pv2, "<**>"+v2s) 132 addFormatterTest("%v", nv2, "<nil>") 133 addFormatterTest("%+v", v2, v2s) 134 addFormatterTest("%+v", pv2, "<*>("+v2Addr+")"+v2s) 135 addFormatterTest("%+v", &pv2, "<**>("+pv2Addr+"->"+v2Addr+")"+v2s) 136 addFormatterTest("%+v", nv2, "<nil>") 137 addFormatterTest("%#v", v2, "("+v2t+")"+v2s) 138 addFormatterTest("%#v", pv2, "(*"+v2t+")"+v2s) 139 addFormatterTest("%#v", &pv2, "(**"+v2t+")"+v2s) 140 addFormatterTest("%#v", nv2, "(*"+v2t+")"+"<nil>") 141 addFormatterTest("%#+v", v2, "("+v2t+")"+v2s) 142 addFormatterTest("%#+v", pv2, "(*"+v2t+")("+v2Addr+")"+v2s) 143 addFormatterTest("%#+v", &pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")"+v2s) 144 addFormatterTest("%#+v", nv2, "(*"+v2t+")"+"<nil>") 145 146 // Max int32. 147 v3 := int32(2147483647) 148 nv3 := (*int32)(nil) 149 pv3 := &v3 150 v3Addr := fmt.Sprintf("%p", pv3) 151 pv3Addr := fmt.Sprintf("%p", &pv3) 152 v3t := "int32" 153 v3s := "2147483647" 154 addFormatterTest("%v", v3, v3s) 155 addFormatterTest("%v", pv3, "<*>"+v3s) 156 addFormatterTest("%v", &pv3, "<**>"+v3s) 157 addFormatterTest("%v", nv3, "<nil>") 158 addFormatterTest("%+v", v3, v3s) 159 addFormatterTest("%+v", pv3, "<*>("+v3Addr+")"+v3s) 160 addFormatterTest("%+v", &pv3, "<**>("+pv3Addr+"->"+v3Addr+")"+v3s) 161 addFormatterTest("%+v", nv3, "<nil>") 162 addFormatterTest("%#v", v3, "("+v3t+")"+v3s) 163 addFormatterTest("%#v", pv3, "(*"+v3t+")"+v3s) 164 addFormatterTest("%#v", &pv3, "(**"+v3t+")"+v3s) 165 addFormatterTest("%#v", nv3, "(*"+v3t+")"+"<nil>") 166 addFormatterTest("%#+v", v3, "("+v3t+")"+v3s) 167 addFormatterTest("%#+v", pv3, "(*"+v3t+")("+v3Addr+")"+v3s) 168 addFormatterTest("%#+v", &pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")"+v3s) 169 addFormatterTest("%#v", nv3, "(*"+v3t+")"+"<nil>") 170 171 // Max int64. 172 v4 := int64(9223372036854775807) 173 nv4 := (*int64)(nil) 174 pv4 := &v4 175 v4Addr := fmt.Sprintf("%p", pv4) 176 pv4Addr := fmt.Sprintf("%p", &pv4) 177 v4t := "int64" 178 v4s := "9223372036854775807" 179 addFormatterTest("%v", v4, v4s) 180 addFormatterTest("%v", pv4, "<*>"+v4s) 181 addFormatterTest("%v", &pv4, "<**>"+v4s) 182 addFormatterTest("%v", nv4, "<nil>") 183 addFormatterTest("%+v", v4, v4s) 184 addFormatterTest("%+v", pv4, "<*>("+v4Addr+")"+v4s) 185 addFormatterTest("%+v", &pv4, "<**>("+pv4Addr+"->"+v4Addr+")"+v4s) 186 addFormatterTest("%+v", nv4, "<nil>") 187 addFormatterTest("%#v", v4, "("+v4t+")"+v4s) 188 addFormatterTest("%#v", pv4, "(*"+v4t+")"+v4s) 189 addFormatterTest("%#v", &pv4, "(**"+v4t+")"+v4s) 190 addFormatterTest("%#v", nv4, "(*"+v4t+")"+"<nil>") 191 addFormatterTest("%#+v", v4, "("+v4t+")"+v4s) 192 addFormatterTest("%#+v", pv4, "(*"+v4t+")("+v4Addr+")"+v4s) 193 addFormatterTest("%#+v", &pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")"+v4s) 194 addFormatterTest("%#+v", nv4, "(*"+v4t+")"+"<nil>") 195 196 // Max int. 197 v5 := int(2147483647) 198 nv5 := (*int)(nil) 199 pv5 := &v5 200 v5Addr := fmt.Sprintf("%p", pv5) 201 pv5Addr := fmt.Sprintf("%p", &pv5) 202 v5t := "int" 203 v5s := "2147483647" 204 addFormatterTest("%v", v5, v5s) 205 addFormatterTest("%v", pv5, "<*>"+v5s) 206 addFormatterTest("%v", &pv5, "<**>"+v5s) 207 addFormatterTest("%v", nv5, "<nil>") 208 addFormatterTest("%+v", v5, v5s) 209 addFormatterTest("%+v", pv5, "<*>("+v5Addr+")"+v5s) 210 addFormatterTest("%+v", &pv5, "<**>("+pv5Addr+"->"+v5Addr+")"+v5s) 211 addFormatterTest("%+v", nv5, "<nil>") 212 addFormatterTest("%#v", v5, "("+v5t+")"+v5s) 213 addFormatterTest("%#v", pv5, "(*"+v5t+")"+v5s) 214 addFormatterTest("%#v", &pv5, "(**"+v5t+")"+v5s) 215 addFormatterTest("%#v", nv5, "(*"+v5t+")"+"<nil>") 216 addFormatterTest("%#+v", v5, "("+v5t+")"+v5s) 217 addFormatterTest("%#+v", pv5, "(*"+v5t+")("+v5Addr+")"+v5s) 218 addFormatterTest("%#+v", &pv5, "(**"+v5t+")("+pv5Addr+"->"+v5Addr+")"+v5s) 219 addFormatterTest("%#+v", nv5, "(*"+v5t+")"+"<nil>") 220 } 221 222 func addUintFormatterTests() { 223 // Max uint8. 224 v := uint8(255) 225 nv := (*uint8)(nil) 226 pv := &v 227 vAddr := fmt.Sprintf("%p", pv) 228 pvAddr := fmt.Sprintf("%p", &pv) 229 vt := "uint8" 230 vs := "255" 231 addFormatterTest("%v", v, vs) 232 addFormatterTest("%v", pv, "<*>"+vs) 233 addFormatterTest("%v", &pv, "<**>"+vs) 234 addFormatterTest("%v", nv, "<nil>") 235 addFormatterTest("%+v", v, vs) 236 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) 237 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) 238 addFormatterTest("%+v", nv, "<nil>") 239 addFormatterTest("%#v", v, "("+vt+")"+vs) 240 addFormatterTest("%#v", pv, "(*"+vt+")"+vs) 241 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) 242 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 243 addFormatterTest("%#+v", v, "("+vt+")"+vs) 244 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) 245 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) 246 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 247 248 // Max uint16. 249 v2 := uint16(65535) 250 nv2 := (*uint16)(nil) 251 pv2 := &v2 252 v2Addr := fmt.Sprintf("%p", pv2) 253 pv2Addr := fmt.Sprintf("%p", &pv2) 254 v2t := "uint16" 255 v2s := "65535" 256 addFormatterTest("%v", v2, v2s) 257 addFormatterTest("%v", pv2, "<*>"+v2s) 258 addFormatterTest("%v", &pv2, "<**>"+v2s) 259 addFormatterTest("%v", nv2, "<nil>") 260 addFormatterTest("%+v", v2, v2s) 261 addFormatterTest("%+v", pv2, "<*>("+v2Addr+")"+v2s) 262 addFormatterTest("%+v", &pv2, "<**>("+pv2Addr+"->"+v2Addr+")"+v2s) 263 addFormatterTest("%+v", nv2, "<nil>") 264 addFormatterTest("%#v", v2, "("+v2t+")"+v2s) 265 addFormatterTest("%#v", pv2, "(*"+v2t+")"+v2s) 266 addFormatterTest("%#v", &pv2, "(**"+v2t+")"+v2s) 267 addFormatterTest("%#v", nv2, "(*"+v2t+")"+"<nil>") 268 addFormatterTest("%#+v", v2, "("+v2t+")"+v2s) 269 addFormatterTest("%#+v", pv2, "(*"+v2t+")("+v2Addr+")"+v2s) 270 addFormatterTest("%#+v", &pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")"+v2s) 271 addFormatterTest("%#+v", nv2, "(*"+v2t+")"+"<nil>") 272 273 // Max uint32. 274 v3 := uint32(4294967295) 275 nv3 := (*uint32)(nil) 276 pv3 := &v3 277 v3Addr := fmt.Sprintf("%p", pv3) 278 pv3Addr := fmt.Sprintf("%p", &pv3) 279 v3t := "uint32" 280 v3s := "4294967295" 281 addFormatterTest("%v", v3, v3s) 282 addFormatterTest("%v", pv3, "<*>"+v3s) 283 addFormatterTest("%v", &pv3, "<**>"+v3s) 284 addFormatterTest("%v", nv3, "<nil>") 285 addFormatterTest("%+v", v3, v3s) 286 addFormatterTest("%+v", pv3, "<*>("+v3Addr+")"+v3s) 287 addFormatterTest("%+v", &pv3, "<**>("+pv3Addr+"->"+v3Addr+")"+v3s) 288 addFormatterTest("%+v", nv3, "<nil>") 289 addFormatterTest("%#v", v3, "("+v3t+")"+v3s) 290 addFormatterTest("%#v", pv3, "(*"+v3t+")"+v3s) 291 addFormatterTest("%#v", &pv3, "(**"+v3t+")"+v3s) 292 addFormatterTest("%#v", nv3, "(*"+v3t+")"+"<nil>") 293 addFormatterTest("%#+v", v3, "("+v3t+")"+v3s) 294 addFormatterTest("%#+v", pv3, "(*"+v3t+")("+v3Addr+")"+v3s) 295 addFormatterTest("%#+v", &pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")"+v3s) 296 addFormatterTest("%#v", nv3, "(*"+v3t+")"+"<nil>") 297 298 // Max uint64. 299 v4 := uint64(18446744073709551615) 300 nv4 := (*uint64)(nil) 301 pv4 := &v4 302 v4Addr := fmt.Sprintf("%p", pv4) 303 pv4Addr := fmt.Sprintf("%p", &pv4) 304 v4t := "uint64" 305 v4s := "18446744073709551615" 306 addFormatterTest("%v", v4, v4s) 307 addFormatterTest("%v", pv4, "<*>"+v4s) 308 addFormatterTest("%v", &pv4, "<**>"+v4s) 309 addFormatterTest("%v", nv4, "<nil>") 310 addFormatterTest("%+v", v4, v4s) 311 addFormatterTest("%+v", pv4, "<*>("+v4Addr+")"+v4s) 312 addFormatterTest("%+v", &pv4, "<**>("+pv4Addr+"->"+v4Addr+")"+v4s) 313 addFormatterTest("%+v", nv4, "<nil>") 314 addFormatterTest("%#v", v4, "("+v4t+")"+v4s) 315 addFormatterTest("%#v", pv4, "(*"+v4t+")"+v4s) 316 addFormatterTest("%#v", &pv4, "(**"+v4t+")"+v4s) 317 addFormatterTest("%#v", nv4, "(*"+v4t+")"+"<nil>") 318 addFormatterTest("%#+v", v4, "("+v4t+")"+v4s) 319 addFormatterTest("%#+v", pv4, "(*"+v4t+")("+v4Addr+")"+v4s) 320 addFormatterTest("%#+v", &pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")"+v4s) 321 addFormatterTest("%#+v", nv4, "(*"+v4t+")"+"<nil>") 322 323 // Max uint. 324 v5 := uint(4294967295) 325 nv5 := (*uint)(nil) 326 pv5 := &v5 327 v5Addr := fmt.Sprintf("%p", pv5) 328 pv5Addr := fmt.Sprintf("%p", &pv5) 329 v5t := "uint" 330 v5s := "4294967295" 331 addFormatterTest("%v", v5, v5s) 332 addFormatterTest("%v", pv5, "<*>"+v5s) 333 addFormatterTest("%v", &pv5, "<**>"+v5s) 334 addFormatterTest("%v", nv5, "<nil>") 335 addFormatterTest("%+v", v5, v5s) 336 addFormatterTest("%+v", pv5, "<*>("+v5Addr+")"+v5s) 337 addFormatterTest("%+v", &pv5, "<**>("+pv5Addr+"->"+v5Addr+")"+v5s) 338 addFormatterTest("%+v", nv5, "<nil>") 339 addFormatterTest("%#v", v5, "("+v5t+")"+v5s) 340 addFormatterTest("%#v", pv5, "(*"+v5t+")"+v5s) 341 addFormatterTest("%#v", &pv5, "(**"+v5t+")"+v5s) 342 addFormatterTest("%#v", nv5, "(*"+v5t+")"+"<nil>") 343 addFormatterTest("%#+v", v5, "("+v5t+")"+v5s) 344 addFormatterTest("%#+v", pv5, "(*"+v5t+")("+v5Addr+")"+v5s) 345 addFormatterTest("%#+v", &pv5, "(**"+v5t+")("+pv5Addr+"->"+v5Addr+")"+v5s) 346 addFormatterTest("%#v", nv5, "(*"+v5t+")"+"<nil>") 347 } 348 349 func addBoolFormatterTests() { 350 // Boolean true. 351 v := bool(true) 352 nv := (*bool)(nil) 353 pv := &v 354 vAddr := fmt.Sprintf("%p", pv) 355 pvAddr := fmt.Sprintf("%p", &pv) 356 vt := "bool" 357 vs := "true" 358 addFormatterTest("%v", v, vs) 359 addFormatterTest("%v", pv, "<*>"+vs) 360 addFormatterTest("%v", &pv, "<**>"+vs) 361 addFormatterTest("%v", nv, "<nil>") 362 addFormatterTest("%+v", v, vs) 363 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) 364 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) 365 addFormatterTest("%+v", nv, "<nil>") 366 addFormatterTest("%#v", v, "("+vt+")"+vs) 367 addFormatterTest("%#v", pv, "(*"+vt+")"+vs) 368 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) 369 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 370 addFormatterTest("%#+v", v, "("+vt+")"+vs) 371 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) 372 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) 373 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 374 375 // Boolean false. 376 v2 := bool(false) 377 pv2 := &v2 378 v2Addr := fmt.Sprintf("%p", pv2) 379 pv2Addr := fmt.Sprintf("%p", &pv2) 380 v2t := "bool" 381 v2s := "false" 382 addFormatterTest("%v", v2, v2s) 383 addFormatterTest("%v", pv2, "<*>"+v2s) 384 addFormatterTest("%v", &pv2, "<**>"+v2s) 385 addFormatterTest("%+v", v2, v2s) 386 addFormatterTest("%+v", pv2, "<*>("+v2Addr+")"+v2s) 387 addFormatterTest("%+v", &pv2, "<**>("+pv2Addr+"->"+v2Addr+")"+v2s) 388 addFormatterTest("%#v", v2, "("+v2t+")"+v2s) 389 addFormatterTest("%#v", pv2, "(*"+v2t+")"+v2s) 390 addFormatterTest("%#v", &pv2, "(**"+v2t+")"+v2s) 391 addFormatterTest("%#+v", v2, "("+v2t+")"+v2s) 392 addFormatterTest("%#+v", pv2, "(*"+v2t+")("+v2Addr+")"+v2s) 393 addFormatterTest("%#+v", &pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")"+v2s) 394 } 395 396 func addFloatFormatterTests() { 397 // Standard float32. 398 v := float32(3.1415) 399 nv := (*float32)(nil) 400 pv := &v 401 vAddr := fmt.Sprintf("%p", pv) 402 pvAddr := fmt.Sprintf("%p", &pv) 403 vt := "float32" 404 vs := "3.1415" 405 addFormatterTest("%v", v, vs) 406 addFormatterTest("%v", pv, "<*>"+vs) 407 addFormatterTest("%v", &pv, "<**>"+vs) 408 addFormatterTest("%v", nv, "<nil>") 409 addFormatterTest("%+v", v, vs) 410 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) 411 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) 412 addFormatterTest("%+v", nv, "<nil>") 413 addFormatterTest("%#v", v, "("+vt+")"+vs) 414 addFormatterTest("%#v", pv, "(*"+vt+")"+vs) 415 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) 416 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 417 addFormatterTest("%#+v", v, "("+vt+")"+vs) 418 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) 419 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) 420 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 421 422 // Standard float64. 423 v2 := float64(3.1415926) 424 nv2 := (*float64)(nil) 425 pv2 := &v2 426 v2Addr := fmt.Sprintf("%p", pv2) 427 pv2Addr := fmt.Sprintf("%p", &pv2) 428 v2t := "float64" 429 v2s := "3.1415926" 430 addFormatterTest("%v", v2, v2s) 431 addFormatterTest("%v", pv2, "<*>"+v2s) 432 addFormatterTest("%v", &pv2, "<**>"+v2s) 433 addFormatterTest("%+v", nv2, "<nil>") 434 addFormatterTest("%+v", v2, v2s) 435 addFormatterTest("%+v", pv2, "<*>("+v2Addr+")"+v2s) 436 addFormatterTest("%+v", &pv2, "<**>("+pv2Addr+"->"+v2Addr+")"+v2s) 437 addFormatterTest("%+v", nv2, "<nil>") 438 addFormatterTest("%#v", v2, "("+v2t+")"+v2s) 439 addFormatterTest("%#v", pv2, "(*"+v2t+")"+v2s) 440 addFormatterTest("%#v", &pv2, "(**"+v2t+")"+v2s) 441 addFormatterTest("%#v", nv2, "(*"+v2t+")"+"<nil>") 442 addFormatterTest("%#+v", v2, "("+v2t+")"+v2s) 443 addFormatterTest("%#+v", pv2, "(*"+v2t+")("+v2Addr+")"+v2s) 444 addFormatterTest("%#+v", &pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")"+v2s) 445 addFormatterTest("%#+v", nv2, "(*"+v2t+")"+"<nil>") 446 } 447 448 func addComplexFormatterTests() { 449 // Standard complex64. 450 v := complex(float32(6), -2) 451 nv := (*complex64)(nil) 452 pv := &v 453 vAddr := fmt.Sprintf("%p", pv) 454 pvAddr := fmt.Sprintf("%p", &pv) 455 vt := "complex64" 456 vs := "(6-2i)" 457 addFormatterTest("%v", v, vs) 458 addFormatterTest("%v", pv, "<*>"+vs) 459 addFormatterTest("%v", &pv, "<**>"+vs) 460 addFormatterTest("%+v", nv, "<nil>") 461 addFormatterTest("%+v", v, vs) 462 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) 463 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) 464 addFormatterTest("%+v", nv, "<nil>") 465 addFormatterTest("%#v", v, "("+vt+")"+vs) 466 addFormatterTest("%#v", pv, "(*"+vt+")"+vs) 467 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) 468 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 469 addFormatterTest("%#+v", v, "("+vt+")"+vs) 470 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) 471 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) 472 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 473 474 // Standard complex128. 475 v2 := complex(float64(-6), 2) 476 nv2 := (*complex128)(nil) 477 pv2 := &v2 478 v2Addr := fmt.Sprintf("%p", pv2) 479 pv2Addr := fmt.Sprintf("%p", &pv2) 480 v2t := "complex128" 481 v2s := "(-6+2i)" 482 addFormatterTest("%v", v2, v2s) 483 addFormatterTest("%v", pv2, "<*>"+v2s) 484 addFormatterTest("%v", &pv2, "<**>"+v2s) 485 addFormatterTest("%+v", nv2, "<nil>") 486 addFormatterTest("%+v", v2, v2s) 487 addFormatterTest("%+v", pv2, "<*>("+v2Addr+")"+v2s) 488 addFormatterTest("%+v", &pv2, "<**>("+pv2Addr+"->"+v2Addr+")"+v2s) 489 addFormatterTest("%+v", nv2, "<nil>") 490 addFormatterTest("%#v", v2, "("+v2t+")"+v2s) 491 addFormatterTest("%#v", pv2, "(*"+v2t+")"+v2s) 492 addFormatterTest("%#v", &pv2, "(**"+v2t+")"+v2s) 493 addFormatterTest("%#v", nv2, "(*"+v2t+")"+"<nil>") 494 addFormatterTest("%#+v", v2, "("+v2t+")"+v2s) 495 addFormatterTest("%#+v", pv2, "(*"+v2t+")("+v2Addr+")"+v2s) 496 addFormatterTest("%#+v", &pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")"+v2s) 497 addFormatterTest("%#+v", nv2, "(*"+v2t+")"+"<nil>") 498 } 499 500 func addArrayFormatterTests() { 501 // Array containing standard ints. 502 v := [3]int{1, 2, 3} 503 nv := (*[3]int)(nil) 504 pv := &v 505 vAddr := fmt.Sprintf("%p", pv) 506 pvAddr := fmt.Sprintf("%p", &pv) 507 vt := "[3]int" 508 vs := "[1 2 3]" 509 addFormatterTest("%v", v, vs) 510 addFormatterTest("%v", pv, "<*>"+vs) 511 addFormatterTest("%v", &pv, "<**>"+vs) 512 addFormatterTest("%+v", nv, "<nil>") 513 addFormatterTest("%+v", v, vs) 514 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) 515 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) 516 addFormatterTest("%+v", nv, "<nil>") 517 addFormatterTest("%#v", v, "("+vt+")"+vs) 518 addFormatterTest("%#v", pv, "(*"+vt+")"+vs) 519 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) 520 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 521 addFormatterTest("%#+v", v, "("+vt+")"+vs) 522 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) 523 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) 524 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 525 526 // Array containing type with custom formatter on pointer receiver only. 527 v2 := [3]pstringer{"1", "2", "3"} 528 nv2 := (*[3]pstringer)(nil) 529 pv2 := &v2 530 v2Addr := fmt.Sprintf("%p", pv2) 531 pv2Addr := fmt.Sprintf("%p", &pv2) 532 v2t := "[3]spew_test.pstringer" 533 v2sp := "[stringer 1 stringer 2 stringer 3]" 534 v2s := v2sp 535 if spew.UnsafeDisabled { 536 v2s = "[1 2 3]" 537 } 538 addFormatterTest("%v", v2, v2s) 539 addFormatterTest("%v", pv2, "<*>"+v2sp) 540 addFormatterTest("%v", &pv2, "<**>"+v2sp) 541 addFormatterTest("%+v", nv2, "<nil>") 542 addFormatterTest("%+v", v2, v2s) 543 addFormatterTest("%+v", pv2, "<*>("+v2Addr+")"+v2sp) 544 addFormatterTest("%+v", &pv2, "<**>("+pv2Addr+"->"+v2Addr+")"+v2sp) 545 addFormatterTest("%+v", nv2, "<nil>") 546 addFormatterTest("%#v", v2, "("+v2t+")"+v2s) 547 addFormatterTest("%#v", pv2, "(*"+v2t+")"+v2sp) 548 addFormatterTest("%#v", &pv2, "(**"+v2t+")"+v2sp) 549 addFormatterTest("%#v", nv2, "(*"+v2t+")"+"<nil>") 550 addFormatterTest("%#+v", v2, "("+v2t+")"+v2s) 551 addFormatterTest("%#+v", pv2, "(*"+v2t+")("+v2Addr+")"+v2sp) 552 addFormatterTest("%#+v", &pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")"+v2sp) 553 addFormatterTest("%#+v", nv2, "(*"+v2t+")"+"<nil>") 554 555 // Array containing interfaces. 556 v3 := [3]interface{}{"one", int(2), uint(3)} 557 nv3 := (*[3]interface{})(nil) 558 pv3 := &v3 559 v3Addr := fmt.Sprintf("%p", pv3) 560 pv3Addr := fmt.Sprintf("%p", &pv3) 561 v3t := "[3]interface {}" 562 v3t2 := "string" 563 v3t3 := "int" 564 v3t4 := "uint" 565 v3s := "[one 2 3]" 566 v3s2 := "[(" + v3t2 + ")one (" + v3t3 + ")2 (" + v3t4 + ")3]" 567 addFormatterTest("%v", v3, v3s) 568 addFormatterTest("%v", pv3, "<*>"+v3s) 569 addFormatterTest("%v", &pv3, "<**>"+v3s) 570 addFormatterTest("%+v", nv3, "<nil>") 571 addFormatterTest("%+v", v3, v3s) 572 addFormatterTest("%+v", pv3, "<*>("+v3Addr+")"+v3s) 573 addFormatterTest("%+v", &pv3, "<**>("+pv3Addr+"->"+v3Addr+")"+v3s) 574 addFormatterTest("%+v", nv3, "<nil>") 575 addFormatterTest("%#v", v3, "("+v3t+")"+v3s2) 576 addFormatterTest("%#v", pv3, "(*"+v3t+")"+v3s2) 577 addFormatterTest("%#v", &pv3, "(**"+v3t+")"+v3s2) 578 addFormatterTest("%#v", nv3, "(*"+v3t+")"+"<nil>") 579 addFormatterTest("%#+v", v3, "("+v3t+")"+v3s2) 580 addFormatterTest("%#+v", pv3, "(*"+v3t+")("+v3Addr+")"+v3s2) 581 addFormatterTest("%#+v", &pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")"+v3s2) 582 addFormatterTest("%#+v", nv3, "(*"+v3t+")"+"<nil>") 583 } 584 585 func addSliceFormatterTests() { 586 // Slice containing standard float32 values. 587 v := []float32{3.14, 6.28, 12.56} 588 nv := (*[]float32)(nil) 589 pv := &v 590 vAddr := fmt.Sprintf("%p", pv) 591 pvAddr := fmt.Sprintf("%p", &pv) 592 vt := "[]float32" 593 vs := "[3.14 6.28 12.56]" 594 addFormatterTest("%v", v, vs) 595 addFormatterTest("%v", pv, "<*>"+vs) 596 addFormatterTest("%v", &pv, "<**>"+vs) 597 addFormatterTest("%+v", nv, "<nil>") 598 addFormatterTest("%+v", v, vs) 599 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) 600 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) 601 addFormatterTest("%+v", nv, "<nil>") 602 addFormatterTest("%#v", v, "("+vt+")"+vs) 603 addFormatterTest("%#v", pv, "(*"+vt+")"+vs) 604 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) 605 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 606 addFormatterTest("%#+v", v, "("+vt+")"+vs) 607 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) 608 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) 609 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 610 611 // Slice containing type with custom formatter on pointer receiver only. 612 v2 := []pstringer{"1", "2", "3"} 613 nv2 := (*[]pstringer)(nil) 614 pv2 := &v2 615 v2Addr := fmt.Sprintf("%p", pv2) 616 pv2Addr := fmt.Sprintf("%p", &pv2) 617 v2t := "[]spew_test.pstringer" 618 v2s := "[stringer 1 stringer 2 stringer 3]" 619 addFormatterTest("%v", v2, v2s) 620 addFormatterTest("%v", pv2, "<*>"+v2s) 621 addFormatterTest("%v", &pv2, "<**>"+v2s) 622 addFormatterTest("%+v", nv2, "<nil>") 623 addFormatterTest("%+v", v2, v2s) 624 addFormatterTest("%+v", pv2, "<*>("+v2Addr+")"+v2s) 625 addFormatterTest("%+v", &pv2, "<**>("+pv2Addr+"->"+v2Addr+")"+v2s) 626 addFormatterTest("%+v", nv2, "<nil>") 627 addFormatterTest("%#v", v2, "("+v2t+")"+v2s) 628 addFormatterTest("%#v", pv2, "(*"+v2t+")"+v2s) 629 addFormatterTest("%#v", &pv2, "(**"+v2t+")"+v2s) 630 addFormatterTest("%#v", nv2, "(*"+v2t+")"+"<nil>") 631 addFormatterTest("%#+v", v2, "("+v2t+")"+v2s) 632 addFormatterTest("%#+v", pv2, "(*"+v2t+")("+v2Addr+")"+v2s) 633 addFormatterTest("%#+v", &pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")"+v2s) 634 addFormatterTest("%#+v", nv2, "(*"+v2t+")"+"<nil>") 635 636 // Slice containing interfaces. 637 v3 := []interface{}{"one", int(2), uint(3), nil} 638 nv3 := (*[]interface{})(nil) 639 pv3 := &v3 640 v3Addr := fmt.Sprintf("%p", pv3) 641 pv3Addr := fmt.Sprintf("%p", &pv3) 642 v3t := "[]interface {}" 643 v3t2 := "string" 644 v3t3 := "int" 645 v3t4 := "uint" 646 v3t5 := "interface {}" 647 v3s := "[one 2 3 <nil>]" 648 v3s2 := "[(" + v3t2 + ")one (" + v3t3 + ")2 (" + v3t4 + ")3 (" + v3t5 + 649 ")<nil>]" 650 addFormatterTest("%v", v3, v3s) 651 addFormatterTest("%v", pv3, "<*>"+v3s) 652 addFormatterTest("%v", &pv3, "<**>"+v3s) 653 addFormatterTest("%+v", nv3, "<nil>") 654 addFormatterTest("%+v", v3, v3s) 655 addFormatterTest("%+v", pv3, "<*>("+v3Addr+")"+v3s) 656 addFormatterTest("%+v", &pv3, "<**>("+pv3Addr+"->"+v3Addr+")"+v3s) 657 addFormatterTest("%+v", nv3, "<nil>") 658 addFormatterTest("%#v", v3, "("+v3t+")"+v3s2) 659 addFormatterTest("%#v", pv3, "(*"+v3t+")"+v3s2) 660 addFormatterTest("%#v", &pv3, "(**"+v3t+")"+v3s2) 661 addFormatterTest("%#v", nv3, "(*"+v3t+")"+"<nil>") 662 addFormatterTest("%#+v", v3, "("+v3t+")"+v3s2) 663 addFormatterTest("%#+v", pv3, "(*"+v3t+")("+v3Addr+")"+v3s2) 664 addFormatterTest("%#+v", &pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")"+v3s2) 665 addFormatterTest("%#+v", nv3, "(*"+v3t+")"+"<nil>") 666 667 // Nil slice. 668 var v4 []int 669 nv4 := (*[]int)(nil) 670 pv4 := &v4 671 v4Addr := fmt.Sprintf("%p", pv4) 672 pv4Addr := fmt.Sprintf("%p", &pv4) 673 v4t := "[]int" 674 v4s := "<nil>" 675 addFormatterTest("%v", v4, v4s) 676 addFormatterTest("%v", pv4, "<*>"+v4s) 677 addFormatterTest("%v", &pv4, "<**>"+v4s) 678 addFormatterTest("%+v", nv4, "<nil>") 679 addFormatterTest("%+v", v4, v4s) 680 addFormatterTest("%+v", pv4, "<*>("+v4Addr+")"+v4s) 681 addFormatterTest("%+v", &pv4, "<**>("+pv4Addr+"->"+v4Addr+")"+v4s) 682 addFormatterTest("%+v", nv4, "<nil>") 683 addFormatterTest("%#v", v4, "("+v4t+")"+v4s) 684 addFormatterTest("%#v", pv4, "(*"+v4t+")"+v4s) 685 addFormatterTest("%#v", &pv4, "(**"+v4t+")"+v4s) 686 addFormatterTest("%#v", nv4, "(*"+v4t+")"+"<nil>") 687 addFormatterTest("%#+v", v4, "("+v4t+")"+v4s) 688 addFormatterTest("%#+v", pv4, "(*"+v4t+")("+v4Addr+")"+v4s) 689 addFormatterTest("%#+v", &pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")"+v4s) 690 addFormatterTest("%#+v", nv4, "(*"+v4t+")"+"<nil>") 691 } 692 693 func addStringFormatterTests() { 694 // Standard string. 695 v := "test" 696 nv := (*string)(nil) 697 pv := &v 698 vAddr := fmt.Sprintf("%p", pv) 699 pvAddr := fmt.Sprintf("%p", &pv) 700 vt := "string" 701 vs := "test" 702 addFormatterTest("%v", v, vs) 703 addFormatterTest("%v", pv, "<*>"+vs) 704 addFormatterTest("%v", &pv, "<**>"+vs) 705 addFormatterTest("%+v", nv, "<nil>") 706 addFormatterTest("%+v", v, vs) 707 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) 708 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) 709 addFormatterTest("%+v", nv, "<nil>") 710 addFormatterTest("%#v", v, "("+vt+")"+vs) 711 addFormatterTest("%#v", pv, "(*"+vt+")"+vs) 712 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) 713 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 714 addFormatterTest("%#+v", v, "("+vt+")"+vs) 715 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) 716 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) 717 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 718 } 719 720 func addInterfaceFormatterTests() { 721 // Nil interface. 722 var v interface{} 723 nv := (*interface{})(nil) 724 pv := &v 725 vAddr := fmt.Sprintf("%p", pv) 726 pvAddr := fmt.Sprintf("%p", &pv) 727 vt := "interface {}" 728 vs := "<nil>" 729 addFormatterTest("%v", v, vs) 730 addFormatterTest("%v", pv, "<*>"+vs) 731 addFormatterTest("%v", &pv, "<**>"+vs) 732 addFormatterTest("%+v", nv, "<nil>") 733 addFormatterTest("%+v", v, vs) 734 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) 735 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) 736 addFormatterTest("%+v", nv, "<nil>") 737 addFormatterTest("%#v", v, "("+vt+")"+vs) 738 addFormatterTest("%#v", pv, "(*"+vt+")"+vs) 739 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) 740 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 741 addFormatterTest("%#+v", v, "("+vt+")"+vs) 742 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) 743 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) 744 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 745 746 // Sub-interface. 747 v2 := interface{}(uint16(65535)) 748 pv2 := &v2 749 v2Addr := fmt.Sprintf("%p", pv2) 750 pv2Addr := fmt.Sprintf("%p", &pv2) 751 v2t := "uint16" 752 v2s := "65535" 753 addFormatterTest("%v", v2, v2s) 754 addFormatterTest("%v", pv2, "<*>"+v2s) 755 addFormatterTest("%v", &pv2, "<**>"+v2s) 756 addFormatterTest("%+v", v2, v2s) 757 addFormatterTest("%+v", pv2, "<*>("+v2Addr+")"+v2s) 758 addFormatterTest("%+v", &pv2, "<**>("+pv2Addr+"->"+v2Addr+")"+v2s) 759 addFormatterTest("%#v", v2, "("+v2t+")"+v2s) 760 addFormatterTest("%#v", pv2, "(*"+v2t+")"+v2s) 761 addFormatterTest("%#v", &pv2, "(**"+v2t+")"+v2s) 762 addFormatterTest("%#+v", v2, "("+v2t+")"+v2s) 763 addFormatterTest("%#+v", pv2, "(*"+v2t+")("+v2Addr+")"+v2s) 764 addFormatterTest("%#+v", &pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")"+v2s) 765 } 766 767 func addMapFormatterTests() { 768 // Map with string keys and int vals. 769 v := map[string]int{"one": 1, "two": 2} 770 nilMap := map[string]int(nil) 771 nv := (*map[string]int)(nil) 772 pv := &v 773 vAddr := fmt.Sprintf("%p", pv) 774 pvAddr := fmt.Sprintf("%p", &pv) 775 vt := "map[string]int" 776 vs := "map[one:1 two:2]" 777 vs2 := "map[two:2 one:1]" 778 addFormatterTest("%v", v, vs, vs2) 779 addFormatterTest("%v", pv, "<*>"+vs, "<*>"+vs2) 780 addFormatterTest("%v", &pv, "<**>"+vs, "<**>"+vs2) 781 addFormatterTest("%+v", nilMap, "<nil>") 782 addFormatterTest("%+v", nv, "<nil>") 783 addFormatterTest("%+v", v, vs, vs2) 784 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs, "<*>("+vAddr+")"+vs2) 785 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs, 786 "<**>("+pvAddr+"->"+vAddr+")"+vs2) 787 addFormatterTest("%+v", nilMap, "<nil>") 788 addFormatterTest("%+v", nv, "<nil>") 789 addFormatterTest("%#v", v, "("+vt+")"+vs, "("+vt+")"+vs2) 790 addFormatterTest("%#v", pv, "(*"+vt+")"+vs, "(*"+vt+")"+vs2) 791 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs, "(**"+vt+")"+vs2) 792 addFormatterTest("%#v", nilMap, "("+vt+")"+"<nil>") 793 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 794 addFormatterTest("%#+v", v, "("+vt+")"+vs, "("+vt+")"+vs2) 795 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs, 796 "(*"+vt+")("+vAddr+")"+vs2) 797 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs, 798 "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs2) 799 addFormatterTest("%#+v", nilMap, "("+vt+")"+"<nil>") 800 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 801 802 // Map with custom formatter type on pointer receiver only keys and vals. 803 v2 := map[pstringer]pstringer{"one": "1"} 804 nv2 := (*map[pstringer]pstringer)(nil) 805 pv2 := &v2 806 v2Addr := fmt.Sprintf("%p", pv2) 807 pv2Addr := fmt.Sprintf("%p", &pv2) 808 v2t := "map[spew_test.pstringer]spew_test.pstringer" 809 v2s := "map[stringer one:stringer 1]" 810 if spew.UnsafeDisabled { 811 v2s = "map[one:1]" 812 } 813 addFormatterTest("%v", v2, v2s) 814 addFormatterTest("%v", pv2, "<*>"+v2s) 815 addFormatterTest("%v", &pv2, "<**>"+v2s) 816 addFormatterTest("%+v", nv2, "<nil>") 817 addFormatterTest("%+v", v2, v2s) 818 addFormatterTest("%+v", pv2, "<*>("+v2Addr+")"+v2s) 819 addFormatterTest("%+v", &pv2, "<**>("+pv2Addr+"->"+v2Addr+")"+v2s) 820 addFormatterTest("%+v", nv2, "<nil>") 821 addFormatterTest("%#v", v2, "("+v2t+")"+v2s) 822 addFormatterTest("%#v", pv2, "(*"+v2t+")"+v2s) 823 addFormatterTest("%#v", &pv2, "(**"+v2t+")"+v2s) 824 addFormatterTest("%#v", nv2, "(*"+v2t+")"+"<nil>") 825 addFormatterTest("%#+v", v2, "("+v2t+")"+v2s) 826 addFormatterTest("%#+v", pv2, "(*"+v2t+")("+v2Addr+")"+v2s) 827 addFormatterTest("%#+v", &pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")"+v2s) 828 addFormatterTest("%#+v", nv2, "(*"+v2t+")"+"<nil>") 829 830 // Map with interface keys and values. 831 v3 := map[interface{}]interface{}{"one": 1} 832 nv3 := (*map[interface{}]interface{})(nil) 833 pv3 := &v3 834 v3Addr := fmt.Sprintf("%p", pv3) 835 pv3Addr := fmt.Sprintf("%p", &pv3) 836 v3t := "map[interface {}]interface {}" 837 v3t1 := "string" 838 v3t2 := "int" 839 v3s := "map[one:1]" 840 v3s2 := "map[(" + v3t1 + ")one:(" + v3t2 + ")1]" 841 addFormatterTest("%v", v3, v3s) 842 addFormatterTest("%v", pv3, "<*>"+v3s) 843 addFormatterTest("%v", &pv3, "<**>"+v3s) 844 addFormatterTest("%+v", nv3, "<nil>") 845 addFormatterTest("%+v", v3, v3s) 846 addFormatterTest("%+v", pv3, "<*>("+v3Addr+")"+v3s) 847 addFormatterTest("%+v", &pv3, "<**>("+pv3Addr+"->"+v3Addr+")"+v3s) 848 addFormatterTest("%+v", nv3, "<nil>") 849 addFormatterTest("%#v", v3, "("+v3t+")"+v3s2) 850 addFormatterTest("%#v", pv3, "(*"+v3t+")"+v3s2) 851 addFormatterTest("%#v", &pv3, "(**"+v3t+")"+v3s2) 852 addFormatterTest("%#v", nv3, "(*"+v3t+")"+"<nil>") 853 addFormatterTest("%#+v", v3, "("+v3t+")"+v3s2) 854 addFormatterTest("%#+v", pv3, "(*"+v3t+")("+v3Addr+")"+v3s2) 855 addFormatterTest("%#+v", &pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")"+v3s2) 856 addFormatterTest("%#+v", nv3, "(*"+v3t+")"+"<nil>") 857 858 // Map with nil interface value 859 v4 := map[string]interface{}{"nil": nil} 860 nv4 := (*map[string]interface{})(nil) 861 pv4 := &v4 862 v4Addr := fmt.Sprintf("%p", pv4) 863 pv4Addr := fmt.Sprintf("%p", &pv4) 864 v4t := "map[string]interface {}" 865 v4t1 := "interface {}" 866 v4s := "map[nil:<nil>]" 867 v4s2 := "map[nil:(" + v4t1 + ")<nil>]" 868 addFormatterTest("%v", v4, v4s) 869 addFormatterTest("%v", pv4, "<*>"+v4s) 870 addFormatterTest("%v", &pv4, "<**>"+v4s) 871 addFormatterTest("%+v", nv4, "<nil>") 872 addFormatterTest("%+v", v4, v4s) 873 addFormatterTest("%+v", pv4, "<*>("+v4Addr+")"+v4s) 874 addFormatterTest("%+v", &pv4, "<**>("+pv4Addr+"->"+v4Addr+")"+v4s) 875 addFormatterTest("%+v", nv4, "<nil>") 876 addFormatterTest("%#v", v4, "("+v4t+")"+v4s2) 877 addFormatterTest("%#v", pv4, "(*"+v4t+")"+v4s2) 878 addFormatterTest("%#v", &pv4, "(**"+v4t+")"+v4s2) 879 addFormatterTest("%#v", nv4, "(*"+v4t+")"+"<nil>") 880 addFormatterTest("%#+v", v4, "("+v4t+")"+v4s2) 881 addFormatterTest("%#+v", pv4, "(*"+v4t+")("+v4Addr+")"+v4s2) 882 addFormatterTest("%#+v", &pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")"+v4s2) 883 addFormatterTest("%#+v", nv4, "(*"+v4t+")"+"<nil>") 884 } 885 886 func addStructFormatterTests() { 887 // Struct with primitives. 888 type s1 struct { 889 a int8 890 b uint8 891 } 892 v := s1{127, 255} 893 nv := (*s1)(nil) 894 pv := &v 895 vAddr := fmt.Sprintf("%p", pv) 896 pvAddr := fmt.Sprintf("%p", &pv) 897 vt := "spew_test.s1" 898 vt2 := "int8" 899 vt3 := "uint8" 900 vs := "{127 255}" 901 vs2 := "{a:127 b:255}" 902 vs3 := "{a:(" + vt2 + ")127 b:(" + vt3 + ")255}" 903 addFormatterTest("%v", v, vs) 904 addFormatterTest("%v", pv, "<*>"+vs) 905 addFormatterTest("%v", &pv, "<**>"+vs) 906 addFormatterTest("%+v", nv, "<nil>") 907 addFormatterTest("%+v", v, vs2) 908 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs2) 909 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs2) 910 addFormatterTest("%+v", nv, "<nil>") 911 addFormatterTest("%#v", v, "("+vt+")"+vs3) 912 addFormatterTest("%#v", pv, "(*"+vt+")"+vs3) 913 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs3) 914 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 915 addFormatterTest("%#+v", v, "("+vt+")"+vs3) 916 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs3) 917 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs3) 918 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 919 920 // Struct that contains another struct. 921 type s2 struct { 922 s1 s1 923 b bool 924 } 925 v2 := s2{s1{127, 255}, true} 926 nv2 := (*s2)(nil) 927 pv2 := &v2 928 v2Addr := fmt.Sprintf("%p", pv2) 929 pv2Addr := fmt.Sprintf("%p", &pv2) 930 v2t := "spew_test.s2" 931 v2t2 := "spew_test.s1" 932 v2t3 := "int8" 933 v2t4 := "uint8" 934 v2t5 := "bool" 935 v2s := "{{127 255} true}" 936 v2s2 := "{s1:{a:127 b:255} b:true}" 937 v2s3 := "{s1:(" + v2t2 + "){a:(" + v2t3 + ")127 b:(" + v2t4 + ")255} b:(" + 938 v2t5 + ")true}" 939 addFormatterTest("%v", v2, v2s) 940 addFormatterTest("%v", pv2, "<*>"+v2s) 941 addFormatterTest("%v", &pv2, "<**>"+v2s) 942 addFormatterTest("%+v", nv2, "<nil>") 943 addFormatterTest("%+v", v2, v2s2) 944 addFormatterTest("%+v", pv2, "<*>("+v2Addr+")"+v2s2) 945 addFormatterTest("%+v", &pv2, "<**>("+pv2Addr+"->"+v2Addr+")"+v2s2) 946 addFormatterTest("%+v", nv2, "<nil>") 947 addFormatterTest("%#v", v2, "("+v2t+")"+v2s3) 948 addFormatterTest("%#v", pv2, "(*"+v2t+")"+v2s3) 949 addFormatterTest("%#v", &pv2, "(**"+v2t+")"+v2s3) 950 addFormatterTest("%#v", nv2, "(*"+v2t+")"+"<nil>") 951 addFormatterTest("%#+v", v2, "("+v2t+")"+v2s3) 952 addFormatterTest("%#+v", pv2, "(*"+v2t+")("+v2Addr+")"+v2s3) 953 addFormatterTest("%#+v", &pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")"+v2s3) 954 addFormatterTest("%#+v", nv2, "(*"+v2t+")"+"<nil>") 955 956 // Struct that contains custom type with Stringer pointer interface via both 957 // exported and unexported fields. 958 type s3 struct { 959 s pstringer 960 S pstringer 961 } 962 v3 := s3{"test", "test2"} 963 nv3 := (*s3)(nil) 964 pv3 := &v3 965 v3Addr := fmt.Sprintf("%p", pv3) 966 pv3Addr := fmt.Sprintf("%p", &pv3) 967 v3t := "spew_test.s3" 968 v3t2 := "spew_test.pstringer" 969 v3s := "{stringer test stringer test2}" 970 v3sp := v3s 971 v3s2 := "{s:stringer test S:stringer test2}" 972 v3s2p := v3s2 973 v3s3 := "{s:(" + v3t2 + ")stringer test S:(" + v3t2 + ")stringer test2}" 974 v3s3p := v3s3 975 if spew.UnsafeDisabled { 976 v3s = "{test test2}" 977 v3sp = "{test stringer test2}" 978 v3s2 = "{s:test S:test2}" 979 v3s2p = "{s:test S:stringer test2}" 980 v3s3 = "{s:(" + v3t2 + ")test S:(" + v3t2 + ")test2}" 981 v3s3p = "{s:(" + v3t2 + ")test S:(" + v3t2 + ")stringer test2}" 982 } 983 addFormatterTest("%v", v3, v3s) 984 addFormatterTest("%v", pv3, "<*>"+v3sp) 985 addFormatterTest("%v", &pv3, "<**>"+v3sp) 986 addFormatterTest("%+v", nv3, "<nil>") 987 addFormatterTest("%+v", v3, v3s2) 988 addFormatterTest("%+v", pv3, "<*>("+v3Addr+")"+v3s2p) 989 addFormatterTest("%+v", &pv3, "<**>("+pv3Addr+"->"+v3Addr+")"+v3s2p) 990 addFormatterTest("%+v", nv3, "<nil>") 991 addFormatterTest("%#v", v3, "("+v3t+")"+v3s3) 992 addFormatterTest("%#v", pv3, "(*"+v3t+")"+v3s3p) 993 addFormatterTest("%#v", &pv3, "(**"+v3t+")"+v3s3p) 994 addFormatterTest("%#v", nv3, "(*"+v3t+")"+"<nil>") 995 addFormatterTest("%#+v", v3, "("+v3t+")"+v3s3) 996 addFormatterTest("%#+v", pv3, "(*"+v3t+")("+v3Addr+")"+v3s3p) 997 addFormatterTest("%#+v", &pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")"+v3s3p) 998 addFormatterTest("%#+v", nv3, "(*"+v3t+")"+"<nil>") 999 1000 // Struct that contains embedded struct and field to same struct. 1001 e := embed{"embedstr"} 1002 v4 := embedwrap{embed: &e, e: &e} 1003 nv4 := (*embedwrap)(nil) 1004 pv4 := &v4 1005 eAddr := fmt.Sprintf("%p", &e) 1006 v4Addr := fmt.Sprintf("%p", pv4) 1007 pv4Addr := fmt.Sprintf("%p", &pv4) 1008 v4t := "spew_test.embedwrap" 1009 v4t2 := "spew_test.embed" 1010 v4t3 := "string" 1011 v4s := "{<*>{embedstr} <*>{embedstr}}" 1012 v4s2 := "{embed:<*>(" + eAddr + "){a:embedstr} e:<*>(" + eAddr + 1013 "){a:embedstr}}" 1014 v4s3 := "{embed:(*" + v4t2 + "){a:(" + v4t3 + ")embedstr} e:(*" + v4t2 + 1015 "){a:(" + v4t3 + ")embedstr}}" 1016 v4s4 := "{embed:(*" + v4t2 + ")(" + eAddr + "){a:(" + v4t3 + 1017 ")embedstr} e:(*" + v4t2 + ")(" + eAddr + "){a:(" + v4t3 + ")embedstr}}" 1018 addFormatterTest("%v", v4, v4s) 1019 addFormatterTest("%v", pv4, "<*>"+v4s) 1020 addFormatterTest("%v", &pv4, "<**>"+v4s) 1021 addFormatterTest("%+v", nv4, "<nil>") 1022 addFormatterTest("%+v", v4, v4s2) 1023 addFormatterTest("%+v", pv4, "<*>("+v4Addr+")"+v4s2) 1024 addFormatterTest("%+v", &pv4, "<**>("+pv4Addr+"->"+v4Addr+")"+v4s2) 1025 addFormatterTest("%+v", nv4, "<nil>") 1026 addFormatterTest("%#v", v4, "("+v4t+")"+v4s3) 1027 addFormatterTest("%#v", pv4, "(*"+v4t+")"+v4s3) 1028 addFormatterTest("%#v", &pv4, "(**"+v4t+")"+v4s3) 1029 addFormatterTest("%#v", nv4, "(*"+v4t+")"+"<nil>") 1030 addFormatterTest("%#+v", v4, "("+v4t+")"+v4s4) 1031 addFormatterTest("%#+v", pv4, "(*"+v4t+")("+v4Addr+")"+v4s4) 1032 addFormatterTest("%#+v", &pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")"+v4s4) 1033 addFormatterTest("%#+v", nv4, "(*"+v4t+")"+"<nil>") 1034 } 1035 1036 func addUintptrFormatterTests() { 1037 // Null pointer. 1038 v := uintptr(0) 1039 nv := (*uintptr)(nil) 1040 pv := &v 1041 vAddr := fmt.Sprintf("%p", pv) 1042 pvAddr := fmt.Sprintf("%p", &pv) 1043 vt := "uintptr" 1044 vs := "<nil>" 1045 addFormatterTest("%v", v, vs) 1046 addFormatterTest("%v", pv, "<*>"+vs) 1047 addFormatterTest("%v", &pv, "<**>"+vs) 1048 addFormatterTest("%+v", nv, "<nil>") 1049 addFormatterTest("%+v", v, vs) 1050 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) 1051 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) 1052 addFormatterTest("%+v", nv, "<nil>") 1053 addFormatterTest("%#v", v, "("+vt+")"+vs) 1054 addFormatterTest("%#v", pv, "(*"+vt+")"+vs) 1055 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) 1056 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 1057 addFormatterTest("%#+v", v, "("+vt+")"+vs) 1058 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) 1059 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) 1060 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 1061 1062 // Address of real variable. 1063 i := 1 1064 v2 := uintptr(unsafe.Pointer(&i)) 1065 pv2 := &v2 1066 v2Addr := fmt.Sprintf("%p", pv2) 1067 pv2Addr := fmt.Sprintf("%p", &pv2) 1068 v2t := "uintptr" 1069 v2s := fmt.Sprintf("%p", &i) 1070 addFormatterTest("%v", v2, v2s) 1071 addFormatterTest("%v", pv2, "<*>"+v2s) 1072 addFormatterTest("%v", &pv2, "<**>"+v2s) 1073 addFormatterTest("%+v", v2, v2s) 1074 addFormatterTest("%+v", pv2, "<*>("+v2Addr+")"+v2s) 1075 addFormatterTest("%+v", &pv2, "<**>("+pv2Addr+"->"+v2Addr+")"+v2s) 1076 addFormatterTest("%#v", v2, "("+v2t+")"+v2s) 1077 addFormatterTest("%#v", pv2, "(*"+v2t+")"+v2s) 1078 addFormatterTest("%#v", &pv2, "(**"+v2t+")"+v2s) 1079 addFormatterTest("%#+v", v2, "("+v2t+")"+v2s) 1080 addFormatterTest("%#+v", pv2, "(*"+v2t+")("+v2Addr+")"+v2s) 1081 addFormatterTest("%#+v", &pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")"+v2s) 1082 } 1083 1084 func addUnsafePointerFormatterTests() { 1085 // Null pointer. 1086 v := unsafe.Pointer(uintptr(0)) 1087 nv := (*unsafe.Pointer)(nil) 1088 pv := &v 1089 vAddr := fmt.Sprintf("%p", pv) 1090 pvAddr := fmt.Sprintf("%p", &pv) 1091 vt := "unsafe.Pointer" 1092 vs := "<nil>" 1093 addFormatterTest("%v", v, vs) 1094 addFormatterTest("%v", pv, "<*>"+vs) 1095 addFormatterTest("%v", &pv, "<**>"+vs) 1096 addFormatterTest("%+v", nv, "<nil>") 1097 addFormatterTest("%+v", v, vs) 1098 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) 1099 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) 1100 addFormatterTest("%+v", nv, "<nil>") 1101 addFormatterTest("%#v", v, "("+vt+")"+vs) 1102 addFormatterTest("%#v", pv, "(*"+vt+")"+vs) 1103 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) 1104 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 1105 addFormatterTest("%#+v", v, "("+vt+")"+vs) 1106 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) 1107 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) 1108 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 1109 1110 // Address of real variable. 1111 i := 1 1112 v2 := unsafe.Pointer(&i) 1113 pv2 := &v2 1114 v2Addr := fmt.Sprintf("%p", pv2) 1115 pv2Addr := fmt.Sprintf("%p", &pv2) 1116 v2t := "unsafe.Pointer" 1117 v2s := fmt.Sprintf("%p", &i) 1118 addFormatterTest("%v", v2, v2s) 1119 addFormatterTest("%v", pv2, "<*>"+v2s) 1120 addFormatterTest("%v", &pv2, "<**>"+v2s) 1121 addFormatterTest("%+v", v2, v2s) 1122 addFormatterTest("%+v", pv2, "<*>("+v2Addr+")"+v2s) 1123 addFormatterTest("%+v", &pv2, "<**>("+pv2Addr+"->"+v2Addr+")"+v2s) 1124 addFormatterTest("%#v", v2, "("+v2t+")"+v2s) 1125 addFormatterTest("%#v", pv2, "(*"+v2t+")"+v2s) 1126 addFormatterTest("%#v", &pv2, "(**"+v2t+")"+v2s) 1127 addFormatterTest("%#+v", v2, "("+v2t+")"+v2s) 1128 addFormatterTest("%#+v", pv2, "(*"+v2t+")("+v2Addr+")"+v2s) 1129 addFormatterTest("%#+v", &pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")"+v2s) 1130 } 1131 1132 func addChanFormatterTests() { 1133 // Nil channel. 1134 var v chan int 1135 pv := &v 1136 nv := (*chan int)(nil) 1137 vAddr := fmt.Sprintf("%p", pv) 1138 pvAddr := fmt.Sprintf("%p", &pv) 1139 vt := "chan int" 1140 vs := "<nil>" 1141 addFormatterTest("%v", v, vs) 1142 addFormatterTest("%v", pv, "<*>"+vs) 1143 addFormatterTest("%v", &pv, "<**>"+vs) 1144 addFormatterTest("%+v", nv, "<nil>") 1145 addFormatterTest("%+v", v, vs) 1146 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) 1147 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) 1148 addFormatterTest("%+v", nv, "<nil>") 1149 addFormatterTest("%#v", v, "("+vt+")"+vs) 1150 addFormatterTest("%#v", pv, "(*"+vt+")"+vs) 1151 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) 1152 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 1153 addFormatterTest("%#+v", v, "("+vt+")"+vs) 1154 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) 1155 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) 1156 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 1157 1158 // Real channel. 1159 v2 := make(chan int) 1160 pv2 := &v2 1161 v2Addr := fmt.Sprintf("%p", pv2) 1162 pv2Addr := fmt.Sprintf("%p", &pv2) 1163 v2t := "chan int" 1164 v2s := fmt.Sprintf("%p", v2) 1165 addFormatterTest("%v", v2, v2s) 1166 addFormatterTest("%v", pv2, "<*>"+v2s) 1167 addFormatterTest("%v", &pv2, "<**>"+v2s) 1168 addFormatterTest("%+v", v2, v2s) 1169 addFormatterTest("%+v", pv2, "<*>("+v2Addr+")"+v2s) 1170 addFormatterTest("%+v", &pv2, "<**>("+pv2Addr+"->"+v2Addr+")"+v2s) 1171 addFormatterTest("%#v", v2, "("+v2t+")"+v2s) 1172 addFormatterTest("%#v", pv2, "(*"+v2t+")"+v2s) 1173 addFormatterTest("%#v", &pv2, "(**"+v2t+")"+v2s) 1174 addFormatterTest("%#+v", v2, "("+v2t+")"+v2s) 1175 addFormatterTest("%#+v", pv2, "(*"+v2t+")("+v2Addr+")"+v2s) 1176 addFormatterTest("%#+v", &pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")"+v2s) 1177 } 1178 1179 func addFuncFormatterTests() { 1180 // Function with no params and no returns. 1181 v := addIntFormatterTests 1182 nv := (*func())(nil) 1183 pv := &v 1184 vAddr := fmt.Sprintf("%p", pv) 1185 pvAddr := fmt.Sprintf("%p", &pv) 1186 vt := "func()" 1187 vs := fmt.Sprintf("%p", v) 1188 addFormatterTest("%v", v, vs) 1189 addFormatterTest("%v", pv, "<*>"+vs) 1190 addFormatterTest("%v", &pv, "<**>"+vs) 1191 addFormatterTest("%+v", nv, "<nil>") 1192 addFormatterTest("%+v", v, vs) 1193 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) 1194 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) 1195 addFormatterTest("%+v", nv, "<nil>") 1196 addFormatterTest("%#v", v, "("+vt+")"+vs) 1197 addFormatterTest("%#v", pv, "(*"+vt+")"+vs) 1198 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) 1199 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 1200 addFormatterTest("%#+v", v, "("+vt+")"+vs) 1201 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) 1202 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) 1203 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 1204 1205 // Function with param and no returns. 1206 v2 := TestFormatter 1207 nv2 := (*func(*testing.T))(nil) 1208 pv2 := &v2 1209 v2Addr := fmt.Sprintf("%p", pv2) 1210 pv2Addr := fmt.Sprintf("%p", &pv2) 1211 v2t := "func(*testing.T)" 1212 v2s := fmt.Sprintf("%p", v2) 1213 addFormatterTest("%v", v2, v2s) 1214 addFormatterTest("%v", pv2, "<*>"+v2s) 1215 addFormatterTest("%v", &pv2, "<**>"+v2s) 1216 addFormatterTest("%+v", nv2, "<nil>") 1217 addFormatterTest("%+v", v2, v2s) 1218 addFormatterTest("%+v", pv2, "<*>("+v2Addr+")"+v2s) 1219 addFormatterTest("%+v", &pv2, "<**>("+pv2Addr+"->"+v2Addr+")"+v2s) 1220 addFormatterTest("%+v", nv2, "<nil>") 1221 addFormatterTest("%#v", v2, "("+v2t+")"+v2s) 1222 addFormatterTest("%#v", pv2, "(*"+v2t+")"+v2s) 1223 addFormatterTest("%#v", &pv2, "(**"+v2t+")"+v2s) 1224 addFormatterTest("%#v", nv2, "(*"+v2t+")"+"<nil>") 1225 addFormatterTest("%#+v", v2, "("+v2t+")"+v2s) 1226 addFormatterTest("%#+v", pv2, "(*"+v2t+")("+v2Addr+")"+v2s) 1227 addFormatterTest("%#+v", &pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")"+v2s) 1228 addFormatterTest("%#+v", nv2, "(*"+v2t+")"+"<nil>") 1229 1230 // Function with multiple params and multiple returns. 1231 var v3 = func(i int, s string) (b bool, err error) { 1232 return true, nil 1233 } 1234 nv3 := (*func(int, string) (bool, error))(nil) 1235 pv3 := &v3 1236 v3Addr := fmt.Sprintf("%p", pv3) 1237 pv3Addr := fmt.Sprintf("%p", &pv3) 1238 v3t := "func(int, string) (bool, error)" 1239 v3s := fmt.Sprintf("%p", v3) 1240 addFormatterTest("%v", v3, v3s) 1241 addFormatterTest("%v", pv3, "<*>"+v3s) 1242 addFormatterTest("%v", &pv3, "<**>"+v3s) 1243 addFormatterTest("%+v", nv3, "<nil>") 1244 addFormatterTest("%+v", v3, v3s) 1245 addFormatterTest("%+v", pv3, "<*>("+v3Addr+")"+v3s) 1246 addFormatterTest("%+v", &pv3, "<**>("+pv3Addr+"->"+v3Addr+")"+v3s) 1247 addFormatterTest("%+v", nv3, "<nil>") 1248 addFormatterTest("%#v", v3, "("+v3t+")"+v3s) 1249 addFormatterTest("%#v", pv3, "(*"+v3t+")"+v3s) 1250 addFormatterTest("%#v", &pv3, "(**"+v3t+")"+v3s) 1251 addFormatterTest("%#v", nv3, "(*"+v3t+")"+"<nil>") 1252 addFormatterTest("%#+v", v3, "("+v3t+")"+v3s) 1253 addFormatterTest("%#+v", pv3, "(*"+v3t+")("+v3Addr+")"+v3s) 1254 addFormatterTest("%#+v", &pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")"+v3s) 1255 addFormatterTest("%#+v", nv3, "(*"+v3t+")"+"<nil>") 1256 } 1257 1258 func addCircularFormatterTests() { 1259 // Struct that is circular through self referencing. 1260 type circular struct { 1261 c *circular 1262 } 1263 v := circular{nil} 1264 v.c = &v 1265 pv := &v 1266 vAddr := fmt.Sprintf("%p", pv) 1267 pvAddr := fmt.Sprintf("%p", &pv) 1268 vt := "spew_test.circular" 1269 vs := "{<*>{<*><shown>}}" 1270 vs2 := "{<*><shown>}" 1271 vs3 := "{c:<*>(" + vAddr + "){c:<*>(" + vAddr + ")<shown>}}" 1272 vs4 := "{c:<*>(" + vAddr + ")<shown>}" 1273 vs5 := "{c:(*" + vt + "){c:(*" + vt + ")<shown>}}" 1274 vs6 := "{c:(*" + vt + ")<shown>}" 1275 vs7 := "{c:(*" + vt + ")(" + vAddr + "){c:(*" + vt + ")(" + vAddr + 1276 ")<shown>}}" 1277 vs8 := "{c:(*" + vt + ")(" + vAddr + ")<shown>}" 1278 addFormatterTest("%v", v, vs) 1279 addFormatterTest("%v", pv, "<*>"+vs2) 1280 addFormatterTest("%v", &pv, "<**>"+vs2) 1281 addFormatterTest("%+v", v, vs3) 1282 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs4) 1283 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs4) 1284 addFormatterTest("%#v", v, "("+vt+")"+vs5) 1285 addFormatterTest("%#v", pv, "(*"+vt+")"+vs6) 1286 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs6) 1287 addFormatterTest("%#+v", v, "("+vt+")"+vs7) 1288 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs8) 1289 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs8) 1290 1291 // Structs that are circular through cross referencing. 1292 v2 := xref1{nil} 1293 ts2 := xref2{&v2} 1294 v2.ps2 = &ts2 1295 pv2 := &v2 1296 ts2Addr := fmt.Sprintf("%p", &ts2) 1297 v2Addr := fmt.Sprintf("%p", pv2) 1298 pv2Addr := fmt.Sprintf("%p", &pv2) 1299 v2t := "spew_test.xref1" 1300 v2t2 := "spew_test.xref2" 1301 v2s := "{<*>{<*>{<*><shown>}}}" 1302 v2s2 := "{<*>{<*><shown>}}" 1303 v2s3 := "{ps2:<*>(" + ts2Addr + "){ps1:<*>(" + v2Addr + "){ps2:<*>(" + 1304 ts2Addr + ")<shown>}}}" 1305 v2s4 := "{ps2:<*>(" + ts2Addr + "){ps1:<*>(" + v2Addr + ")<shown>}}" 1306 v2s5 := "{ps2:(*" + v2t2 + "){ps1:(*" + v2t + "){ps2:(*" + v2t2 + 1307 ")<shown>}}}" 1308 v2s6 := "{ps2:(*" + v2t2 + "){ps1:(*" + v2t + ")<shown>}}" 1309 v2s7 := "{ps2:(*" + v2t2 + ")(" + ts2Addr + "){ps1:(*" + v2t + 1310 ")(" + v2Addr + "){ps2:(*" + v2t2 + ")(" + ts2Addr + 1311 ")<shown>}}}" 1312 v2s8 := "{ps2:(*" + v2t2 + ")(" + ts2Addr + "){ps1:(*" + v2t + 1313 ")(" + v2Addr + ")<shown>}}" 1314 addFormatterTest("%v", v2, v2s) 1315 addFormatterTest("%v", pv2, "<*>"+v2s2) 1316 addFormatterTest("%v", &pv2, "<**>"+v2s2) 1317 addFormatterTest("%+v", v2, v2s3) 1318 addFormatterTest("%+v", pv2, "<*>("+v2Addr+")"+v2s4) 1319 addFormatterTest("%+v", &pv2, "<**>("+pv2Addr+"->"+v2Addr+")"+v2s4) 1320 addFormatterTest("%#v", v2, "("+v2t+")"+v2s5) 1321 addFormatterTest("%#v", pv2, "(*"+v2t+")"+v2s6) 1322 addFormatterTest("%#v", &pv2, "(**"+v2t+")"+v2s6) 1323 addFormatterTest("%#+v", v2, "("+v2t+")"+v2s7) 1324 addFormatterTest("%#+v", pv2, "(*"+v2t+")("+v2Addr+")"+v2s8) 1325 addFormatterTest("%#+v", &pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")"+v2s8) 1326 1327 // Structs that are indirectly circular. 1328 v3 := indirCir1{nil} 1329 tic2 := indirCir2{nil} 1330 tic3 := indirCir3{&v3} 1331 tic2.ps3 = &tic3 1332 v3.ps2 = &tic2 1333 pv3 := &v3 1334 tic2Addr := fmt.Sprintf("%p", &tic2) 1335 tic3Addr := fmt.Sprintf("%p", &tic3) 1336 v3Addr := fmt.Sprintf("%p", pv3) 1337 pv3Addr := fmt.Sprintf("%p", &pv3) 1338 v3t := "spew_test.indirCir1" 1339 v3t2 := "spew_test.indirCir2" 1340 v3t3 := "spew_test.indirCir3" 1341 v3s := "{<*>{<*>{<*>{<*><shown>}}}}" 1342 v3s2 := "{<*>{<*>{<*><shown>}}}" 1343 v3s3 := "{ps2:<*>(" + tic2Addr + "){ps3:<*>(" + tic3Addr + "){ps1:<*>(" + 1344 v3Addr + "){ps2:<*>(" + tic2Addr + ")<shown>}}}}" 1345 v3s4 := "{ps2:<*>(" + tic2Addr + "){ps3:<*>(" + tic3Addr + "){ps1:<*>(" + 1346 v3Addr + ")<shown>}}}" 1347 v3s5 := "{ps2:(*" + v3t2 + "){ps3:(*" + v3t3 + "){ps1:(*" + v3t + 1348 "){ps2:(*" + v3t2 + ")<shown>}}}}" 1349 v3s6 := "{ps2:(*" + v3t2 + "){ps3:(*" + v3t3 + "){ps1:(*" + v3t + 1350 ")<shown>}}}" 1351 v3s7 := "{ps2:(*" + v3t2 + ")(" + tic2Addr + "){ps3:(*" + v3t3 + ")(" + 1352 tic3Addr + "){ps1:(*" + v3t + ")(" + v3Addr + "){ps2:(*" + v3t2 + 1353 ")(" + tic2Addr + ")<shown>}}}}" 1354 v3s8 := "{ps2:(*" + v3t2 + ")(" + tic2Addr + "){ps3:(*" + v3t3 + ")(" + 1355 tic3Addr + "){ps1:(*" + v3t + ")(" + v3Addr + ")<shown>}}}" 1356 addFormatterTest("%v", v3, v3s) 1357 addFormatterTest("%v", pv3, "<*>"+v3s2) 1358 addFormatterTest("%v", &pv3, "<**>"+v3s2) 1359 addFormatterTest("%+v", v3, v3s3) 1360 addFormatterTest("%+v", pv3, "<*>("+v3Addr+")"+v3s4) 1361 addFormatterTest("%+v", &pv3, "<**>("+pv3Addr+"->"+v3Addr+")"+v3s4) 1362 addFormatterTest("%#v", v3, "("+v3t+")"+v3s5) 1363 addFormatterTest("%#v", pv3, "(*"+v3t+")"+v3s6) 1364 addFormatterTest("%#v", &pv3, "(**"+v3t+")"+v3s6) 1365 addFormatterTest("%#+v", v3, "("+v3t+")"+v3s7) 1366 addFormatterTest("%#+v", pv3, "(*"+v3t+")("+v3Addr+")"+v3s8) 1367 addFormatterTest("%#+v", &pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")"+v3s8) 1368 } 1369 1370 func addPanicFormatterTests() { 1371 // Type that panics in its Stringer interface. 1372 v := panicer(127) 1373 nv := (*panicer)(nil) 1374 pv := &v 1375 vAddr := fmt.Sprintf("%p", pv) 1376 pvAddr := fmt.Sprintf("%p", &pv) 1377 vt := "spew_test.panicer" 1378 vs := "(PANIC=test panic)127" 1379 addFormatterTest("%v", v, vs) 1380 addFormatterTest("%v", pv, "<*>"+vs) 1381 addFormatterTest("%v", &pv, "<**>"+vs) 1382 addFormatterTest("%v", nv, "<nil>") 1383 addFormatterTest("%+v", v, vs) 1384 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) 1385 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) 1386 addFormatterTest("%+v", nv, "<nil>") 1387 addFormatterTest("%#v", v, "("+vt+")"+vs) 1388 addFormatterTest("%#v", pv, "(*"+vt+")"+vs) 1389 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) 1390 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 1391 addFormatterTest("%#+v", v, "("+vt+")"+vs) 1392 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) 1393 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) 1394 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 1395 } 1396 1397 func addErrorFormatterTests() { 1398 // Type that has a custom Error interface. 1399 v := customError(127) 1400 nv := (*customError)(nil) 1401 pv := &v 1402 vAddr := fmt.Sprintf("%p", pv) 1403 pvAddr := fmt.Sprintf("%p", &pv) 1404 vt := "spew_test.customError" 1405 vs := "error: 127" 1406 addFormatterTest("%v", v, vs) 1407 addFormatterTest("%v", pv, "<*>"+vs) 1408 addFormatterTest("%v", &pv, "<**>"+vs) 1409 addFormatterTest("%v", nv, "<nil>") 1410 addFormatterTest("%+v", v, vs) 1411 addFormatterTest("%+v", pv, "<*>("+vAddr+")"+vs) 1412 addFormatterTest("%+v", &pv, "<**>("+pvAddr+"->"+vAddr+")"+vs) 1413 addFormatterTest("%+v", nv, "<nil>") 1414 addFormatterTest("%#v", v, "("+vt+")"+vs) 1415 addFormatterTest("%#v", pv, "(*"+vt+")"+vs) 1416 addFormatterTest("%#v", &pv, "(**"+vt+")"+vs) 1417 addFormatterTest("%#v", nv, "(*"+vt+")"+"<nil>") 1418 addFormatterTest("%#+v", v, "("+vt+")"+vs) 1419 addFormatterTest("%#+v", pv, "(*"+vt+")("+vAddr+")"+vs) 1420 addFormatterTest("%#+v", &pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")"+vs) 1421 addFormatterTest("%#+v", nv, "(*"+vt+")"+"<nil>") 1422 } 1423 1424 func addPassthroughFormatterTests() { 1425 // %x passthrough with uint. 1426 v := uint(4294967295) 1427 pv := &v 1428 vAddr := fmt.Sprintf("%x", pv) 1429 pvAddr := fmt.Sprintf("%x", &pv) 1430 vs := "ffffffff" 1431 addFormatterTest("%x", v, vs) 1432 addFormatterTest("%x", pv, vAddr) 1433 addFormatterTest("%x", &pv, pvAddr) 1434 1435 // %#x passthrough with uint. 1436 v2 := int(2147483647) 1437 pv2 := &v2 1438 v2Addr := fmt.Sprintf("%#x", pv2) 1439 pv2Addr := fmt.Sprintf("%#x", &pv2) 1440 v2s := "0x7fffffff" 1441 addFormatterTest("%#x", v2, v2s) 1442 addFormatterTest("%#x", pv2, v2Addr) 1443 addFormatterTest("%#x", &pv2, pv2Addr) 1444 1445 // %f passthrough with precision. 1446 addFormatterTest("%.2f", 3.1415, "3.14") 1447 addFormatterTest("%.3f", 3.1415, "3.142") 1448 addFormatterTest("%.4f", 3.1415, "3.1415") 1449 1450 // %f passthrough with width and precision. 1451 addFormatterTest("%5.2f", 3.1415, " 3.14") 1452 addFormatterTest("%6.3f", 3.1415, " 3.142") 1453 addFormatterTest("%7.4f", 3.1415, " 3.1415") 1454 1455 // %d passthrough with width. 1456 addFormatterTest("%3d", 127, "127") 1457 addFormatterTest("%4d", 127, " 127") 1458 addFormatterTest("%5d", 127, " 127") 1459 1460 // %q passthrough with string. 1461 addFormatterTest("%q", "test", "\"test\"") 1462 } 1463 1464 // TestFormatter executes all of the tests described by formatterTests. 1465 func TestFormatter(t *testing.T) { 1466 // Setup tests. 1467 addIntFormatterTests() 1468 addUintFormatterTests() 1469 addBoolFormatterTests() 1470 addFloatFormatterTests() 1471 addComplexFormatterTests() 1472 addArrayFormatterTests() 1473 addSliceFormatterTests() 1474 addStringFormatterTests() 1475 addInterfaceFormatterTests() 1476 addMapFormatterTests() 1477 addStructFormatterTests() 1478 addUintptrFormatterTests() 1479 addUnsafePointerFormatterTests() 1480 addChanFormatterTests() 1481 addFuncFormatterTests() 1482 addCircularFormatterTests() 1483 addPanicFormatterTests() 1484 addErrorFormatterTests() 1485 addPassthroughFormatterTests() 1486 1487 t.Logf("Running %d tests", len(formatterTests)) 1488 for i, test := range formatterTests { 1489 buf := new(bytes.Buffer) 1490 spew.Fprintf(buf, test.format, test.in) 1491 s := buf.String() 1492 if testFailed(s, test.wants) { 1493 t.Errorf("Formatter #%d format: %s got: %s %s", i, test.format, s, 1494 stringizeWants(test.wants)) 1495 continue 1496 } 1497 } 1498 } 1499 1500 type testStruct struct { 1501 x int 1502 } 1503 1504 func (ts testStruct) String() string { 1505 return fmt.Sprintf("ts.%d", ts.x) 1506 } 1507 1508 type testStructP struct { 1509 x int 1510 } 1511 1512 func (ts *testStructP) String() string { 1513 return fmt.Sprintf("ts.%d", ts.x) 1514 } 1515 1516 func TestPrintSortedKeys(t *testing.T) { 1517 cfg := spew.ConfigState{SortKeys: true} 1518 s := cfg.Sprint(map[int]string{1: "1", 3: "3", 2: "2"}) 1519 expected := "map[1:1 2:2 3:3]" 1520 if s != expected { 1521 t.Errorf("Sorted keys mismatch 1:\n %v %v", s, expected) 1522 } 1523 1524 s = cfg.Sprint(map[stringer]int{"1": 1, "3": 3, "2": 2}) 1525 expected = "map[stringer 1:1 stringer 2:2 stringer 3:3]" 1526 if s != expected { 1527 t.Errorf("Sorted keys mismatch 2:\n %v %v", s, expected) 1528 } 1529 1530 s = cfg.Sprint(map[pstringer]int{pstringer("1"): 1, pstringer("3"): 3, pstringer("2"): 2}) 1531 expected = "map[stringer 1:1 stringer 2:2 stringer 3:3]" 1532 if spew.UnsafeDisabled { 1533 expected = "map[1:1 2:2 3:3]" 1534 } 1535 if s != expected { 1536 t.Errorf("Sorted keys mismatch 3:\n %v %v", s, expected) 1537 } 1538 1539 s = cfg.Sprint(map[testStruct]int{testStruct{1}: 1, testStruct{3}: 3, testStruct{2}: 2}) 1540 expected = "map[ts.1:1 ts.2:2 ts.3:3]" 1541 if s != expected { 1542 t.Errorf("Sorted keys mismatch 4:\n %v %v", s, expected) 1543 } 1544 1545 if !spew.UnsafeDisabled { 1546 s = cfg.Sprint(map[testStructP]int{testStructP{1}: 1, testStructP{3}: 3, testStructP{2}: 2}) 1547 expected = "map[ts.1:1 ts.2:2 ts.3:3]" 1548 if s != expected { 1549 t.Errorf("Sorted keys mismatch 5:\n %v %v", s, expected) 1550 } 1551 } 1552 1553 s = cfg.Sprint(map[customError]int{customError(1): 1, customError(3): 3, customError(2): 2}) 1554 expected = "map[error: 1:1 error: 2:2 error: 3:3]" 1555 if s != expected { 1556 t.Errorf("Sorted keys mismatch 6:\n %v %v", s, expected) 1557 } 1558 }