github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/utils/merge_test.go (about)

     1  /*
     2  Licensed under the Apache License, Version 2.0 (the "License");
     3  you may not use this file except in compliance with the License.
     4  You may obtain a copy of the License at
     5  
     6      http://www.apache.org/licenses/LICENSE-2.0
     7  
     8  Unless required by applicable law or agreed to in writing, software
     9  distributed under the License is distributed on an "AS IS" BASIS,
    10  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  See the License for the specific language governing permissions and
    12  limitations under the License.
    13  */
    14  
    15  // This is mostly a copy of https://github.com/divideandconquer/go-merge/blob/master/merge/merge_test.go
    16  // with minimal number of changes to reflect the different approach taken to slice merging (+ different package name)
    17  // Original test function names are retained
    18  
    19  package utils
    20  
    21  import (
    22  	"reflect"
    23  	"testing"
    24  )
    25  
    26  func TestMerge(t *testing.T) {
    27  	for _, tc := range []struct {
    28  		name string
    29  		a    interface{}
    30  		b    interface{}
    31  		r    interface{}
    32  	}{
    33  		{
    34  			name: "non-overlapping maps",
    35  			a:    map[string]string{"a": "X"},
    36  			b:    map[string]string{"b": "Y"},
    37  			r:    map[string]string{"a": "X", "b": "Y"},
    38  		},
    39  		{
    40  			name: "overlapping maps",
    41  			a:    map[string]string{"a": "X", "b": "Y"},
    42  			b:    map[string]string{"c": "Z", "b": "Q"},
    43  			r:    map[string]string{"a": "X", "c": "Z", "b": "Q"},
    44  		},
    45  		{
    46  			name: "slices",
    47  			a:    []int{1, 2},
    48  			b:    []int{2, 3},
    49  			r:    []int{1, 2, 2, 3},
    50  		},
    51  		{
    52  			name: "slice-maps",
    53  			a:    map[string][]int{"a": {1, 2}},
    54  			b:    map[string][]int{"a": {3, 4}},
    55  			r:    map[string][]int{"a": {1, 2, 3, 4}},
    56  		},
    57  		{
    58  			name: "scalars",
    59  			a:    3,
    60  			b:    0,
    61  			r:    0,
    62  		},
    63  		{
    64  			name: "incompatible types",
    65  			a:    []string{"a", "b"},
    66  			b:    []int{1, 2},
    67  			r:    []int{1, 2},
    68  		},
    69  		{
    70  			name: "nil-slice",
    71  			a:    []int{1, 2},
    72  			b:    []int(nil),
    73  			r:    []int{1, 2},
    74  		},
    75  		{
    76  			name: "double nil-slice",
    77  			a:    []int(nil),
    78  			b:    []int(nil),
    79  			r:    []int(nil),
    80  		},
    81  		{
    82  			name: "nils",
    83  			a:    nil,
    84  			b:    nil,
    85  			r:    nil,
    86  		},
    87  	} {
    88  		t.Run(tc.name, func(t *testing.T) {
    89  			r := Merge(tc.a, tc.b)
    90  			if !reflect.DeepEqual(r, tc.r) {
    91  				t.Errorf("expected: %v, actual: %v", tc.r, r)
    92  			}
    93  		})
    94  	}
    95  }