Debian Codegraph @

internal/util/string_test.go

raw
1 // Copyright (c) 2018-2022, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package util_test
8
9 import (
10 "bytes"
11 "math"
12 "reflect"
13 "runtime"
14 "strings"
15 "testing"
16
17 "github.com/maxatome/go-testdeep/internal/test"
18 "github.com/maxatome/go-testdeep/internal/types"
19 "github.com/maxatome/go-testdeep/internal/util"
20 )
21
22 type myTestDeepStringer struct {
23 types.TestDeepStamp
24 }
25
26 func (m myTestDeepStringer) String() string {
27 return "TesT!"
28 }
29
30 func TestToString(t *testing.T) {
31 for _, curTest := range []struct {
32 paramGot any
33 expected string
34 }{
35 {paramGot: nil, expected: "nil"},
36 {paramGot: "foobar", expected: `"foobar"`},
37 {paramGot: "foo\rbar", expected: `(string) (len=7) "foo\rbar"`},
38 {paramGot: "foo\u2028bar", expected: `(string) (len=9) "foo\u2028bar"`},
39 {paramGot: `foo"bar`, expected: "`foo\"bar`"},
40 {paramGot: "foo\n\"bar", expected: "`foo\n\"bar`"},
41 {paramGot: "foo`\"\nbar", expected: "(string) (len=9) \"foo`\\\"\\nbar\""},
42 {paramGot: "foo`\n\"bar", expected: "(string) (len=9) \"foo`\\n\\\"bar\""},
43 {paramGot: "foo\n`\"bar", expected: "(string) (len=9) \"foo\\n`\\\"bar\""},
44 {paramGot: "foo\n\"`bar", expected: "(string) (len=9) \"foo\\n\\\"`bar\""},
45 {paramGot: reflect.ValueOf("foobar"), expected: `"foobar"`},
46 {
47 paramGot: reflect.ValueOf(reflect.ValueOf(42)),
48 expected: `(reflect.Value) <int Value>`,
49 },
50 {
51 paramGot: []reflect.Value{reflect.ValueOf("foo"), reflect.ValueOf("bar")},
52 expected: `("foo",
53 "bar")`,
54 },
55 {
56 paramGot: reflect.ValueOf([]reflect.Value{}),
57 expected: "([]reflect.Value) {\n}",
58 },
59 {paramGot: types.RawString("test"), expected: "test"},
60 {paramGot: types.RawInt(42), expected: "42"},
61 {paramGot: myTestDeepStringer{}, expected: "TesT!"},
62 {paramGot: 42, expected: "42"},
63 {paramGot: true, expected: "true"},
64 {paramGot: false, expected: "false"},
65 {paramGot: int64(42), expected: "(int64) 42"},
66 {paramGot: float64(42), expected: "42.0"},
67 {paramGot: float64(42.56), expected: "42.56"},
68 {paramGot: float64(4e56), expected: "4e+56"},
69 {paramGot: math.Inf(1), expected: "+Inf"},
70 {paramGot: math.Inf(-1), expected: "-Inf"},
71 {paramGot: math.NaN(), expected: "NaN"},
72 } {
73 test.EqualStr(t, util.ToString(curTest.paramGot), curTest.expected)
74 }
75 }
76
77 func TestIndentString(t *testing.T) {
78 for _, curTest := range []struct {
79 ParamGot string
80 Expected string
81 }{
82 {ParamGot: "", Expected: ""},
83 {ParamGot: "pipo", Expected: "pipo"},
84 {ParamGot: "pipo\nbingo\nzip", Expected: "pipo\n-bingo\n-zip"},
85 } {
86 test.EqualStr(t, util.IndentString(curTest.ParamGot, "-"), curTest.Expected)
87
88 var buf bytes.Buffer
89 util.IndentStringIn(&buf, curTest.ParamGot, "-")
90 test.EqualStr(t, buf.String(), curTest.Expected)
91
92 buf.Reset()
93 util.IndentColorizeStringIn(&buf, curTest.ParamGot, "-", "", "")
94 test.EqualStr(t, buf.String(), curTest.Expected)
95 }
96
97 for _, curTest := range []struct {
98 ParamGot string
99 Expected string
100 }{
101 {ParamGot: "", Expected: ""},
102 {ParamGot: "pipo", Expected: "<<pipo>>"},
103 {ParamGot: "pipo\nbingo\nzip", Expected: "<<pipo>>\n-<<bingo>>\n-<<zip>>"},
104 } {
105 var buf bytes.Buffer
106 util.IndentColorizeStringIn(&buf, curTest.ParamGot, "-", "<<", ">>")
107 test.EqualStr(t, buf.String(), curTest.Expected)
108 }
109 }
110
111 func TestSliceToBuffer(t *testing.T) {
112 for _, curTest := range []struct {
113 BufInit string
114 Items []any
115 Expected string
116 }{
117 {BufInit: ">", Items: nil, Expected: ">()"},
118 {BufInit: ">", Items: []any{"pipo"}, Expected: `>("pipo")`},
119 {
120 BufInit: ">",
121 Items: []any{"pipo", "bingo", "zip"},
122 Expected: `>("pipo",
123 "bingo",
124 "zip")`,
125 },
126 {
127 BufInit: "List\n of\nitems:\n>",
128 Items: []any{"pipo", "bingo", "zip"},
129 Expected: `List
130 of
131 items:
132 >("pipo",
133 "bingo",
134 "zip")`,
135 },
136 } {
137 var items []reflect.Value
138 if curTest.Items != nil {
139 items = make([]reflect.Value, len(curTest.Items))
140 for i, val := range curTest.Items {
141 items[i] = reflect.ValueOf(val)
142 }
143 }
144
145 var buf strings.Builder
146 buf.WriteString(curTest.BufInit)
147 test.EqualStr(t, util.SliceToString(&buf, items).String(),
148 curTest.Expected)
149 }
150 }
151
152 func TestTypeFullName(t *testing.T) {
153 // our full package name
154 pc, _, _, _ := runtime.Caller(0)
155 pkg := strings.TrimSuffix(runtime.FuncForPC(pc).Name(), ".TestTypeFullName")
156
157 test.EqualStr(t, util.TypeFullName(reflect.TypeOf(123)), "int")
158 test.EqualStr(t, util.TypeFullName(reflect.TypeOf([]int{})), "[]int")
159 test.EqualStr(t, util.TypeFullName(reflect.TypeOf([3]int{})), "[3]int")
160 test.EqualStr(t, util.TypeFullName(reflect.TypeOf((**float64)(nil))), "**float64")
161 test.EqualStr(t, util.TypeFullName(reflect.TypeOf(map[int]float64{})), "map[int]float64")
162 test.EqualStr(t, util.TypeFullName(reflect.TypeOf(struct{}{})), "struct {}")
163 test.EqualStr(t, util.TypeFullName(reflect.TypeOf(struct {
164 a int
165 b bool
166 }{})), "struct { a int; b bool }")
167 test.EqualStr(t, util.TypeFullName(reflect.TypeOf(struct {
168 s struct{ a []int }
169 b bool
170 }{})), "struct { s struct { a []int }; b bool }")
171
172 type anon struct{ a []int } //nolint: unused
173 test.EqualStr(t, util.TypeFullName(reflect.TypeOf(struct {
174 anon
175 b bool
176 }{})), "struct { "+pkg+".anon; b bool }")
177
178 test.EqualStr(t, util.TypeFullName(reflect.TypeOf(func() {})), "func()")
179 test.EqualStr(t,
180 util.TypeFullName(reflect.TypeOf(func(a int) {})),
181 "func(int)")
182 test.EqualStr(t,
183 util.TypeFullName(reflect.TypeOf(func(a int, b ...bool) rune { return 0 })),
184 "func(int, ...bool) int32")
185 test.EqualStr(t,
186 util.TypeFullName(reflect.TypeOf(func() (int, bool, int) { return 0, true, 0 })),
187 "func() (int, bool, int)")
188
189 test.EqualStr(t, util.TypeFullName(reflect.TypeOf(func() {})), "func()")
190 test.EqualStr(t,
191 util.TypeFullName(reflect.TypeOf(func(a int) {})),
192 "func(int)")
193 test.EqualStr(t,
194 util.TypeFullName(reflect.TypeOf(func(a int, b ...bool) rune { return 0 })),
195 "func(int, ...bool) int32")
196 test.EqualStr(t,
197 util.TypeFullName(reflect.TypeOf(func() (int, bool, int) { return 0, true, 0 })),
198 "func() (int, bool, int)")
199
200 test.EqualStr(t,
201 util.TypeFullName(reflect.TypeOf((<-chan []int)(nil))),
202 "<-chan []int")
203 test.EqualStr(t,
204 util.TypeFullName(reflect.TypeOf((chan<- []int)(nil))),
205 "chan<- []int")
206 test.EqualStr(t,
207 util.TypeFullName(reflect.TypeOf((chan []int)(nil))),
208 "chan []int")
209
210 test.EqualStr(t,
211 util.TypeFullName(reflect.TypeOf((*any)(nil))),
212 "*interface {}")
213 }