1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package pretty
16
17 import (
18 "fmt"
19 "strings"
20 "testing"
21 )
22
23 type S struct {
24 X int
25 Y bool
26 z *string
27 }
28
29 func TestSprint(t *testing.T) {
30 Indent = "~"
31 i := 17
32
33 for _, test := range []struct {
34 value interface{}
35 want string
36 }{
37
38 {nil, "nil"},
39 {3, "3"},
40 {9.8, "9.8"},
41 {true, "true"},
42 {"foo", `"foo"`},
43 {&i, "&17"},
44
45 {[3]int{1, 2, 3}, "[3]int{\n~1,\n~2,\n~3,\n}"},
46 {[]int{1, 2, 3}, "[]int{\n~1,\n~2,\n~3,\n}"},
47 {[]int{}, "[]int{}"},
48 {[]string{"foo"}, "[]string{\n~\"foo\",\n}"},
49
50 {map[int]bool{}, "map[int]bool{}"},
51 {map[int]bool{1: true, 2: false, 3: true},
52 "map[int]bool{\n~1: true,\n~3: true,\n}"},
53
54 {S{}, "pretty.S{\n}"},
55 {S{3, true, ptr("foo")},
56 "pretty.S{\n~X: 3,\n~Y: true,\n~z: &\"foo\",\n}"},
57
58 {[]interface{}{&i}, "[]interface {}{\n~&17,\n}"},
59
60 {[]S{{1, false, ptr("a")}, {2, true, ptr("b")}},
61 `[]pretty.S{
62 ~pretty.S{
63 ~~X: 1,
64 ~~z: &"a",
65 ~},
66 ~pretty.S{
67 ~~X: 2,
68 ~~Y: true,
69 ~~z: &"b",
70 ~},
71 }`},
72 } {
73 got := fmt.Sprintf("%v", Value(test.value))
74 if got != test.want {
75 t.Errorf("%v: got:\n%q\nwant:\n%q", test.value, got, test.want)
76 }
77 }
78 }
79
80 func TestWithDefaults(t *testing.T) {
81 Indent = "~"
82 for _, test := range []struct {
83 value interface{}
84 want string
85 }{
86 {map[int]bool{1: true, 2: false, 3: true},
87 "map[int]bool{\n~1: true,\n~2: false,\n~3: true,\n}"},
88 {S{}, "pretty.S{\n~X: 0,\n~Y: false,\n~z: nil,\n}"},
89 } {
90 got := fmt.Sprintf("%+v", Value(test.value))
91 if got != test.want {
92 t.Errorf("%v: got:\n%q\nwant:\n%q", test.value, got, test.want)
93 }
94 }
95 }
96
97 func TestBadVerb(t *testing.T) {
98 got := fmt.Sprintf("%d", Value(8))
99 want := "%!d("
100 if !strings.HasPrefix(got, want) {
101 t.Errorf("got %q, want prefix %q", got, want)
102 }
103 }
104
105 func ptr(s string) *string { return &s }
106
View as plain text