Repository navigation
Expand file tree
/
Copy pathcompare_test.go
More file actions
91 lines (82 loc) · 2.01 KB
/
Copy pathcompare_test.go
File metadata and controls
91 lines (82 loc) · 2.01 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
package jsondiff
import (
"bytes"
"encoding/json"
"errors"
"testing"
)
func Test_marshalUnmarshal_invalid_JSON(t *testing.T) {
t.Run("marshal", func(t *testing.T) {
e := errors.New("marshal")
_, _, err := marshalUnmarshal(nil, options{
// Override marshal func to simulate error.
marshal: func(any) ([]byte, error) { return nil, e },
unmarshal: json.Unmarshal,
})
if !errors.Is(err, e) {
t.Errorf("expected non-nil error")
}
})
t.Run("unmarshal", func(t *testing.T) {
e := errors.New("unmarshal")
_, _, err := marshalUnmarshal(nil, options{
// Override unmarshal func to simulate error.
marshal: json.Marshal,
unmarshal: func([]byte, any) error { return e },
})
if !errors.Is(err, e) {
t.Errorf("expected non-nil error")
}
})
}
func TestCompareWithoutMarshal_error(t *testing.T) {
type custom struct {
foo string
bar int
}
_, err := CompareWithoutMarshal(custom{"a", 1}, custom{"b", 2})
if err == nil {
t.Errorf("expected non-nil error")
}
t.Log(err)
}
type skip struct{}
var skipBytes = []byte("skip")
func Test_compare_marshaling_error(t *testing.T) {
e := errors.New("")
d := Differ{opts: options{
marshal: func(a any) ([]byte, error) {
if _, ok := a.(skip); ok {
return skipBytes, nil
}
return nil, e
},
unmarshal: func(b []byte, _ any) error {
if bytes.Equal(b, skipBytes) {
return nil
}
return e
},
}}
if _, err := compare(&d, nil, nil); !errors.Is(err, e) {
t.Errorf("expected non-nil error")
}
if _, err := compare(&d, skip{}, nil); !errors.Is(err, e) {
t.Errorf("expected non-nil error")
}
}
func Test_compareJSON_marshaling_error(t *testing.T) {
e := errors.New("")
unmarshalFn := func(b []byte, _ any) error {
if bytes.Equal(b, skipBytes) {
return nil
}
return e
}
if _, err := compareJSON(nil, nil, nil, unmarshalFn); !errors.Is(err, e) {
t.Errorf("expected non-nil error")
}
if _, err := compareJSON(nil, []byte("skip"), nil, unmarshalFn); !errors.Is(err, e) {
t.Errorf("expected non-nil error")
}
}