-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_diff.go
More file actions
34 lines (27 loc) · 863 Bytes
/
Copy pathjson_diff.go
File metadata and controls
34 lines (27 loc) · 863 Bytes
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
package testastic
import (
"encoding/json"
"fmt"
"strings"
)
// formatJSONDiffInline generates a git-style inline diff between expected and actual JSON.
// Shows the full JSON with - prefix for removed lines and + prefix for added lines.
func formatJSONDiffInline(diagnostic treeDiagnostic) string {
expJSON, err := json.MarshalIndent(diagnostic.expected, "", " ")
if err != nil {
return fmt.Sprintf("error formatting expected: %v", err)
}
actJSON, err := json.MarshalIndent(diagnostic.actual, "", " ")
if err != nil {
return fmt.Sprintf("error formatting actual: %v", err)
}
expLines := strings.Split(string(expJSON), "\n")
actLines := strings.Split(string(actJSON), "\n")
diff := computeDiff(expLines, actLines)
var sb strings.Builder
for _, line := range diff {
sb.WriteString(line)
sb.WriteString("\n")
}
return sb.String()
}