├── .gitignore ├── LICENSE ├── README.md ├── bypass.go ├── bypasssafe.go ├── common.go ├── common_test.go ├── config.go ├── doc.go ├── dump.go ├── dump_test.go ├── dumpcgo_test.go ├── dumpnocgo_test.go ├── example_test.go ├── format.go ├── format_test.go ├── go.mod ├── hex.go ├── internal_test.go ├── internalunsafe_test.go ├── spew.go ├── spew_test.go └── testdata └── dumpcgo.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Common files using profiling 15 | *.cpu 16 | *.mem 17 | 18 | # Dependency directories (remove the comment below to include it) 19 | # vendor/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ISC License 2 | 3 | Copyright (c) 2012-2016 Dave Collins 4 | Copyright (c) 2021 Anner van Hardenbroek 5 | 6 | Permission to use, copy, modify, and/or distribute this software for any 7 | purpose with or without fee is hereby granted, provided that the above 8 | copyright notice and this permission notice appear in all copies. 9 | 10 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | spew 2 | ==== 3 | 4 | [![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org) 5 | 6 | Spew implements a deep pretty printer for Go data structures to aid in 7 | debugging. A comprehensive suite of tests with 100% test coverage is provided 8 | to ensure proper functionality. Spew is licensed under the liberal ISC license, 9 | so it may be used in open source or commercial projects. 10 | 11 | If you're interested in reading about how this package came to life and some 12 | of the challenges involved in providing a deep pretty printer, there is a blog 13 | post about it 14 | [here](https://web.archive.org/web/20160304013555/https://blog.cyphertite.com/go-spew-a-journey-into-dumping-go-data-structures/). 15 | 16 | ## Documentation 17 | 18 | [![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](http://godoc.org/github.com/spewerspew/spew) 19 | 20 | Full `go doc` style documentation for the project can be viewed online without 21 | installing this package by using the excellent GoDoc site here: 22 | http://godoc.org/github.com/spewerspew/spew 23 | 24 | ## Installation 25 | 26 | ```bash 27 | $ go get -u github.com/spewerspew/spew 28 | ``` 29 | 30 | ## Quick Start 31 | 32 | Add this import line to the file you're working in: 33 | 34 | ```Go 35 | import "github.com/spewerspew/spew" 36 | ``` 37 | 38 | To dump a variable with full newlines, indentation, type, and pointer 39 | information use Dump, Fdump, or Sdump: 40 | 41 | ```Go 42 | spew.Dump(myVar1, myVar2, ...) 43 | spew.Fdump(someWriter, myVar1, myVar2, ...) 44 | str := spew.Sdump(myVar1, myVar2, ...) 45 | ``` 46 | 47 | Alternatively, if you would prefer to use format strings with a compacted inline 48 | printing style, use the convenience wrappers Printf, Fprintf, etc with %v (most 49 | compact), %+v (adds pointer addresses), %#v (adds types), or %#+v (adds types 50 | and pointer addresses): 51 | 52 | ```Go 53 | spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2) 54 | spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 55 | spew.Fprintf(someWriter, "myVar1: %v -- myVar2: %+v", myVar1, myVar2) 56 | spew.Fprintf(someWriter, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 57 | ``` 58 | 59 | ## Debugging a Web Application Example 60 | 61 | Here is an example of how you can use `spew.Sdump()` to help debug a web application. Please be sure to wrap your output using the `html.EscapeString()` function for safety reasons. You should also only use this debugging technique in a development environment, never in production. 62 | 63 | ```Go 64 | package main 65 | 66 | import ( 67 | "fmt" 68 | "html" 69 | "net/http" 70 | 71 | "github.com/spewerspew/spew" 72 | ) 73 | 74 | func handler(w http.ResponseWriter, r *http.Request) { 75 | w.Header().Set("Content-Type", "text/html") 76 | fmt.Fprintf(w, "Hi there, %s!", r.URL.Path[1:]) 77 | fmt.Fprintf(w, "") 78 | } 79 | 80 | func main() { 81 | http.HandleFunc("/", handler) 82 | http.ListenAndServe(":8080", nil) 83 | } 84 | ``` 85 | 86 | ## Sample Dump Output 87 | 88 | ``` 89 | (main.Foo) { 90 | unexportedField: (*main.Bar)(0xf84002e210)({ 91 | flag: (main.Flag) flagTwo, 92 | data: (uintptr) 93 | }), 94 | ExportedField: (map[interface {}]interface {}) { 95 | (string) "one": (bool) true 96 | } 97 | } 98 | ([]uint8) { 99 | 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... | 100 | 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0| 101 | 00000020 31 32 |12| 102 | } 103 | ``` 104 | 105 | ## Sample Formatter Output 106 | 107 | Double pointer to a uint8: 108 | ``` 109 | %v: <**>5 110 | %+v: <**>(0xf8400420d0->0xf8400420c8)5 111 | %#v: (**uint8)5 112 | %#+v: (**uint8)(0xf8400420d0->0xf8400420c8)5 113 | ``` 114 | 115 | Pointer to circular struct with a uint8 field and a pointer to itself: 116 | ``` 117 | %v: <*>{1 <*>} 118 | %+v: <*>(0xf84003e260){ui8:1 c:<*>(0xf84003e260)} 119 | %#v: (*main.circular){ui8:(uint8)1 c:(*main.circular)} 120 | %#+v: (*main.circular)(0xf84003e260){ui8:(uint8)1 c:(*main.circular)(0xf84003e260)} 121 | ``` 122 | 123 | ## Configuration Options 124 | 125 | Configuration of spew is handled by fields in the ConfigState type. For 126 | convenience, all of the top-level functions use a global state available via the 127 | spew.Config global. 128 | 129 | It is also possible to create a ConfigState instance that provides methods 130 | equivalent to the top-level functions. This allows concurrent configuration 131 | options. See the ConfigState documentation for more details. 132 | 133 | ``` 134 | * Indent 135 | String to use for each indentation level for Dump functions. 136 | It is a single space by default. A popular alternative is "\t". 137 | 138 | * MaxDepth 139 | Maximum number of levels to descend into nested data structures. 140 | There is no limit by default. 141 | 142 | * DisableMethods 143 | Disables invocation of error and Stringer interface methods. 144 | Method invocation is enabled by default. 145 | 146 | * DisablePointerMethods 147 | Disables invocation of error and Stringer interface methods on types 148 | which only accept pointer receivers from non-pointer variables. This option 149 | relies on access to the unsafe package, so it will not have any effect when 150 | running in environments without access to the unsafe package such as Google 151 | App Engine or with the "safe" build tag specified. 152 | Pointer method invocation is enabled by default. 153 | 154 | * DisablePointerAddresses 155 | DisablePointerAddresses specifies whether to disable the printing of 156 | pointer addresses. This is useful when diffing data structures in tests. 157 | 158 | * DisableCapacities 159 | DisableCapacities specifies whether to disable the printing of capacities 160 | for arrays, slices, maps and channels. This is useful when diffing data 161 | structures in tests. 162 | 163 | * ContinueOnMethod 164 | Enables recursion into types after invoking error and Stringer interface 165 | methods. Recursion after method invocation is disabled by default. 166 | 167 | * SortKeys 168 | Specifies map keys should be sorted before being printed. Use 169 | this to have a more deterministic, diffable output. Note that 170 | only native types (bool, int, uint, floats, uintptr and string) 171 | and types which implement error or Stringer interfaces are supported, 172 | with other types sorted according to the reflect.Value.String() output 173 | which guarantees display stability. Natural map order is used by 174 | default. 175 | 176 | * SpewKeys 177 | SpewKeys specifies that, as a last resort attempt, map keys should be 178 | spewed to strings and sorted by those strings. This is only considered 179 | if SortKeys is true. 180 | 181 | ``` 182 | 183 | ## Unsafe Package Dependency 184 | 185 | This package relies on the unsafe package to perform some of the more advanced 186 | features, however it also supports a "limited" mode which allows it to work in 187 | environments where the unsafe package is not available. By default, it will 188 | operate in this mode on Google App Engine and when compiled with GopherJS. The 189 | "safe" build tag may also be specified to force the package to build without 190 | using the unsafe package. 191 | 192 | ## License 193 | 194 | Spew is licensed under the [copyfree](http://copyfree.org) ISC License. 195 | -------------------------------------------------------------------------------- /bypass.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2016 Dave Collins 2 | // Copyright (c) 2021 Anner van Hardenbroek 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 | // NOTE: Due to the following build constraints, this file will only be compiled 17 | // when the code is not running on Google App Engine, compiled by GopherJS, and 18 | // "-tags safe" or "-tags purego" is not added to the go build command line. 19 | // The "disableunsafe" tag is deprecated and thus should not be used. 20 | // Go versions prior to 1.4 are disabled because they use a different layout 21 | // for interfaces which make the implementation of unsafeReflectValue more complex. 22 | //go:build !js && !appengine && !safe && !purego && !disableunsafe && go1.4 23 | // +build !js,!appengine,!safe,!purego,!disableunsafe,go1.4 24 | 25 | package spew 26 | 27 | import ( 28 | "reflect" 29 | "unsafe" 30 | ) 31 | 32 | const ( 33 | // UnsafeDisabled is a build-time constant which specifies whether or 34 | // not access to the unsafe package is available. 35 | UnsafeDisabled = false 36 | ) 37 | 38 | type flag uintptr 39 | 40 | var ( 41 | // flagRO indicates whether the value field of a reflect.Value 42 | // is read-only. 43 | flagRO flag 44 | 45 | // flagAddr indicates whether the address of the reflect.Value's 46 | // value may be taken. 47 | flagAddr flag 48 | ) 49 | 50 | // flagKindMask holds the bits that make up the kind 51 | // part of the flags field. In all the supported versions, 52 | // it is in the lower 5 bits. 53 | const flagKindMask = flag(0x1f) 54 | 55 | // Different versions of Go have used different 56 | // bit layouts for the flags type. This table 57 | // records the known combinations. 58 | var okFlags = []struct { 59 | ro, addr flag 60 | }{{ 61 | // From Go 1.4 to 1.5 62 | ro: 1 << 5, 63 | addr: 1 << 7, 64 | }, { 65 | // Up to Go tip. 66 | ro: 1<<5 | 1<<6, 67 | addr: 1 << 8, 68 | }} 69 | 70 | var flagValOffset = func() uintptr { 71 | field, ok := reflect.TypeOf(reflect.Value{}).FieldByName("flag") 72 | if !ok { 73 | panic("reflect.Value has no flag field") 74 | } 75 | return field.Offset 76 | }() 77 | 78 | // flagField returns a pointer to the flag field of a reflect.Value. 79 | func flagField(v *reflect.Value) *flag { 80 | return (*flag)(unsafe.Pointer(uintptr(unsafe.Pointer(v)) + flagValOffset)) 81 | } 82 | 83 | // unsafeReflectValue converts the passed reflect.Value into a one that bypasses 84 | // the typical safety restrictions preventing access to unaddressable and 85 | // unexported data. It works by digging the raw pointer to the underlying 86 | // value out of the protected value and generating a new unprotected (unsafe) 87 | // reflect.Value to it. 88 | // 89 | // This allows us to check for implementations of the Stringer and error 90 | // interfaces to be used for pretty printing ordinarily unaddressable and 91 | // inaccessible values such as unexported struct fields. 92 | func unsafeReflectValue(v reflect.Value) reflect.Value { 93 | if !v.IsValid() || (v.CanInterface() && v.CanAddr()) { 94 | return v 95 | } 96 | flagFieldPtr := flagField(&v) 97 | *flagFieldPtr &^= flagRO 98 | *flagFieldPtr |= flagAddr 99 | return v 100 | } 101 | 102 | // Sanity checks against future reflect package changes 103 | // to the type or semantics of the Value.flag field. 104 | func init() { 105 | field, ok := reflect.TypeOf(reflect.Value{}).FieldByName("flag") 106 | if !ok { 107 | panic("reflect.Value has no flag field") 108 | } 109 | if field.Type.Kind() != reflect.TypeOf(flag(0)).Kind() { 110 | panic("reflect.Value flag field has changed kind") 111 | } 112 | type t0 int 113 | var t struct { 114 | A t0 115 | // t0 will have flagEmbedRO set. 116 | t0 117 | // a will have flagStickyRO set 118 | a t0 119 | } 120 | vA := reflect.ValueOf(t).FieldByName("A") 121 | va := reflect.ValueOf(t).FieldByName("a") 122 | vt0 := reflect.ValueOf(t).FieldByName("t0") 123 | 124 | // Infer flagRO from the difference between the flags 125 | // for the (otherwise identical) fields in t. 126 | flagPublic := *flagField(&vA) 127 | flagWithRO := *flagField(&va) | *flagField(&vt0) 128 | flagRO = flagPublic ^ flagWithRO 129 | 130 | // Infer flagAddr from the difference between a value 131 | // taken from a pointer and not. 132 | vPtrA := reflect.ValueOf(&t).Elem().FieldByName("A") 133 | flagNoPtr := *flagField(&vA) 134 | flagPtr := *flagField(&vPtrA) 135 | flagAddr = flagNoPtr ^ flagPtr 136 | 137 | // Check that the inferred flags tally with one of the known versions. 138 | for _, f := range okFlags { 139 | if flagRO == f.ro && flagAddr == f.addr { 140 | return 141 | } 142 | } 143 | panic("reflect.Value read-only flag has changed semantics") 144 | } 145 | -------------------------------------------------------------------------------- /bypasssafe.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2016 Dave Collins 2 | // Copyright (c) 2021 Anner van Hardenbroek 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 | // NOTE: Due to the following build constraints, this file will only be compiled 17 | // when the code is running on Google App Engine, compiled by GopherJS, or 18 | // "-tags safe" or "-tags purego" is added to the go build command line. 19 | // The "disableunsafe" tag is deprecated and thus should not be used. 20 | //go:build js || appengine || safe || purego || disableunsafe || !go1.4 21 | // +build js appengine safe purego disableunsafe !go1.4 22 | 23 | package spew 24 | 25 | import "reflect" 26 | 27 | const ( 28 | // UnsafeDisabled is a build-time constant which specifies whether or 29 | // not access to the unsafe package is available. 30 | UnsafeDisabled = true 31 | ) 32 | 33 | // unsafeReflectValue typically converts the passed reflect.Value into a one 34 | // that bypasses the typical safety restrictions preventing access to 35 | // unaddressable and unexported data. However, doing this relies on access to 36 | // the unsafe package. This is a stub version which simply returns the passed 37 | // reflect.Value when the unsafe package is not available. 38 | func unsafeReflectValue(v reflect.Value) reflect.Value { 39 | return v 40 | } 41 | -------------------------------------------------------------------------------- /common.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * Copyright (c) 2021, 2023 Anner van Hardenbroek 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | 18 | package spew 19 | 20 | import ( 21 | "bytes" 22 | "fmt" 23 | "io" 24 | "reflect" 25 | "sort" 26 | "strconv" 27 | "sync" 28 | ) 29 | 30 | // Some constants in the form of bytes to avoid string overhead. This mirrors 31 | // the technique used in the fmt package. 32 | var ( 33 | panicBytes = []byte("(PANIC=") 34 | plusBytes = []byte("+") 35 | iBytes = []byte("i") 36 | trueBytes = []byte("true") 37 | falseBytes = []byte("false") 38 | interfaceBytes = []byte("(interface {})") 39 | commaNewlineBytes = []byte(",\n") 40 | newlineBytes = []byte("\n") 41 | openBraceBytes = []byte("{") 42 | openBraceNewlineBytes = []byte("{\n") 43 | closeBraceBytes = []byte("}") 44 | asteriskBytes = []byte("*") 45 | colonBytes = []byte(":") 46 | colonSpaceBytes = []byte(": ") 47 | openParenBytes = []byte("(") 48 | closeParenBytes = []byte(")") 49 | spaceBytes = []byte(" ") 50 | pointerChainBytes = []byte("->") 51 | nilAngleBytes = []byte("") 52 | maxNewlineBytes = []byte("\n") 53 | maxShortBytes = []byte("") 54 | circularBytes = []byte("") 55 | circularShortBytes = []byte("") 56 | invalidAngleBytes = []byte("") 57 | openBracketBytes = []byte("[") 58 | closeBracketBytes = []byte("]") 59 | percentBytes = []byte("%") 60 | precisionBytes = []byte(".") 61 | openAngleBytes = []byte("<") 62 | closeAngleBytes = []byte(">") 63 | openMapBytes = []byte("map[") 64 | closeMapBytes = []byte("]") 65 | lenEqualsBytes = []byte("len=") 66 | capEqualsBytes = []byte("cap=") 67 | ) 68 | 69 | // hexDigits is used to map a decimal value to a hex digit. 70 | const hexDigits = "0123456789abcdef" 71 | 72 | // catchPanic handles any panics that might occur during the handleMethods 73 | // calls. 74 | func catchPanic(w io.Writer, v reflect.Value) { 75 | if err := recover(); err != nil { 76 | w.Write(panicBytes) 77 | fmt.Fprintf(w, "%v", err) 78 | w.Write(closeParenBytes) 79 | } 80 | } 81 | 82 | // canForcedAddr tests if value v can forced into a 83 | // pointer value without a nil pointer panic. 84 | func canForcedAddr(v reflect.Value) bool { 85 | switch v.Kind() { 86 | case reflect.Chan, reflect.Func, reflect.Map: 87 | return !v.CanAddr() 88 | } 89 | return v.CanAddr() 90 | } 91 | 92 | // handleMethods attempts to call the Error and String methods on the underlying 93 | // type the passed reflect.Value represents and outputes the result to Writer w. 94 | // 95 | // It handles panics in any called methods by catching and displaying the error 96 | // as the formatted value. 97 | func handleMethods(cs *ConfigState, w io.Writer, v reflect.Value) (handled bool) { 98 | // We need an interface to check if the type implements the error or 99 | // Stringer interface. However, the reflect package won't give us an 100 | // interface on certain things like unexported struct fields in order 101 | // to enforce visibility rules. We use unsafe, when it's available, 102 | // to bypass these restrictions since this package does not mutate the 103 | // values. 104 | if !v.CanInterface() { 105 | if UnsafeDisabled { 106 | return false 107 | } 108 | 109 | v = unsafeReflectValue(v) 110 | } 111 | 112 | // Choose whether or not to do error and Stringer interface lookups against 113 | // the base type or a pointer to the base type depending on settings. 114 | // Technically calling one of these methods with a pointer receiver can 115 | // mutate the value, however, types which choose to satisify an error or 116 | // Stringer interface with a pointer receiver should not be mutating their 117 | // state inside these interface methods. 118 | if !cs.DisablePointerMethods && !UnsafeDisabled && !canForcedAddr(v) { 119 | v = unsafeReflectValue(v) 120 | } 121 | if v.CanAddr() { 122 | v = v.Addr() 123 | } 124 | 125 | // Is it an error or Stringer? 126 | switch iface := v.Interface().(type) { 127 | case error: 128 | defer catchPanic(w, v) 129 | if cs.ContinueOnMethod { 130 | w.Write(openParenBytes) 131 | io.WriteString(w, iface.Error()) 132 | w.Write(closeParenBytes) 133 | w.Write(spaceBytes) 134 | return false 135 | } 136 | io.WriteString(w, iface.Error()) 137 | return true 138 | 139 | case fmt.Stringer: 140 | defer catchPanic(w, v) 141 | if cs.ContinueOnMethod { 142 | w.Write(openParenBytes) 143 | io.WriteString(w, iface.String()) 144 | w.Write(closeParenBytes) 145 | w.Write(spaceBytes) 146 | return false 147 | } 148 | io.WriteString(w, iface.String()) 149 | return true 150 | } 151 | return false 152 | } 153 | 154 | // printInt outputs a signed integer value to Writer w. 155 | func printInt(w io.Writer, val int64, base int) { 156 | b := bufferGet() 157 | defer bufferPool.Put(b) 158 | b.SetBytes(strconv.AppendInt(b.Bytes(), val, base)) 159 | w.Write(b.Bytes()) 160 | } 161 | 162 | // printUint outputs an unsigned integer value to Writer w. 163 | func printUint(w io.Writer, val uint64, base int) { 164 | b := bufferGet() 165 | defer bufferPool.Put(b) 166 | b.SetBytes(strconv.AppendUint(b.Bytes(), val, base)) 167 | w.Write(b.Bytes()) 168 | } 169 | 170 | // printFloat outputs a floating point value using the specified precision, 171 | // which is expected to be 32 or 64bit, to Writer w. 172 | func printFloat(w io.Writer, val float64, precision int) { 173 | b := bufferGet() 174 | defer bufferPool.Put(b) 175 | b.SetBytes(strconv.AppendFloat(b.Bytes(), val, 'g', -1, precision)) 176 | w.Write(b.Bytes()) 177 | } 178 | 179 | // printComplex outputs a complex value using the specified float precision 180 | // for the real and imaginary parts to Writer w. 181 | func printComplex(w io.Writer, c complex128, floatPrecision int) { 182 | b := bufferGet() 183 | defer bufferPool.Put(b) 184 | r := real(c) 185 | w.Write(openParenBytes) 186 | b.SetBytes(strconv.AppendFloat(b.Bytes(), r, 'g', -1, floatPrecision)) 187 | w.Write(b.Bytes()) 188 | i := imag(c) 189 | if i >= 0 { 190 | w.Write(plusBytes) 191 | } 192 | b.Reset() 193 | b.SetBytes(strconv.AppendFloat(b.Bytes(), i, 'g', -1, floatPrecision)) 194 | w.Write(b.Bytes()) 195 | w.Write(iBytes) 196 | w.Write(closeParenBytes) 197 | } 198 | 199 | type buffer struct { 200 | buf []byte 201 | } 202 | 203 | func (b *buffer) Bytes() []byte { return b.buf } 204 | func (b *buffer) SetBytes(p []byte) { b.buf = p } 205 | func (b *buffer) Reset() { b.buf = b.buf[:0] } 206 | func (b *buffer) Grow(n int) { 207 | if l := len(b.buf); n < cap(b.buf)-l { 208 | b.buf = b.buf[:l+n] 209 | } else { 210 | buf := make([]byte, l+n) 211 | copy(buf, b.buf) 212 | b.buf = buf 213 | } 214 | } 215 | 216 | var bufferPool = sync.Pool{New: func() interface{} { 217 | return new(buffer) 218 | }} 219 | 220 | func bufferPut(b *buffer) { bufferPool.Put(b) } 221 | func bufferGet() *buffer { 222 | b := bufferPool.Get().(*buffer) 223 | b.Reset() 224 | return b 225 | } 226 | 227 | // printHexPtr outputs a uintptr formatted as hexadecimal with a leading '0x' 228 | // prefix to Writer w. 229 | func printHexPtr(w io.Writer, p uintptr) { 230 | // Null pointer. 231 | num := uint64(p) 232 | if num == 0 { 233 | w.Write(nilAngleBytes) 234 | return 235 | } 236 | 237 | // Max uint64 is 16 bytes in hex + 2 bytes for '0x' prefix 238 | b := bufferGet() 239 | defer bufferPut(b) 240 | b.Grow(18) 241 | buf := b.Bytes() 242 | 243 | // It's simpler to construct the hex string right to left. 244 | base := uint64(16) 245 | i := len(buf) - 1 246 | for num >= base { 247 | buf[i] = hexDigits[num%base] 248 | num /= base 249 | i-- 250 | } 251 | buf[i] = hexDigits[num] 252 | 253 | // Add '0x' prefix. 254 | i-- 255 | buf[i] = 'x' 256 | i-- 257 | buf[i] = '0' 258 | 259 | // Strip unused leading bytes and write. 260 | w.Write(buf[i:]) 261 | } 262 | 263 | // cycleInfo stores circular pointer information. 264 | type cycleInfo struct { 265 | pointers map[uintptr]int 266 | pointerChain []uintptr 267 | nilFound bool 268 | cycleFound bool 269 | indirects int 270 | } 271 | 272 | func (ci *cycleInfo) Reset() { 273 | for k := range ci.pointers { 274 | delete(ci.pointers, k) 275 | } 276 | *ci = cycleInfo{ 277 | pointers: ci.pointers, 278 | pointerChain: ci.pointerChain[:0], 279 | } 280 | } 281 | 282 | var cycleInfoPool = sync.Pool{New: func() interface{} { 283 | return &cycleInfo{pointers: make(map[uintptr]int)} 284 | }} 285 | 286 | func cycleInfoPut(ci *cycleInfo) { cycleInfoPool.Put(ci) } 287 | func cycleInfoGet() *cycleInfo { 288 | ci := cycleInfoPool.Get().(*cycleInfo) 289 | ci.Reset() 290 | return ci 291 | } 292 | 293 | // derefPtr dereferences a pointer and unpacks interfaces down 294 | // the chain while detecting circular references. 295 | func derefPtr(v reflect.Value, depth int, ci *cycleInfo) reflect.Value { 296 | ci.pointerChain = ci.pointerChain[:0] 297 | 298 | // Remove pointers at or below the current depth from map used to detect 299 | // circular refs. 300 | for k, d := range ci.pointers { 301 | if d >= depth { 302 | delete(ci.pointers, k) 303 | } 304 | } 305 | 306 | // Figure out how many levels of indirection there are by dereferencing 307 | // pointers and unpacking interfaces down the chain while detecting circular 308 | // references. 309 | ci.nilFound = false 310 | ci.cycleFound = false 311 | ci.indirects = 0 312 | ve := v 313 | for ve.Kind() == reflect.Ptr { 314 | if ve.IsNil() { 315 | ci.nilFound = true 316 | break 317 | } 318 | ci.indirects++ 319 | addr := ve.Pointer() 320 | ci.pointerChain = append(ci.pointerChain, addr) 321 | if pd, ok := ci.pointers[addr]; ok && pd < depth { 322 | ci.cycleFound = true 323 | ci.indirects-- 324 | break 325 | } 326 | ci.pointers[addr] = depth 327 | 328 | ve = ve.Elem() 329 | if ve.Kind() == reflect.Interface { 330 | if ve.IsNil() { 331 | ci.nilFound = true 332 | break 333 | } 334 | ve = ve.Elem() 335 | } 336 | } 337 | return ve 338 | } 339 | 340 | type printer interface { 341 | printArray(v reflect.Value) 342 | printString(v reflect.Value) 343 | printMap(v reflect.Value) 344 | printStruct(v reflect.Value) 345 | defaultFormat() string 346 | } 347 | 348 | func printValue(w io.Writer, p printer, v reflect.Value, kind reflect.Kind, cs *ConfigState) { 349 | // Call Stringer/error interfaces if they exist and the handle methods 350 | // flag is enabled. 351 | if !cs.DisableMethods { 352 | if kind != reflect.Invalid && kind != reflect.Interface { 353 | if handled := handleMethods(cs, w, v); handled { 354 | return 355 | } 356 | } 357 | } 358 | 359 | switch kind { 360 | case reflect.Invalid: 361 | // Do nothing. We should never get here since invalid has already 362 | // been handled before. 363 | 364 | case reflect.Bool: 365 | if v.Bool() { 366 | w.Write(trueBytes) 367 | } else { 368 | w.Write(falseBytes) 369 | } 370 | 371 | case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int: 372 | printInt(w, v.Int(), 10) 373 | 374 | case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint: 375 | printUint(w, v.Uint(), 10) 376 | 377 | case reflect.Float32: 378 | printFloat(w, v.Float(), 32) 379 | 380 | case reflect.Float64: 381 | printFloat(w, v.Float(), 64) 382 | 383 | case reflect.Complex64: 384 | printComplex(w, v.Complex(), 32) 385 | 386 | case reflect.Complex128: 387 | printComplex(w, v.Complex(), 64) 388 | 389 | case reflect.Slice: 390 | if v.IsNil() { 391 | w.Write(nilAngleBytes) 392 | break 393 | } 394 | fallthrough 395 | 396 | case reflect.Array: 397 | p.printArray(v) 398 | 399 | case reflect.String: 400 | p.printString(v) 401 | 402 | case reflect.Interface: 403 | // The only time we should get here is for nil interfaces due to 404 | // unpackValue calls. 405 | if v.IsNil() { 406 | w.Write(nilAngleBytes) 407 | } 408 | 409 | case reflect.Ptr: 410 | // Do nothing. We should never get here since pointers have already 411 | // been handled above. 412 | 413 | case reflect.Map: 414 | // nil maps should be indicated as different than empty maps 415 | if v.IsNil() { 416 | w.Write(nilAngleBytes) 417 | break 418 | } 419 | p.printMap(v) 420 | 421 | case reflect.Struct: 422 | p.printStruct(v) 423 | 424 | case reflect.Uintptr: 425 | printHexPtr(w, uintptr(v.Uint())) 426 | 427 | case reflect.UnsafePointer, reflect.Chan, reflect.Func: 428 | printHexPtr(w, v.Pointer()) 429 | 430 | // There were not any other types at the time this code was written, but 431 | // fall back to letting the default fmt package handle it if any get added. 432 | default: 433 | format := p.defaultFormat() 434 | if v.CanInterface() { 435 | fmt.Fprintf(w, format, v.Interface()) 436 | } else { 437 | fmt.Fprintf(w, format, v.String()) 438 | } 439 | } 440 | } 441 | 442 | // valuesSorter implements sort.Interface to allow a slice of reflect.Value 443 | // elements to be sorted. 444 | type valuesSorter struct { 445 | values []reflect.Value 446 | strings []string // either nil or same len as values 447 | cs *ConfigState 448 | } 449 | 450 | // newValuesSorter initializes a valuesSorter instance, which holds a set of 451 | // surrogate keys on which the data should be sorted. It uses flags in 452 | // ConfigState to decide if and how to populate those surrogate keys. 453 | func newValuesSorter(values []reflect.Value, cs *ConfigState) sort.Interface { 454 | vs := &valuesSorter{values: values, cs: cs} 455 | if canSortSimply(vs.values[0].Kind()) { 456 | return vs 457 | } 458 | if !cs.DisableMethods { 459 | vs.strings = make([]string, len(values)) 460 | for i := range vs.values { 461 | var b bytes.Buffer 462 | if !handleMethods(cs, &b, vs.values[i]) { 463 | vs.strings = nil 464 | break 465 | } 466 | vs.strings[i] = b.String() 467 | } 468 | } 469 | if vs.strings == nil && cs.SpewKeys { 470 | vs.strings = make([]string, len(values)) 471 | for i := range vs.values { 472 | vs.strings[i] = Sprintf("%#v", vs.values[i].Interface()) 473 | } 474 | } 475 | return vs 476 | } 477 | 478 | // canSortSimply tests whether a reflect.Kind is a primitive that can be sorted 479 | // directly, or whether it should be considered for sorting by surrogate keys 480 | // (if the ConfigState allows it). 481 | func canSortSimply(kind reflect.Kind) bool { 482 | // This switch parallels valueSortLess, except for the default case. 483 | switch kind { 484 | case reflect.Bool: 485 | return true 486 | case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int: 487 | return true 488 | case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint: 489 | return true 490 | case reflect.Float32, reflect.Float64: 491 | return true 492 | case reflect.String: 493 | return true 494 | case reflect.Uintptr: 495 | return true 496 | case reflect.Array: 497 | return true 498 | } 499 | return false 500 | } 501 | 502 | // Len returns the number of values in the slice. It is part of the 503 | // sort.Interface implementation. 504 | func (s *valuesSorter) Len() int { 505 | return len(s.values) 506 | } 507 | 508 | // Swap swaps the values at the passed indices. It is part of the 509 | // sort.Interface implementation. 510 | func (s *valuesSorter) Swap(i, j int) { 511 | s.values[i], s.values[j] = s.values[j], s.values[i] 512 | if s.strings != nil { 513 | s.strings[i], s.strings[j] = s.strings[j], s.strings[i] 514 | } 515 | } 516 | 517 | // valueSortLess returns whether the first value should sort before the second 518 | // value. It is used by valueSorter.Less as part of the sort.Interface 519 | // implementation. 520 | func valueSortLess(a, b reflect.Value) bool { 521 | switch a.Kind() { 522 | case reflect.Bool: 523 | return !a.Bool() && b.Bool() 524 | case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int: 525 | return a.Int() < b.Int() 526 | case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint: 527 | return a.Uint() < b.Uint() 528 | case reflect.Float32, reflect.Float64: 529 | return a.Float() < b.Float() 530 | case reflect.String: 531 | return a.String() < b.String() 532 | case reflect.Uintptr: 533 | return a.Uint() < b.Uint() 534 | case reflect.Array: 535 | // Compare the contents of both arrays. 536 | l := a.Len() 537 | for i := 0; i < l; i++ { 538 | av := a.Index(i) 539 | bv := b.Index(i) 540 | if av.Interface() == bv.Interface() { 541 | continue 542 | } 543 | return valueSortLess(av, bv) 544 | } 545 | } 546 | return a.String() < b.String() 547 | } 548 | 549 | // Less returns whether the value at index i should sort before the 550 | // value at index j. It is part of the sort.Interface implementation. 551 | func (s *valuesSorter) Less(i, j int) bool { 552 | if s.strings == nil { 553 | return valueSortLess(s.values[i], s.values[j]) 554 | } 555 | return s.strings[i] < s.strings[j] 556 | } 557 | 558 | // sortValues is a sort function that handles both native types and any type that 559 | // can be converted to error or Stringer. Other inputs are sorted according to 560 | // their Value.String() value to ensure display stability. 561 | func sortValues(values []reflect.Value, cs *ConfigState) { 562 | if len(values) == 0 { 563 | return 564 | } 565 | sort.Sort(newValuesSorter(values, cs)) 566 | } 567 | 568 | var bytesBufferPool = sync.Pool{New: func() interface{} { 569 | return new(bytes.Buffer) 570 | }} 571 | 572 | func bytesBufferPut(b *bytes.Buffer) { bytesBufferPool.Put(b) } 573 | func bytesBufferGet() *bytes.Buffer { 574 | b := bytesBufferPool.Get().(*bytes.Buffer) 575 | b.Reset() 576 | return b 577 | } 578 | -------------------------------------------------------------------------------- /common_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * Copyright (c) 2021, 2023 Anner van Hardenbroek 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | 18 | package spew_test 19 | 20 | import ( 21 | "fmt" 22 | "reflect" 23 | "sort" 24 | "strings" 25 | "testing" 26 | 27 | "github.com/spewerspew/spew" 28 | ) 29 | 30 | // custom type to test Stinger interface on non-pointer receiver. 31 | type stringer string 32 | 33 | // String implements the Stringer interface for testing invocation of custom 34 | // stringers on types with non-pointer receivers. 35 | func (s stringer) String() string { 36 | return "stringer " + string(s) 37 | } 38 | 39 | // custom type to test Stinger interface on pointer receiver. 40 | type pstringer string 41 | 42 | // String implements the Stringer interface for testing invocation of custom 43 | // stringers on types with only pointer receivers. 44 | func (s *pstringer) String() string { 45 | return "stringer " + string(*s) 46 | } 47 | 48 | // custom type to test Stringer interface on non-pointer channel receiver. 49 | type chanStringer chan struct{} 50 | 51 | // String implements the Stringer interface for testing invocations of custom 52 | // stringers on types with non-pointer receivers. 53 | func (s chanStringer) String() string { 54 | return "chan stringer" 55 | } 56 | 57 | // custom type to test Stringer interface on non-pointer func receiver. 58 | type funcStringer func() 59 | 60 | // String implements the Stringer interface for testing invocations of custom 61 | // stringers on types with non-pointer receivers. 62 | func (s funcStringer) String() string { 63 | return "func stringer" 64 | } 65 | 66 | // custom type to test Stringer interface on non-pointer map receiver. 67 | type mapStringer map[string]struct{} 68 | 69 | // String implements the Stringer interface for testing invocations of custom 70 | // stringers on types with non-pointer receivers. 71 | func (s mapStringer) String() string { 72 | return "map stringer" 73 | } 74 | 75 | // xref1 and xref2 are cross referencing structs for testing circular reference 76 | // detection. 77 | type xref1 struct { 78 | ps2 *xref2 79 | } 80 | type xref2 struct { 81 | ps1 *xref1 82 | } 83 | 84 | // indirCir1, indirCir2, and indirCir3 are used to generate an indirect circular 85 | // reference for testing detection. 86 | type indirCir1 struct { 87 | ps2 *indirCir2 88 | } 89 | type indirCir2 struct { 90 | ps3 *indirCir3 91 | } 92 | type indirCir3 struct { 93 | ps1 *indirCir1 94 | } 95 | 96 | // embed is used to test embedded structures. 97 | type embed struct { 98 | a string 99 | } 100 | 101 | // embedwrap is used to test embedded structures. 102 | type embedwrap struct { 103 | *embed 104 | e *embed 105 | } 106 | 107 | // panicer is used to intentionally cause a panic for testing spew properly 108 | // handles them 109 | type panicer int 110 | 111 | func (p panicer) String() string { 112 | panic("test panic") 113 | } 114 | 115 | // customError is used to test custom error interface invocation. 116 | type customError int 117 | 118 | func (e customError) Error() string { 119 | return fmt.Sprintf("error: %d", int(e)) 120 | } 121 | 122 | type chanError chan struct{} 123 | 124 | func (e chanError) Error() string { 125 | return "chan error" 126 | } 127 | 128 | type funcError func() 129 | 130 | func (e funcError) Error() string { 131 | return "func error" 132 | } 133 | 134 | type mapError map[string]struct{} 135 | 136 | func (e mapError) Error() string { 137 | return "map error" 138 | } 139 | 140 | // stringizeWants converts a slice of wanted test output into a format suitable 141 | // for a test error message. 142 | func stringizeWants(wants []string) string { 143 | if len(wants) == 1 { 144 | return fmt.Sprintf("want: %q", wants[0]) 145 | } 146 | var b strings.Builder 147 | for i, want := range wants { 148 | if i > 0 { 149 | fmt.Fprintf(&b, "want%d: %q\n", i+1, want) 150 | } else { 151 | fmt.Fprintf(&b, "want: %q", want) 152 | } 153 | } 154 | return b.String() 155 | } 156 | 157 | // testFailed returns whether or not a test failed by checking if the result 158 | // of the test is in the slice of wanted strings. 159 | func testFailed(result string, wants []string) bool { 160 | for _, want := range wants { 161 | if result == want { 162 | return false 163 | } 164 | } 165 | return true 166 | } 167 | 168 | type sortableStruct struct { 169 | x int 170 | } 171 | 172 | func (ss sortableStruct) String() string { 173 | return fmt.Sprintf("ss.%d", ss.x) 174 | } 175 | 176 | type unsortableStruct struct { 177 | x int 178 | } 179 | 180 | type sortTestCase struct { 181 | input []reflect.Value 182 | expected []reflect.Value 183 | } 184 | 185 | func helpTestSortValues(tests []sortTestCase, cs *spew.ConfigState, t *testing.T) { 186 | getInterfaces := func(values []reflect.Value) []interface{} { 187 | interfaces := []interface{}{} 188 | for _, v := range values { 189 | interfaces = append(interfaces, v.Interface()) 190 | } 191 | return interfaces 192 | } 193 | 194 | for _, test := range tests { 195 | spew.SortValues(test.input, cs) 196 | // reflect.DeepEqual cannot really make sense of reflect.Value, 197 | // probably because of all the pointer tricks. For instance, 198 | // v(2.0) != v(2.0) on a 32-bits system. Turn them into interface{} 199 | // instead. 200 | input := getInterfaces(test.input) 201 | expected := getInterfaces(test.expected) 202 | if !reflect.DeepEqual(input, expected) { 203 | t.Errorf("Sort mismatch:\n %v != %v", input, expected) 204 | } 205 | } 206 | } 207 | 208 | // TestSortValues ensures the sort functionality for relect.Value based sorting 209 | // works as intended. 210 | func TestSortValues(t *testing.T) { 211 | v := reflect.ValueOf 212 | 213 | a := v("a") 214 | b := v("b") 215 | c := v("c") 216 | embedA := v(embed{"a"}) 217 | embedB := v(embed{"b"}) 218 | embedC := v(embed{"c"}) 219 | tests := []sortTestCase{ 220 | // No values. 221 | { 222 | []reflect.Value{}, 223 | []reflect.Value{}, 224 | }, 225 | // Bools. 226 | { 227 | []reflect.Value{v(false), v(true), v(false)}, 228 | []reflect.Value{v(false), v(false), v(true)}, 229 | }, 230 | // Ints. 231 | { 232 | []reflect.Value{v(2), v(1), v(3)}, 233 | []reflect.Value{v(1), v(2), v(3)}, 234 | }, 235 | // Uints. 236 | { 237 | []reflect.Value{v(uint8(2)), v(uint8(1)), v(uint8(3))}, 238 | []reflect.Value{v(uint8(1)), v(uint8(2)), v(uint8(3))}, 239 | }, 240 | // Floats. 241 | { 242 | []reflect.Value{v(2.0), v(1.0), v(3.0)}, 243 | []reflect.Value{v(1.0), v(2.0), v(3.0)}, 244 | }, 245 | // Strings. 246 | { 247 | []reflect.Value{b, a, c}, 248 | []reflect.Value{a, b, c}, 249 | }, 250 | // Array 251 | { 252 | []reflect.Value{v([3]int{3, 2, 1}), v([3]int{1, 3, 2}), v([3]int{1, 2, 3})}, 253 | []reflect.Value{v([3]int{1, 2, 3}), v([3]int{1, 3, 2}), v([3]int{3, 2, 1})}, 254 | }, 255 | // Uintptrs. 256 | { 257 | []reflect.Value{v(uintptr(2)), v(uintptr(1)), v(uintptr(3))}, 258 | []reflect.Value{v(uintptr(1)), v(uintptr(2)), v(uintptr(3))}, 259 | }, 260 | // SortableStructs. 261 | { 262 | // Note: not sorted - DisableMethods is set. 263 | []reflect.Value{v(sortableStruct{2}), v(sortableStruct{1}), v(sortableStruct{3})}, 264 | []reflect.Value{v(sortableStruct{2}), v(sortableStruct{1}), v(sortableStruct{3})}, 265 | }, 266 | // UnsortableStructs. 267 | { 268 | // Note: not sorted - SpewKeys is false. 269 | []reflect.Value{v(unsortableStruct{2}), v(unsortableStruct{1}), v(unsortableStruct{3})}, 270 | []reflect.Value{v(unsortableStruct{2}), v(unsortableStruct{1}), v(unsortableStruct{3})}, 271 | }, 272 | // Invalid. 273 | { 274 | []reflect.Value{embedB, embedA, embedC}, 275 | []reflect.Value{embedB, embedA, embedC}, 276 | }, 277 | } 278 | cs := spew.ConfigState{DisableMethods: true, SpewKeys: false} 279 | helpTestSortValues(tests, &cs, t) 280 | } 281 | 282 | // TestSortValuesWithMethods ensures the sort functionality for relect.Value 283 | // based sorting works as intended when using string methods. 284 | func TestSortValuesWithMethods(t *testing.T) { 285 | v := reflect.ValueOf 286 | 287 | a := v("a") 288 | b := v("b") 289 | c := v("c") 290 | tests := []sortTestCase{ 291 | // Ints. 292 | { 293 | []reflect.Value{v(2), v(1), v(3)}, 294 | []reflect.Value{v(1), v(2), v(3)}, 295 | }, 296 | // Strings. 297 | { 298 | []reflect.Value{b, a, c}, 299 | []reflect.Value{a, b, c}, 300 | }, 301 | // SortableStructs. 302 | { 303 | []reflect.Value{v(sortableStruct{2}), v(sortableStruct{1}), v(sortableStruct{3})}, 304 | []reflect.Value{v(sortableStruct{1}), v(sortableStruct{2}), v(sortableStruct{3})}, 305 | }, 306 | // UnsortableStructs. 307 | { 308 | // Note: not sorted - SpewKeys is false. 309 | []reflect.Value{v(unsortableStruct{2}), v(unsortableStruct{1}), v(unsortableStruct{3})}, 310 | []reflect.Value{v(unsortableStruct{2}), v(unsortableStruct{1}), v(unsortableStruct{3})}, 311 | }, 312 | } 313 | cs := spew.ConfigState{DisableMethods: false, SpewKeys: false} 314 | helpTestSortValues(tests, &cs, t) 315 | } 316 | 317 | // TestSortValuesWithSpew ensures the sort functionality for relect.Value 318 | // based sorting works as intended when using spew to stringify keys. 319 | func TestSortValuesWithSpew(t *testing.T) { 320 | v := reflect.ValueOf 321 | 322 | a := v("a") 323 | b := v("b") 324 | c := v("c") 325 | tests := []sortTestCase{ 326 | // Ints. 327 | { 328 | []reflect.Value{v(2), v(1), v(3)}, 329 | []reflect.Value{v(1), v(2), v(3)}, 330 | }, 331 | // Strings. 332 | { 333 | []reflect.Value{b, a, c}, 334 | []reflect.Value{a, b, c}, 335 | }, 336 | // SortableStructs. 337 | { 338 | []reflect.Value{v(sortableStruct{2}), v(sortableStruct{1}), v(sortableStruct{3})}, 339 | []reflect.Value{v(sortableStruct{1}), v(sortableStruct{2}), v(sortableStruct{3})}, 340 | }, 341 | // UnsortableStructs. 342 | { 343 | []reflect.Value{v(unsortableStruct{2}), v(unsortableStruct{1}), v(unsortableStruct{3})}, 344 | []reflect.Value{v(unsortableStruct{1}), v(unsortableStruct{2}), v(unsortableStruct{3})}, 345 | }, 346 | } 347 | cs := spew.ConfigState{DisableMethods: true, SpewKeys: true} 348 | helpTestSortValues(tests, &cs, t) 349 | } 350 | 351 | func typeNameOf(v interface{}) string { 352 | rv := reflect.ValueOf(v) 353 | 354 | switch rv.Kind() { 355 | case reflect.Invalid: 356 | return "nil" 357 | 358 | case reflect.Ptr: 359 | for rv.Kind() == reflect.Ptr { 360 | if rv.IsNil() { 361 | return rv.Type().Elem().String() 362 | } 363 | rv = rv.Elem() 364 | } 365 | 366 | case reflect.Array, reflect.Slice: 367 | return rv.Type().String() 368 | } 369 | 370 | return rv.Type().String() 371 | } 372 | 373 | func uniqueStringSlice(s []string) []string { 374 | sort.Strings(s) 375 | 376 | i := 0 377 | for j := 0; j < len(s); j++ { 378 | if !(s[i] < s[j]) { 379 | continue 380 | } 381 | i++ 382 | s[i] = s[j] 383 | } 384 | 385 | i++ 386 | return s[:i] 387 | } 388 | 389 | func TestUniqueStringSlice(t *testing.T) { 390 | in := []string{"x", "k", "y", "m", "a", "x", "x", "y"} 391 | want := []string{"a", "k", "m", "x", "y"} 392 | 393 | got := uniqueStringSlice(in) 394 | if !reflect.DeepEqual(got, want) { 395 | t.Errorf("got %#v, want: %#v", got, want) 396 | } 397 | } 398 | -------------------------------------------------------------------------------- /config.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * Copyright (c) 2021 Anner van Hardenbroek 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | 18 | package spew 19 | 20 | import ( 21 | "fmt" 22 | "io" 23 | "os" 24 | ) 25 | 26 | // ConfigState houses the configuration options used by spew to format and 27 | // display values. There is a global instance, Config, that is used to control 28 | // all top-level Formatter and Dump functionality. Each ConfigState instance 29 | // provides methods equivalent to the top-level functions. 30 | // 31 | // The zero value for ConfigState provides no indentation. You would typically 32 | // want to set it to a space or a tab. 33 | // 34 | // Alternatively, you can use NewDefaultConfig to get a ConfigState instance 35 | // with default settings. See the documentation of NewDefaultConfig for default 36 | // values. 37 | type ConfigState struct { 38 | // Indent specifies the string to use for each indentation level. The 39 | // global config instance that all top-level functions use set this to a 40 | // single space by default. If you would like more indentation, you might 41 | // set this to a tab with "\t" or perhaps two spaces with " ". 42 | Indent string 43 | 44 | // MaxDepth controls the maximum number of levels to descend into nested 45 | // data structures. The default, 0, means there is no limit. 46 | // 47 | // NOTE: Circular data structures are properly detected, so it is not 48 | // necessary to set this value unless you specifically want to limit deeply 49 | // nested data structures. 50 | MaxDepth int 51 | 52 | // DisableMethods specifies whether or not error and Stringer interfaces are 53 | // invoked for types that implement them. 54 | DisableMethods bool 55 | 56 | // DisablePointerMethods specifies whether or not to check for and invoke 57 | // error and Stringer interfaces on types which only accept a pointer 58 | // receiver when the current type is not a pointer. 59 | // 60 | // NOTE: This might be an unsafe action since calling one of these methods 61 | // with a pointer receiver could technically mutate the value, however, 62 | // in practice, types which choose to satisify an error or Stringer 63 | // interface with a pointer receiver should not be mutating their state 64 | // inside these interface methods. As a result, this option relies on 65 | // access to the unsafe package, so it will not have any effect when 66 | // running in environments without access to the unsafe package such as 67 | // Google App Engine or with the "safe" build tag specified. 68 | DisablePointerMethods bool 69 | 70 | // DisablePointerAddresses specifies whether to disable the printing of 71 | // pointer addresses. This is useful when diffing data structures in tests. 72 | DisablePointerAddresses bool 73 | 74 | // DisableCapacities specifies whether to disable the printing of capacities 75 | // for arrays, slices, maps and channels. This is useful when diffing 76 | // data structures in tests. 77 | DisableCapacities bool 78 | 79 | // ContinueOnMethod specifies whether or not recursion should continue once 80 | // a custom error or Stringer interface is invoked. The default, false, 81 | // means it will print the results of invoking the custom error or Stringer 82 | // interface and return immediately instead of continuing to recurse into 83 | // the internals of the data type. 84 | // 85 | // NOTE: This flag does not have any effect if method invocation is disabled 86 | // via the DisableMethods or DisablePointerMethods options. 87 | ContinueOnMethod bool 88 | 89 | // SortKeys specifies map keys should be sorted before being printed. Use 90 | // this to have a more deterministic, diffable output. Note that only 91 | // native types (bool, int, uint, floats, uintptr and string) and types 92 | // that support the error or Stringer interfaces (if methods are 93 | // enabled) are supported, with other types sorted according to the 94 | // reflect.Value.String() output which guarantees display stability. 95 | SortKeys bool 96 | 97 | // SpewKeys specifies that, as a last resort attempt, map keys should 98 | // be spewed to strings and sorted by those strings. This is only 99 | // considered if SortKeys is true. 100 | SpewKeys bool 101 | } 102 | 103 | // Config is the active configuration of the top-level functions. 104 | // The configuration can be changed by modifying the contents of spew.Config. 105 | var Config = ConfigState{Indent: " "} 106 | 107 | // Errorf is a wrapper for fmt.Errorf that treats each argument as if it were 108 | // passed with a Formatter interface returned by c.NewFormatter. It returns 109 | // the formatted string as a value that satisfies error. See NewFormatter 110 | // for formatting details. 111 | // 112 | // This function is shorthand for the following syntax: 113 | // 114 | // fmt.Errorf(format, c.NewFormatter(a), c.NewFormatter(b)) 115 | func (c *ConfigState) Errorf(format string, a ...interface{}) (err error) { 116 | pv, formatters := formattersGet(c, a) 117 | defer formattersPut(pv) 118 | return fmt.Errorf(format, formatters...) 119 | } 120 | 121 | // Fprint is a wrapper for fmt.Fprint that treats each argument as if it were 122 | // passed with a Formatter interface returned by c.NewFormatter. It returns 123 | // the number of bytes written and any write error encountered. See 124 | // NewFormatter for formatting details. 125 | // 126 | // This function is shorthand for the following syntax: 127 | // 128 | // fmt.Fprint(w, c.NewFormatter(a), c.NewFormatter(b)) 129 | func (c *ConfigState) Fprint(w io.Writer, a ...interface{}) (n int, err error) { 130 | pv, formatters := formattersGet(c, a) 131 | defer formattersPut(pv) 132 | return fmt.Fprint(w, formatters...) 133 | } 134 | 135 | // Fprintf is a wrapper for fmt.Fprintf that treats each argument as if it were 136 | // passed with a Formatter interface returned by c.NewFormatter. It returns 137 | // the number of bytes written and any write error encountered. See 138 | // NewFormatter for formatting details. 139 | // 140 | // This function is shorthand for the following syntax: 141 | // 142 | // fmt.Fprintf(w, format, c.NewFormatter(a), c.NewFormatter(b)) 143 | func (c *ConfigState) Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) { 144 | pv, formatters := formattersGet(c, a) 145 | defer formattersPut(pv) 146 | return fmt.Fprintf(w, format, formatters...) 147 | } 148 | 149 | // Fprintln is a wrapper for fmt.Fprintln that treats each argument as if it 150 | // passed with a Formatter interface returned by c.NewFormatter. See 151 | // NewFormatter for formatting details. 152 | // 153 | // This function is shorthand for the following syntax: 154 | // 155 | // fmt.Fprintln(w, c.NewFormatter(a), c.NewFormatter(b)) 156 | func (c *ConfigState) Fprintln(w io.Writer, a ...interface{}) (n int, err error) { 157 | pv, formatters := formattersGet(c, a) 158 | defer formattersPut(pv) 159 | return fmt.Fprintln(w, formatters...) 160 | } 161 | 162 | // Print is a wrapper for fmt.Print that treats each argument as if it were 163 | // passed with a Formatter interface returned by c.NewFormatter. It returns 164 | // the number of bytes written and any write error encountered. See 165 | // NewFormatter for formatting details. 166 | // 167 | // This function is shorthand for the following syntax: 168 | // 169 | // fmt.Print(c.NewFormatter(a), c.NewFormatter(b)) 170 | func (c *ConfigState) Print(a ...interface{}) (n int, err error) { 171 | pv, formatters := formattersGet(c, a) 172 | defer formattersPut(pv) 173 | return fmt.Print(formatters...) 174 | } 175 | 176 | // Printf is a wrapper for fmt.Printf that treats each argument as if it were 177 | // passed with a Formatter interface returned by c.NewFormatter. It returns 178 | // the number of bytes written and any write error encountered. See 179 | // NewFormatter for formatting details. 180 | // 181 | // This function is shorthand for the following syntax: 182 | // 183 | // fmt.Printf(format, c.NewFormatter(a), c.NewFormatter(b)) 184 | func (c *ConfigState) Printf(format string, a ...interface{}) (n int, err error) { 185 | pv, formatters := formattersGet(c, a) 186 | defer formattersPut(pv) 187 | return fmt.Printf(format, formatters...) 188 | } 189 | 190 | // Println is a wrapper for fmt.Println that treats each argument as if it were 191 | // passed with a Formatter interface returned by c.NewFormatter. It returns 192 | // the number of bytes written and any write error encountered. See 193 | // NewFormatter for formatting details. 194 | // 195 | // This function is shorthand for the following syntax: 196 | // 197 | // fmt.Println(c.NewFormatter(a), c.NewFormatter(b)) 198 | func (c *ConfigState) Println(a ...interface{}) (n int, err error) { 199 | pv, formatters := formattersGet(c, a) 200 | defer formattersPut(pv) 201 | return fmt.Println(formatters...) 202 | } 203 | 204 | // Sprint is a wrapper for fmt.Sprint that treats each argument as if it were 205 | // passed with a Formatter interface returned by c.NewFormatter. It returns 206 | // the resulting string. See NewFormatter for formatting details. 207 | // 208 | // This function is shorthand for the following syntax: 209 | // 210 | // fmt.Sprint(c.NewFormatter(a), c.NewFormatter(b)) 211 | func (c *ConfigState) Sprint(a ...interface{}) string { 212 | pv, formatters := formattersGet(c, a) 213 | defer formattersPut(pv) 214 | return fmt.Sprint(formatters...) 215 | } 216 | 217 | // Sprintf is a wrapper for fmt.Sprintf that treats each argument as if it were 218 | // passed with a Formatter interface returned by c.NewFormatter. It returns 219 | // the resulting string. See NewFormatter for formatting details. 220 | // 221 | // This function is shorthand for the following syntax: 222 | // 223 | // fmt.Sprintf(format, c.NewFormatter(a), c.NewFormatter(b)) 224 | func (c *ConfigState) Sprintf(format string, a ...interface{}) string { 225 | pv, formatters := formattersGet(c, a) 226 | defer formattersPut(pv) 227 | return fmt.Sprintf(format, formatters...) 228 | } 229 | 230 | // Sprintln is a wrapper for fmt.Sprintln that treats each argument as if it 231 | // were passed with a Formatter interface returned by c.NewFormatter. It 232 | // returns the resulting string. See NewFormatter for formatting details. 233 | // 234 | // This function is shorthand for the following syntax: 235 | // 236 | // fmt.Sprintln(c.NewFormatter(a), c.NewFormatter(b)) 237 | func (c *ConfigState) Sprintln(a ...interface{}) string { 238 | pv, formatters := formattersGet(c, a) 239 | defer formattersPut(pv) 240 | return fmt.Sprintln(formatters...) 241 | } 242 | 243 | /* 244 | NewFormatter returns a custom formatter that satisfies the fmt.Formatter 245 | interface. As a result, it integrates cleanly with standard fmt package 246 | printing functions. The formatter is useful for inline printing of smaller data 247 | types similar to the standard %v format specifier. 248 | 249 | The custom formatter only responds to the %v (most compact), %+v (adds pointer 250 | addresses), %#v (adds types), and %#+v (adds types and pointer addresses) verb 251 | combinations. Any other verbs such as %x and %q will be sent to the the 252 | standard fmt package for formatting. In addition, the custom formatter ignores 253 | the width and precision arguments (however they will still work on the format 254 | specifiers not handled by the custom formatter). 255 | 256 | Typically this function shouldn't be called directly. It is much easier to make 257 | use of the custom formatter by calling one of the convenience functions such as 258 | c.Printf, c.Println, or c.Printf. 259 | */ 260 | func (c *ConfigState) NewFormatter(v interface{}) fmt.Formatter { 261 | return newFormatter(c, v) 262 | } 263 | 264 | // Fdump formats and displays the passed arguments to io.Writer w. It formats 265 | // exactly the same as Dump. 266 | func (c *ConfigState) Fdump(w io.Writer, a ...interface{}) { 267 | fdump(c, w, a...) 268 | } 269 | 270 | /* 271 | Dump displays the passed parameters to standard out with newlines, customizable 272 | indentation, and additional debug information such as complete types and all 273 | pointer addresses used to indirect to the final value. It provides the 274 | following features over the built-in printing facilities provided by the fmt 275 | package: 276 | 277 | * Pointers are dereferenced and followed 278 | * Circular data structures are detected and handled properly 279 | * Custom Stringer/error interfaces are optionally invoked, including 280 | on unexported types 281 | * Custom types which only implement the Stringer/error interfaces via 282 | a pointer receiver are optionally invoked when passing non-pointer 283 | variables 284 | * Byte arrays and slices are dumped like the hexdump -C command which 285 | includes offsets, byte values in hex, and ASCII output 286 | 287 | The configuration options are controlled by modifying the public members 288 | of c. See ConfigState for options documentation. 289 | 290 | See Fdump if you would prefer dumping to an arbitrary io.Writer or Sdump to 291 | get the formatted result as a string. 292 | */ 293 | func (c *ConfigState) Dump(a ...interface{}) { 294 | fdump(c, os.Stdout, a...) 295 | } 296 | 297 | // Sdump returns a string with the passed arguments formatted exactly the same 298 | // as Dump. 299 | func (c *ConfigState) Sdump(a ...interface{}) string { 300 | buf := bytesBufferGet() 301 | defer bytesBufferPut(buf) 302 | fdump(c, buf, a...) 303 | return buf.String() 304 | } 305 | 306 | // NewDefaultConfig returns a ConfigState with the following default settings. 307 | // 308 | // Indent: " " 309 | // MaxDepth: 0 310 | // DisableMethods: false 311 | // DisablePointerMethods: false 312 | // ContinueOnMethod: false 313 | // SortKeys: false 314 | func NewDefaultConfig() *ConfigState { 315 | return &ConfigState{Indent: " "} 316 | } 317 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 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 | Package spew implements a deep pretty printer for Go data structures to aid in 19 | debugging. 20 | 21 | A quick overview of the additional features spew provides over the built-in 22 | printing facilities for Go data types are as follows: 23 | 24 | * Pointers are dereferenced and followed 25 | * Circular data structures are detected and handled properly 26 | * Custom Stringer/error interfaces are optionally invoked, including 27 | on unexported types 28 | * Custom types which only implement the Stringer/error interfaces via 29 | a pointer receiver are optionally invoked when passing non-pointer 30 | variables 31 | * Byte arrays and slices are dumped like the hexdump -C command which 32 | includes offsets, byte values in hex, and ASCII output (only when using 33 | Dump style) 34 | 35 | There are two different approaches spew allows for dumping Go data structures: 36 | 37 | * Dump style which prints with newlines, customizable indentation, 38 | and additional debug information such as types and all pointer addresses 39 | used to indirect to the final value 40 | * A custom Formatter interface that integrates cleanly with the standard fmt 41 | package and replaces %v, %+v, %#v, and %#+v to provide inline printing 42 | similar to the default %v while providing the additional functionality 43 | outlined above and passing unsupported format verbs such as %x and %q 44 | along to fmt 45 | 46 | Quick Start 47 | 48 | This section demonstrates how to quickly get started with spew. See the 49 | sections below for further details on formatting and configuration options. 50 | 51 | To dump a variable with full newlines, indentation, type, and pointer 52 | information use Dump, Fdump, or Sdump: 53 | spew.Dump(myVar1, myVar2, ...) 54 | spew.Fdump(someWriter, myVar1, myVar2, ...) 55 | str := spew.Sdump(myVar1, myVar2, ...) 56 | 57 | Alternatively, if you would prefer to use format strings with a compacted inline 58 | printing style, use the convenience wrappers Printf, Fprintf, etc with 59 | %v (most compact), %+v (adds pointer addresses), %#v (adds types), or 60 | %#+v (adds types and pointer addresses): 61 | spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2) 62 | spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 63 | spew.Fprintf(someWriter, "myVar1: %v -- myVar2: %+v", myVar1, myVar2) 64 | spew.Fprintf(someWriter, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 65 | 66 | Configuration Options 67 | 68 | Configuration of spew is handled by fields in the ConfigState type. For 69 | convenience, all of the top-level functions use a global state available 70 | via the spew.Config global. 71 | 72 | It is also possible to create a ConfigState instance that provides methods 73 | equivalent to the top-level functions. This allows concurrent configuration 74 | options. See the ConfigState documentation for more details. 75 | 76 | The following configuration options are available: 77 | * Indent 78 | String to use for each indentation level for Dump functions. 79 | It is a single space by default. A popular alternative is "\t". 80 | 81 | * MaxDepth 82 | Maximum number of levels to descend into nested data structures. 83 | There is no limit by default. 84 | 85 | * DisableMethods 86 | Disables invocation of error and Stringer interface methods. 87 | Method invocation is enabled by default. 88 | 89 | * DisablePointerMethods 90 | Disables invocation of error and Stringer interface methods on types 91 | which only accept pointer receivers from non-pointer variables. 92 | Pointer method invocation is enabled by default. 93 | 94 | * DisablePointerAddresses 95 | DisablePointerAddresses specifies whether to disable the printing of 96 | pointer addresses. This is useful when diffing data structures in tests. 97 | 98 | * DisableCapacities 99 | DisableCapacities specifies whether to disable the printing of 100 | capacities for arrays, slices, maps and channels. This is useful when 101 | diffing data structures in tests. 102 | 103 | * ContinueOnMethod 104 | Enables recursion into types after invoking error and Stringer interface 105 | methods. Recursion after method invocation is disabled by default. 106 | 107 | * SortKeys 108 | Specifies map keys should be sorted before being printed. Use 109 | this to have a more deterministic, diffable output. Note that 110 | only native types (bool, int, uint, floats, uintptr and string) 111 | and types which implement error or Stringer interfaces are 112 | supported with other types sorted according to the 113 | reflect.Value.String() output which guarantees display 114 | stability. Natural map order is used by default. 115 | 116 | * SpewKeys 117 | Specifies that, as a last resort attempt, map keys should be 118 | spewed to strings and sorted by those strings. This is only 119 | considered if SortKeys is true. 120 | 121 | Dump Usage 122 | 123 | Simply call spew.Dump with a list of variables you want to dump: 124 | 125 | spew.Dump(myVar1, myVar2, ...) 126 | 127 | You may also call spew.Fdump if you would prefer to output to an arbitrary 128 | io.Writer. For example, to dump to standard error: 129 | 130 | spew.Fdump(os.Stderr, myVar1, myVar2, ...) 131 | 132 | A third option is to call spew.Sdump to get the formatted output as a string: 133 | 134 | str := spew.Sdump(myVar1, myVar2, ...) 135 | 136 | Sample Dump Output 137 | 138 | See the Dump example for details on the setup of the types and variables being 139 | shown here. 140 | 141 | (main.Foo) { 142 | unexportedField: (*main.Bar)(0xf84002e210)({ 143 | flag: (main.Flag) flagTwo, 144 | data: (uintptr) 145 | }), 146 | ExportedField: (map[interface {}]interface {}) (len=1) { 147 | (string) (len=3) "one": (bool) true 148 | } 149 | } 150 | 151 | Byte (and uint8) arrays and slices are displayed uniquely like the hexdump -C 152 | command as shown. 153 | ([]uint8) (len=32 cap=32) { 154 | 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... | 155 | 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0| 156 | 00000020 31 32 |12| 157 | } 158 | 159 | Custom Formatter 160 | 161 | Spew provides a custom formatter that implements the fmt.Formatter interface 162 | so that it integrates cleanly with standard fmt package printing functions. The 163 | formatter is useful for inline printing of smaller data types similar to the 164 | standard %v format specifier. 165 | 166 | The custom formatter only responds to the %v (most compact), %+v (adds pointer 167 | addresses), %#v (adds types), or %#+v (adds types and pointer addresses) verb 168 | combinations. Any other verbs such as %x and %q will be sent to the the 169 | standard fmt package for formatting. In addition, the custom formatter ignores 170 | the width and precision arguments (however they will still work on the format 171 | specifiers not handled by the custom formatter). 172 | 173 | Custom Formatter Usage 174 | 175 | The simplest way to make use of the spew custom formatter is to call one of the 176 | convenience functions such as spew.Printf, spew.Println, or spew.Printf. The 177 | functions have syntax you are most likely already familiar with: 178 | 179 | spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2) 180 | spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 181 | spew.Println(myVar, myVar2) 182 | spew.Fprintf(os.Stderr, "myVar1: %v -- myVar2: %+v", myVar1, myVar2) 183 | spew.Fprintf(os.Stderr, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 184 | 185 | See the Index for the full list convenience functions. 186 | 187 | Sample Formatter Output 188 | 189 | Double pointer to a uint8: 190 | %v: <**>5 191 | %+v: <**>(0xf8400420d0->0xf8400420c8)5 192 | %#v: (**uint8)5 193 | %#+v: (**uint8)(0xf8400420d0->0xf8400420c8)5 194 | 195 | Pointer to circular struct with a uint8 field and a pointer to itself: 196 | %v: <*>{1 <*>} 197 | %+v: <*>(0xf84003e260){ui8:1 c:<*>(0xf84003e260)} 198 | %#v: (*main.circular){ui8:(uint8)1 c:(*main.circular)} 199 | %#+v: (*main.circular)(0xf84003e260){ui8:(uint8)1 c:(*main.circular)(0xf84003e260)} 200 | 201 | See the Printf example for details on the setup of variables being shown 202 | here. 203 | 204 | Errors 205 | 206 | Since it is possible for custom Stringer/error interfaces to panic, spew 207 | detects them and handles them internally by printing the panic information 208 | inline with the output. Since spew is intended to provide deep pretty printing 209 | capabilities on structures, it intentionally does not return any errors. 210 | */ 211 | package spew 212 | -------------------------------------------------------------------------------- /dump.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * Copyright (c) 2021 Anner van Hardenbroek 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | 18 | package spew 19 | 20 | import ( 21 | "fmt" 22 | "io" 23 | "reflect" 24 | "regexp" 25 | "strconv" 26 | "strings" 27 | "sync" 28 | ) 29 | 30 | var ( 31 | // uint8Type is a reflect.Type representing a uint8. It is used to 32 | // convert cgo types to uint8 slices for hexdumping. 33 | uint8Type = reflect.TypeOf(uint8(0)) 34 | 35 | // fmtStringerType is a reflect.Type representing fmt.Stringer. 36 | // It is used to pretty print types instead of hexdump them. 37 | fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem() 38 | 39 | // cCharRE is a regular expression that matches a cgo char. 40 | // It is used to detect character arrays to hexdump them. 41 | cCharRE = regexp.MustCompile(`^.*\._Ctype_char$`) 42 | 43 | // cUnsignedCharRE is a regular expression that matches a cgo unsigned 44 | // char. It is used to detect unsigned character arrays to hexdump 45 | // them. 46 | cUnsignedCharRE = regexp.MustCompile(`^.*\._Ctype_unsignedchar$`) 47 | 48 | // cUint8tCharRE is a regular expression that matches a cgo uint8_t. 49 | // It is used to detect uint8_t arrays to hexdump them. 50 | cUint8tCharRE = regexp.MustCompile(`^.*\._Ctype_uint8_t$`) 51 | ) 52 | 53 | // indentCacheKey is used as key in the indent cache. 54 | type indentCacheKey struct { 55 | indent string 56 | depth int 57 | } 58 | 59 | // indentCache is a cache that caches indents of different lengths. 60 | var indentCache sync.Map // map[indentCacheKey]string 61 | 62 | // dumpState contains information about the state of a dump operation. 63 | type dumpState struct { 64 | w io.Writer 65 | depth int 66 | ignoreNextType bool 67 | ignoreNextIndent bool 68 | ci *cycleInfo 69 | cs *ConfigState 70 | } 71 | 72 | // indent performs indentation according to the depth level and cs.Indent 73 | // option. 74 | func (d *dumpState) indent() { 75 | if d.ignoreNextIndent { 76 | d.ignoreNextIndent = false 77 | return 78 | } 79 | for i := 0; i < d.depth; i++ { 80 | io.WriteString(d.w, d.cs.Indent) 81 | } 82 | } 83 | 84 | // unpackValue returns values inside of non-nil interfaces when possible. 85 | // This is useful for data types like structs, arrays, slices, and maps which 86 | // can contain varying types packed inside an interface. 87 | func (d *dumpState) unpackValue(v reflect.Value) reflect.Value { 88 | if v.Kind() == reflect.Interface && !v.IsNil() { 89 | v = v.Elem() 90 | } 91 | return v 92 | } 93 | 94 | // dumpPtr handles formatting of pointers by indirecting them as necessary. 95 | func (d *dumpState) dumpPtr(v reflect.Value) { 96 | // Figure out how many levels of indirection there are by dereferencing 97 | // pointers and unpacking interfaces down the chain while detecting circular 98 | // references. 99 | ve := derefPtr(v, d.depth, d.ci) 100 | 101 | // Display type information. 102 | d.w.Write(openParenBytes) 103 | for i := 0; i < d.ci.indirects; i++ { 104 | d.w.Write(asteriskBytes) 105 | } 106 | io.WriteString(d.w, ve.Type().String()) 107 | d.w.Write(closeParenBytes) 108 | 109 | // Display pointer information. 110 | if !d.cs.DisablePointerAddresses && len(d.ci.pointerChain) > 0 { 111 | d.w.Write(openParenBytes) 112 | for i, addr := range d.ci.pointerChain { 113 | if i > 0 { 114 | d.w.Write(pointerChainBytes) 115 | } 116 | printHexPtr(d.w, addr) 117 | } 118 | d.w.Write(closeParenBytes) 119 | } 120 | 121 | // Display dereferenced value. 122 | d.w.Write(openParenBytes) 123 | switch { 124 | case d.ci.nilFound: 125 | d.w.Write(nilAngleBytes) 126 | 127 | case d.ci.cycleFound: 128 | d.w.Write(circularBytes) 129 | 130 | default: 131 | d.ignoreNextType = true 132 | d.dump(ve) 133 | } 134 | d.w.Write(closeParenBytes) 135 | } 136 | 137 | // dumpSlice handles formatting of arrays and slices. Byte (uint8 under 138 | // reflection) arrays and slices are dumped in hexdump -C fashion. 139 | func (d *dumpState) dumpSlice(v reflect.Value) { 140 | // Determine whether this type should be hex dumped or not. Also, 141 | // for types which should be hexdumped, try to use the underlying data 142 | // first, then fall back to trying to convert them to a uint8 slice. 143 | var buf []uint8 144 | doConvert := false 145 | doHexDump := false 146 | numEntries := v.Len() 147 | if numEntries > 0 { 148 | vt := v.Index(0).Type() 149 | vts := vt.String() 150 | switch { 151 | // C types that need to be converted. 152 | case cCharRE.MatchString(vts): 153 | fallthrough 154 | case cUnsignedCharRE.MatchString(vts): 155 | fallthrough 156 | case cUint8tCharRE.MatchString(vts): 157 | doConvert = true 158 | 159 | // Try to use existing uint8 slices and fall back to converting 160 | // and copying if that fails. 161 | case vt.Kind() == reflect.Uint8: 162 | if vt.Implements(fmtStringerType) { 163 | doConvert = d.cs.DisableMethods 164 | } else { 165 | // We need an addressable interface to convert the type 166 | // to a byte slice. However, the reflect package won't 167 | // give us an interface on certain things like 168 | // unexported struct fields in order to enforce 169 | // visibility rules. We use unsafe, when available, to 170 | // bypass these restrictions since this package does not 171 | // mutate the values. 172 | vs := v 173 | if !vs.CanInterface() || !vs.CanAddr() { 174 | vs = unsafeReflectValue(vs) 175 | } 176 | if !UnsafeDisabled { 177 | vs = vs.Slice(0, numEntries) 178 | 179 | // Use the existing uint8 slice if it can be 180 | // type asserted. 181 | iface := vs.Interface() 182 | if slice, ok := iface.([]uint8); ok { 183 | buf = slice 184 | doHexDump = true 185 | break 186 | } 187 | } 188 | 189 | // The underlying data needs to be converted if it can't 190 | // be type asserted to a uint8 slice. 191 | doConvert = true 192 | } 193 | } 194 | 195 | // Copy and convert the underlying type if needed. 196 | if doConvert && vt.ConvertibleTo(uint8Type) { 197 | // Convert and copy each element into a uint8 byte 198 | // slice. 199 | buf = make([]uint8, numEntries) 200 | for i := 0; i < numEntries; i++ { 201 | vv := v.Index(i) 202 | buf[i] = uint8(vv.Convert(uint8Type).Uint()) 203 | } 204 | doHexDump = true 205 | } 206 | } 207 | 208 | // Hexdump the entire slice as needed. 209 | if doHexDump { 210 | var indent string 211 | key := indentCacheKey{d.cs.Indent, d.depth} 212 | if cv, ok := indentCache.Load(key); ok { 213 | indent = cv.(string) 214 | } else { 215 | indent = strings.Repeat(d.cs.Indent, d.depth) 216 | indentCache.Store(key, indent) 217 | } 218 | hexDump(d.w, buf, indent) 219 | return 220 | } 221 | 222 | // Recursively call dump for each item. 223 | for i := 0; i < numEntries; i++ { 224 | d.dump(d.unpackValue(v.Index(i))) 225 | if i < (numEntries - 1) { 226 | d.w.Write(commaNewlineBytes) 227 | } else { 228 | d.w.Write(newlineBytes) 229 | } 230 | } 231 | } 232 | 233 | // dump is the main workhorse for dumping a value. It uses the passed reflect 234 | // value to figure out what kind of object we are dealing with and formats it 235 | // appropriately. It is a recursive function, however circular data structures 236 | // are detected and handled properly. 237 | func (d *dumpState) dump(v reflect.Value) { 238 | // Handle invalid reflect values immediately. 239 | kind := v.Kind() 240 | if kind == reflect.Invalid { 241 | d.w.Write(invalidAngleBytes) 242 | return 243 | } 244 | 245 | // Handle pointers specially. 246 | if kind == reflect.Ptr { 247 | d.indent() 248 | d.dumpPtr(v) 249 | return 250 | } 251 | 252 | // Print type information unless already handled elsewhere. 253 | if !d.ignoreNextType { 254 | d.indent() 255 | d.w.Write(openParenBytes) 256 | io.WriteString(d.w, v.Type().String()) 257 | d.w.Write(closeParenBytes) 258 | d.w.Write(spaceBytes) 259 | } 260 | d.ignoreNextType = false 261 | 262 | // Display length and capacity if the built-in len and cap functions 263 | // work with the value's kind and the len/cap itself is non-zero. 264 | valueLen, valueCap := 0, 0 265 | switch v.Kind() { 266 | case reflect.Array, reflect.Slice, reflect.Chan: 267 | valueLen, valueCap = v.Len(), v.Cap() 268 | case reflect.Map, reflect.String: 269 | valueLen = v.Len() 270 | } 271 | if valueLen != 0 || !d.cs.DisableCapacities && valueCap != 0 { 272 | d.w.Write(openParenBytes) 273 | if valueLen != 0 { 274 | d.w.Write(lenEqualsBytes) 275 | printInt(d.w, int64(valueLen), 10) 276 | } 277 | if !d.cs.DisableCapacities && valueCap != 0 { 278 | if valueLen != 0 { 279 | d.w.Write(spaceBytes) 280 | } 281 | d.w.Write(capEqualsBytes) 282 | printInt(d.w, int64(valueCap), 10) 283 | } 284 | d.w.Write(closeParenBytes) 285 | d.w.Write(spaceBytes) 286 | } 287 | 288 | printValue(d.w, d, v, kind, d.cs) 289 | } 290 | 291 | func (d *dumpState) printArray(v reflect.Value) { 292 | d.w.Write(openBraceNewlineBytes) 293 | d.depth++ 294 | if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) { 295 | d.indent() 296 | d.w.Write(maxNewlineBytes) 297 | } else { 298 | d.dumpSlice(v) 299 | } 300 | d.depth-- 301 | d.indent() 302 | d.w.Write(closeBraceBytes) 303 | } 304 | 305 | func (d *dumpState) printString(v reflect.Value) { 306 | b := bufferGet() 307 | defer bufferPut(b) 308 | b.SetBytes(strconv.AppendQuote(b.Bytes(), v.String())) 309 | d.w.Write(b.Bytes()) 310 | } 311 | 312 | func (d *dumpState) printMap(v reflect.Value) { 313 | d.w.Write(openBraceNewlineBytes) 314 | d.depth++ 315 | if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) { 316 | d.indent() 317 | d.w.Write(maxNewlineBytes) 318 | } else { 319 | numEntries := v.Len() 320 | keys := v.MapKeys() 321 | if d.cs.SortKeys { 322 | sortValues(keys, d.cs) 323 | } 324 | for i, key := range keys { 325 | d.dump(d.unpackValue(key)) 326 | d.w.Write(colonSpaceBytes) 327 | d.ignoreNextIndent = true 328 | d.dump(d.unpackValue(v.MapIndex(key))) 329 | if i < (numEntries - 1) { 330 | d.w.Write(commaNewlineBytes) 331 | } else { 332 | d.w.Write(newlineBytes) 333 | } 334 | } 335 | } 336 | d.depth-- 337 | d.indent() 338 | d.w.Write(closeBraceBytes) 339 | } 340 | 341 | func (d *dumpState) printStruct(v reflect.Value) { 342 | d.w.Write(openBraceNewlineBytes) 343 | d.depth++ 344 | if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) { 345 | d.indent() 346 | d.w.Write(maxNewlineBytes) 347 | } else { 348 | vt := v.Type() 349 | numFields := v.NumField() 350 | for i := 0; i < numFields; i++ { 351 | d.indent() 352 | vtf := vt.Field(i) 353 | io.WriteString(d.w, vtf.Name) 354 | d.w.Write(colonSpaceBytes) 355 | d.ignoreNextIndent = true 356 | d.dump(d.unpackValue(v.Field(i))) 357 | if i < (numFields - 1) { 358 | d.w.Write(commaNewlineBytes) 359 | } else { 360 | d.w.Write(newlineBytes) 361 | } 362 | } 363 | } 364 | d.depth-- 365 | d.indent() 366 | d.w.Write(closeBraceBytes) 367 | } 368 | 369 | func (d *dumpState) defaultFormat() string { 370 | return "%v" 371 | } 372 | 373 | func (d *dumpState) Reset(w io.Writer, cs *ConfigState) { 374 | *d = dumpState{w: w, ci: d.ci, cs: cs} 375 | } 376 | 377 | // fdump is a helper function to consolidate the logic from the various public 378 | // methods which take varying writers and config states. 379 | func fdump(cs *ConfigState, w io.Writer, a ...interface{}) { 380 | d := dumpStateGet(w, cs) 381 | defer dumpStatePut(d) 382 | 383 | for _, arg := range a { 384 | if arg == nil { 385 | w.Write(interfaceBytes) 386 | w.Write(spaceBytes) 387 | w.Write(nilAngleBytes) 388 | w.Write(newlineBytes) 389 | continue 390 | } 391 | 392 | for k := range d.ci.pointers { 393 | delete(d.ci.pointers, k) 394 | } 395 | 396 | d.dump(reflect.ValueOf(arg)) 397 | d.w.Write(newlineBytes) 398 | } 399 | } 400 | 401 | var dumpStatePool = sync.Pool{New: func() interface{} { 402 | return new(dumpState) 403 | }} 404 | 405 | func dumpStateGet(w io.Writer, cs *ConfigState) *dumpState { 406 | d := dumpStatePool.Get().(*dumpState) 407 | d.ci = cycleInfoGet() 408 | d.Reset(w, cs) 409 | return d 410 | } 411 | 412 | func dumpStatePut(d *dumpState) { 413 | cycleInfoPut(d.ci) 414 | d.ci = nil 415 | dumpStatePool.Put(d) 416 | } 417 | -------------------------------------------------------------------------------- /dump_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * Copyright (c) 2021, 2023 Anner van Hardenbroek 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | 18 | /* 19 | Test Summary: 20 | NOTE: For each test, a nil pointer, a single pointer and double pointer to the 21 | base test element are also tested to ensure proper indirection across all types. 22 | 23 | - Max int8, int16, int32, int64, int 24 | - Max uint8, uint16, uint32, uint64, uint 25 | - Boolean true and false 26 | - Standard complex64 and complex128 27 | - Array containing standard ints 28 | - Array containing type with custom formatter on pointer receiver only 29 | - Array containing interfaces 30 | - Array containing bytes 31 | - Slice containing standard float32 values 32 | - Slice containing type with custom formatter on pointer receiver only 33 | - Slice containing interfaces 34 | - Slice containing bytes 35 | - Nil slice 36 | - Standard string 37 | - Nil interface 38 | - Sub-interface 39 | - Map with string keys and int vals 40 | - Map with custom formatter type on pointer receiver only keys and vals 41 | - Map with interface keys and values 42 | - Map with nil interface value 43 | - Struct with primitives 44 | - Struct that contains another struct 45 | - Struct that contains custom type with Stringer pointer interface via both 46 | exported and unexported fields 47 | - Struct that contains embedded struct and field to same struct 48 | - Uintptr to 0 (null pointer) 49 | - Uintptr address of real variable 50 | - Unsafe.Pointer to 0 (null pointer) 51 | - Unsafe.Pointer to address of real variable 52 | - Nil channel 53 | - Standard int channel 54 | - Function with no params and no returns 55 | - Function with param and no returns 56 | - Function with multiple params and multiple returns 57 | - Struct that is circular through self referencing 58 | - Structs that are circular through cross referencing 59 | - Structs that are indirectly circular 60 | - Type that panics in its Stringer interface 61 | */ 62 | 63 | package spew_test 64 | 65 | import ( 66 | "bytes" 67 | "fmt" 68 | "io" 69 | "strconv" 70 | "strings" 71 | "testing" 72 | "unsafe" 73 | 74 | "github.com/spewerspew/spew" 75 | ) 76 | 77 | // dumpTest is used to describe a test to be performed against the Dump method. 78 | type dumpTest struct { 79 | in interface{} 80 | wants []string 81 | typ string 82 | } 83 | 84 | // dumpTests houses all of the tests to be performed against the Dump method. 85 | var dumpTests []dumpTest 86 | 87 | // addDumpTest is a helper method to append the passed input and desired result 88 | // to dumpTests 89 | func addDumpTest(in interface{}, wants ...string) { 90 | test := dumpTest{in, wants, typeNameOf(in)} 91 | dumpTests = append(dumpTests, test) 92 | } 93 | 94 | func addIntDumpTests() { 95 | // Max int8. 96 | v := int8(127) 97 | nv := (*int8)(nil) 98 | pv := &v 99 | vAddr := fmt.Sprintf("%p", pv) 100 | pvAddr := fmt.Sprintf("%p", &pv) 101 | vt := "int8" 102 | vs := "127" 103 | addDumpTest(v, "("+vt+") "+vs+"\n") 104 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 105 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 106 | addDumpTest(nv, "(*"+vt+")()\n") 107 | 108 | // Max int16. 109 | v2 := int16(32767) 110 | nv2 := (*int16)(nil) 111 | pv2 := &v2 112 | v2Addr := fmt.Sprintf("%p", pv2) 113 | pv2Addr := fmt.Sprintf("%p", &pv2) 114 | v2t := "int16" 115 | v2s := "32767" 116 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 117 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 118 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 119 | addDumpTest(nv2, "(*"+v2t+")()\n") 120 | 121 | // Max int32. 122 | v3 := int32(2147483647) 123 | nv3 := (*int32)(nil) 124 | pv3 := &v3 125 | v3Addr := fmt.Sprintf("%p", pv3) 126 | pv3Addr := fmt.Sprintf("%p", &pv3) 127 | v3t := "int32" 128 | v3s := "2147483647" 129 | addDumpTest(v3, "("+v3t+") "+v3s+"\n") 130 | addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n") 131 | addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n") 132 | addDumpTest(nv3, "(*"+v3t+")()\n") 133 | 134 | // Max int64. 135 | v4 := int64(9223372036854775807) 136 | nv4 := (*int64)(nil) 137 | pv4 := &v4 138 | v4Addr := fmt.Sprintf("%p", pv4) 139 | pv4Addr := fmt.Sprintf("%p", &pv4) 140 | v4t := "int64" 141 | v4s := "9223372036854775807" 142 | addDumpTest(v4, "("+v4t+") "+v4s+"\n") 143 | addDumpTest(pv4, "(*"+v4t+")("+v4Addr+")("+v4s+")\n") 144 | addDumpTest(&pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")("+v4s+")\n") 145 | addDumpTest(nv4, "(*"+v4t+")()\n") 146 | 147 | // Max int. 148 | v5 := int(2147483647) 149 | nv5 := (*int)(nil) 150 | pv5 := &v5 151 | v5Addr := fmt.Sprintf("%p", pv5) 152 | pv5Addr := fmt.Sprintf("%p", &pv5) 153 | v5t := "int" 154 | v5s := "2147483647" 155 | addDumpTest(v5, "("+v5t+") "+v5s+"\n") 156 | addDumpTest(pv5, "(*"+v5t+")("+v5Addr+")("+v5s+")\n") 157 | addDumpTest(&pv5, "(**"+v5t+")("+pv5Addr+"->"+v5Addr+")("+v5s+")\n") 158 | addDumpTest(nv5, "(*"+v5t+")()\n") 159 | } 160 | 161 | func addUintDumpTests() { 162 | // Max uint8. 163 | v := uint8(255) 164 | nv := (*uint8)(nil) 165 | pv := &v 166 | vAddr := fmt.Sprintf("%p", pv) 167 | pvAddr := fmt.Sprintf("%p", &pv) 168 | vt := "uint8" 169 | vs := "255" 170 | addDumpTest(v, "("+vt+") "+vs+"\n") 171 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 172 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 173 | addDumpTest(nv, "(*"+vt+")()\n") 174 | 175 | // Max uint16. 176 | v2 := uint16(65535) 177 | nv2 := (*uint16)(nil) 178 | pv2 := &v2 179 | v2Addr := fmt.Sprintf("%p", pv2) 180 | pv2Addr := fmt.Sprintf("%p", &pv2) 181 | v2t := "uint16" 182 | v2s := "65535" 183 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 184 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 185 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 186 | addDumpTest(nv2, "(*"+v2t+")()\n") 187 | 188 | // Max uint32. 189 | v3 := uint32(4294967295) 190 | nv3 := (*uint32)(nil) 191 | pv3 := &v3 192 | v3Addr := fmt.Sprintf("%p", pv3) 193 | pv3Addr := fmt.Sprintf("%p", &pv3) 194 | v3t := "uint32" 195 | v3s := "4294967295" 196 | addDumpTest(v3, "("+v3t+") "+v3s+"\n") 197 | addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n") 198 | addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n") 199 | addDumpTest(nv3, "(*"+v3t+")()\n") 200 | 201 | // Max uint64. 202 | v4 := uint64(18446744073709551615) 203 | nv4 := (*uint64)(nil) 204 | pv4 := &v4 205 | v4Addr := fmt.Sprintf("%p", pv4) 206 | pv4Addr := fmt.Sprintf("%p", &pv4) 207 | v4t := "uint64" 208 | v4s := "18446744073709551615" 209 | addDumpTest(v4, "("+v4t+") "+v4s+"\n") 210 | addDumpTest(pv4, "(*"+v4t+")("+v4Addr+")("+v4s+")\n") 211 | addDumpTest(&pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")("+v4s+")\n") 212 | addDumpTest(nv4, "(*"+v4t+")()\n") 213 | 214 | // Max uint. 215 | v5 := uint(4294967295) 216 | nv5 := (*uint)(nil) 217 | pv5 := &v5 218 | v5Addr := fmt.Sprintf("%p", pv5) 219 | pv5Addr := fmt.Sprintf("%p", &pv5) 220 | v5t := "uint" 221 | v5s := "4294967295" 222 | addDumpTest(v5, "("+v5t+") "+v5s+"\n") 223 | addDumpTest(pv5, "(*"+v5t+")("+v5Addr+")("+v5s+")\n") 224 | addDumpTest(&pv5, "(**"+v5t+")("+pv5Addr+"->"+v5Addr+")("+v5s+")\n") 225 | addDumpTest(nv5, "(*"+v5t+")()\n") 226 | } 227 | 228 | func addBoolDumpTests() { 229 | // Boolean true. 230 | v := bool(true) 231 | nv := (*bool)(nil) 232 | pv := &v 233 | vAddr := fmt.Sprintf("%p", pv) 234 | pvAddr := fmt.Sprintf("%p", &pv) 235 | vt := "bool" 236 | vs := "true" 237 | addDumpTest(v, "("+vt+") "+vs+"\n") 238 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 239 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 240 | addDumpTest(nv, "(*"+vt+")()\n") 241 | 242 | // Boolean false. 243 | v2 := bool(false) 244 | pv2 := &v2 245 | v2Addr := fmt.Sprintf("%p", pv2) 246 | pv2Addr := fmt.Sprintf("%p", &pv2) 247 | v2t := "bool" 248 | v2s := "false" 249 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 250 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 251 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 252 | } 253 | 254 | func addFloatDumpTests() { 255 | // Standard float32. 256 | v := float32(3.1415) 257 | nv := (*float32)(nil) 258 | pv := &v 259 | vAddr := fmt.Sprintf("%p", pv) 260 | pvAddr := fmt.Sprintf("%p", &pv) 261 | vt := "float32" 262 | vs := "3.1415" 263 | addDumpTest(v, "("+vt+") "+vs+"\n") 264 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 265 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 266 | addDumpTest(nv, "(*"+vt+")()\n") 267 | 268 | // Standard float64. 269 | v2 := float64(3.1415926) 270 | nv2 := (*float64)(nil) 271 | pv2 := &v2 272 | v2Addr := fmt.Sprintf("%p", pv2) 273 | pv2Addr := fmt.Sprintf("%p", &pv2) 274 | v2t := "float64" 275 | v2s := "3.1415926" 276 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 277 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 278 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 279 | addDumpTest(nv2, "(*"+v2t+")()\n") 280 | } 281 | 282 | func addComplexDumpTests() { 283 | // Standard complex64. 284 | v := complex(float32(6), -2) 285 | nv := (*complex64)(nil) 286 | pv := &v 287 | vAddr := fmt.Sprintf("%p", pv) 288 | pvAddr := fmt.Sprintf("%p", &pv) 289 | vt := "complex64" 290 | vs := "(6-2i)" 291 | addDumpTest(v, "("+vt+") "+vs+"\n") 292 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 293 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 294 | addDumpTest(nv, "(*"+vt+")()\n") 295 | 296 | // Standard complex128. 297 | v2 := complex(float64(-6), 2) 298 | nv2 := (*complex128)(nil) 299 | pv2 := &v2 300 | v2Addr := fmt.Sprintf("%p", pv2) 301 | pv2Addr := fmt.Sprintf("%p", &pv2) 302 | v2t := "complex128" 303 | v2s := "(-6+2i)" 304 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 305 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 306 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 307 | addDumpTest(nv2, "(*"+v2t+")()\n") 308 | } 309 | 310 | func addArrayDumpTests() { 311 | // Array containing standard ints. 312 | v := [3]int{1, 2, 3} 313 | vLen := fmt.Sprintf("%d", len(v)) 314 | vCap := fmt.Sprintf("%d", cap(v)) 315 | nv := (*[3]int)(nil) 316 | pv := &v 317 | vAddr := fmt.Sprintf("%p", pv) 318 | pvAddr := fmt.Sprintf("%p", &pv) 319 | vt := "int" 320 | vs := "(len=" + vLen + " cap=" + vCap + ") {\n (" + vt + ") 1,\n (" + 321 | vt + ") 2,\n (" + vt + ") 3\n}" 322 | addDumpTest(v, "([3]"+vt+") "+vs+"\n") 323 | addDumpTest(pv, "(*[3]"+vt+")("+vAddr+")("+vs+")\n") 324 | addDumpTest(&pv, "(**[3]"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 325 | addDumpTest(nv, "(*[3]"+vt+")()\n") 326 | 327 | // Array containing type with custom formatter on pointer receiver only. 328 | v2i0 := pstringer("1") 329 | v2i1 := pstringer("2") 330 | v2i2 := pstringer("3") 331 | v2 := [3]pstringer{v2i0, v2i1, v2i2} 332 | v2i0Len := fmt.Sprintf("%d", len(v2i0)) 333 | v2i1Len := fmt.Sprintf("%d", len(v2i1)) 334 | v2i2Len := fmt.Sprintf("%d", len(v2i2)) 335 | v2Len := fmt.Sprintf("%d", len(v2)) 336 | v2Cap := fmt.Sprintf("%d", cap(v2)) 337 | nv2 := (*[3]pstringer)(nil) 338 | pv2 := &v2 339 | v2Addr := fmt.Sprintf("%p", pv2) 340 | pv2Addr := fmt.Sprintf("%p", &pv2) 341 | v2t := "spew_test.pstringer" 342 | v2sp := "(len=" + v2Len + " cap=" + v2Cap + ") {\n (" + v2t + 343 | ") (len=" + v2i0Len + ") stringer 1,\n (" + v2t + 344 | ") (len=" + v2i1Len + ") stringer 2,\n (" + v2t + 345 | ") (len=" + v2i2Len + ") " + "stringer 3\n}" 346 | v2s := v2sp 347 | if spew.UnsafeDisabled { 348 | v2s = "(len=" + v2Len + " cap=" + v2Cap + ") {\n (" + v2t + 349 | ") (len=" + v2i0Len + ") \"1\",\n (" + v2t + ") (len=" + 350 | v2i1Len + ") \"2\",\n (" + v2t + ") (len=" + v2i2Len + 351 | ") " + "\"3\"\n}" 352 | } 353 | addDumpTest(v2, "([3]"+v2t+") "+v2s+"\n") 354 | addDumpTest(pv2, "(*[3]"+v2t+")("+v2Addr+")("+v2sp+")\n") 355 | addDumpTest(&pv2, "(**[3]"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2sp+")\n") 356 | addDumpTest(nv2, "(*[3]"+v2t+")()\n") 357 | 358 | // Array containing interfaces. 359 | v3i0 := "one" 360 | v3 := [3]interface{}{v3i0, int(2), uint(3)} 361 | v3i0Len := fmt.Sprintf("%d", len(v3i0)) 362 | v3Len := fmt.Sprintf("%d", len(v3)) 363 | v3Cap := fmt.Sprintf("%d", cap(v3)) 364 | nv3 := (*[3]interface{})(nil) 365 | pv3 := &v3 366 | v3Addr := fmt.Sprintf("%p", pv3) 367 | pv3Addr := fmt.Sprintf("%p", &pv3) 368 | v3t := "[3]interface {}" 369 | v3t2 := "string" 370 | v3t3 := "int" 371 | v3t4 := "uint" 372 | v3s := "(len=" + v3Len + " cap=" + v3Cap + ") {\n (" + v3t2 + ") " + 373 | "(len=" + v3i0Len + ") \"one\",\n (" + v3t3 + ") 2,\n (" + 374 | v3t4 + ") 3\n}" 375 | addDumpTest(v3, "("+v3t+") "+v3s+"\n") 376 | addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n") 377 | addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n") 378 | addDumpTest(nv3, "(*"+v3t+")()\n") 379 | 380 | // Array containing bytes. 381 | v4 := [34]byte{ 382 | 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 383 | 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 384 | 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 385 | 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 386 | 0x31, 0x32, 387 | } 388 | v4Len := fmt.Sprintf("%d", len(v4)) 389 | v4Cap := fmt.Sprintf("%d", cap(v4)) 390 | nv4 := (*[34]byte)(nil) 391 | pv4 := &v4 392 | v4Addr := fmt.Sprintf("%p", pv4) 393 | pv4Addr := fmt.Sprintf("%p", &pv4) 394 | v4t := "[34]uint8" 395 | v4s := "(len=" + v4Len + " cap=" + v4Cap + ") " + 396 | "{\n 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20" + 397 | " |............... |\n" + 398 | " 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30" + 399 | " |!\"#$%&'()*+,-./0|\n" + 400 | " 00000020 31 32 " + 401 | " |12|\n}" 402 | addDumpTest(v4, "("+v4t+") "+v4s+"\n") 403 | addDumpTest(pv4, "(*"+v4t+")("+v4Addr+")("+v4s+")\n") 404 | addDumpTest(&pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")("+v4s+")\n") 405 | addDumpTest(nv4, "(*"+v4t+")()\n") 406 | } 407 | 408 | func addSliceDumpTests() { 409 | // Slice containing standard float32 values. 410 | v := []float32{3.14, 6.28, 12.56} 411 | vLen := fmt.Sprintf("%d", len(v)) 412 | vCap := fmt.Sprintf("%d", cap(v)) 413 | nv := (*[]float32)(nil) 414 | pv := &v 415 | vAddr := fmt.Sprintf("%p", pv) 416 | pvAddr := fmt.Sprintf("%p", &pv) 417 | vt := "float32" 418 | vs := "(len=" + vLen + " cap=" + vCap + ") {\n (" + vt + ") 3.14,\n (" + 419 | vt + ") 6.28,\n (" + vt + ") 12.56\n}" 420 | addDumpTest(v, "([]"+vt+") "+vs+"\n") 421 | addDumpTest(pv, "(*[]"+vt+")("+vAddr+")("+vs+")\n") 422 | addDumpTest(&pv, "(**[]"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 423 | addDumpTest(nv, "(*[]"+vt+")()\n") 424 | 425 | // Slice containing type with custom formatter on pointer receiver only. 426 | v2i0 := pstringer("1") 427 | v2i1 := pstringer("2") 428 | v2i2 := pstringer("3") 429 | v2 := []pstringer{v2i0, v2i1, v2i2} 430 | v2i0Len := fmt.Sprintf("%d", len(v2i0)) 431 | v2i1Len := fmt.Sprintf("%d", len(v2i1)) 432 | v2i2Len := fmt.Sprintf("%d", len(v2i2)) 433 | v2Len := fmt.Sprintf("%d", len(v2)) 434 | v2Cap := fmt.Sprintf("%d", cap(v2)) 435 | nv2 := (*[]pstringer)(nil) 436 | pv2 := &v2 437 | v2Addr := fmt.Sprintf("%p", pv2) 438 | pv2Addr := fmt.Sprintf("%p", &pv2) 439 | v2t := "spew_test.pstringer" 440 | v2s := "(len=" + v2Len + " cap=" + v2Cap + ") {\n (" + v2t + ") (len=" + 441 | v2i0Len + ") stringer 1,\n (" + v2t + ") (len=" + v2i1Len + 442 | ") stringer 2,\n (" + v2t + ") (len=" + v2i2Len + ") " + 443 | "stringer 3\n}" 444 | addDumpTest(v2, "([]"+v2t+") "+v2s+"\n") 445 | addDumpTest(pv2, "(*[]"+v2t+")("+v2Addr+")("+v2s+")\n") 446 | addDumpTest(&pv2, "(**[]"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 447 | addDumpTest(nv2, "(*[]"+v2t+")()\n") 448 | 449 | // Slice containing interfaces. 450 | v3i0 := "one" 451 | v3 := []interface{}{v3i0, int(2), uint(3), nil} 452 | v3i0Len := fmt.Sprintf("%d", len(v3i0)) 453 | v3Len := fmt.Sprintf("%d", len(v3)) 454 | v3Cap := fmt.Sprintf("%d", cap(v3)) 455 | nv3 := (*[]interface{})(nil) 456 | pv3 := &v3 457 | v3Addr := fmt.Sprintf("%p", pv3) 458 | pv3Addr := fmt.Sprintf("%p", &pv3) 459 | v3t := "[]interface {}" 460 | v3t2 := "string" 461 | v3t3 := "int" 462 | v3t4 := "uint" 463 | v3t5 := "interface {}" 464 | v3s := "(len=" + v3Len + " cap=" + v3Cap + ") {\n (" + v3t2 + ") " + 465 | "(len=" + v3i0Len + ") \"one\",\n (" + v3t3 + ") 2,\n (" + 466 | v3t4 + ") 3,\n (" + v3t5 + ") \n}" 467 | addDumpTest(v3, "("+v3t+") "+v3s+"\n") 468 | addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n") 469 | addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n") 470 | addDumpTest(nv3, "(*"+v3t+")()\n") 471 | 472 | // Slice containing bytes. 473 | v4 := []byte{ 474 | 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 475 | 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 476 | 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 477 | 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 478 | 0x31, 0x32, 479 | } 480 | v4Len := fmt.Sprintf("%d", len(v4)) 481 | v4Cap := fmt.Sprintf("%d", cap(v4)) 482 | nv4 := (*[]byte)(nil) 483 | pv4 := &v4 484 | v4Addr := fmt.Sprintf("%p", pv4) 485 | pv4Addr := fmt.Sprintf("%p", &pv4) 486 | v4t := "[]uint8" 487 | v4s := "(len=" + v4Len + " cap=" + v4Cap + ") " + 488 | "{\n 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20" + 489 | " |............... |\n" + 490 | " 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30" + 491 | " |!\"#$%&'()*+,-./0|\n" + 492 | " 00000020 31 32 " + 493 | " |12|\n}" 494 | addDumpTest(v4, "("+v4t+") "+v4s+"\n") 495 | addDumpTest(pv4, "(*"+v4t+")("+v4Addr+")("+v4s+")\n") 496 | addDumpTest(&pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")("+v4s+")\n") 497 | addDumpTest(nv4, "(*"+v4t+")()\n") 498 | 499 | // Nil slice. 500 | v5 := []int(nil) 501 | nv5 := (*[]int)(nil) 502 | pv5 := &v5 503 | v5Addr := fmt.Sprintf("%p", pv5) 504 | pv5Addr := fmt.Sprintf("%p", &pv5) 505 | v5t := "[]int" 506 | v5s := "" 507 | addDumpTest(v5, "("+v5t+") "+v5s+"\n") 508 | addDumpTest(pv5, "(*"+v5t+")("+v5Addr+")("+v5s+")\n") 509 | addDumpTest(&pv5, "(**"+v5t+")("+pv5Addr+"->"+v5Addr+")("+v5s+")\n") 510 | addDumpTest(nv5, "(*"+v5t+")()\n") 511 | } 512 | 513 | func addStringDumpTests() { 514 | // Standard string. 515 | v := "test" 516 | vLen := fmt.Sprintf("%d", len(v)) 517 | nv := (*string)(nil) 518 | pv := &v 519 | vAddr := fmt.Sprintf("%p", pv) 520 | pvAddr := fmt.Sprintf("%p", &pv) 521 | vt := "string" 522 | vs := "(len=" + vLen + ") \"test\"" 523 | addDumpTest(v, "("+vt+") "+vs+"\n") 524 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 525 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 526 | addDumpTest(nv, "(*"+vt+")()\n") 527 | } 528 | 529 | func addInterfaceDumpTests() { 530 | // Nil interface. 531 | var v interface{} 532 | nv := (*interface{})(nil) 533 | pv := &v 534 | vAddr := fmt.Sprintf("%p", pv) 535 | pvAddr := fmt.Sprintf("%p", &pv) 536 | vt := "interface {}" 537 | vs := "" 538 | addDumpTest(v, "("+vt+") "+vs+"\n") 539 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 540 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 541 | addDumpTest(nv, "(*"+vt+")()\n") 542 | 543 | // Sub-interface. 544 | v2 := interface{}(uint16(65535)) 545 | pv2 := &v2 546 | v2Addr := fmt.Sprintf("%p", pv2) 547 | pv2Addr := fmt.Sprintf("%p", &pv2) 548 | v2t := "uint16" 549 | v2s := "65535" 550 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 551 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 552 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 553 | } 554 | 555 | func addMapDumpTests() { 556 | // Map with string keys and int vals. 557 | k := "one" 558 | kk := "two" 559 | m := map[string]int{k: 1, kk: 2} 560 | klen := fmt.Sprintf("%d", len(k)) // not kLen to shut golint up 561 | kkLen := fmt.Sprintf("%d", len(kk)) 562 | mLen := fmt.Sprintf("%d", len(m)) 563 | nilMap := map[string]int(nil) 564 | nm := (*map[string]int)(nil) 565 | pm := &m 566 | mAddr := fmt.Sprintf("%p", pm) 567 | pmAddr := fmt.Sprintf("%p", &pm) 568 | mt := "map[string]int" 569 | mt1 := "string" 570 | mt2 := "int" 571 | ms := "(len=" + mLen + ") {\n (" + mt1 + ") (len=" + klen + ") " + 572 | "\"one\": (" + mt2 + ") 1,\n (" + mt1 + ") (len=" + kkLen + 573 | ") \"two\": (" + mt2 + ") 2\n}" 574 | ms2 := "(len=" + mLen + ") {\n (" + mt1 + ") (len=" + kkLen + ") " + 575 | "\"two\": (" + mt2 + ") 2,\n (" + mt1 + ") (len=" + klen + 576 | ") \"one\": (" + mt2 + ") 1\n}" 577 | addDumpTest(m, "("+mt+") "+ms+"\n", "("+mt+") "+ms2+"\n") 578 | addDumpTest(pm, "(*"+mt+")("+mAddr+")("+ms+")\n", 579 | "(*"+mt+")("+mAddr+")("+ms2+")\n") 580 | addDumpTest(&pm, "(**"+mt+")("+pmAddr+"->"+mAddr+")("+ms+")\n", 581 | "(**"+mt+")("+pmAddr+"->"+mAddr+")("+ms2+")\n") 582 | addDumpTest(nm, "(*"+mt+")()\n") 583 | addDumpTest(nilMap, "("+mt+") \n") 584 | 585 | // Map with custom formatter type on pointer receiver only keys and vals. 586 | k2 := pstringer("one") 587 | v2 := pstringer("1") 588 | m2 := map[pstringer]pstringer{k2: v2} 589 | k2Len := fmt.Sprintf("%d", len(k2)) 590 | v2Len := fmt.Sprintf("%d", len(v2)) 591 | m2Len := fmt.Sprintf("%d", len(m2)) 592 | nilMap2 := map[pstringer]pstringer(nil) 593 | nm2 := (*map[pstringer]pstringer)(nil) 594 | pm2 := &m2 595 | m2Addr := fmt.Sprintf("%p", pm2) 596 | pm2Addr := fmt.Sprintf("%p", &pm2) 597 | m2t := "map[spew_test.pstringer]spew_test.pstringer" 598 | m2t1 := "spew_test.pstringer" 599 | m2t2 := "spew_test.pstringer" 600 | m2s := "(len=" + m2Len + ") {\n (" + m2t1 + ") (len=" + k2Len + ") " + 601 | "stringer one: (" + m2t2 + ") (len=" + v2Len + ") stringer 1\n}" 602 | if spew.UnsafeDisabled { 603 | m2s = "(len=" + m2Len + ") {\n (" + m2t1 + ") (len=" + k2Len + 604 | ") " + "\"one\": (" + m2t2 + ") (len=" + v2Len + 605 | ") \"1\"\n}" 606 | } 607 | addDumpTest(m2, "("+m2t+") "+m2s+"\n") 608 | addDumpTest(pm2, "(*"+m2t+")("+m2Addr+")("+m2s+")\n") 609 | addDumpTest(&pm2, "(**"+m2t+")("+pm2Addr+"->"+m2Addr+")("+m2s+")\n") 610 | addDumpTest(nm2, "(*"+m2t+")()\n") 611 | addDumpTest(nilMap2, "("+m2t+") \n") 612 | 613 | // Map with interface keys and values. 614 | k3 := "one" 615 | k3Len := fmt.Sprintf("%d", len(k3)) 616 | m3 := map[interface{}]interface{}{k3: 1} 617 | m3Len := fmt.Sprintf("%d", len(m3)) 618 | nilMap3 := map[interface{}]interface{}(nil) 619 | nm3 := (*map[interface{}]interface{})(nil) 620 | pm3 := &m3 621 | m3Addr := fmt.Sprintf("%p", pm3) 622 | pm3Addr := fmt.Sprintf("%p", &pm3) 623 | m3t := "map[interface {}]interface {}" 624 | m3t1 := "string" 625 | m3t2 := "int" 626 | m3s := "(len=" + m3Len + ") {\n (" + m3t1 + ") (len=" + k3Len + ") " + 627 | "\"one\": (" + m3t2 + ") 1\n}" 628 | addDumpTest(m3, "("+m3t+") "+m3s+"\n") 629 | addDumpTest(pm3, "(*"+m3t+")("+m3Addr+")("+m3s+")\n") 630 | addDumpTest(&pm3, "(**"+m3t+")("+pm3Addr+"->"+m3Addr+")("+m3s+")\n") 631 | addDumpTest(nm3, "(*"+m3t+")()\n") 632 | addDumpTest(nilMap3, "("+m3t+") \n") 633 | 634 | // Map with nil interface value. 635 | k4 := "nil" 636 | k4Len := fmt.Sprintf("%d", len(k4)) 637 | m4 := map[string]interface{}{k4: nil} 638 | m4Len := fmt.Sprintf("%d", len(m4)) 639 | nilMap4 := map[string]interface{}(nil) 640 | nm4 := (*map[string]interface{})(nil) 641 | pm4 := &m4 642 | m4Addr := fmt.Sprintf("%p", pm4) 643 | pm4Addr := fmt.Sprintf("%p", &pm4) 644 | m4t := "map[string]interface {}" 645 | m4t1 := "string" 646 | m4t2 := "interface {}" 647 | m4s := "(len=" + m4Len + ") {\n (" + m4t1 + ") (len=" + k4Len + ")" + 648 | " \"nil\": (" + m4t2 + ") \n}" 649 | addDumpTest(m4, "("+m4t+") "+m4s+"\n") 650 | addDumpTest(pm4, "(*"+m4t+")("+m4Addr+")("+m4s+")\n") 651 | addDumpTest(&pm4, "(**"+m4t+")("+pm4Addr+"->"+m4Addr+")("+m4s+")\n") 652 | addDumpTest(nm4, "(*"+m4t+")()\n") 653 | addDumpTest(nilMap4, "("+m4t+") \n") 654 | } 655 | 656 | func addStructDumpTests() { 657 | // Struct with primitives. 658 | type s1 struct { 659 | a int8 660 | b uint8 661 | } 662 | v := s1{127, 255} 663 | nv := (*s1)(nil) 664 | pv := &v 665 | vAddr := fmt.Sprintf("%p", pv) 666 | pvAddr := fmt.Sprintf("%p", &pv) 667 | vt := "spew_test.s1" 668 | vt2 := "int8" 669 | vt3 := "uint8" 670 | vs := "{\n a: (" + vt2 + ") 127,\n b: (" + vt3 + ") 255\n}" 671 | addDumpTest(v, "("+vt+") "+vs+"\n") 672 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 673 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 674 | addDumpTest(nv, "(*"+vt+")()\n") 675 | 676 | // Struct that contains another struct. 677 | type s2 struct { 678 | s1 s1 679 | b bool 680 | } 681 | v2 := s2{s1{127, 255}, true} 682 | nv2 := (*s2)(nil) 683 | pv2 := &v2 684 | v2Addr := fmt.Sprintf("%p", pv2) 685 | pv2Addr := fmt.Sprintf("%p", &pv2) 686 | v2t := "spew_test.s2" 687 | v2t2 := "spew_test.s1" 688 | v2t3 := "int8" 689 | v2t4 := "uint8" 690 | v2t5 := "bool" 691 | v2s := "{\n s1: (" + v2t2 + ") {\n a: (" + v2t3 + ") 127,\n b: (" + 692 | v2t4 + ") 255\n },\n b: (" + v2t5 + ") true\n}" 693 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 694 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 695 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 696 | addDumpTest(nv2, "(*"+v2t+")()\n") 697 | 698 | // Struct that contains custom type with Stringer pointer interface via both 699 | // exported and unexported fields. 700 | type s3 struct { 701 | s pstringer 702 | S pstringer 703 | } 704 | v3 := s3{"test", "test2"} 705 | nv3 := (*s3)(nil) 706 | pv3 := &v3 707 | v3Addr := fmt.Sprintf("%p", pv3) 708 | pv3Addr := fmt.Sprintf("%p", &pv3) 709 | v3t := "spew_test.s3" 710 | v3t2 := "spew_test.pstringer" 711 | v3s := "{\n s: (" + v3t2 + ") (len=4) stringer test,\n S: (" + v3t2 + 712 | ") (len=5) stringer test2\n}" 713 | v3sp := v3s 714 | if spew.UnsafeDisabled { 715 | v3s = "{\n s: (" + v3t2 + ") (len=4) \"test\",\n S: (" + 716 | v3t2 + ") (len=5) \"test2\"\n}" 717 | v3sp = "{\n s: (" + v3t2 + ") (len=4) \"test\",\n S: (" + 718 | v3t2 + ") (len=5) stringer test2\n}" 719 | } 720 | addDumpTest(v3, "("+v3t+") "+v3s+"\n") 721 | addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3sp+")\n") 722 | addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3sp+")\n") 723 | addDumpTest(nv3, "(*"+v3t+")()\n") 724 | 725 | // Struct that contains embedded struct and field to same struct. 726 | e := embed{"embedstr"} 727 | eLen := fmt.Sprintf("%d", len("embedstr")) 728 | v4 := embedwrap{embed: &e, e: &e} 729 | nv4 := (*embedwrap)(nil) 730 | pv4 := &v4 731 | eAddr := fmt.Sprintf("%p", &e) 732 | v4Addr := fmt.Sprintf("%p", pv4) 733 | pv4Addr := fmt.Sprintf("%p", &pv4) 734 | v4t := "spew_test.embedwrap" 735 | v4t2 := "spew_test.embed" 736 | v4t3 := "string" 737 | v4s := "{\n embed: (*" + v4t2 + ")(" + eAddr + ")({\n a: (" + v4t3 + 738 | ") (len=" + eLen + ") \"embedstr\"\n }),\n e: (*" + v4t2 + 739 | ")(" + eAddr + ")({\n a: (" + v4t3 + ") (len=" + eLen + ")" + 740 | " \"embedstr\"\n })\n}" 741 | addDumpTest(v4, "("+v4t+") "+v4s+"\n") 742 | addDumpTest(pv4, "(*"+v4t+")("+v4Addr+")("+v4s+")\n") 743 | addDumpTest(&pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")("+v4s+")\n") 744 | addDumpTest(nv4, "(*"+v4t+")()\n") 745 | } 746 | 747 | func addUintptrDumpTests() { 748 | // Null pointer. 749 | v := uintptr(0) 750 | pv := &v 751 | vAddr := fmt.Sprintf("%p", pv) 752 | pvAddr := fmt.Sprintf("%p", &pv) 753 | vt := "uintptr" 754 | vs := "" 755 | addDumpTest(v, "("+vt+") "+vs+"\n") 756 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 757 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 758 | 759 | // Address of real variable. 760 | i := 1 761 | v2 := uintptr(unsafe.Pointer(&i)) 762 | nv2 := (*uintptr)(nil) 763 | pv2 := &v2 764 | v2Addr := fmt.Sprintf("%p", pv2) 765 | pv2Addr := fmt.Sprintf("%p", &pv2) 766 | v2t := "uintptr" 767 | v2s := fmt.Sprintf("%p", &i) 768 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 769 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 770 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 771 | addDumpTest(nv2, "(*"+v2t+")()\n") 772 | } 773 | 774 | func addUnsafePointerDumpTests() { 775 | // Null pointer. 776 | v := unsafe.Pointer(nil) 777 | nv := (*unsafe.Pointer)(nil) 778 | pv := &v 779 | vAddr := fmt.Sprintf("%p", pv) 780 | pvAddr := fmt.Sprintf("%p", &pv) 781 | vt := "unsafe.Pointer" 782 | vs := "" 783 | addDumpTest(v, "("+vt+") "+vs+"\n") 784 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 785 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 786 | addDumpTest(nv, "(*"+vt+")()\n") 787 | 788 | // Address of real variable. 789 | i := 1 790 | v2 := unsafe.Pointer(&i) 791 | pv2 := &v2 792 | v2Addr := fmt.Sprintf("%p", pv2) 793 | pv2Addr := fmt.Sprintf("%p", &pv2) 794 | v2t := "unsafe.Pointer" 795 | v2s := fmt.Sprintf("%p", &i) 796 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 797 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 798 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 799 | addDumpTest(nv, "(*"+vt+")()\n") 800 | } 801 | 802 | func addChanDumpTests() { 803 | // Nil channel. 804 | var v chan int 805 | pv := &v 806 | nv := (*chan int)(nil) 807 | vAddr := fmt.Sprintf("%p", pv) 808 | pvAddr := fmt.Sprintf("%p", &pv) 809 | vt := "chan int" 810 | vs := "" 811 | addDumpTest(v, "("+vt+") "+vs+"\n") 812 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 813 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 814 | addDumpTest(nv, "(*"+vt+")()\n") 815 | 816 | // Real channel. 817 | v2 := make(chan int) 818 | pv2 := &v2 819 | v2Addr := fmt.Sprintf("%p", pv2) 820 | pv2Addr := fmt.Sprintf("%p", &pv2) 821 | v2t := "chan int" 822 | v2s := fmt.Sprintf("%p", v2) 823 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 824 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 825 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 826 | } 827 | 828 | func addFuncDumpTests() { 829 | // Function with no params and no returns. 830 | v := addIntDumpTests 831 | nv := (*func())(nil) 832 | pv := &v 833 | vAddr := fmt.Sprintf("%p", pv) 834 | pvAddr := fmt.Sprintf("%p", &pv) 835 | vt := "func()" 836 | vs := fmt.Sprintf("%p", v) 837 | addDumpTest(v, "("+vt+") "+vs+"\n") 838 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 839 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 840 | addDumpTest(nv, "(*"+vt+")()\n") 841 | 842 | // Function with param and no returns. 843 | v2 := TestDump 844 | nv2 := (*func(*testing.T))(nil) 845 | pv2 := &v2 846 | v2Addr := fmt.Sprintf("%p", pv2) 847 | pv2Addr := fmt.Sprintf("%p", &pv2) 848 | v2t := "func(*testing.T)" 849 | v2s := fmt.Sprintf("%p", v2) 850 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 851 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 852 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 853 | addDumpTest(nv2, "(*"+v2t+")()\n") 854 | 855 | // Function with multiple params and multiple returns. 856 | var v3 = func(i int, s string) (b bool, err error) { 857 | return true, nil 858 | } 859 | nv3 := (*func(int, string) (bool, error))(nil) 860 | pv3 := &v3 861 | v3Addr := fmt.Sprintf("%p", pv3) 862 | pv3Addr := fmt.Sprintf("%p", &pv3) 863 | v3t := "func(int, string) (bool, error)" 864 | v3s := fmt.Sprintf("%p", v3) 865 | addDumpTest(v3, "("+v3t+") "+v3s+"\n") 866 | addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n") 867 | addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n") 868 | addDumpTest(nv3, "(*"+v3t+")()\n") 869 | } 870 | 871 | func addCircularDumpTests() { 872 | // Struct that is circular through self referencing. 873 | type circular struct { 874 | c *circular 875 | } 876 | v := circular{nil} 877 | v.c = &v 878 | pv := &v 879 | vAddr := fmt.Sprintf("%p", pv) 880 | pvAddr := fmt.Sprintf("%p", &pv) 881 | vt := "spew_test.circular" 882 | vs := "{\n c: (*" + vt + ")(" + vAddr + ")({\n c: (*" + vt + ")(" + 883 | vAddr + ")()\n })\n}" 884 | vs2 := "{\n c: (*" + vt + ")(" + vAddr + ")()\n}" 885 | addDumpTest(v, "("+vt+") "+vs+"\n") 886 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs2+")\n") 887 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs2+")\n") 888 | 889 | // Structs that are circular through cross referencing. 890 | v2 := xref1{nil} 891 | ts2 := xref2{&v2} 892 | v2.ps2 = &ts2 893 | pv2 := &v2 894 | ts2Addr := fmt.Sprintf("%p", &ts2) 895 | v2Addr := fmt.Sprintf("%p", pv2) 896 | pv2Addr := fmt.Sprintf("%p", &pv2) 897 | v2t := "spew_test.xref1" 898 | v2t2 := "spew_test.xref2" 899 | v2s := "{\n ps2: (*" + v2t2 + ")(" + ts2Addr + ")({\n ps1: (*" + v2t + 900 | ")(" + v2Addr + ")({\n ps2: (*" + v2t2 + ")(" + ts2Addr + 901 | ")()\n })\n })\n}" 902 | v2s2 := "{\n ps2: (*" + v2t2 + ")(" + ts2Addr + ")({\n ps1: (*" + v2t + 903 | ")(" + v2Addr + ")()\n })\n}" 904 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 905 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s2+")\n") 906 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s2+")\n") 907 | 908 | // Structs that are indirectly circular. 909 | v3 := indirCir1{nil} 910 | tic2 := indirCir2{nil} 911 | tic3 := indirCir3{&v3} 912 | tic2.ps3 = &tic3 913 | v3.ps2 = &tic2 914 | pv3 := &v3 915 | tic2Addr := fmt.Sprintf("%p", &tic2) 916 | tic3Addr := fmt.Sprintf("%p", &tic3) 917 | v3Addr := fmt.Sprintf("%p", pv3) 918 | pv3Addr := fmt.Sprintf("%p", &pv3) 919 | v3t := "spew_test.indirCir1" 920 | v3t2 := "spew_test.indirCir2" 921 | v3t3 := "spew_test.indirCir3" 922 | v3s := "{\n ps2: (*" + v3t2 + ")(" + tic2Addr + ")({\n ps3: (*" + v3t3 + 923 | ")(" + tic3Addr + ")({\n ps1: (*" + v3t + ")(" + v3Addr + 924 | ")({\n ps2: (*" + v3t2 + ")(" + tic2Addr + 925 | ")()\n })\n })\n })\n}" 926 | v3s2 := "{\n ps2: (*" + v3t2 + ")(" + tic2Addr + ")({\n ps3: (*" + v3t3 + 927 | ")(" + tic3Addr + ")({\n ps1: (*" + v3t + ")(" + v3Addr + 928 | ")()\n })\n })\n}" 929 | addDumpTest(v3, "("+v3t+") "+v3s+"\n") 930 | addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s2+")\n") 931 | addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s2+")\n") 932 | } 933 | 934 | func addPanicDumpTests() { 935 | // Type that panics in its Stringer interface. 936 | v := panicer(127) 937 | nv := (*panicer)(nil) 938 | pv := &v 939 | vAddr := fmt.Sprintf("%p", pv) 940 | pvAddr := fmt.Sprintf("%p", &pv) 941 | vt := "spew_test.panicer" 942 | vs := "(PANIC=test panic)127" 943 | addDumpTest(v, "("+vt+") "+vs+"\n") 944 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 945 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 946 | addDumpTest(nv, "(*"+vt+")()\n") 947 | } 948 | 949 | func addStringerDumpTests() { 950 | // Type that has a custom Stringer interface. 951 | v := stringer("test") 952 | var nv stringer 953 | pv := &v 954 | vAddr := fmt.Sprintf("%p", pv) 955 | pvAddr := fmt.Sprintf("%p", &pv) 956 | vl := fmt.Sprintf("(len=%d) ", len(v)) 957 | vt := "spew_test.stringer" 958 | vs := "stringer test" 959 | addDumpTest(v, "("+vt+") "+vl+vs+"\n") 960 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vl+vs+")\n") 961 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vl+vs+")\n") 962 | addDumpTest(nv, "("+vt+") stringer \n") 963 | 964 | v1 := pstringer("test") 965 | var nv1 pstringer 966 | pv1 := &v1 967 | v1Addr := fmt.Sprintf("%p", pv1) 968 | pv1Addr := fmt.Sprintf("%p", &pv1) 969 | v1l := fmt.Sprintf("(len=%d) ", len(v1)) 970 | v1t := "spew_test.pstringer" 971 | v1s := "stringer test" 972 | addDumpTest(v1, "("+v1t+") "+v1l+v1s+"\n") 973 | addDumpTest(pv1, "(*"+v1t+")("+v1Addr+")("+v1l+v1s+")\n") 974 | addDumpTest(&pv1, "(**"+v1t+")("+pv1Addr+"->"+v1Addr+")("+v1l+v1s+")\n") 975 | addDumpTest(nv1, "("+v1t+") stringer \n") 976 | 977 | v2 := make(chanStringer) 978 | var nv2 chanStringer 979 | pv2 := &v2 980 | v2Addr := fmt.Sprintf("%p", pv2) 981 | pv2Addr := fmt.Sprintf("%p", &pv2) 982 | v2t := "spew_test.chanStringer" 983 | v2s := "chan stringer" 984 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 985 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 986 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 987 | addDumpTest(nv2, "("+v2t+") chan stringer\n") 988 | 989 | v3 := funcStringer(func() {}) 990 | var nv3 funcStringer 991 | pv3 := &v3 992 | v3Addr := fmt.Sprintf("%p", pv3) 993 | pv3Addr := fmt.Sprintf("%p", &pv3) 994 | v3t := "spew_test.funcStringer" 995 | v3s := "func stringer" 996 | addDumpTest(v3, "("+v3t+") "+v3s+"\n") 997 | addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n") 998 | addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n") 999 | addDumpTest(nv3, "("+v3t+") func stringer\n") 1000 | 1001 | v4 := make(mapStringer) 1002 | var nv4 mapStringer 1003 | pv4 := &v4 1004 | v4Addr := fmt.Sprintf("%p", pv4) 1005 | pv4Addr := fmt.Sprintf("%p", &pv4) 1006 | v4t := "spew_test.mapStringer" 1007 | v4s := "map stringer" 1008 | addDumpTest(v4, "("+v4t+") "+v4s+"\n") 1009 | addDumpTest(pv4, "(*"+v4t+")("+v4Addr+")("+v4s+")\n") 1010 | addDumpTest(&pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")("+v4s+")\n") 1011 | addDumpTest(nv4, "("+v4t+") map stringer\n") 1012 | } 1013 | 1014 | func addErrorDumpTests() { 1015 | // Type that has a custom Error interface. 1016 | v := customError(127) 1017 | nv := (*customError)(nil) 1018 | pv := &v 1019 | vAddr := fmt.Sprintf("%p", pv) 1020 | pvAddr := fmt.Sprintf("%p", &pv) 1021 | vt := "spew_test.customError" 1022 | vs := "error: 127" 1023 | addDumpTest(v, "("+vt+") "+vs+"\n") 1024 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 1025 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 1026 | addDumpTest(nv, "(*"+vt+")()\n") 1027 | 1028 | v1 := make(chanError) 1029 | var nv1 chanError 1030 | pv1 := &v1 1031 | v1Addr := fmt.Sprintf("%p", pv1) 1032 | pv1Addr := fmt.Sprintf("%p", &pv1) 1033 | v1t := "spew_test.chanError" 1034 | v1s := "chan error" 1035 | addDumpTest(v1, "("+v1t+") "+v1s+"\n") 1036 | addDumpTest(pv1, "(*"+v1t+")("+v1Addr+")("+v1s+")\n") 1037 | addDumpTest(&pv1, "(**"+v1t+")("+pv1Addr+"->"+v1Addr+")("+v1s+")\n") 1038 | addDumpTest(nv1, "("+v1t+") chan error\n") 1039 | 1040 | v2 := funcError(func() {}) 1041 | var nv2 funcError 1042 | pv2 := &v2 1043 | v2Addr := fmt.Sprintf("%p", pv2) 1044 | pv2Addr := fmt.Sprintf("%p", &pv2) 1045 | v2t := "spew_test.funcError" 1046 | v2s := "func error" 1047 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 1048 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 1049 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 1050 | addDumpTest(nv2, "("+v2t+") func error\n") 1051 | 1052 | v3 := make(mapError) 1053 | var nv3 mapError 1054 | pv3 := &v3 1055 | v3Addr := fmt.Sprintf("%p", pv3) 1056 | pv3Addr := fmt.Sprintf("%p", &pv3) 1057 | v3t := "spew_test.mapError" 1058 | v3s := "map error" 1059 | addDumpTest(v3, "("+v3t+") "+v3s+"\n") 1060 | addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n") 1061 | addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n") 1062 | addDumpTest(nv3, "("+v3t+") map error\n") 1063 | } 1064 | 1065 | func setupDumpTests() { 1066 | if len(dumpTests) == 0 { 1067 | addIntDumpTests() 1068 | addUintDumpTests() 1069 | addBoolDumpTests() 1070 | addFloatDumpTests() 1071 | addComplexDumpTests() 1072 | addArrayDumpTests() 1073 | addSliceDumpTests() 1074 | addStringDumpTests() 1075 | addInterfaceDumpTests() 1076 | addMapDumpTests() 1077 | addStructDumpTests() 1078 | addUintptrDumpTests() 1079 | addUnsafePointerDumpTests() 1080 | addChanDumpTests() 1081 | addFuncDumpTests() 1082 | addCircularDumpTests() 1083 | addPanicDumpTests() 1084 | addStringerDumpTests() 1085 | addErrorDumpTests() 1086 | addCgoDumpTests() 1087 | } 1088 | } 1089 | 1090 | // TestDump executes all of the tests described by dumpTests. 1091 | func TestDump(t *testing.T) { 1092 | setupDumpTests() 1093 | 1094 | t.Logf("Running %d tests", len(dumpTests)) 1095 | for i, test := range dumpTests { 1096 | t.Run(strconv.Itoa(i), func(t *testing.T) { 1097 | buf := new(bytes.Buffer) 1098 | spew.Fdump(buf, test.in) 1099 | s := buf.String() 1100 | if testFailed(s, test.wants) { 1101 | t.Errorf("\ngot: %q\n%s", s, stringizeWants(test.wants)) 1102 | } 1103 | }) 1104 | } 1105 | } 1106 | 1107 | func BenchmarkDump(b *testing.B) { 1108 | setupDumpTests() 1109 | 1110 | b.Logf("Running %d benchmarks", len(dumpTests)) 1111 | for _, test := range dumpTests { 1112 | s := test.wants[0] 1113 | name := test.typ + "/" + s[:strings.IndexByte(s, '\n')] 1114 | b.Run(name, func(b *testing.B) { 1115 | b.ReportAllocs() 1116 | b.ResetTimer() 1117 | for i := 0; i < b.N; i++ { 1118 | spew.Fdump(io.Discard, test.in) 1119 | } 1120 | }) 1121 | } 1122 | } 1123 | 1124 | func TestDumpSortedKeys(t *testing.T) { 1125 | cfg := spew.ConfigState{SortKeys: true} 1126 | s := cfg.Sdump(map[int]string{1: "1", 3: "3", 2: "2"}) 1127 | expected := "(map[int]string) (len=3) {\n(int) 1: (string) (len=1) " + 1128 | "\"1\",\n(int) 2: (string) (len=1) \"2\",\n(int) 3: (string) " + 1129 | "(len=1) \"3\"\n" + 1130 | "}\n" 1131 | if s != expected { 1132 | t.Errorf("Sorted keys mismatch:\n %v %v", s, expected) 1133 | } 1134 | 1135 | s = cfg.Sdump(map[stringer]int{"1": 1, "3": 3, "2": 2}) 1136 | expected = "(map[spew_test.stringer]int) (len=3) {\n" + 1137 | "(spew_test.stringer) (len=1) stringer 1: (int) 1,\n" + 1138 | "(spew_test.stringer) (len=1) stringer 2: (int) 2,\n" + 1139 | "(spew_test.stringer) (len=1) stringer 3: (int) 3\n" + 1140 | "}\n" 1141 | if s != expected { 1142 | t.Errorf("Sorted keys mismatch:\n %v %v", s, expected) 1143 | } 1144 | 1145 | s = cfg.Sdump(map[pstringer]int{pstringer("1"): 1, pstringer("3"): 3, pstringer("2"): 2}) 1146 | expected = "(map[spew_test.pstringer]int) (len=3) {\n" + 1147 | "(spew_test.pstringer) (len=1) stringer 1: (int) 1,\n" + 1148 | "(spew_test.pstringer) (len=1) stringer 2: (int) 2,\n" + 1149 | "(spew_test.pstringer) (len=1) stringer 3: (int) 3\n" + 1150 | "}\n" 1151 | if spew.UnsafeDisabled { 1152 | expected = "(map[spew_test.pstringer]int) (len=3) {\n" + 1153 | "(spew_test.pstringer) (len=1) \"1\": (int) 1,\n" + 1154 | "(spew_test.pstringer) (len=1) \"2\": (int) 2,\n" + 1155 | "(spew_test.pstringer) (len=1) \"3\": (int) 3\n" + 1156 | "}\n" 1157 | } 1158 | if s != expected { 1159 | t.Errorf("Sorted keys mismatch:\n %v %v", s, expected) 1160 | } 1161 | 1162 | s = cfg.Sdump(map[customError]int{customError(1): 1, customError(3): 3, customError(2): 2}) 1163 | expected = "(map[spew_test.customError]int) (len=3) {\n" + 1164 | "(spew_test.customError) error: 1: (int) 1,\n" + 1165 | "(spew_test.customError) error: 2: (int) 2,\n" + 1166 | "(spew_test.customError) error: 3: (int) 3\n" + 1167 | "}\n" 1168 | if s != expected { 1169 | t.Errorf("Sorted keys mismatch:\n %v %v", s, expected) 1170 | } 1171 | 1172 | } 1173 | -------------------------------------------------------------------------------- /dumpcgo_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013-2016 Dave Collins 2 | // 3 | // Permission to use, copy, modify, and distribute this software for any 4 | // purpose with or without fee is hereby granted, provided that the above 5 | // copyright notice and this permission notice appear in all copies. 6 | // 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | 15 | // NOTE: Due to the following build constraints, this file will only be compiled 16 | // when both cgo is supported and "-tags testcgo" is added to the go test 17 | // command line. This means the cgo tests are only added (and hence run) when 18 | // specifially requested. This configuration is used because spew itself 19 | // does not require cgo to run even though it does handle certain cgo types 20 | // specially. Rather than forcing all clients to require cgo and an external 21 | // C compiler just to run the tests, this scheme makes them optional. 22 | //go:build cgo && testcgo 23 | // +build cgo,testcgo 24 | 25 | package spew_test 26 | 27 | import ( 28 | "fmt" 29 | 30 | "github.com/spewerspew/spew/testdata" 31 | ) 32 | 33 | func addCgoDumpTests() { 34 | // C char pointer. 35 | v := testdata.GetCgoCharPointer() 36 | nv := testdata.GetCgoNullCharPointer() 37 | pv := &v 38 | vcAddr := fmt.Sprintf("%p", v) 39 | vAddr := fmt.Sprintf("%p", pv) 40 | pvAddr := fmt.Sprintf("%p", &pv) 41 | vt := "*testdata._Ctype_char" 42 | vs := "116" 43 | addDumpTest(v, "("+vt+")("+vcAddr+")("+vs+")\n") 44 | addDumpTest(pv, "(*"+vt+")("+vAddr+"->"+vcAddr+")("+vs+")\n") 45 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+"->"+vcAddr+")("+vs+")\n") 46 | addDumpTest(nv, "("+vt+")()\n") 47 | 48 | // C char array. 49 | v2, v2l, v2c := testdata.GetCgoCharArray() 50 | v2Len := fmt.Sprintf("%d", v2l) 51 | v2Cap := fmt.Sprintf("%d", v2c) 52 | v2t := "[6]testdata._Ctype_char" 53 | v2s := "(len=" + v2Len + " cap=" + v2Cap + ") " + 54 | "{\n 00000000 74 65 73 74 32 00 " + 55 | " |test2.|\n}" 56 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 57 | 58 | // C unsigned char array. 59 | v3, v3l, v3c := testdata.GetCgoUnsignedCharArray() 60 | v3Len := fmt.Sprintf("%d", v3l) 61 | v3Cap := fmt.Sprintf("%d", v3c) 62 | v3t := "[6]testdata._Ctype_unsignedchar" 63 | v3t2 := "[6]testdata._Ctype_uchar" 64 | v3s := "(len=" + v3Len + " cap=" + v3Cap + ") " + 65 | "{\n 00000000 74 65 73 74 33 00 " + 66 | " |test3.|\n}" 67 | addDumpTest(v3, "("+v3t+") "+v3s+"\n", "("+v3t2+") "+v3s+"\n") 68 | 69 | // C signed char array. 70 | v4, v4l, v4c := testdata.GetCgoSignedCharArray() 71 | v4Len := fmt.Sprintf("%d", v4l) 72 | v4Cap := fmt.Sprintf("%d", v4c) 73 | v4t := "[6]testdata._Ctype_schar" 74 | v4t2 := "testdata._Ctype_schar" 75 | v4s := "(len=" + v4Len + " cap=" + v4Cap + ") " + 76 | "{\n (" + v4t2 + ") 116,\n (" + v4t2 + ") 101,\n (" + v4t2 + 77 | ") 115,\n (" + v4t2 + ") 116,\n (" + v4t2 + ") 52,\n (" + v4t2 + 78 | ") 0\n}" 79 | addDumpTest(v4, "("+v4t+") "+v4s+"\n") 80 | 81 | // C uint8_t array. 82 | v5, v5l, v5c := testdata.GetCgoUint8tArray() 83 | v5Len := fmt.Sprintf("%d", v5l) 84 | v5Cap := fmt.Sprintf("%d", v5c) 85 | v5t := "[6]testdata._Ctype_uint8_t" 86 | v5t2 := "[6]testdata._Ctype_uchar" 87 | v5s := "(len=" + v5Len + " cap=" + v5Cap + ") " + 88 | "{\n 00000000 74 65 73 74 35 00 " + 89 | " |test5.|\n}" 90 | addDumpTest(v5, "("+v5t+") "+v5s+"\n", "("+v5t2+") "+v5s+"\n") 91 | 92 | // C typedefed unsigned char array. 93 | v6, v6l, v6c := testdata.GetCgoTypdefedUnsignedCharArray() 94 | v6Len := fmt.Sprintf("%d", v6l) 95 | v6Cap := fmt.Sprintf("%d", v6c) 96 | v6t := "[6]testdata._Ctype_custom_uchar_t" 97 | v6t2 := "[6]testdata._Ctype_uchar" 98 | v6s := "(len=" + v6Len + " cap=" + v6Cap + ") " + 99 | "{\n 00000000 74 65 73 74 36 00 " + 100 | " |test6.|\n}" 101 | addDumpTest(v6, "("+v6t+") "+v6s+"\n", "("+v6t2+") "+v6s+"\n") 102 | } 103 | -------------------------------------------------------------------------------- /dumpnocgo_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 Dave Collins 2 | // 3 | // Permission to use, copy, modify, and distribute this software for any 4 | // purpose with or without fee is hereby granted, provided that the above 5 | // copyright notice and this permission notice appear in all copies. 6 | // 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | 15 | // NOTE: Due to the following build constraints, this file will only be compiled 16 | // when either cgo is not supported or "-tags testcgo" is not added to the go 17 | // test command line. This file intentionally does not setup any cgo tests in 18 | // this scenario. 19 | //go:build !cgo || !testcgo 20 | // +build !cgo !testcgo 21 | 22 | package spew_test 23 | 24 | func addCgoDumpTests() { 25 | // Don't add any tests for cgo since this file is only compiled when 26 | // there should not be any cgo tests. 27 | } 28 | -------------------------------------------------------------------------------- /example_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 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 | package spew_test 18 | 19 | import ( 20 | "fmt" 21 | 22 | "github.com/spewerspew/spew" 23 | ) 24 | 25 | type Flag int 26 | 27 | const ( 28 | flagOne Flag = iota 29 | flagTwo 30 | ) 31 | 32 | var flagStrings = map[Flag]string{ 33 | flagOne: "flagOne", 34 | flagTwo: "flagTwo", 35 | } 36 | 37 | func (f Flag) String() string { 38 | if s, ok := flagStrings[f]; ok { 39 | return s 40 | } 41 | return fmt.Sprintf("Unknown flag (%d)", int(f)) 42 | } 43 | 44 | type Bar struct { 45 | data uintptr 46 | } 47 | 48 | type Foo struct { 49 | unexportedField Bar 50 | ExportedField map[interface{}]interface{} 51 | } 52 | 53 | // This example demonstrates how to use Dump to dump variables to stdout. 54 | func ExampleDump() { 55 | // The following package level declarations are assumed for this example: 56 | /* 57 | type Flag int 58 | 59 | const ( 60 | flagOne Flag = iota 61 | flagTwo 62 | ) 63 | 64 | var flagStrings = map[Flag]string{ 65 | flagOne: "flagOne", 66 | flagTwo: "flagTwo", 67 | } 68 | 69 | func (f Flag) String() string { 70 | if s, ok := flagStrings[f]; ok { 71 | return s 72 | } 73 | return fmt.Sprintf("Unknown flag (%d)", int(f)) 74 | } 75 | 76 | type Bar struct { 77 | data uintptr 78 | } 79 | 80 | type Foo struct { 81 | unexportedField Bar 82 | ExportedField map[interface{}]interface{} 83 | } 84 | */ 85 | 86 | // Setup some sample data structures for the example. 87 | bar := Bar{uintptr(0)} 88 | s1 := Foo{bar, map[interface{}]interface{}{"one": true}} 89 | f := Flag(5) 90 | b := []byte{ 91 | 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 92 | 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 93 | 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 94 | 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 95 | 0x31, 0x32, 96 | } 97 | 98 | // Dump! 99 | spew.Dump(s1, f, b) 100 | 101 | // Output: 102 | // (spew_test.Foo) { 103 | // unexportedField: (spew_test.Bar) { 104 | // data: (uintptr) 105 | // }, 106 | // ExportedField: (map[interface {}]interface {}) (len=1) { 107 | // (string) (len=3) "one": (bool) true 108 | // } 109 | // } 110 | // (spew_test.Flag) Unknown flag (5) 111 | // ([]uint8) (len=34 cap=34) { 112 | // 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... | 113 | // 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0| 114 | // 00000020 31 32 |12| 115 | // } 116 | // 117 | } 118 | 119 | // This example demonstrates how to use Printf to display a variable with a 120 | // format string and inline formatting. 121 | func ExamplePrintf() { 122 | // Create a double pointer to a uint 8. 123 | ui8 := uint8(5) 124 | pui8 := &ui8 125 | ppui8 := &pui8 126 | 127 | // Create a circular data type. 128 | type circular struct { 129 | ui8 uint8 130 | c *circular 131 | } 132 | c := circular{ui8: 1} 133 | c.c = &c 134 | 135 | // Print! 136 | spew.Printf("ppui8: %v\n", ppui8) 137 | spew.Printf("circular: %v\n", c) 138 | 139 | // Output: 140 | // ppui8: <**>5 141 | // circular: {1 <*>{1 <*>}} 142 | } 143 | 144 | // This example demonstrates how to use a ConfigState. 145 | func ExampleConfigState() { 146 | // Modify the indent level of the ConfigState only. The global 147 | // configuration is not modified. 148 | scs := spew.ConfigState{Indent: "\t"} 149 | 150 | // Output using the ConfigState instance. 151 | v := map[string]int{"one": 1} 152 | scs.Printf("v: %v\n", v) 153 | scs.Dump(v) 154 | 155 | // Output: 156 | // v: map[one:1] 157 | // (map[string]int) (len=1) { 158 | // (string) (len=3) "one": (int) 1 159 | // } 160 | } 161 | 162 | // This example demonstrates how to use ConfigState.Dump to dump variables to 163 | // stdout 164 | func ExampleConfigState_Dump() { 165 | // See the top-level Dump example for details on the types used in this 166 | // example. 167 | 168 | // Create two ConfigState instances with different indentation. 169 | scs := spew.ConfigState{Indent: "\t"} 170 | scs2 := spew.ConfigState{Indent: " "} 171 | 172 | // Setup some sample data structures for the example. 173 | bar := Bar{uintptr(0)} 174 | s1 := Foo{bar, map[interface{}]interface{}{"one": true}} 175 | 176 | // Dump using the ConfigState instances. 177 | scs.Dump(s1) 178 | scs2.Dump(s1) 179 | 180 | // Output: 181 | // (spew_test.Foo) { 182 | // unexportedField: (spew_test.Bar) { 183 | // data: (uintptr) 184 | // }, 185 | // ExportedField: (map[interface {}]interface {}) (len=1) { 186 | // (string) (len=3) "one": (bool) true 187 | // } 188 | // } 189 | // (spew_test.Foo) { 190 | // unexportedField: (spew_test.Bar) { 191 | // data: (uintptr) 192 | // }, 193 | // ExportedField: (map[interface {}]interface {}) (len=1) { 194 | // (string) (len=3) "one": (bool) true 195 | // } 196 | // } 197 | // 198 | } 199 | 200 | // This example demonstrates how to use ConfigState.Printf to display a variable 201 | // with a format string and inline formatting. 202 | func ExampleConfigState_Printf() { 203 | // See the top-level Dump example for details on the types used in this 204 | // example. 205 | 206 | // Create two ConfigState instances and modify the method handling of the 207 | // first ConfigState only. 208 | scs := spew.NewDefaultConfig() 209 | scs2 := spew.NewDefaultConfig() 210 | scs.DisableMethods = true 211 | 212 | // Alternatively 213 | // scs := spew.ConfigState{Indent: " ", DisableMethods: true} 214 | // scs2 := spew.ConfigState{Indent: " "} 215 | 216 | // This is of type Flag which implements a Stringer and has raw value 1. 217 | f := flagTwo 218 | 219 | // Dump using the ConfigState instances. 220 | scs.Printf("f: %v\n", f) 221 | scs2.Printf("f: %v\n", f) 222 | 223 | // Output: 224 | // f: 1 225 | // f: flagTwo 226 | } 227 | -------------------------------------------------------------------------------- /format.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * Copyright (c) 2021 Anner van Hardenbroek 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | 18 | package spew 19 | 20 | import ( 21 | "fmt" 22 | "io" 23 | "reflect" 24 | "strconv" 25 | "strings" 26 | "sync" 27 | ) 28 | 29 | // supportedFlags is a list of all the character flags supported by fmt package. 30 | const supportedFlags = "0-+# " 31 | 32 | // formatState implements the fmt.Formatter interface and contains information 33 | // about the state of a formatting operation. The NewFormatter function can 34 | // be used to get a new Formatter which can be used directly as arguments 35 | // in standard fmt package printing calls. 36 | type formatState struct { 37 | value interface{} 38 | fs fmt.State 39 | depth int 40 | ignoreNextType bool 41 | ci *cycleInfo 42 | cs *ConfigState 43 | } 44 | 45 | // buildDefaultFormat recreates the original format string without precision 46 | // and width information to pass in to fmt.Sprintf in the case of an 47 | // unrecognized type. Unless new types are added to the language, this 48 | // function won't ever be called. 49 | func (f *formatState) buildDefaultFormat() (format string) { 50 | buf := bytesBufferGet() 51 | defer bytesBufferPut(buf) 52 | buf.Write(percentBytes) 53 | 54 | for _, flag := range supportedFlags { 55 | if f.fs.Flag(int(flag)) { 56 | buf.WriteRune(flag) 57 | } 58 | } 59 | 60 | buf.WriteRune('v') 61 | return buf.String() 62 | } 63 | 64 | // constructOrigFormat recreates the original format string including precision 65 | // and width information to pass along to the standard fmt package. This allows 66 | // automatic deferral of all format strings this package doesn't support. 67 | func (f *formatState) constructOrigFormat(verb rune) (format string) { 68 | buf := bytesBufferGet() 69 | defer bytesBufferPut(buf) 70 | buf.Write(percentBytes) 71 | 72 | for _, flag := range supportedFlags { 73 | if f.fs.Flag(int(flag)) { 74 | buf.WriteRune(flag) 75 | } 76 | } 77 | 78 | if width, ok := f.fs.Width(); ok { 79 | buf.WriteString(strconv.Itoa(width)) 80 | } 81 | 82 | if precision, ok := f.fs.Precision(); ok { 83 | buf.Write(precisionBytes) 84 | buf.WriteString(strconv.Itoa(precision)) 85 | } 86 | 87 | buf.WriteRune(verb) 88 | return buf.String() 89 | } 90 | 91 | // unpackValue returns values inside of non-nil interfaces when possible and 92 | // ensures that types for values which have been unpacked from an interface 93 | // are displayed when the show types flag is also set. 94 | // This is useful for data types like structs, arrays, slices, and maps which 95 | // can contain varying types packed inside an interface. 96 | func (f *formatState) unpackValue(v reflect.Value) reflect.Value { 97 | if v.Kind() == reflect.Interface { 98 | f.ignoreNextType = false 99 | if !v.IsNil() { 100 | v = v.Elem() 101 | } 102 | } 103 | return v 104 | } 105 | 106 | // formatPtr handles formatting of pointers by indirecting them as necessary. 107 | func (f *formatState) formatPtr(v reflect.Value) { 108 | // Display nil if top level pointer is nil. 109 | showTypes := f.fs.Flag('#') 110 | if v.IsNil() && (!showTypes || f.ignoreNextType) { 111 | f.fs.Write(nilAngleBytes) 112 | return 113 | } 114 | 115 | // Figure out how many levels of indirection there are by derferencing 116 | // pointers and unpacking interfaces down the chain while detecting circular 117 | // references. 118 | ve := derefPtr(v, f.depth, f.ci) 119 | 120 | // Display type or indirection level depending on flags. 121 | if showTypes && !f.ignoreNextType { 122 | f.fs.Write(openParenBytes) 123 | for i := 0; i < f.ci.indirects; i++ { 124 | f.fs.Write(asteriskBytes) 125 | } 126 | io.WriteString(f.fs, ve.Type().String()) 127 | f.fs.Write(closeParenBytes) 128 | } else { 129 | if f.ci.nilFound || f.ci.cycleFound { 130 | f.ci.indirects += strings.Count(ve.Type().String(), "*") 131 | } 132 | f.fs.Write(openAngleBytes) 133 | for i := 0; i < f.ci.indirects; i++ { 134 | io.WriteString(f.fs, "*") 135 | } 136 | f.fs.Write(closeAngleBytes) 137 | } 138 | 139 | // Display pointer information depending on flags. 140 | if f.fs.Flag('+') && (len(f.ci.pointerChain) > 0) { 141 | f.fs.Write(openParenBytes) 142 | for i, addr := range f.ci.pointerChain { 143 | if i > 0 { 144 | f.fs.Write(pointerChainBytes) 145 | } 146 | printHexPtr(f.fs, addr) 147 | } 148 | f.fs.Write(closeParenBytes) 149 | } 150 | 151 | // Display dereferenced value. 152 | switch { 153 | case f.ci.nilFound: 154 | f.fs.Write(nilAngleBytes) 155 | 156 | case f.ci.cycleFound: 157 | f.fs.Write(circularShortBytes) 158 | 159 | default: 160 | f.ignoreNextType = true 161 | f.format(ve) 162 | } 163 | } 164 | 165 | // format is the main workhorse for providing the Formatter interface. It 166 | // uses the passed reflect value to figure out what kind of object we are 167 | // dealing with and formats it appropriately. It is a recursive function, 168 | // however circular data structures are detected and handled properly. 169 | func (f *formatState) format(v reflect.Value) { 170 | // Handle invalid reflect values immediately. 171 | kind := v.Kind() 172 | if kind == reflect.Invalid { 173 | f.fs.Write(invalidAngleBytes) 174 | return 175 | } 176 | 177 | // Handle pointers specially. 178 | if kind == reflect.Ptr { 179 | f.formatPtr(v) 180 | return 181 | } 182 | 183 | // Print type information unless already handled elsewhere. 184 | if !f.ignoreNextType && f.fs.Flag('#') { 185 | f.fs.Write(openParenBytes) 186 | io.WriteString(f.fs, v.Type().String()) 187 | f.fs.Write(closeParenBytes) 188 | } 189 | f.ignoreNextType = false 190 | 191 | printValue(f.fs, f, v, kind, f.cs) 192 | } 193 | 194 | // Format satisfies the fmt.Formatter interface. See NewFormatter for usage 195 | // details. 196 | func (f *formatState) Format(fs fmt.State, verb rune) { 197 | f.fs = fs 198 | 199 | // Use standard formatting for verbs that are not v. 200 | if verb != 'v' { 201 | format := f.constructOrigFormat(verb) 202 | fmt.Fprintf(fs, format, f.value) 203 | return 204 | } 205 | 206 | if f.value == nil { 207 | if fs.Flag('#') { 208 | fs.Write(interfaceBytes) 209 | } 210 | fs.Write(nilAngleBytes) 211 | return 212 | } 213 | 214 | f.ci = cycleInfoGet() 215 | defer func() { 216 | cycleInfoPut(f.ci) 217 | f.ci = nil 218 | }() 219 | f.format(reflect.ValueOf(f.value)) 220 | } 221 | 222 | func (f *formatState) printArray(v reflect.Value) { 223 | f.fs.Write(openBracketBytes) 224 | f.depth++ 225 | if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) { 226 | f.fs.Write(maxShortBytes) 227 | } else { 228 | numEntries := v.Len() 229 | for i := 0; i < numEntries; i++ { 230 | if i > 0 { 231 | f.fs.Write(spaceBytes) 232 | } 233 | f.ignoreNextType = true 234 | f.format(f.unpackValue(v.Index(i))) 235 | } 236 | } 237 | f.depth-- 238 | f.fs.Write(closeBracketBytes) 239 | } 240 | 241 | func (f *formatState) printString(v reflect.Value) { 242 | io.WriteString(f.fs, v.String()) 243 | } 244 | 245 | func (f *formatState) printMap(v reflect.Value) { 246 | f.fs.Write(openMapBytes) 247 | f.depth++ 248 | if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) { 249 | f.fs.Write(maxShortBytes) 250 | } else { 251 | keys := v.MapKeys() 252 | if f.cs.SortKeys { 253 | sortValues(keys, f.cs) 254 | } 255 | for i, key := range keys { 256 | if i > 0 { 257 | f.fs.Write(spaceBytes) 258 | } 259 | f.ignoreNextType = true 260 | f.format(f.unpackValue(key)) 261 | f.fs.Write(colonBytes) 262 | f.ignoreNextType = true 263 | f.format(f.unpackValue(v.MapIndex(key))) 264 | } 265 | } 266 | f.depth-- 267 | f.fs.Write(closeMapBytes) 268 | } 269 | 270 | func (f *formatState) printStruct(v reflect.Value) { 271 | numFields := v.NumField() 272 | f.fs.Write(openBraceBytes) 273 | f.depth++ 274 | if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) { 275 | f.fs.Write(maxShortBytes) 276 | } else { 277 | vt := v.Type() 278 | for i := 0; i < numFields; i++ { 279 | if i > 0 { 280 | f.fs.Write(spaceBytes) 281 | } 282 | vtf := vt.Field(i) 283 | if f.fs.Flag('+') || f.fs.Flag('#') { 284 | io.WriteString(f.fs, vtf.Name) 285 | f.fs.Write(colonBytes) 286 | } 287 | f.format(f.unpackValue(v.Field(i))) 288 | } 289 | } 290 | f.depth-- 291 | f.fs.Write(closeBraceBytes) 292 | } 293 | 294 | func (f *formatState) defaultFormat() string { 295 | return f.buildDefaultFormat() 296 | } 297 | 298 | // newFormatter is a helper function to consolidate the logic from the various 299 | // public methods which take varying config states. 300 | func newFormatter(cs *ConfigState, v interface{}) fmt.Formatter { 301 | var f formatState 302 | f.Reset(cs, v) 303 | return &f 304 | } 305 | 306 | // Reset resets the formatter state. 307 | func (f *formatState) Reset(cs *ConfigState, v interface{}) { 308 | *f = formatState{value: v, cs: cs} 309 | } 310 | 311 | /* 312 | NewFormatter returns a custom formatter that satisfies the fmt.Formatter 313 | interface. As a result, it integrates cleanly with standard fmt package 314 | printing functions. The formatter is useful for inline printing of smaller data 315 | types similar to the standard %v format specifier. 316 | 317 | The custom formatter only responds to the %v (most compact), %+v (adds pointer 318 | addresses), %#v (adds types), or %#+v (adds types and pointer addresses) verb 319 | combinations. Any other verbs such as %x and %q will be sent to the the 320 | standard fmt package for formatting. In addition, the custom formatter ignores 321 | the width and precision arguments (however they will still work on the format 322 | specifiers not handled by the custom formatter). 323 | 324 | Typically this function shouldn't be called directly. It is much easier to make 325 | use of the custom formatter by calling one of the convenience functions such as 326 | Printf, Println, or Fprintf. 327 | */ 328 | func NewFormatter(v interface{}) fmt.Formatter { 329 | return newFormatter(&Config, v) 330 | } 331 | 332 | var formattersPool sync.Pool 333 | 334 | func formattersPut(pv interface{}) { formattersPool.Put(pv) } 335 | func formattersGet(cs *ConfigState, args []interface{}) (pv interface{}, formatters []interface{}) { 336 | pv = formattersPool.Get() 337 | if pv != nil { 338 | formatters = pv.([]interface{}) 339 | if len(formatters) < len(args) { 340 | formattersPool.Put(pv) 341 | pv = nil 342 | } 343 | } 344 | if pv == nil { 345 | pv = make([]interface{}, len(args)) 346 | formatters = pv.([]interface{}) 347 | for i := 0; i < len(args); i++ { 348 | formatters[i] = new(formatState) 349 | } 350 | } 351 | if len(formatters) > len(args) { 352 | formatters = formatters[:len(args)] 353 | } 354 | for i, arg := range args { 355 | formatters[i].(*formatState).Reset(cs, arg) 356 | } 357 | return pv, formatters 358 | } 359 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/spewerspew/spew 2 | 3 | go 1.17 4 | -------------------------------------------------------------------------------- /hex.go: -------------------------------------------------------------------------------- 1 | // Copyright 2009 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package spew 6 | 7 | import ( 8 | "encoding/hex" 9 | "io" 10 | "sync" 11 | ) 12 | 13 | var hexDumperPool = sync.Pool{ 14 | New: func() interface{} { 15 | return new(hexDumper) 16 | }, 17 | } 18 | 19 | func hexDump(w io.Writer, data []byte, indent string) { 20 | h := hexDumperPool.Get().(*hexDumper) 21 | h.w = w 22 | h.used = 0 23 | h.n = 0 24 | h.closed = false 25 | h.indent = indent 26 | 27 | h.Write(data) 28 | h.Close() 29 | hexDumperPool.Put(h) 30 | } 31 | 32 | type hexDumper struct { 33 | w io.Writer 34 | rightChars [18]byte 35 | buf [14]byte 36 | used int // number of bytes in the current line 37 | n uint // number of bytes, total 38 | closed bool 39 | indent string 40 | } 41 | 42 | func toChar(b byte) byte { 43 | if b < 32 || b > 126 { 44 | return '.' 45 | } 46 | return b 47 | } 48 | 49 | func (h *hexDumper) Write(data []byte) (n int, err error) { 50 | if h.closed { 51 | panic("dumper closed") 52 | } 53 | 54 | // Output lines look like: 55 | // 00000010 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d |./0123456789:;<=| 56 | // ^ offset ^ extra space ^ ASCII of line. 57 | for i := range data { 58 | if h.used == 0 { 59 | // Before the beginning of a line we print the indent. 60 | _, err = io.WriteString(h.w, h.indent) 61 | if err != nil { 62 | return n, err 63 | } 64 | 65 | // At the beginning of a line we print the current 66 | // offset in hex. 67 | h.buf[0] = byte(h.n >> 24) 68 | h.buf[1] = byte(h.n >> 16) 69 | h.buf[2] = byte(h.n >> 8) 70 | h.buf[3] = byte(h.n) 71 | hex.Encode(h.buf[4:], h.buf[:4]) 72 | h.buf[12] = ' ' 73 | h.buf[13] = ' ' 74 | _, err = h.w.Write(h.buf[4:]) 75 | if err != nil { 76 | return n, err 77 | } 78 | } 79 | hex.Encode(h.buf[:], data[i:i+1]) 80 | h.buf[2] = ' ' 81 | l := 3 82 | if h.used == 7 { 83 | // There's an additional space after the 8th byte. 84 | h.buf[3] = ' ' 85 | l = 4 86 | } else if h.used == 15 { 87 | // At the end of the line there's an extra space and 88 | // the bar for the right column. 89 | h.buf[3] = ' ' 90 | h.buf[4] = '|' 91 | l = 5 92 | } 93 | _, err = h.w.Write(h.buf[:l]) 94 | if err != nil { 95 | return n, err 96 | } 97 | n++ 98 | h.rightChars[h.used] = toChar(data[i]) 99 | h.used++ 100 | h.n++ 101 | if h.used == 16 { 102 | h.rightChars[16] = '|' 103 | h.rightChars[17] = '\n' 104 | _, err = h.w.Write(h.rightChars[:]) 105 | if err != nil { 106 | return n, err 107 | } 108 | h.used = 0 109 | } 110 | } 111 | return n, err 112 | } 113 | 114 | func (h *hexDumper) Close() (err error) { 115 | // See the comments in Write() for the details of this format. 116 | if h.closed { 117 | return 118 | } 119 | h.closed = true 120 | if h.used == 0 { 121 | return 122 | } 123 | h.buf[0] = ' ' 124 | h.buf[1] = ' ' 125 | h.buf[2] = ' ' 126 | h.buf[3] = ' ' 127 | h.buf[4] = '|' 128 | nBytes := h.used 129 | for h.used < 16 { 130 | l := 3 131 | if h.used == 7 { 132 | l = 4 133 | } else if h.used == 15 { 134 | l = 5 135 | } 136 | _, err = h.w.Write(h.buf[:l]) 137 | if err != nil { 138 | return 139 | } 140 | h.used++ 141 | } 142 | h.rightChars[nBytes] = '|' 143 | h.rightChars[nBytes+1] = '\n' 144 | _, err = h.w.Write(h.rightChars[:nBytes+2]) 145 | return err 146 | } 147 | -------------------------------------------------------------------------------- /internal_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 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 | This test file is part of the spew package rather than than the spew_test 19 | package because it needs access to internals to properly test certain cases 20 | which are not possible via the public interface since they should never happen. 21 | */ 22 | 23 | package spew 24 | 25 | import ( 26 | "bytes" 27 | "reflect" 28 | "testing" 29 | ) 30 | 31 | // dummyFmtState implements a fake fmt.State to use for testing invalid 32 | // reflect.Value handling. This is necessary because the fmt package catches 33 | // invalid values before invoking the formatter on them. 34 | type dummyFmtState struct { 35 | bytes.Buffer 36 | } 37 | 38 | func (dfs *dummyFmtState) Flag(f int) bool { 39 | return f == int('+') 40 | } 41 | 42 | func (dfs *dummyFmtState) Precision() (int, bool) { 43 | return 0, false 44 | } 45 | 46 | func (dfs *dummyFmtState) Width() (int, bool) { 47 | return 0, false 48 | } 49 | 50 | // TestInvalidReflectValue ensures the dump and formatter code handles an 51 | // invalid reflect value properly. This needs access to internal state since it 52 | // should never happen in real code and therefore can't be tested via the public 53 | // API. 54 | func TestInvalidReflectValue(t *testing.T) { 55 | i := 1 56 | 57 | // Dump invalid reflect value. 58 | v := new(reflect.Value) 59 | buf := new(bytes.Buffer) 60 | d := dumpState{w: buf, cs: &Config} 61 | d.dump(*v) 62 | s := buf.String() 63 | want := "" 64 | if s != want { 65 | t.Errorf("InvalidReflectValue #%d\n got: %s want: %s", i, s, want) 66 | } 67 | i++ 68 | 69 | // Formatter invalid reflect value. 70 | buf2 := new(dummyFmtState) 71 | f := formatState{value: *v, cs: &Config, fs: buf2} 72 | f.format(*v) 73 | s = buf2.String() 74 | want = "" 75 | if s != want { 76 | t.Errorf("InvalidReflectValue #%d got: %s want: %s", i, s, want) 77 | } 78 | } 79 | 80 | // SortValues makes the internal sortValues function available to the test 81 | // package. 82 | func SortValues(values []reflect.Value, cs *ConfigState) { 83 | sortValues(values, cs) 84 | } 85 | -------------------------------------------------------------------------------- /internalunsafe_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013-2016 Dave Collins 2 | 3 | // Permission to use, copy, modify, and distribute this software for any 4 | // purpose with or without fee is hereby granted, provided that the above 5 | // copyright notice and this permission notice appear in all copies. 6 | 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | 15 | // NOTE: Due to the following build constraints, this file will only be compiled 16 | // when the code is not running on Google App Engine, compiled by GopherJS, and 17 | // "-tags safe" is not added to the go build command line. The "disableunsafe" 18 | // tag is deprecated and thus should not be used. 19 | //go:build !js && !appengine && !safe && !disableunsafe && go1.4 20 | // +build !js,!appengine,!safe,!disableunsafe,go1.4 21 | 22 | /* 23 | This test file is part of the spew package rather than than the spew_test 24 | package because it needs access to internals to properly test certain cases 25 | which are not possible via the public interface since they should never happen. 26 | */ 27 | 28 | package spew 29 | 30 | import ( 31 | "bytes" 32 | "reflect" 33 | "testing" 34 | ) 35 | 36 | // changeKind uses unsafe to intentionally change the kind of a reflect.Value to 37 | // the maximum kind value which does not exist. This is needed to test the 38 | // fallback code which punts to the standard fmt library for new types that 39 | // might get added to the language. 40 | func changeKind(v *reflect.Value, readOnly bool) { 41 | flags := flagField(v) 42 | if readOnly { 43 | *flags |= flagRO 44 | } else { 45 | *flags &^= flagRO 46 | } 47 | *flags |= flagKindMask 48 | } 49 | 50 | // TestAddedReflectValue tests functionaly of the dump and formatter code which 51 | // falls back to the standard fmt library for new types that might get added to 52 | // the language. 53 | func TestAddedReflectValue(t *testing.T) { 54 | i := 1 55 | 56 | // Dump using a reflect.Value that is exported. 57 | v := reflect.ValueOf(int8(5)) 58 | changeKind(&v, false) 59 | buf := new(bytes.Buffer) 60 | d := dumpState{w: buf, cs: &Config} 61 | d.dump(v) 62 | s := buf.String() 63 | want := "(int8) 5" 64 | if s != want { 65 | t.Errorf("TestAddedReflectValue #%d\n got: %s want: %s", i, s, want) 66 | } 67 | i++ 68 | 69 | // Dump using a reflect.Value that is not exported. 70 | changeKind(&v, true) 71 | buf.Reset() 72 | d.dump(v) 73 | s = buf.String() 74 | want = "(int8) " 75 | if s != want { 76 | t.Errorf("TestAddedReflectValue #%d\n got: %s want: %s", i, s, want) 77 | } 78 | i++ 79 | 80 | // Formatter using a reflect.Value that is exported. 81 | changeKind(&v, false) 82 | buf2 := new(dummyFmtState) 83 | f := formatState{value: v, cs: &Config, fs: buf2} 84 | f.format(v) 85 | s = buf2.String() 86 | want = "5" 87 | if s != want { 88 | t.Errorf("TestAddedReflectValue #%d got: %s want: %s", i, s, want) 89 | } 90 | i++ 91 | 92 | // Formatter using a reflect.Value that is not exported. 93 | changeKind(&v, true) 94 | buf2.Reset() 95 | f = formatState{value: v, cs: &Config, fs: buf2} 96 | f.format(v) 97 | s = buf2.String() 98 | want = "" 99 | if s != want { 100 | t.Errorf("TestAddedReflectValue #%d got: %s want: %s", i, s, want) 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /spew.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * Copyright (c) 2021 Anner van Hardenbroek 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | 18 | package spew 19 | 20 | import "io" 21 | 22 | // Errorf is a wrapper for fmt.Errorf that treats each argument as if it were 23 | // passed with a default Formatter interface returned by NewFormatter. It 24 | // returns the formatted string as a value that satisfies error. See 25 | // NewFormatter for formatting details. 26 | // 27 | // This function is shorthand for the following syntax: 28 | // 29 | // fmt.Errorf(format, spew.NewFormatter(a), spew.NewFormatter(b)) 30 | func Errorf(format string, a ...interface{}) (err error) { 31 | return Config.Errorf(format, a...) 32 | } 33 | 34 | // Fprint is a wrapper for fmt.Fprint that treats each argument as if it were 35 | // passed with a default Formatter interface returned by NewFormatter. It 36 | // returns the number of bytes written and any write error encountered. See 37 | // NewFormatter for formatting details. 38 | // 39 | // This function is shorthand for the following syntax: 40 | // 41 | // fmt.Fprint(w, spew.NewFormatter(a), spew.NewFormatter(b)) 42 | func Fprint(w io.Writer, a ...interface{}) (n int, err error) { 43 | return Config.Fprint(w, a...) 44 | } 45 | 46 | // Fprintf is a wrapper for fmt.Fprintf that treats each argument as if it were 47 | // passed with a default Formatter interface returned by NewFormatter. It 48 | // returns the number of bytes written and any write error encountered. See 49 | // NewFormatter for formatting details. 50 | // 51 | // This function is shorthand for the following syntax: 52 | // 53 | // fmt.Fprintf(w, format, spew.NewFormatter(a), spew.NewFormatter(b)) 54 | func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) { 55 | return Config.Fprintf(w, format, a...) 56 | } 57 | 58 | // Fprintln is a wrapper for fmt.Fprintln that treats each argument as if it 59 | // passed with a default Formatter interface returned by NewFormatter. See 60 | // NewFormatter for formatting details. 61 | // 62 | // This function is shorthand for the following syntax: 63 | // 64 | // fmt.Fprintln(w, spew.NewFormatter(a), spew.NewFormatter(b)) 65 | func Fprintln(w io.Writer, a ...interface{}) (n int, err error) { 66 | return Config.Fprintln(w, a...) 67 | } 68 | 69 | // Print is a wrapper for fmt.Print that treats each argument as if it were 70 | // passed with a default Formatter interface returned by NewFormatter. It 71 | // returns the number of bytes written and any write error encountered. See 72 | // NewFormatter for formatting details. 73 | // 74 | // This function is shorthand for the following syntax: 75 | // 76 | // fmt.Print(spew.NewFormatter(a), spew.NewFormatter(b)) 77 | func Print(a ...interface{}) (n int, err error) { 78 | return Config.Print(a...) 79 | } 80 | 81 | // Printf is a wrapper for fmt.Printf that treats each argument as if it were 82 | // passed with a default Formatter interface returned by NewFormatter. It 83 | // returns the number of bytes written and any write error encountered. See 84 | // NewFormatter for formatting details. 85 | // 86 | // This function is shorthand for the following syntax: 87 | // 88 | // fmt.Printf(format, spew.NewFormatter(a), spew.NewFormatter(b)) 89 | func Printf(format string, a ...interface{}) (n int, err error) { 90 | return Config.Printf(format, a...) 91 | } 92 | 93 | // Println is a wrapper for fmt.Println that treats each argument as if it were 94 | // passed with a default Formatter interface returned by NewFormatter. It 95 | // returns the number of bytes written and any write error encountered. See 96 | // NewFormatter for formatting details. 97 | // 98 | // This function is shorthand for the following syntax: 99 | // 100 | // fmt.Println(spew.NewFormatter(a), spew.NewFormatter(b)) 101 | func Println(a ...interface{}) (n int, err error) { 102 | return Config.Println(a...) 103 | } 104 | 105 | // Sprint is a wrapper for fmt.Sprint that treats each argument as if it were 106 | // passed with a default Formatter interface returned by NewFormatter. It 107 | // returns the resulting string. See NewFormatter for formatting details. 108 | // 109 | // This function is shorthand for the following syntax: 110 | // 111 | // fmt.Sprint(spew.NewFormatter(a), spew.NewFormatter(b)) 112 | func Sprint(a ...interface{}) string { 113 | return Config.Sprint(a...) 114 | } 115 | 116 | // Sprintf is a wrapper for fmt.Sprintf that treats each argument as if it were 117 | // passed with a default Formatter interface returned by NewFormatter. It 118 | // returns the resulting string. See NewFormatter for formatting details. 119 | // 120 | // This function is shorthand for the following syntax: 121 | // 122 | // fmt.Sprintf(format, spew.NewFormatter(a), spew.NewFormatter(b)) 123 | func Sprintf(format string, a ...interface{}) string { 124 | return Config.Sprintf(format, a...) 125 | } 126 | 127 | // Sprintln is a wrapper for fmt.Sprintln that treats each argument as if it 128 | // were passed with a default Formatter interface returned by NewFormatter. It 129 | // returns the resulting string. See NewFormatter for formatting details. 130 | // 131 | // This function is shorthand for the following syntax: 132 | // 133 | // fmt.Sprintln(spew.NewFormatter(a), spew.NewFormatter(b)) 134 | func Sprintln(a ...interface{}) string { 135 | return Config.Sprintln(a...) 136 | } 137 | 138 | // Fdump formats and displays the passed arguments to io.Writer w. It formats 139 | // exactly the same as Dump. 140 | func Fdump(w io.Writer, a ...interface{}) { 141 | Config.Fdump(w, a...) 142 | } 143 | 144 | // Sdump returns a string with the passed arguments formatted exactly the same 145 | // as Dump. 146 | func Sdump(a ...interface{}) string { 147 | return Config.Sdump(a...) 148 | } 149 | 150 | /* 151 | Dump displays the passed parameters to standard out with newlines, customizable 152 | indentation, and additional debug information such as complete types and all 153 | pointer addresses used to indirect to the final value. It provides the 154 | following features over the built-in printing facilities provided by the fmt 155 | package: 156 | 157 | * Pointers are dereferenced and followed 158 | * Circular data structures are detected and handled properly 159 | * Custom Stringer/error interfaces are optionally invoked, including 160 | on unexported types 161 | * Custom types which only implement the Stringer/error interfaces via 162 | a pointer receiver are optionally invoked when passing non-pointer 163 | variables 164 | * Byte arrays and slices are dumped like the hexdump -C command which 165 | includes offsets, byte values in hex, and ASCII output 166 | 167 | The configuration options are controlled by an exported package global, 168 | spew.Config. See ConfigState for options documentation. 169 | 170 | See Fdump if you would prefer dumping to an arbitrary io.Writer or Sdump to 171 | get the formatted result as a string. 172 | */ 173 | func Dump(a ...interface{}) { 174 | Config.Dump(a...) 175 | } 176 | -------------------------------------------------------------------------------- /spew_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * Copyright (c) 2023 Anner van Hardenbroek 4 | * 5 | * Permission to use, copy, modify, and distribute this software for any 6 | * purpose with or without fee is hereby granted, provided that the above 7 | * copyright notice and this permission notice appear in all copies. 8 | * 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | */ 17 | 18 | package spew_test 19 | 20 | import ( 21 | "bytes" 22 | "fmt" 23 | "io/ioutil" 24 | "os" 25 | "testing" 26 | 27 | "github.com/spewerspew/spew" 28 | ) 29 | 30 | // spewFunc is used to identify which public function of the spew package or 31 | // ConfigState a test applies to. 32 | type spewFunc int 33 | 34 | const ( 35 | fCSFdump spewFunc = iota 36 | fCSFprint 37 | fCSFprintf 38 | fCSFprintln 39 | fCSPrint 40 | fCSPrintln 41 | fCSSdump 42 | fCSSprint 43 | fCSSprintf 44 | fCSSprintln 45 | fCSErrorf 46 | fCSNewFormatter 47 | fErrorf 48 | fFprint 49 | fFprintln 50 | fPrint 51 | fPrintln 52 | fSdump 53 | fSprint 54 | fSprintf 55 | fSprintln 56 | ) 57 | 58 | // Map of spewFunc values to names for pretty printing. 59 | var spewFuncStrings = map[spewFunc]string{ 60 | fCSFdump: "ConfigState.Fdump", 61 | fCSFprint: "ConfigState.Fprint", 62 | fCSFprintf: "ConfigState.Fprintf", 63 | fCSFprintln: "ConfigState.Fprintln", 64 | fCSSdump: "ConfigState.Sdump", 65 | fCSPrint: "ConfigState.Print", 66 | fCSPrintln: "ConfigState.Println", 67 | fCSSprint: "ConfigState.Sprint", 68 | fCSSprintf: "ConfigState.Sprintf", 69 | fCSSprintln: "ConfigState.Sprintln", 70 | fCSErrorf: "ConfigState.Errorf", 71 | fCSNewFormatter: "ConfigState.NewFormatter", 72 | fErrorf: "spew.Errorf", 73 | fFprint: "spew.Fprint", 74 | fFprintln: "spew.Fprintln", 75 | fPrint: "spew.Print", 76 | fPrintln: "spew.Println", 77 | fSdump: "spew.Sdump", 78 | fSprint: "spew.Sprint", 79 | fSprintf: "spew.Sprintf", 80 | fSprintln: "spew.Sprintln", 81 | } 82 | 83 | func (f spewFunc) String() string { 84 | if s, ok := spewFuncStrings[f]; ok { 85 | return s 86 | } 87 | return fmt.Sprintf("Unknown spewFunc (%d)", int(f)) 88 | } 89 | 90 | // spewTest is used to describe a test to be performed against the public 91 | // functions of the spew package or ConfigState. 92 | type spewTest struct { 93 | cs *spew.ConfigState 94 | f spewFunc 95 | format string 96 | in interface{} 97 | want string 98 | } 99 | 100 | // spewTests houses the tests to be performed against the public functions of 101 | // the spew package and ConfigState. 102 | // 103 | // These tests are only intended to ensure the public functions are exercised 104 | // and are intentionally not exhaustive of types. The exhaustive type 105 | // tests are handled in the dump and format tests. 106 | var spewTests []spewTest 107 | 108 | // redirStdout is a helper function to return the standard output from f as a 109 | // byte slice. 110 | func redirStdout(f func()) ([]byte, error) { 111 | tempFile, err := ioutil.TempFile("", "ss-test") 112 | if err != nil { 113 | return nil, err 114 | } 115 | fileName := tempFile.Name() 116 | defer os.Remove(fileName) // Ignore error 117 | 118 | origStdout := os.Stdout 119 | os.Stdout = tempFile 120 | f() 121 | os.Stdout = origStdout 122 | tempFile.Close() 123 | 124 | return ioutil.ReadFile(fileName) 125 | } 126 | 127 | func initSpewTests() { 128 | // Config states with various settings. 129 | scsDefault := spew.NewDefaultConfig() 130 | scsNoMethods := &spew.ConfigState{Indent: " ", DisableMethods: true} 131 | scsNoPmethods := &spew.ConfigState{Indent: " ", DisablePointerMethods: true} 132 | scsMaxDepth := &spew.ConfigState{Indent: " ", MaxDepth: 1} 133 | scsContinue := &spew.ConfigState{Indent: " ", ContinueOnMethod: true} 134 | scsNoPtrAddr := &spew.ConfigState{DisablePointerAddresses: true} 135 | scsNoCap := &spew.ConfigState{DisableCapacities: true} 136 | 137 | // Variables for tests on types which implement Stringer interface with and 138 | // without a pointer receiver. 139 | ts := stringer("test") 140 | tps := pstringer("test") 141 | 142 | // Variables for tests on types which implement Stringer interface without 143 | // a pointer receiver. 144 | cs := make(chanStringer) 145 | fs := funcStringer(func() {}) 146 | ms := mapStringer{} 147 | 148 | type ptrTester struct { 149 | s *struct{} 150 | } 151 | tptr := &ptrTester{s: &struct{}{}} 152 | 153 | // depthTester is used to test max depth handling for structs, array, slices 154 | // and maps. 155 | type depthTester struct { 156 | ic indirCir1 157 | arr [1]string 158 | slice []string 159 | m map[string]int 160 | } 161 | dt := depthTester{indirCir1{nil}, [1]string{"arr"}, []string{"slice"}, 162 | map[string]int{"one": 1}} 163 | 164 | // Variable for tests on types which implement error interface. 165 | te := customError(10) 166 | 167 | spewTests = []spewTest{ 168 | {scsDefault, fCSFdump, "", int8(127), "(int8) 127\n"}, 169 | {scsDefault, fCSFprint, "", int16(32767), "32767"}, 170 | {scsDefault, fCSFprintf, "%v", int32(2147483647), "2147483647"}, 171 | {scsDefault, fCSFprintln, "", int(2147483647), "2147483647\n"}, 172 | {scsDefault, fCSPrint, "", int64(9223372036854775807), "9223372036854775807"}, 173 | {scsDefault, fCSPrintln, "", uint8(255), "255\n"}, 174 | {scsDefault, fCSSdump, "", uint8(64), "(uint8) 64\n"}, 175 | {scsDefault, fCSSprint, "", complex(1, 2), "(1+2i)"}, 176 | {scsDefault, fCSSprintf, "%v", complex(float32(3), 4), "(3+4i)"}, 177 | {scsDefault, fCSSprintln, "", complex(float64(5), 6), "(5+6i)\n"}, 178 | {scsDefault, fCSErrorf, "%#v", uint16(65535), "(uint16)65535"}, 179 | {scsDefault, fCSNewFormatter, "%v", uint32(4294967295), "4294967295"}, 180 | {scsDefault, fErrorf, "%v", uint64(18446744073709551615), "18446744073709551615"}, 181 | {scsDefault, fFprint, "", float32(3.14), "3.14"}, 182 | {scsDefault, fFprintln, "", float64(6.28), "6.28\n"}, 183 | {scsDefault, fPrint, "", true, "true"}, 184 | {scsDefault, fPrintln, "", false, "false\n"}, 185 | {scsDefault, fSdump, "", complex(-10, -20), "(complex128) (-10-20i)\n"}, 186 | {scsDefault, fSprint, "", complex(-1, -2), "(-1-2i)"}, 187 | {scsDefault, fSprintf, "%v", complex(float32(-3), -4), "(-3-4i)"}, 188 | {scsDefault, fSprintln, "", complex(float64(-5), -6), "(-5-6i)\n"}, 189 | {scsNoMethods, fCSFprint, "", ts, "test"}, 190 | {scsNoMethods, fCSFprint, "", &ts, "<*>test"}, 191 | {scsNoMethods, fCSFprint, "", tps, "test"}, 192 | {scsNoMethods, fCSFprint, "", &tps, "<*>test"}, 193 | {scsNoPmethods, fCSFprint, "", ts, "stringer test"}, 194 | {scsNoPmethods, fCSFprint, "", &ts, "<*>stringer test"}, 195 | {scsNoPmethods, fCSFprint, "", tps, "test"}, 196 | {scsNoPmethods, fCSFprint, "", &tps, "<*>stringer test"}, 197 | {scsMaxDepth, fCSFprint, "", dt, "{{} [] [] map[]}"}, 198 | {scsMaxDepth, fCSFdump, "", dt, "(spew_test.depthTester) {\n" + 199 | " ic: (spew_test.indirCir1) {\n \n },\n" + 200 | " arr: ([1]string) (len=1 cap=1) {\n \n },\n" + 201 | " slice: ([]string) (len=1 cap=1) {\n \n },\n" + 202 | " m: (map[string]int) (len=1) {\n \n }\n}\n"}, 203 | {scsContinue, fCSFprint, "", ts, "(stringer test) test"}, 204 | {scsContinue, fCSFdump, "", ts, "(spew_test.stringer) " + 205 | "(len=4) (stringer test) \"test\"\n"}, 206 | {scsContinue, fCSFprint, "", te, "(error: 10) 10"}, 207 | {scsContinue, fCSFdump, "", te, "(spew_test.customError) " + 208 | "(error: 10) 10\n"}, 209 | {scsNoPtrAddr, fCSFprint, "", tptr, "<*>{<*>{}}"}, 210 | {scsNoPtrAddr, fCSSdump, "", tptr, "(*spew_test.ptrTester)({\ns: (*struct {})({\n})\n})\n"}, 211 | {scsNoCap, fCSSdump, "", make([]string, 0, 10), "([]string) {\n}\n"}, 212 | {scsNoCap, fCSSdump, "", make([]string, 1, 10), "([]string) (len=1) {\n(string) \"\"\n}\n"}, 213 | 214 | {scsDefault, fCSFprint, "", cs, "chan stringer"}, 215 | {scsDefault, fCSFprint, "", &cs, "<*>chan stringer"}, 216 | {scsNoPmethods, fCSFprint, "", cs, "chan stringer"}, 217 | {scsNoPmethods, fCSFprint, "", &cs, "<*>chan stringer"}, 218 | {scsNoMethods, fCSFprint, "", cs, fmt.Sprintf("%p", cs)}, 219 | {scsNoMethods, fCSFprint, "", &cs, fmt.Sprintf("<*>%p", cs)}, 220 | 221 | {scsDefault, fFprint, "", ms, "map stringer"}, 222 | {scsDefault, fFprint, "", &ms, "<*>map stringer"}, 223 | {scsNoMethods, fCSFprint, "", ms, "map[]"}, 224 | {scsNoMethods, fCSFprint, "", &ms, "<*>map[]"}, 225 | {scsNoPmethods, fCSFprint, "", ms, "map stringer"}, 226 | {scsNoPmethods, fCSFprint, "", &ms, "<*>map stringer"}, 227 | 228 | {scsDefault, fFprint, "", fs, "func stringer"}, 229 | {scsDefault, fFprint, "", &fs, "<*>func stringer"}, 230 | {scsNoMethods, fCSFprint, "", fs, fmt.Sprintf("%p", fs)}, 231 | {scsNoMethods, fCSFprint, "", &fs, fmt.Sprintf("<*>%p", fs)}, 232 | {scsNoPmethods, fCSFprint, "", fs, "func stringer"}, 233 | {scsNoPmethods, fCSFprint, "", &fs, "<*>func stringer"}, 234 | } 235 | } 236 | 237 | // TestSpew executes all of the tests described by spewTests. 238 | func TestSpew(t *testing.T) { 239 | initSpewTests() 240 | 241 | t.Logf("Running %d tests", len(spewTests)) 242 | for i, test := range spewTests { 243 | buf := new(bytes.Buffer) 244 | switch test.f { 245 | case fCSFdump: 246 | test.cs.Fdump(buf, test.in) 247 | 248 | case fCSFprint: 249 | test.cs.Fprint(buf, test.in) 250 | 251 | case fCSFprintf: 252 | test.cs.Fprintf(buf, test.format, test.in) 253 | 254 | case fCSFprintln: 255 | test.cs.Fprintln(buf, test.in) 256 | 257 | case fCSPrint: 258 | b, err := redirStdout(func() { test.cs.Print(test.in) }) 259 | if err != nil { 260 | t.Errorf("%v #%d %v", test.f, i, err) 261 | continue 262 | } 263 | buf.Write(b) 264 | 265 | case fCSPrintln: 266 | b, err := redirStdout(func() { test.cs.Println(test.in) }) 267 | if err != nil { 268 | t.Errorf("%v #%d %v", test.f, i, err) 269 | continue 270 | } 271 | buf.Write(b) 272 | 273 | case fCSSdump: 274 | str := test.cs.Sdump(test.in) 275 | buf.WriteString(str) 276 | 277 | case fCSSprint: 278 | str := test.cs.Sprint(test.in) 279 | buf.WriteString(str) 280 | 281 | case fCSSprintf: 282 | str := test.cs.Sprintf(test.format, test.in) 283 | buf.WriteString(str) 284 | 285 | case fCSSprintln: 286 | str := test.cs.Sprintln(test.in) 287 | buf.WriteString(str) 288 | 289 | case fCSErrorf: 290 | err := test.cs.Errorf(test.format, test.in) 291 | buf.WriteString(err.Error()) 292 | 293 | case fCSNewFormatter: 294 | fmt.Fprintf(buf, test.format, test.cs.NewFormatter(test.in)) 295 | 296 | case fErrorf: 297 | err := spew.Errorf(test.format, test.in) 298 | buf.WriteString(err.Error()) 299 | 300 | case fFprint: 301 | spew.Fprint(buf, test.in) 302 | 303 | case fFprintln: 304 | spew.Fprintln(buf, test.in) 305 | 306 | case fPrint: 307 | b, err := redirStdout(func() { spew.Print(test.in) }) 308 | if err != nil { 309 | t.Errorf("%v #%d %v", test.f, i, err) 310 | continue 311 | } 312 | buf.Write(b) 313 | 314 | case fPrintln: 315 | b, err := redirStdout(func() { spew.Println(test.in) }) 316 | if err != nil { 317 | t.Errorf("%v #%d %v", test.f, i, err) 318 | continue 319 | } 320 | buf.Write(b) 321 | 322 | case fSdump: 323 | str := spew.Sdump(test.in) 324 | buf.WriteString(str) 325 | 326 | case fSprint: 327 | str := spew.Sprint(test.in) 328 | buf.WriteString(str) 329 | 330 | case fSprintf: 331 | str := spew.Sprintf(test.format, test.in) 332 | buf.WriteString(str) 333 | 334 | case fSprintln: 335 | str := spew.Sprintln(test.in) 336 | buf.WriteString(str) 337 | 338 | default: 339 | t.Errorf("%v #%d unrecognized function", test.f, i) 340 | continue 341 | } 342 | s := buf.String() 343 | if test.want != s { 344 | t.Errorf("ConfigState #%d\n got: %s want: %s", i, s, test.want) 345 | continue 346 | } 347 | } 348 | } 349 | -------------------------------------------------------------------------------- /testdata/dumpcgo.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 Dave Collins 2 | // Copyright (c) 2023 Anner van Hardenbroek 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 | // NOTE: Due to the following build constraints, this file will only be compiled 17 | // when both cgo is supported and "-tags testcgo" is added to the go test 18 | // command line. This code should really only be in the dumpcgo_test.go file, 19 | // but unfortunately Go will not allow cgo in test files, so this is a 20 | // workaround to allow cgo types to be tested. This configuration is used 21 | // because spew itself does not require cgo to run even though it does handle 22 | // certain cgo types specially. Rather than forcing all clients to require cgo 23 | // and an external C compiler just to run the tests, this scheme makes them 24 | // optional. 25 | //go:build cgo && testcgo 26 | // +build cgo,testcgo 27 | 28 | package testdata 29 | 30 | /* 31 | #include 32 | typedef unsigned char custom_uchar_t; 33 | 34 | char *ncp = 0; 35 | char *cp = "test"; 36 | char ca[6] = {'t', 'e', 's', 't', '2', '\0'}; 37 | unsigned char uca[6] = {'t', 'e', 's', 't', '3', '\0'}; 38 | signed char sca[6] = {'t', 'e', 's', 't', '4', '\0'}; 39 | uint8_t ui8ta[6] = {'t', 'e', 's', 't', '5', '\0'}; 40 | custom_uchar_t tuca[6] = {'t', 'e', 's', 't', '6', '\0'}; 41 | */ 42 | import "C" 43 | 44 | // GetCgoNullCharPointer returns a null char pointer via cgo. This is only 45 | // used for tests. 46 | func GetCgoNullCharPointer() interface{} { 47 | return C.ncp 48 | } 49 | 50 | // GetCgoCharPointer returns a char pointer via cgo. This is only used for 51 | // tests. 52 | func GetCgoCharPointer() interface{} { 53 | return C.cp 54 | } 55 | 56 | // GetCgoCharArray returns a char array via cgo and the array's len and cap. 57 | // This is only used for tests. 58 | func GetCgoCharArray() (interface{}, int, int) { 59 | return C.ca, len(C.ca), cap(C.ca) 60 | } 61 | 62 | // GetCgoUnsignedCharArray returns an unsigned char array via cgo and the 63 | // array's len and cap. This is only used for tests. 64 | func GetCgoUnsignedCharArray() (interface{}, int, int) { 65 | return C.uca, len(C.uca), cap(C.uca) 66 | } 67 | 68 | // GetCgoSignedCharArray returns a signed char array via cgo and the array's len 69 | // and cap. This is only used for tests. 70 | func GetCgoSignedCharArray() (interface{}, int, int) { 71 | return C.sca, len(C.sca), cap(C.sca) 72 | } 73 | 74 | // GetCgoUint8tArray returns a uint8_t array via cgo and the array's len and 75 | // cap. This is only used for tests. 76 | func GetCgoUint8tArray() (interface{}, int, int) { 77 | return C.ui8ta, len(C.ui8ta), cap(C.ui8ta) 78 | } 79 | 80 | // GetCgoTypdefedUnsignedCharArray returns a typedefed unsigned char array via 81 | // cgo and the array's len and cap. This is only used for tests. 82 | func GetCgoTypdefedUnsignedCharArray() (interface{}, int, int) { 83 | return C.tuca, len(C.tuca), cap(C.tuca) 84 | } 85 | --------------------------------------------------------------------------------