| 1 |
package check |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"reflect" |
| 8 |
"strconv" |
| 9 |
"strings" |
| 10 |
"unicode/utf8" |
| 11 |
|
| 12 |
"github.com/powerman/check/internal/difflib" |
| 13 |
"github.com/powerman/check/internal/spew" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
var spewCfg = spew.ConfigState{ |
| 18 |
Indent: " ", |
| 19 |
DisablePointerAddresses: true, |
| 20 |
DisableCapacities: true, |
| 21 |
SortKeys: true, |
| 22 |
SpewKeys: true, |
| 23 |
} |
| 24 |
|
| 25 |
type dump struct { |
| 26 |
dump string |
| 27 |
indirectType reflect.Type |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
func (v dump) String() string { |
| 32 |
return v.dump |
| 33 |
} |
| 34 |
|
| 35 |
func (v dump) diff(expected dump) string { |
| 36 |
if v.indirectType != expected.indirectType { |
| 37 |
return "" |
| 38 |
} |
| 39 |
if !strings.ContainsRune(v.dump[:len(v.dump)-1], '\n') && |
| 40 |
!strings.ContainsRune(expected.dump[:len(expected.dump)-1], '\n') { |
| 41 |
return "" |
| 42 |
} |
| 43 |
|
| 44 |
diff, err := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ |
| 45 |
A: difflib.SplitLines(expected.dump), |
| 46 |
B: difflib.SplitLines(v.dump), |
| 47 |
FromFile: "Expected", |
| 48 |
FromDate: "", |
| 49 |
ToFile: "Actual", |
| 50 |
ToDate: "", |
| 51 |
Context: 1, |
| 52 |
}) |
| 53 |
if err != nil { |
| 54 |
return "" |
| 55 |
} |
| 56 |
return "Diff:\n" + diff |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
func newDump(i any) (d dump) { |
| 72 |
d.dump = spewCfg.Sdump(i) |
| 73 |
|
| 74 |
if i == nil { |
| 75 |
d.dump = "<nil>\n" |
| 76 |
return d |
| 77 |
} |
| 78 |
|
| 79 |
val := reflect.ValueOf(i) |
| 80 |
typ := reflect.TypeOf(i) |
| 81 |
kind := typ.Kind() |
| 82 |
if kind == reflect.Pointer { |
| 83 |
if val.IsNil() { |
| 84 |
return d |
| 85 |
} |
| 86 |
val = val.Elem() |
| 87 |
typ = typ.Elem() |
| 88 |
kind = typ.Kind() |
| 89 |
} |
| 90 |
d.indirectType = typ |
| 91 |
|
| 92 |
switch { |
| 93 |
case typ == reflect.TypeFor[json.RawMessage](): |
| 94 |
v := val.Bytes() |
| 95 |
var buf bytes.Buffer |
| 96 |
if json.Indent(&buf, v, "", " ") == nil { |
| 97 |
d.dump = fmt.Sprintf("(%T) (len=%d) '\n%s\n'\n", i, len(v), buf.String()) |
| 98 |
} |
| 99 |
|
| 100 |
case kind == reflect.Uint8: |
| 101 |
v := byte(val.Uint()) |
| 102 |
d.dump = fmt.Sprintf("(%T) 0x%02X\n", i, v) |
| 103 |
|
| 104 |
case kind == reflect.Int32: |
| 105 |
v := rune(val.Int()) |
| 106 |
if utf8.ValidRune(v) { |
| 107 |
d.dump = fmt.Sprintf("(%T) %q\n", i, v) |
| 108 |
} |
| 109 |
|
| 110 |
case kind == reflect.Slice && typ.Elem().Kind() == reflect.Int32: |
| 111 |
valid := true |
| 112 |
for k := 0; k < val.Len() && valid; k++ { |
| 113 |
valid = valid && utf8.ValidRune(rune(val.Index(k).Int())) |
| 114 |
} |
| 115 |
if valid { |
| 116 |
d.dump = fmt.Sprintf("(%T) %q\n", i, i) |
| 117 |
} |
| 118 |
|
| 119 |
case kind == reflect.String: |
| 120 |
v := val.String() |
| 121 |
if utf8.ValidString(v) { |
| 122 |
d.dump = fmt.Sprintf("(%T) (len=%d) %s\n", i, len(v), quote(v)) |
| 123 |
} else { |
| 124 |
d.dump = strings.Replace(spewCfg.Sdump([]byte(v)), "([]uint8)", fmt.Sprintf("(%T)", i), 1) |
| 125 |
} |
| 126 |
|
| 127 |
case kind == reflect.Slice && typ.Elem().Kind() == reflect.Uint8: |
| 128 |
v := val.Bytes() |
| 129 |
if len(v) > 0 && utf8.Valid(v) || len(v) == 0 && !val.IsNil() { |
| 130 |
d.dump = fmt.Sprintf("(%T) (len=%d) %s\n", i, len(v), quote(string(v))) |
| 131 |
} |
| 132 |
} |
| 133 |
return d |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
func quote(s string) string { |
| 138 |
r := []rune(strconv.Quote(s)) |
| 139 |
q := r[:0] |
| 140 |
var multiline, esc bool |
| 141 |
for _, c := range r[1 : len(r)-1] { |
| 142 |
if esc { |
| 143 |
esc = false |
| 144 |
switch c { |
| 145 |
case 'n': |
| 146 |
c = '\n' |
| 147 |
multiline = true |
| 148 |
case '"': |
| 149 |
default: |
| 150 |
q = append(q, '\\') |
| 151 |
} |
| 152 |
} else if c == '\\' { |
| 153 |
esc = true |
| 154 |
continue |
| 155 |
} |
| 156 |
q = append(q, c) |
| 157 |
} |
| 158 |
if multiline { |
| 159 |
return fmt.Sprintf("'\n%s\n'", string(q)) |
| 160 |
} |
| 161 |
return fmt.Sprintf("'%s'", string(q)) |
| 162 |
} |