format_test.go 56 KB

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