Debian Codegraph @

internal/spew/example_test.go

raw
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 package spew_test
18
19 import (
20 "fmt"
21
22 "github.com/maxatome/go-testdeep/internal/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[any]any
51 }
52
53 // This example demonstrates how to use Sdump to dump variables to stdout.
54 func ExampleSdump() {
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[any]any
83 }
84 */
85
86 // Setup some sample data structures for the example.
87 bar := Bar{uintptr(0x1234)}
88 s1 := Foo{bar, map[any]any{"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 fmt.Println(spew.Sdump(s1))
100 fmt.Println(spew.Sdump(f))
101 fmt.Println(spew.Sdump(b))
102
103 // Output:
104 // (spew_test.Foo) {
105 // unexportedField: (spew_test.Bar) {
106 // data: (uintptr) 0x1234
107 // },
108 // ExportedField: (map[interface {}]interface {}) (len=1) {
109 // (string) (len=3) "one": (bool) true
110 // }
111 // }
112 // (spew_test.Flag) Unknown flag (5)
113 // ([]uint8) (len=34) {
114 // 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... |
115 // 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0|
116 // 00000020 31 32 |12|
117 // }
118 //
119 }