...

Source file src/k8s.io/utils/diff/diff_test.go

Documentation: k8s.io/utils/diff

     1  /*
     2  Copyright 2016 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 diff
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestObjectReflectDiff(t *testing.T) {
    24  	type struct1 struct{ A []int }
    25  
    26  	testCases := map[string]struct {
    27  		a, b interface{}
    28  		out  string
    29  	}{
    30  		"map": {
    31  			a: map[string]int{},
    32  			b: map[string]int{},
    33  		},
    34  		"detect nil map": {
    35  			a: map[string]int(nil),
    36  			b: map[string]int{},
    37  			out: `
    38  object:
    39    a: map[string]int(nil)
    40    b: map[string]int{}`,
    41  		},
    42  		"detect map changes": {
    43  			a: map[string]int{"test": 1, "other": 2},
    44  			b: map[string]int{"test": 2, "third": 3},
    45  			out: `
    46  object[other]:
    47    a: 2
    48    b: <nil>
    49  object[test]:
    50    a: 1
    51    b: 2
    52  object[third]:
    53    a: <nil>
    54    b: 3`,
    55  		},
    56  		"nil slice":   {a: struct1{A: nil}, b: struct1{A: nil}},
    57  		"empty slice": {a: struct1{A: []int{}}, b: struct1{A: []int{}}},
    58  		"detect slice changes 1": {a: struct1{A: []int{1}}, b: struct1{A: []int{2}}, out: `
    59  object.A[0]:
    60    a: 1
    61    b: 2`,
    62  		},
    63  		"detect slice changes 2": {a: struct1{A: []int{}}, b: struct1{A: []int{2}}, out: `
    64  object.A[0]:
    65    a: <nil>
    66    b: 2`,
    67  		},
    68  		"detect slice changes 3": {a: struct1{A: []int{1}}, b: struct1{A: []int{}}, out: `
    69  object.A[0]:
    70    a: 1
    71    b: <nil>`,
    72  		},
    73  		"detect nil vs empty slices": {a: struct1{A: nil}, b: struct1{A: []int{}}, out: `
    74  object.A:
    75    a: []int(nil)
    76    b: []int{}`,
    77  		},
    78  		"display type differences": {a: []interface{}{int64(1)}, b: []interface{}{uint64(1)}, out: `
    79  object[0]:
    80    a: 1 (int64)
    81    b: 0x1 (uint64)`,
    82  		},
    83  	}
    84  	for name, test := range testCases {
    85  		expect := test.out
    86  		if len(expect) == 0 {
    87  			expect = "<no diffs>"
    88  		}
    89  		if actual := ObjectReflectDiff(test.a, test.b); actual != expect {
    90  			t.Errorf("%s: unexpected output: %s", name, actual)
    91  		}
    92  	}
    93  }
    94  
    95  func TestStringDiff(t *testing.T) {
    96  	diff := StringDiff("aaabb", "aaacc")
    97  	expect := "aaa\n\nA: bb\n\nB: cc\n\n"
    98  	if diff != expect {
    99  		t.Errorf("diff returned %v", diff)
   100  	}
   101  }
   102  
   103  func TestLimit(t *testing.T) {
   104  	testcases := []struct {
   105  		a       interface{}
   106  		b       interface{}
   107  		expectA string
   108  		expectB string
   109  	}{
   110  		{
   111  			a:       `short a`,
   112  			b:       `short b`,
   113  			expectA: `"short a"`,
   114  			expectB: `"short b"`,
   115  		},
   116  		{
   117  			a:       `short a`,
   118  			b:       `long b needs truncating`,
   119  			expectA: `"short a"`,
   120  			expectB: `"long b ne...`,
   121  		},
   122  		{
   123  			a:       `long a needs truncating`,
   124  			b:       `long b needs truncating`,
   125  			expectA: `...g a needs ...`,
   126  			expectB: `...g b needs ...`,
   127  		},
   128  		{
   129  			a:       `long common prefix with different stuff at the end of a`,
   130  			b:       `long common prefix with different stuff at the end of b`,
   131  			expectA: `...end of a"`,
   132  			expectB: `...end of b"`,
   133  		},
   134  		{
   135  			a:       `long common prefix with different stuff at the end of a`,
   136  			b:       `long common prefix with different stuff at the end of b which continues`,
   137  			expectA: `...of a"`,
   138  			expectB: `...of b which...`,
   139  		},
   140  	}
   141  
   142  	for _, tc := range testcases {
   143  		a, b := limit(tc.a, tc.b, 10)
   144  		if a != tc.expectA || b != tc.expectB {
   145  			t.Errorf("limit(%q, %q)\n\texpected: %s, %s\n\tgot:      %s, %s", tc.a, tc.b, tc.expectA, tc.expectB, a, b)
   146  		}
   147  	}
   148  }
   149  

View as plain text