-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.go
More file actions
260 lines (207 loc) · 5.84 KB
/
diff.go
File metadata and controls
260 lines (207 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package typotestcolor
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
)
func DiffPrintColor(color ANSIForeground, highlight bool) []byte {
var (
print []byte
ansiConfig ANSIConfig
ansiForeground ANSIForeground
ansiBackground ANSIBackground
)
switch color {
case ANSIForegroundGreen:
ansiForeground = ANSIForegroundGreen
ansiBackground = ANSIBackgroundGreen
case ANSIForegroundRed:
ansiForeground = ANSIForegroundRed
ansiBackground = ANSIBackgroundRed
default:
ansiForeground = ANSIForegroundBlue
ansiBackground = ANSIBackgroundBlue
}
if highlight {
ansiConfig = ANSIConfig{
Style: ColorANSIStyle[ANSIStyleNormal],
Foreground: ColorANSIForeground[ANSIForegroundBlack],
Background: ColorANSIBackground[ansiBackground],
}
} else {
ansiConfig = ANSIConfig{
Style: ColorANSIStyle[ANSIStyleNormal],
Foreground: ColorANSIForeground[ansiForeground],
Background: ColorANSIBackground[ANSIBackgroundNone],
}
// INFO: need to reset for ANSIBackgroundNone to works
print = append(print, ColorReset...)
}
print = append(print, ColorANSI(ansiConfig)...)
return print
}
func ToBytes(value any) ([]byte, error) {
reflectValue := reflect.ValueOf(value)
if reflectValue.Kind() == reflect.Func && reflectValue.Type().NumIn() == 0 {
results := reflectValue.Call(nil)
if len(results) > 0 {
return ToBytes(results[0].Interface())
}
}
switch valueTyped := value.(type) {
case []byte:
return valueTyped, nil
case string:
return []byte(valueTyped), nil
case int, int8, int16, int32, int64:
return fmt.Appendf(nil, "%d", valueTyped), nil
case uint, uint8, uint16, uint32, uint64:
return fmt.Appendf(nil, "%d", valueTyped), nil
case float32, float64:
return fmt.Appendf(nil, "%g", valueTyped), nil
case bool:
return []byte(strconv.FormatBool(valueTyped)), nil
case nil:
return []byte("nil"), nil
case fmt.Stringer:
return []byte(valueTyped.String()), nil
case func() []byte:
return valueTyped(), nil
case func() string:
return []byte(valueTyped()), nil
// INFO: cannot aggregate func() type
case func() int:
return fmt.Appendf(nil, "%d", valueTyped()), nil
case func() int8:
return fmt.Appendf(nil, "%d", valueTyped()), nil
case func() int16:
return fmt.Appendf(nil, "%d", valueTyped()), nil
case func() int32:
return fmt.Appendf(nil, "%d", valueTyped()), nil
case func() int64:
return fmt.Appendf(nil, "%d", valueTyped()), nil
case func() uint:
return fmt.Appendf(nil, "%d", valueTyped()), nil
case func() uint8:
return fmt.Appendf(nil, "%d", valueTyped()), nil
case func() uint16:
return fmt.Appendf(nil, "%d", valueTyped()), nil
case func() uint32:
return fmt.Appendf(nil, "%d", valueTyped()), nil
case func() uint64:
return fmt.Appendf(nil, "%d", valueTyped()), nil
case func() float32:
return fmt.Appendf(nil, "%g", valueTyped()), nil
case func() float64:
return fmt.Appendf(nil, "%g", valueTyped()), nil
case func() bool:
return []byte(strconv.FormatBool(valueTyped())), nil
case func():
return []byte("nil"), nil
case func() fmt.Stringer:
return []byte(valueTyped().String()), nil
default:
// fallback JSON (struct, map, slice, etc.)
json, err := json.Marshal(valueTyped)
if err != nil {
return nil, fmt.Errorf("cannot convert %T to []byte: %w", valueTyped, err)
}
return json, nil
}
}
type TestDiffOpts = struct {
opts Opts
printToASCII bool
}
func TestDiffNewDefaultOpts() TestDiffOpts {
return TestDiffOpts{
opts: NewDefaultOpts(),
printToASCII: false,
}
}
// TODO: mettre printToASCII dans la configuration globale pour simplifier le tout.
// TODO: créer une fonction qui me permet de passer mon propre opts
func TestDiffDefault(expected any, got any) error {
return TestDiff(expected, got, TestDiffNewDefaultOpts())
}
// TODO: colorier la valeur length et mettre en highlight la différence
func TestDiff(expected any, got any, opts TestDiffOpts) error {
expectedBytes, err := ToBytes(expected)
if err != nil {
return err
}
gotBytes, err := ToBytes(got)
if err != nil {
return err
}
// everything is fine
if bytes.Equal(expectedBytes, gotBytes) {
return nil
}
var print strings.Builder
expectedLen := len(expectedBytes)
gotLen := len(gotBytes)
expectedPrefix := fmt.Sprintf("expected (length: %d): ", expectedLen)
gotPrefix := fmt.Sprintf("got (length: %d): ", gotLen)
expectedPrefixLen := len(expectedPrefix)
gotPrefixLen := len(gotPrefix)
if expectedPrefixLen > gotPrefixLen {
for i := 0; i < expectedPrefixLen-gotPrefixLen; i++ {
gotPrefix += " "
}
} else if gotPrefixLen > expectedPrefixLen {
for i := 0; i < gotPrefixLen-expectedPrefixLen; i++ {
expectedPrefix += " "
}
}
// start header print
print.WriteByte('\n')
// expected part
print.WriteString(expectedPrefix)
print.Write(DiffPrintColor(ANSIForegroundGreen, false))
print.Write(expectedBytes)
print.WriteByte('\n')
// got part
print.WriteString(gotPrefix)
print.Write(DiffPrintColor(ANSIForegroundRed, false))
errorBefore := false
// build got print
for i := 0; i < min(expectedLen, gotLen); i++ {
// same
if expectedBytes[i] == gotBytes[i] {
// remove error highlight
if errorBefore {
print.Write(DiffPrintColor(ANSIForegroundRed, false))
errorBefore = false
}
// diff
} else {
// add error highlight
if !errorBefore {
print.Write(DiffPrintColor(ANSIForegroundRed, true))
errorBefore = true
}
}
print.WriteByte(gotBytes[i])
}
// add got trailing bytes
if expectedLen < gotLen {
if !errorBefore {
print.Write(DiffPrintColor(ANSIForegroundRed, true))
errorBefore = true
}
print.Write(gotBytes[expectedLen:])
}
// reset ANSI styles
print.Write(ColorReset)
print.WriteByte('\n')
if opts.printToASCII {
return errors.New(strconv.QuoteToASCII(print.String()))
} else {
return errors.New(print.String())
}
}