File: comparison_test.go

package info (click to toggle)
golang-k8s-sigs-structured-merge-diff 4.1.2%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,580 kB
  • sloc: makefile: 4
file content (76 lines) | stat: -rw-r--r-- 2,325 bytes parent folder | download | duplicates (3)
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
package typed_test

import (
	"testing"

	"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
	"sigs.k8s.io/structured-merge-diff/v4/typed"
)

func TestComparisonExcludeFields(t *testing.T) {
	cases := []struct {
		name       string
		Comparison *typed.Comparison
		Remove     *fieldpath.Set
		Expect     *typed.Comparison
		Fails      bool
	}{
		{
			name: "works on nil set",
			Comparison: &typed.Comparison{
				Added:    fieldpath.NewSet(fieldpath.MakePathOrDie("a")),
				Modified: fieldpath.NewSet(fieldpath.MakePathOrDie("b")),
				Removed:  fieldpath.NewSet(fieldpath.MakePathOrDie("c")),
			},
			Remove: nil,
			Expect: &typed.Comparison{
				Added:    fieldpath.NewSet(fieldpath.MakePathOrDie("a")),
				Modified: fieldpath.NewSet(fieldpath.MakePathOrDie("b")),
				Removed:  fieldpath.NewSet(fieldpath.MakePathOrDie("c")),
			},
		},
		{
			name: "works on empty set",
			Comparison: &typed.Comparison{
				Added:    fieldpath.NewSet(fieldpath.MakePathOrDie("a")),
				Modified: fieldpath.NewSet(fieldpath.MakePathOrDie("b")),
				Removed:  fieldpath.NewSet(fieldpath.MakePathOrDie("c")),
			},
			Remove: fieldpath.NewSet(),
			Expect: &typed.Comparison{
				Added:    fieldpath.NewSet(fieldpath.MakePathOrDie("a")),
				Modified: fieldpath.NewSet(fieldpath.MakePathOrDie("b")),
				Removed:  fieldpath.NewSet(fieldpath.MakePathOrDie("c")),
			},
		},
		{
			name: "removes from entire object",
			Comparison: &typed.Comparison{
				Added:    fieldpath.NewSet(fieldpath.MakePathOrDie("a", "aa")),
				Modified: fieldpath.NewSet(fieldpath.MakePathOrDie("b", "ba")),
				Removed:  fieldpath.NewSet(fieldpath.MakePathOrDie("c", "ca")),
			},
			Remove: fieldpath.NewSet(
				fieldpath.MakePathOrDie("a"),
				fieldpath.MakePathOrDie("b"),
				fieldpath.MakePathOrDie("c"),
			),
			Expect: &typed.Comparison{
				Added:    fieldpath.NewSet(),
				Modified: fieldpath.NewSet(),
				Removed:  fieldpath.NewSet(),
			},
		},
	}

	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			c.Comparison.ExcludeFields(c.Remove)
			if (!c.Comparison.Added.Equals(c.Expect.Added) ||
				!c.Comparison.Modified.Equals(c.Expect.Modified) ||
				!c.Comparison.Removed.Equals(c.Expect.Removed)) != c.Fails {
				t.Fatalf("remove expected: \n%v\nremoved:\n%v\ngot:\n%v\n", c.Expect, c.Remove, c.Comparison)
			}
		})
	}
}