...

Source file src/sigs.k8s.io/structured-merge-diff/v4/typed/comparison_test.go

Documentation: sigs.k8s.io/structured-merge-diff/v4/typed

     1  package typed_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
     7  	"sigs.k8s.io/structured-merge-diff/v4/typed"
     8  )
     9  
    10  func TestComparisonExcludeFields(t *testing.T) {
    11  	cases := []struct {
    12  		name       string
    13  		Comparison *typed.Comparison
    14  		Remove     *fieldpath.Set
    15  		Expect     *typed.Comparison
    16  		Fails      bool
    17  	}{
    18  		{
    19  			name: "works on nil set",
    20  			Comparison: &typed.Comparison{
    21  				Added:    fieldpath.NewSet(fieldpath.MakePathOrDie("a")),
    22  				Modified: fieldpath.NewSet(fieldpath.MakePathOrDie("b")),
    23  				Removed:  fieldpath.NewSet(fieldpath.MakePathOrDie("c")),
    24  			},
    25  			Remove: nil,
    26  			Expect: &typed.Comparison{
    27  				Added:    fieldpath.NewSet(fieldpath.MakePathOrDie("a")),
    28  				Modified: fieldpath.NewSet(fieldpath.MakePathOrDie("b")),
    29  				Removed:  fieldpath.NewSet(fieldpath.MakePathOrDie("c")),
    30  			},
    31  		},
    32  		{
    33  			name: "works on empty set",
    34  			Comparison: &typed.Comparison{
    35  				Added:    fieldpath.NewSet(fieldpath.MakePathOrDie("a")),
    36  				Modified: fieldpath.NewSet(fieldpath.MakePathOrDie("b")),
    37  				Removed:  fieldpath.NewSet(fieldpath.MakePathOrDie("c")),
    38  			},
    39  			Remove: fieldpath.NewSet(),
    40  			Expect: &typed.Comparison{
    41  				Added:    fieldpath.NewSet(fieldpath.MakePathOrDie("a")),
    42  				Modified: fieldpath.NewSet(fieldpath.MakePathOrDie("b")),
    43  				Removed:  fieldpath.NewSet(fieldpath.MakePathOrDie("c")),
    44  			},
    45  		},
    46  		{
    47  			name: "removes from entire object",
    48  			Comparison: &typed.Comparison{
    49  				Added:    fieldpath.NewSet(fieldpath.MakePathOrDie("a", "aa")),
    50  				Modified: fieldpath.NewSet(fieldpath.MakePathOrDie("b", "ba")),
    51  				Removed:  fieldpath.NewSet(fieldpath.MakePathOrDie("c", "ca")),
    52  			},
    53  			Remove: fieldpath.NewSet(
    54  				fieldpath.MakePathOrDie("a"),
    55  				fieldpath.MakePathOrDie("b"),
    56  				fieldpath.MakePathOrDie("c"),
    57  			),
    58  			Expect: &typed.Comparison{
    59  				Added:    fieldpath.NewSet(),
    60  				Modified: fieldpath.NewSet(),
    61  				Removed:  fieldpath.NewSet(),
    62  			},
    63  		},
    64  	}
    65  
    66  	for _, c := range cases {
    67  		t.Run(c.name, func(t *testing.T) {
    68  			c.Comparison.ExcludeFields(c.Remove)
    69  			if (!c.Comparison.Added.Equals(c.Expect.Added) ||
    70  				!c.Comparison.Modified.Equals(c.Expect.Modified) ||
    71  				!c.Comparison.Removed.Equals(c.Expect.Removed)) != c.Fails {
    72  				t.Fatalf("remove expected: \n%v\nremoved:\n%v\ngot:\n%v\n", c.Expect, c.Remove, c.Comparison)
    73  			}
    74  		})
    75  	}
    76  }
    77  

View as plain text