-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
diff.go
54 lines (49 loc) · 1.16 KB
/
diff.go
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
package autogold
import (
"fmt"
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"
"github.com/hexops/valast"
)
func diff(got, want string, opts []Option) string {
edits := myers.ComputeEdits(span.URIFromPath("out"), string(want), got)
return fmt.Sprint(gotextdiff.ToUnified("want", "got", string(want), edits))
}
// Raw denotes a raw string.
type Raw string
func stringify(v interface{}, opts []Option) string {
var (
allowRaw, trailingNewline bool
valastOpt = &valast.Options{}
)
for _, opt := range opts {
opt := opt.(*option)
if opt.exportedOnly {
valastOpt.ExportedOnly = true
}
if opt.forPackageName != "" {
valastOpt.PackageName = opt.forPackageName
}
if opt.forPackagePath != "" {
valastOpt.PackagePath = opt.forPackagePath
}
if isBazel() {
valastOpt.PackagePathToName = bazelPackagePathToName
}
if opt.allowRaw {
allowRaw = true
}
if opt.trailingNewline {
trailingNewline = true
}
}
if v, ok := v.(Raw); ok && allowRaw {
return string(v)
}
s := valast.StringWithOptions(v, valastOpt)
if trailingNewline {
return s + "\n"
}
return s
}