...

Source file src/sigs.k8s.io/structured-merge-diff/v4/fieldpath/element_test.go

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

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package fieldpath
    18  
    19  import (
    20  	"testing"
    21  
    22  	"sigs.k8s.io/structured-merge-diff/v4/value"
    23  )
    24  
    25  func TestPathElementSet(t *testing.T) {
    26  	s := &PathElementSet{}
    27  	s.Has(PathElement{})
    28  	s2 := &PathElementSet{}
    29  	s2.Insert(PathElement{})
    30  	if s2.Equals(s) {
    31  		t.Errorf("unequal sets should not equal")
    32  	}
    33  	if !s2.Has(PathElement{}) {
    34  		t.Errorf("expected to have something: %#v", s2)
    35  	}
    36  
    37  	n1 := "aoeu"
    38  	n2 := "asdf"
    39  	s2.Insert(PathElement{FieldName: &n1})
    40  	if !s2.Has(PathElement{FieldName: &n1}) {
    41  		t.Errorf("expected to have something: %#v", s2)
    42  	}
    43  	if s2.Has(PathElement{FieldName: &n2}) {
    44  		t.Errorf("expected to not have something: %#v", s2)
    45  	}
    46  
    47  	s2.Insert(PathElement{FieldName: &n2})
    48  	expected := []*string{&n1, &n2, nil}
    49  	i := 0
    50  	s2.Iterate(func(pe PathElement) {
    51  		e, a := expected[i], pe.FieldName
    52  		if e == nil || a == nil {
    53  			if e != a {
    54  				t.Errorf("index %v wanted %#v, got %#v", i, e, a)
    55  			}
    56  		} else {
    57  			if *e != *a {
    58  				t.Errorf("index %v wanted %#v, got %#v", i, *e, *a)
    59  			}
    60  		}
    61  		i++
    62  	})
    63  }
    64  
    65  func strptr(s string) *string { return &s }
    66  func intptr(i int) *int       { return &i }
    67  func valptr(i interface{}) *value.Value {
    68  	v := value.NewValueInterface(i)
    69  	return &v
    70  }
    71  
    72  func TestPathElementLess(t *testing.T) {
    73  	table := []struct {
    74  		name string
    75  		// we expect a < b and !(b < a) unless eq is true, in which
    76  		// case we expect less to return false in both orders.
    77  		a, b PathElement
    78  		eq   bool
    79  	}{
    80  		{
    81  			name: "FieldName-0",
    82  			a:    PathElement{},
    83  			b:    PathElement{},
    84  			eq:   true,
    85  		}, {
    86  			name: "FieldName-1",
    87  			a:    PathElement{FieldName: strptr("anteater")},
    88  			b:    PathElement{FieldName: strptr("zebra")},
    89  		}, {
    90  			name: "FieldName-2",
    91  			a:    PathElement{FieldName: strptr("bee")},
    92  			b:    PathElement{FieldName: strptr("bee")},
    93  			eq:   true,
    94  		}, {
    95  			name: "FieldName-3",
    96  			a:    PathElement{FieldName: strptr("capybara")},
    97  			b:    PathElement{Key: KeyByFields("dog", 3)},
    98  		}, {
    99  			name: "FieldName-4",
   100  			a:    PathElement{FieldName: strptr("elephant")},
   101  			b:    PathElement{Value: valptr(4)},
   102  		}, {
   103  			name: "FieldName-5",
   104  			a:    PathElement{FieldName: strptr("falcon")},
   105  			b:    PathElement{Index: intptr(5)},
   106  		}, {
   107  			name: "Key-1",
   108  			a:    PathElement{Key: KeyByFields("goat", 1)},
   109  			b:    PathElement{Key: KeyByFields("goat", 1)},
   110  			eq:   true,
   111  		}, {
   112  			name: "Key-2",
   113  			a:    PathElement{Key: KeyByFields("horse", 1)},
   114  			b:    PathElement{Key: KeyByFields("horse", 2)},
   115  		}, {
   116  			name: "Key-3",
   117  			a:    PathElement{Key: KeyByFields("ibex", 1)},
   118  			b:    PathElement{Key: KeyByFields("jay", 1)},
   119  		}, {
   120  			name: "Key-4",
   121  			a:    PathElement{Key: KeyByFields("kite", 1)},
   122  			b:    PathElement{Key: KeyByFields("kite", 1, "kite-2", 1)},
   123  		}, {
   124  			name: "Key-5",
   125  			a:    PathElement{Key: KeyByFields("kite", 1)},
   126  			b:    PathElement{Value: valptr(1)},
   127  		}, {
   128  			name: "Key-6",
   129  			a:    PathElement{Key: KeyByFields("kite", 1)},
   130  			b:    PathElement{Index: intptr(5)},
   131  		}, {
   132  			name: "Value-1",
   133  			a:    PathElement{Value: valptr(1)},
   134  			b:    PathElement{Value: valptr(2)},
   135  		}, {
   136  			name: "Value-2",
   137  			a:    PathElement{Value: valptr(1)},
   138  			b:    PathElement{Value: valptr(1)},
   139  			eq:   true,
   140  		}, {
   141  			name: "Value-3",
   142  			a:    PathElement{Value: valptr(1)},
   143  			b:    PathElement{Index: intptr(1)},
   144  		}, {
   145  			name: "Index-1",
   146  			a:    PathElement{Index: intptr(1)},
   147  			b:    PathElement{Index: intptr(2)},
   148  		}, {
   149  			name: "Index-2",
   150  			a:    PathElement{Index: intptr(1)},
   151  			b:    PathElement{Index: intptr(1)},
   152  			eq:   true,
   153  		},
   154  	}
   155  
   156  	for i := range table {
   157  		i := i
   158  		t.Run(table[i].name, func(t *testing.T) {
   159  			tt := table[i]
   160  			if tt.eq {
   161  				if tt.a.Less(tt.b) {
   162  					t.Errorf("oops, a < b: %#v (%v), %#v (%v)", tt.a, tt.a, tt.b, tt.b)
   163  				}
   164  			} else {
   165  				if !tt.a.Less(tt.b) {
   166  					t.Errorf("oops, a >= b: %#v (%v), %#v (%v)", tt.a, tt.a, tt.b, tt.b)
   167  				}
   168  			}
   169  			if tt.b.Less(tt.b) {
   170  				t.Errorf("oops, b < a: %#v (%v), %#v (%v)", tt.b, tt.b, tt.a, tt.a)
   171  			}
   172  		})
   173  	}
   174  }
   175  

View as plain text