github.com/kortschak/utter@v1.5.0/example_test.go (about) 1 /* 2 * Copyright (c) 2013 Dave Collins <dave@davec.name> 3 * Copyright (c) 2015 Dan Kortschak <dan.kortschak@adelaide.edu.au> 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 utter_test 19 20 import ( 21 "fmt" 22 23 "github.com/kortschak/utter" 24 ) 25 26 type Flag int 27 28 const ( 29 flagOne Flag = iota 30 flagTwo 31 ) 32 33 var flagStrings = map[Flag]string{ 34 flagOne: "flagOne", 35 flagTwo: "flagTwo", 36 } 37 38 func (f Flag) String() string { 39 if s, ok := flagStrings[f]; ok { 40 return s 41 } 42 return fmt.Sprintf("Unknown flag (%d)", int(f)) 43 } 44 45 type Bar struct { 46 flag Flag 47 data uintptr 48 } 49 50 type Foo struct { 51 unexportedField Bar 52 ExportedField map[interface{}]interface{} 53 } 54 55 // This example demonstrates how to use Dump to dump variables to stdout. 56 func ExampleDump() { 57 // The following package level declarations are assumed for this example: 58 /* 59 type Flag int 60 61 const ( 62 flagOne Flag = iota 63 flagTwo 64 ) 65 66 var flagStrings = map[Flag]string{ 67 flagOne: "flagOne", 68 flagTwo: "flagTwo", 69 } 70 71 func (f Flag) String() string { 72 if s, ok := flagStrings[f]; ok { 73 return s 74 } 75 return fmt.Sprintf("Unknown flag (%d)", int(f)) 76 } 77 78 type Bar struct { 79 flag Flag 80 data uintptr 81 } 82 83 type Foo struct { 84 unexportedField Bar 85 ExportedField map[interface{}]interface{} 86 } 87 */ 88 89 // Setup some sample data structures for the example. 90 bar := Bar{Flag(flagTwo), uintptr(0)} 91 s1 := Foo{bar, map[interface{}]interface{}{"one": true}} 92 f := Flag(5) 93 b := []byte{ 94 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 95 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 96 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 97 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 98 0x31, 0x32, 99 } 100 101 // Dump! 102 utter.Dump([]interface{}{s1, f, b}) 103 104 // Output: 105 // 106 // []interface{}{ 107 // utter_test.Foo{ 108 // unexportedField: utter_test.Bar{ 109 // flag: utter_test.Flag(1), 110 // data: uintptr(0), 111 // }, 112 // ExportedField: map[interface{}]interface{}{ 113 // string("one"): bool(true), 114 // }, 115 // }, 116 // utter_test.Flag(5), 117 // []uint8{ 118 // 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, // |............... | 119 // 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, // |!"#$%&'()*+,-./0| 120 // 0x31, 0x32, /* */ // |12| 121 // }, 122 // } 123 } 124 125 // This example demonstrates how to use a ConfigState. 126 func ExampleConfigState() { 127 // Modify the indent level of the ConfigState only. The global 128 // configuration is not modified. 129 scs := utter.ConfigState{Indent: "\t"} 130 131 // Output using the ConfigState instance. 132 v := map[string]int{"one": 1} 133 scs.Dump(v) 134 135 // Output: 136 // 137 // map[string]int{ 138 // string("one"): int(1), 139 // } 140 } 141 142 // This example demonstrates how to use a Quoting strategy. 143 func ExampleConfigState_Quoting() { 144 scs := utter.ConfigState{ 145 Indent: "\t", ElideType: true, SortKeys: true, 146 147 // Avoid escape sequences when present and force 148 // use of backquotes even when the complete string is 149 // not backquotable. 150 Quoting: utter.AvoidEscapes | utter.Force, 151 } 152 153 v := map[string]string{ 154 "1. one": "this\ntext\nspans\nlines\n", 155 "2. two": "this text doesn't", 156 "3.\nt\nh\nr\ne\ne\n": "vertical key", 157 "4. four": "contains \\backslashes\\ and `backquotes`", 158 } 159 scs.Dump(v) 160 161 // Output: 162 // 163 // map[string]string{ 164 // "1. one": `this 165 // text 166 // spans 167 // lines 168 // `, 169 // "2. two": "this text doesn't", 170 // `3. 171 // t 172 // h 173 // r 174 // e 175 // e 176 // `: "vertical key", 177 // "4. four": `contains \backslashes\ and `+"`"+`backquotes`+"`", 178 // } 179 } 180 181 // This example demonstrates how to use ConfigState.Dump to dump variables to 182 // stdout 183 func ExampleConfigState_Dump() { 184 // See the top-level Dump example for details on the types used in this 185 // example. 186 187 // Create two ConfigState instances with different indentation. 188 scs := utter.ConfigState{Indent: "\t"} 189 scs2 := utter.ConfigState{Indent: " "} 190 191 // Setup some sample data structures for the example. 192 bar := Bar{Flag(flagTwo), uintptr(0)} 193 s1 := Foo{bar, map[interface{}]interface{}{"one": true}} 194 195 // Dump using the ConfigState instances. 196 scs.Dump(s1) 197 scs2.Dump(s1) 198 199 // Output: 200 // 201 // utter_test.Foo{ 202 // unexportedField: utter_test.Bar{ 203 // flag: utter_test.Flag(1), 204 // data: uintptr(0), 205 // }, 206 // ExportedField: map[interface{}]interface{}{ 207 // string("one"): bool(true), 208 // }, 209 // } 210 // utter_test.Foo{ 211 // unexportedField: utter_test.Bar{ 212 // flag: utter_test.Flag(1), 213 // data: uintptr(0), 214 // }, 215 // ExportedField: map[interface{}]interface{}{ 216 // string("one"): bool(true), 217 // }, 218 // } 219 }