...

Source file src/sigs.k8s.io/structured-merge-diff/v4/value/less_test.go

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

     1  /*
     2  Copyright 2019 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 value
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  var InvalidValue = struct{}{}
    24  
    25  func TestValueLess(t *testing.T) {
    26  	table := []struct {
    27  		name string
    28  		// we expect a < b and !(b < a) unless eq is true, in which
    29  		// case we expect less to return false in both orders.
    30  		a, b interface{}
    31  		eq   bool
    32  	}{
    33  		{
    34  			name: "Invalid-1",
    35  			a:    InvalidValue,
    36  			b:    InvalidValue,
    37  			eq:   true,
    38  		}, {
    39  			name: "Invalid-2",
    40  			a:    1.,
    41  			b:    InvalidValue,
    42  		}, {
    43  			name: "Invalid-3",
    44  			a:    1,
    45  			b:    InvalidValue,
    46  		}, {
    47  			name: "Invalid-4",
    48  			a:    "aoeu",
    49  			b:    InvalidValue,
    50  		}, {
    51  			name: "Invalid-5",
    52  			a:    true,
    53  			b:    InvalidValue,
    54  		}, {
    55  			name: "Invalid-6",
    56  			a:    []interface{}{},
    57  			b:    InvalidValue,
    58  		}, {
    59  			name: "Invalid-7",
    60  			a:    map[string]interface{}{},
    61  			b:    InvalidValue,
    62  		}, {
    63  			name: "Invalid-8",
    64  			a:    nil,
    65  			b:    InvalidValue,
    66  		}, {
    67  			name: "Float-1",
    68  			a:    1.14,
    69  			b:    3.14,
    70  		}, {
    71  			name: "Float-2",
    72  			a:    1.,
    73  			b:    1.,
    74  			eq:   true,
    75  		}, {
    76  			name: "Float-3",
    77  			a:    1.,
    78  			b:    1,
    79  			eq:   true,
    80  		}, {
    81  			name: "Float-4",
    82  			a:    1.,
    83  			b:    2,
    84  		}, {
    85  			name: "Float-5",
    86  			a:    1.,
    87  			b:    "aoeu",
    88  		}, {
    89  			name: "Float-6",
    90  			a:    1.,
    91  			b:    true,
    92  		}, {
    93  			name: "Float-7",
    94  			a:    1.,
    95  			b:    []interface{}{},
    96  		}, {
    97  			name: "Float-8",
    98  			a:    1.,
    99  			b:    map[string]interface{}{},
   100  		}, {
   101  			name: "Float-9",
   102  			a:    1.,
   103  			b:    nil,
   104  		}, {
   105  			name: "Int-1",
   106  			a:    1,
   107  			b:    2,
   108  		}, {
   109  			name: "Int-2",
   110  			a:    1,
   111  			b:    1,
   112  			eq:   true,
   113  		}, {
   114  			name: "Int-3",
   115  			a:    1,
   116  			b:    1.,
   117  			eq:   true,
   118  		}, {
   119  			name: "Int-4",
   120  			a:    1,
   121  			b:    2.,
   122  		}, {
   123  			name: "Int-5",
   124  			a:    1,
   125  			b:    "aoeu",
   126  		}, {
   127  			name: "Int-6",
   128  			a:    1,
   129  			b:    true,
   130  		}, {
   131  			name: "Int-7",
   132  			a:    1,
   133  			b:    []interface{}{},
   134  		}, {
   135  			name: "Int-8",
   136  			a:    1,
   137  			b:    map[string]interface{}{},
   138  		}, {
   139  			name: "Int-9",
   140  			a:    1,
   141  			b:    nil,
   142  		}, {
   143  			name: "String-1",
   144  			a:    "b-12",
   145  			b:    "b-9",
   146  		}, {
   147  			name: "String-2",
   148  			a:    "folate",
   149  			b:    "folate",
   150  			eq:   true,
   151  		}, {
   152  			name: "String-3",
   153  			a:    "folate",
   154  			b:    true,
   155  		}, {
   156  			name: "String-4",
   157  			a:    "folate",
   158  			b:    []interface{}{},
   159  		}, {
   160  			name: "String-5",
   161  			a:    "folate",
   162  			b:    map[string]interface{}{},
   163  		}, {
   164  			name: "String-6",
   165  			a:    "folate",
   166  			b:    nil,
   167  		}, {
   168  			name: "Bool-1",
   169  			a:    false,
   170  			b:    true,
   171  		}, {
   172  			name: "Bool-2",
   173  			a:    false,
   174  			b:    false,
   175  			eq:   true,
   176  		}, {
   177  			name: "Bool-3",
   178  			a:    true,
   179  			b:    true,
   180  			eq:   true,
   181  		}, {
   182  			name: "Bool-4",
   183  			a:    false,
   184  			b:    []interface{}{},
   185  		}, {
   186  			name: "Bool-5",
   187  			a:    false,
   188  			b:    map[string]interface{}{},
   189  		}, {
   190  			name: "Bool-6",
   191  			a:    false,
   192  			b:    nil,
   193  		}, {
   194  			name: "List-1",
   195  			a:    []interface{}{},
   196  			b:    []interface{}{},
   197  			eq:   true,
   198  		}, {
   199  			name: "List-2",
   200  			a:    []interface{}{1},
   201  			b:    []interface{}{1},
   202  			eq:   true,
   203  		}, {
   204  			name: "List-3",
   205  			a:    []interface{}{1},
   206  			b:    []interface{}{2},
   207  		}, {
   208  			name: "List-4",
   209  			a:    []interface{}{1},
   210  			b:    []interface{}{1, 1},
   211  		}, {
   212  			name: "List-5",
   213  			a:    []interface{}{1, 1},
   214  			b:    []interface{}{2},
   215  		}, {
   216  			name: "List-6",
   217  			a:    []interface{}{},
   218  			b:    map[string]interface{}{},
   219  		}, {
   220  			name: "List-7",
   221  			a:    []interface{}{},
   222  			b:    nil,
   223  		}, {
   224  			name: "Map-1",
   225  			a:    map[string]interface{}{"carotine": 1},
   226  			b:    map[string]interface{}{"carotine": 1},
   227  			eq:   true,
   228  		}, {
   229  			name: "Map-2",
   230  			a:    map[string]interface{}{"carotine": 1},
   231  			b:    map[string]interface{}{"carotine": 2},
   232  		}, {
   233  			name: "Map-3",
   234  			a:    map[string]interface{}{"carotine": 1},
   235  			b:    map[string]interface{}{"ethanol": 1},
   236  		}, {
   237  			name: "Map-4",
   238  			a:    map[string]interface{}{"carotine": 1},
   239  			b:    map[string]interface{}{"ethanol": 1, "carotine": 2},
   240  		}, {
   241  			name: "Map-5",
   242  			a:    map[string]interface{}{"carotine": 1},
   243  			b:    map[string]interface{}{"carotine": 1, "ethanol": 1},
   244  		}, {
   245  			name: "Map-6",
   246  			a:    map[string]interface{}{"carotine": 1, "ethanol": 1},
   247  			b:    map[string]interface{}{"carotine": 2},
   248  		}, {
   249  			name: "Map-7",
   250  			a:    map[string]interface{}{},
   251  			b:    nil,
   252  		},
   253  	}
   254  
   255  	for i := range table {
   256  		i := i
   257  		t.Run(table[i].name, func(t *testing.T) {
   258  			tt := table[i]
   259  			a, b := NewValueInterface(tt.a), NewValueInterface(tt.b)
   260  			if tt.eq {
   261  				if !Equals(a, b) {
   262  					t.Errorf("oops, a != b: %#v, %#v", tt.a, tt.b)
   263  				}
   264  				if !Equals(b, a) {
   265  					t.Errorf("oops, b != a: %#v, %#v", tt.b, tt.a)
   266  				}
   267  				if Less(a, b) {
   268  					t.Errorf("oops, a < b: %#v, %#v", tt.a, tt.b)
   269  				}
   270  			} else {
   271  				if Equals(a, b) {
   272  					t.Errorf("oops, a == b: %#v, %#v", tt.a, tt.b)
   273  				}
   274  				if Equals(b, a) {
   275  					t.Errorf("oops, b == a: %#v, %#v", tt.b, tt.a)
   276  				}
   277  				if !Less(a, b) {
   278  					t.Errorf("oops, a >= b: %#v, %#v", tt.a, tt.b)
   279  				}
   280  			}
   281  			if Less(b, a) {
   282  				t.Errorf("oops, b < a: %#v, %#v", tt.b, tt.a)
   283  			}
   284  
   285  			if tt.eq {
   286  				if Compare(a, b) != 0 || Compare(b, a) != 0 {
   287  					t.Errorf("oops, a is not equal b: %#v, %#v", tt.a, tt.b)
   288  				}
   289  			} else {
   290  				if !(Compare(a, b) < 0) {
   291  					t.Errorf("oops, a is not less than b: %#v, %#v", tt.a, tt.b)
   292  				}
   293  				if !(Compare(b, a) > 0) {
   294  					t.Errorf("oops, b is not more than a: %#v, %#v", tt.a, tt.b)
   295  				}
   296  			}
   297  		})
   298  	}
   299  
   300  }
   301  

View as plain text