| 1 |
/* |
| 2 |
* Copyright (c) 2013-2016 Dave Collins <[email protected]> |
| 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 |
Originally copied from https://github.com/davecgh/go-spew it has been |
| 22 |
patched to fit go-testdeep needs as the original repository seems to |
| 23 |
have no activity anymore. |
| 24 |
|
| 25 |
A quick overview of the additional features spew provides over the built-in |
| 26 |
printing facilities for Go data types are as follows: |
| 27 |
|
| 28 |
- Pointers are dereferenced and followed |
| 29 |
- Circular data structures are detected and handled properly |
| 30 |
- Custom fmt.Stringer/error interfaces are optionally invoked, including |
| 31 |
on unexported types |
| 32 |
- Custom types which only implement the fmt.Stringer/error interfaces via |
| 33 |
a pointer receiver are optionally invoked when passing non-pointer |
| 34 |
variables |
| 35 |
- Byte arrays and slices are dumped like the hexdump -C command which |
| 36 |
includes offsets, byte values in hex, and ASCII output |
| 37 |
|
| 38 |
spew dumps Go data structures using a style which prints with newlines, |
| 39 |
customizable indentation, and additional debug information such as |
| 40 |
types and all pointer addresses used to indirect to the final value. |
| 41 |
|
| 42 |
# Quick Start |
| 43 |
|
| 44 |
This section demonstrates how to quickly get started with spew. See the |
| 45 |
sections below for further details on formatting and configuration options. |
| 46 |
|
| 47 |
To dump a variable with full newlines, indentation, type and pointer |
| 48 |
information use Sdump: |
| 49 |
|
| 50 |
str := spew.Sdump(myVar1) |
| 51 |
|
| 52 |
# Configuration Options |
| 53 |
|
| 54 |
Configuration of spew is handled by fields in the ConfigState type. For |
| 55 |
convenience, all of the top-level functions use a global state available |
| 56 |
via the spew.Config global. |
| 57 |
|
| 58 |
It is also possible to create a ConfigState instance that provides methods |
| 59 |
equivalent to the top-level functions. This allows concurrent configuration |
| 60 |
options. See the ConfigState documentation for more details. |
| 61 |
|
| 62 |
The following configuration options are available: |
| 63 |
|
| 64 |
- Indent |
| 65 |
String to use for each indentation level for Sdump function. |
| 66 |
It is a single space by default. A popular alternative is "\t". |
| 67 |
|
| 68 |
- MaxDepth |
| 69 |
Maximum number of levels to descend into nested data structures. |
| 70 |
There is no limit by default. |
| 71 |
|
| 72 |
- DisableMethods |
| 73 |
Disables invocation of error and fmt.Stringer interface methods. |
| 74 |
Method invocation is enabled by default. |
| 75 |
|
| 76 |
- DisablePointerMethods |
| 77 |
Disables invocation of error and fmt.Stringer interface methods on types |
| 78 |
which only accept pointer receivers from non-pointer variables. |
| 79 |
Pointer method invocation is enabled by default. |
| 80 |
|
| 81 |
- DisablePointerAddresses |
| 82 |
DisablePointerAddresses specifies whether to disable the printing of |
| 83 |
pointer addresses. This is useful when diffing data structures in tests. |
| 84 |
|
| 85 |
- EnableCapacities |
| 86 |
EnableCapacities specifies whether to enaable the printing of |
| 87 |
capacities for arrays, slices and channels. |
| 88 |
|
| 89 |
# Sdump Usage |
| 90 |
|
| 91 |
Simply call spew.Sdump with a variable you want to dump as a string: |
| 92 |
|
| 93 |
str := spew.Sdump(myVar1) |
| 94 |
|
| 95 |
# Sample Sdump Output |
| 96 |
|
| 97 |
(main.Foo) { |
| 98 |
unexportedField: (*main.Bar)(0xf84002e210)({ |
| 99 |
flag: (main.Flag) flagTwo, |
| 100 |
data: (uintptr) <nil> |
| 101 |
}), |
| 102 |
ExportedField: (map[interface {}]interface {}) (len=1) { |
| 103 |
(string) (len=3) "one": (bool) true |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
Byte (and uint8) arrays and slices are displayed uniquely like the hexdump -C |
| 108 |
command as shown. |
| 109 |
|
| 110 |
([]uint8) (len=32) { |
| 111 |
00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... | |
| 112 |
00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0| |
| 113 |
00000020 31 32 |12| |
| 114 |
} |
| 115 |
|
| 116 |
# Errors |
| 117 |
|
| 118 |
Since it is possible for custom fmt.Stringer/error interfaces to panic, |
| 119 |
spew detects them and handles them internally by printing the panic |
| 120 |
information inline with the output. Since spew is intended to |
| 121 |
provide deep pretty printing capabilities on structures, it |
| 122 |
intentionally does not return any errors. |
| 123 |
*/ |
| 124 |
package spew |