-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffx_test.go
More file actions
87 lines (71 loc) · 2.35 KB
/
diffx_test.go
File metadata and controls
87 lines (71 loc) · 2.35 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
package diffx
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDiff_NoDifference(t *testing.T) {
result := Diff("hello\n", "hello\n")
assert.Empty(t, result)
}
func TestDiff_BasicChange(t *testing.T) {
result := Diff("line1\nline2\n", "line1\nchanged\n")
assert.Contains(t, result, "-2 | line2")
assert.Contains(t, result, "+2 | changed")
// No ANSI codes by default.
assert.NotContains(t, result, "\033[")
}
func TestDiff_WithColorForced(t *testing.T) {
result := Diff("a\n", "b\n", WithColor(true))
assert.Contains(t, result, "\033[")
}
func TestWriteDiff_Basic(t *testing.T) {
var buf bytes.Buffer
err := WriteDiff(&buf, "old\n", "new\n")
require.NoError(t, err)
assert.Contains(t, buf.String(), "old")
assert.Contains(t, buf.String(), "new")
}
func TestJSONDiff_SortedKeys(t *testing.T) {
before := `{"b": 2, "a": 1}`
after := `{"a": 1, "b": 3}`
result, err := JSONDiff(before, after)
require.NoError(t, err)
// Keys should be sorted, so only the value change on "b" should show.
assert.Contains(t, result, `"b": 2`)
assert.Contains(t, result, `"b": 3`)
// "a" is unchanged and only appears as context, not as a diff line.
assert.NotContains(t, result, `"a": 1,`+"\n+")
}
func TestJSONDiff_NoDifference(t *testing.T) {
before := `{"b": 2, "a": 1}`
after := `{"a": 1, "b": 2}`
result, err := JSONDiff(before, after)
require.NoError(t, err)
assert.Empty(t, result, "same JSON with different key order should produce no diff")
}
func TestJSONDiff_InvalidJSON(t *testing.T) {
_, err := JSONDiff("not json", `{"a": 1}`)
assert.Error(t, err)
assert.Contains(t, err.Error(), "before")
_, err = JSONDiff(`{"a": 1}`, "not json")
assert.Error(t, err)
assert.Contains(t, err.Error(), "after")
}
func TestJSONDiff_PreservesNumbers(t *testing.T) {
before := `{"value": 12345678901234567890}`
after := `{"value": 12345678901234567891}`
result, err := JSONDiff(before, after)
require.NoError(t, err)
// Numbers should not be mangled by float64 conversion.
assert.Contains(t, result, "12345678901234567890")
assert.Contains(t, result, "12345678901234567891")
}
func TestWriteJSONDiff_Basic(t *testing.T) {
var buf bytes.Buffer
err := WriteJSONDiff(&buf, `{"a":1}`, `{"a":2}`)
require.NoError(t, err)
assert.Contains(t, buf.String(), `"a": 1`)
assert.Contains(t, buf.String(), `"a": 2`)
}