| 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 |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"fmt" |
| 12 |
"io" |
| 13 |
"reflect" |
| 14 |
"strconv" |
| 15 |
"strings" |
| 16 |
|
| 17 |
"github.com/maxatome/go-testdeep/helpers/tdutil" |
| 18 |
"github.com/maxatome/go-testdeep/internal/dark" |
| 19 |
"github.com/maxatome/go-testdeep/internal/types" |
| 20 |
) |
| 21 |
|
| 22 |
// ToString does its best to stringify val. inReflectValue is used |
| 23 |
// internally to avoid treating specifically reflect.Value type. |
| 24 |
func ToString(val any, inReflectValue ...bool) string { |
| 25 |
if val == nil { |
| 26 |
return "nil" |
| 27 |
} |
| 28 |
|
| 29 |
switch tval := val.(type) { |
| 30 |
case reflect.Value: |
| 31 |
if len(inReflectValue) > 0 && inReflectValue[0] { |
| 32 |
break |
| 33 |
} |
| 34 |
newVal, ok := dark.GetInterface(tval, true) |
| 35 |
if ok { |
| 36 |
return ToString(newVal, true) |
| 37 |
} |
| 38 |
|
| 39 |
case []reflect.Value: |
| 40 |
if len(inReflectValue) > 0 && inReflectValue[0] { |
| 41 |
break |
| 42 |
} |
| 43 |
var buf strings.Builder |
| 44 |
SliceToString(&buf, tval) |
| 45 |
return buf.String() |
| 46 |
|
| 47 |
// no "(string) " prefix for printable strings |
| 48 |
case string: |
| 49 |
return tdutil.FormatString(tval) |
| 50 |
|
| 51 |
// no "(int) " prefix for ints |
| 52 |
case int: |
| 53 |
return strconv.Itoa(tval) |
| 54 |
|
| 55 |
// no "(float64) " prefix for float64s |
| 56 |
case float64: |
| 57 |
s := strconv.FormatFloat(tval, 'g', -1, 64) |
| 58 |
if strings.ContainsAny(s, "e.IN") { // I for Inf, N for NaN |
| 59 |
return s |
| 60 |
} |
| 61 |
return s + ".0" // to distinguish from ints |
| 62 |
|
| 63 |
// no "(bool) " prefix for booleans |
| 64 |
case bool: |
| 65 |
if tval { |
| 66 |
return "true" |
| 67 |
} |
| 68 |
return "false" |
| 69 |
|
| 70 |
case types.TestDeepStringer: |
| 71 |
return tval.String() |
| 72 |
} |
| 73 |
|
| 74 |
return tdutil.SpewString(val) |
| 75 |
} |
| 76 |
|
| 77 |
// IndentString indents str lines (from 2nd one = 1st line is not |
| 78 |
// indented) by indent. |
| 79 |
func IndentString(str, indent string) string { |
| 80 |
return strings.ReplaceAll(str, "\n", "\n"+indent) |
| 81 |
} |
| 82 |
|
| 83 |
// IndentStringIn indents str lines (from 2nd one = 1st line is not |
| 84 |
// indented) by indent and write it to w. |
| 85 |
func IndentStringIn(w io.Writer, str, indent string) { |
| 86 |
repl := strings.NewReplacer("\n", "\n"+indent) |
| 87 |
repl.WriteString(w, str) //nolint: errcheck |
| 88 |
} |
| 89 |
|
| 90 |
// IndentColorizeStringIn indents str lines (from 2nd one = 1st line |
| 91 |
// is not indented) by indent and write it to w. Before each end of |
| 92 |
// line, colOff is inserted, and after each indent on new line, colOn |
| 93 |
// is inserted. |
| 94 |
func IndentColorizeStringIn(w io.Writer, str, indent, colOn, colOff string) { |
| 95 |
if str != "" { |
| 96 |
if colOn == "" && colOff == "" { |
| 97 |
IndentStringIn(w, str, indent) |
| 98 |
return |
| 99 |
} |
| 100 |
repl := strings.NewReplacer("\n", colOff+"\n"+indent+colOn) |
| 101 |
io.WriteString(w, colOn) //nolint: errcheck |
| 102 |
repl.WriteString(w, str) //nolint: errcheck |
| 103 |
io.WriteString(w, colOff) //nolint: errcheck |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
// SliceToString stringifies items slice into buf then returns buf. |
| 108 |
func SliceToString(buf *strings.Builder, items []reflect.Value) *strings.Builder { |
| 109 |
buf.WriteByte('(') |
| 110 |
|
| 111 |
begLine := strings.LastIndexByte(buf.String(), '\n') + 1 |
| 112 |
prefix := strings.Repeat(" ", buf.Len()-begLine) |
| 113 |
|
| 114 |
if len(items) < 2 { |
| 115 |
if len(items) > 0 { |
| 116 |
buf.WriteString(IndentString(ToString(items[0]), prefix)) |
| 117 |
} |
| 118 |
} else { |
| 119 |
buf.WriteString(IndentString(ToString(items[0]), prefix)) |
| 120 |
for _, item := range items[1:] { |
| 121 |
buf.WriteString(",\n") |
| 122 |
buf.WriteString(prefix) |
| 123 |
buf.WriteString(IndentString(ToString(item), prefix)) |
| 124 |
} |
| 125 |
} |
| 126 |
buf.WriteByte(')') |
| 127 |
|
| 128 |
return buf |
| 129 |
} |
| 130 |
|
| 131 |
// TypeFullName returns the t type name with packages fully visible |
| 132 |
// instead of the last package part in t.String(). |
| 133 |
func TypeFullName(t reflect.Type) string { |
| 134 |
var b bytes.Buffer |
| 135 |
typeFullName(&b, t) |
| 136 |
return b.String() |
| 137 |
} |
| 138 |
|
| 139 |
func typeFullName(b *bytes.Buffer, t reflect.Type) { |
| 140 |
if t.Name() != "" { |
| 141 |
if pkg := t.PkgPath(); pkg != "" { |
| 142 |
fmt.Fprintf(b, "%s.", pkg) |
| 143 |
} |
| 144 |
b.WriteString(t.Name()) |
| 145 |
return |
| 146 |
} |
| 147 |
|
| 148 |
switch t.Kind() { |
| 149 |
case reflect.Ptr: |
| 150 |
b.WriteByte('*') |
| 151 |
typeFullName(b, t.Elem()) |
| 152 |
|
| 153 |
case reflect.Slice: |
| 154 |
b.WriteString("[]") |
| 155 |
typeFullName(b, t.Elem()) |
| 156 |
|
| 157 |
case reflect.Array: |
| 158 |
fmt.Fprintf(b, "[%d]", t.Len()) |
| 159 |
typeFullName(b, t.Elem()) |
| 160 |
|
| 161 |
case reflect.Map: |
| 162 |
b.WriteString("map[") |
| 163 |
typeFullName(b, t.Key()) |
| 164 |
b.WriteByte(']') |
| 165 |
typeFullName(b, t.Elem()) |
| 166 |
|
| 167 |
case reflect.Struct: |
| 168 |
b.WriteString("struct {") |
| 169 |
if num := t.NumField(); num > 0 { |
| 170 |
for i := 0; i < num; i++ { |
| 171 |
sf := t.Field(i) |
| 172 |
if !sf.Anonymous { |
| 173 |
b.WriteByte(' ') |
| 174 |
b.WriteString(sf.Name) |
| 175 |
} |
| 176 |
b.WriteByte(' ') |
| 177 |
typeFullName(b, sf.Type) |
| 178 |
b.WriteByte(';') |
| 179 |
} |
| 180 |
b.Truncate(b.Len() - 1) |
| 181 |
b.WriteByte(' ') |
| 182 |
} |
| 183 |
b.WriteByte('}') |
| 184 |
|
| 185 |
case reflect.Func: |
| 186 |
b.WriteString("func(") |
| 187 |
if num := t.NumIn(); num > 0 { |
| 188 |
for i := 0; i < num; i++ { |
| 189 |
if i == num-1 && t.IsVariadic() { |
| 190 |
b.WriteString("...") |
| 191 |
typeFullName(b, t.In(i).Elem()) |
| 192 |
} else { |
| 193 |
typeFullName(b, t.In(i)) |
| 194 |
} |
| 195 |
b.WriteString(", ") |
| 196 |
} |
| 197 |
b.Truncate(b.Len() - 2) |
| 198 |
} |
| 199 |
b.WriteByte(')') |
| 200 |
|
| 201 |
if num := t.NumOut(); num > 0 { |
| 202 |
if num == 1 { |
| 203 |
b.WriteByte(' ') |
| 204 |
} else { |
| 205 |
b.WriteString(" (") |
| 206 |
} |
| 207 |
for i := 0; i < num; i++ { |
| 208 |
typeFullName(b, t.Out(i)) |
| 209 |
b.WriteString(", ") |
| 210 |
} |
| 211 |
b.Truncate(b.Len() - 2) |
| 212 |
if num > 1 { |
| 213 |
b.WriteByte(')') |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
case reflect.Chan: |
| 218 |
switch t.ChanDir() { |
| 219 |
case reflect.RecvDir: |
| 220 |
b.WriteString("<-chan ") |
| 221 |
case reflect.SendDir: |
| 222 |
b.WriteString("chan<- ") |
| 223 |
case reflect.BothDir: |
| 224 |
b.WriteString("chan ") |
| 225 |
} |
| 226 |
typeFullName(b, t.Elem()) |
| 227 |
|
| 228 |
default: |
| 229 |
// Fallback to default implementation |
| 230 |
b.WriteString(t.String()) |
| 231 |
} |
| 232 |
} |