-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
60 lines (50 loc) · 1.96 KB
/
main_test.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
55
56
57
58
59
60
package equl_test
import (
"github.com/ramizpolic/equl"
"strings"
"testing"
)
type Parent struct {
Name string
Child string
}
type Object struct {
Base int
Parent Parent
}
func TestMainAbc(t *testing.T) {
objA := Object{
Base: 1,
Parent: Parent{Name: "same", Child: "diff-A"},
}
objB := Object{
Base: 1,
Parent: Parent{Name: "same", Child: "diff-B"},
}
// By default, all fields between objects will be compared
result, _ := equl.Diff(objA, objB)
result.Equal() // False
result.Diffs() // map[.Parent.Child:[diff-A diff-B]]
// Fields specified using WithFields will be compared, including all their children
result, _ = equl.Diff(objA, objB, equl.WithFields(".Base", ".Parent"))
result.Equal() // False
result.Diffs() // map[.Parent.Child:[diff-A diff-B]]
// Fields specified using WithoutFields will be ignored, including all their children
result, _ = equl.Diff(objA, objB, equl.WithoutFields(".Parent.Child"))
result.Equal() // True
// Specifying both WithFields and WithoutFields allows to create dynamic rule-based comparisons,
// for example: compare the whole .Base and .Parent structs, but ignore everything in .Parent.Child
result, _ = equl.Diff(objA, objB, equl.WithFields(".Base", ".Parent"), equl.WithoutFields(".Parent.Child"))
result.Equal() // True
// It is also possible to specify a custom field fitler function to decide which fields should be
// compared and which ones ignored.
result, _ = equl.Diff(objA, objB, equl.WithFieldFilter(func(key string) bool {
return strings.HasPrefix(key, ".Parent") // Anything that starts with .Parent
}))
result.Equal() // False
result.Diffs() // map[.Parent.Child:[diff-A diff-B]]
// Equal dynamically compares if two objects are equal. This is faster that Diff
// since it does not need to calculate difference map. Same rules apply.
_, _ = equl.Equal(objA, objB) // False
_, _ = equl.Equal(objA, objB, equl.WithoutFields(".Parent.Child")) // True
}