...

Source file src/k8s.io/kubectl/pkg/util/slice/slice_test.go

Documentation: k8s.io/kubectl/pkg/util/slice

     1  /*
     2  Copyright 2017 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 slice
    18  
    19  import (
    20  	"reflect"
    21  	"sort"
    22  	"testing"
    23  )
    24  
    25  func TestSortInts64(t *testing.T) {
    26  	src := []int64{10, 1, 2, 3, 4, 5, 6}
    27  	expected := []int64{1, 2, 3, 4, 5, 6, 10}
    28  	SortInts64(src)
    29  	if !reflect.DeepEqual(src, expected) {
    30  		t.Errorf("func Ints64 didnt sort correctly, %v !- %v", src, expected)
    31  	}
    32  }
    33  
    34  func TestToSet(t *testing.T) {
    35  	testCases := map[string]struct {
    36  		input    [][]string
    37  		expected []string
    38  	}{
    39  		"nil should be returned if no slices are passed to the function": {
    40  			input:    [][]string{},
    41  			expected: nil,
    42  		},
    43  		"empty slice should be returned if an empty slice is passed to the function": {
    44  			input:    [][]string{{}},
    45  			expected: []string{},
    46  		},
    47  		"a single slice with no duplicates should have the same values": {
    48  			input:    [][]string{{"a", "b", "c"}},
    49  			expected: []string{"a", "b", "c"},
    50  		},
    51  		"duplicates should be removed from a single slice": {
    52  			input:    [][]string{{"a", "b", "a", "c", "b"}},
    53  			expected: []string{"a", "b", "c"},
    54  		},
    55  		"multiple slices with no duplicates should be combined": {
    56  			input:    [][]string{{"a", "b", "c"}, {"d", "e", "f"}},
    57  			expected: []string{"a", "b", "c", "d", "e", "f"},
    58  		},
    59  		"duplicates should be removed from multiple slices": {
    60  			input:    [][]string{{"a", "b", "c"}, {"d", "b", "e"}, {"e", "f", "a"}},
    61  			expected: []string{"a", "b", "c", "d", "e", "f"},
    62  		},
    63  	}
    64  	for name, tc := range testCases {
    65  		t.Run(name, func(t *testing.T) {
    66  			actual := ToSet(tc.input...)
    67  			sort.Strings(actual) // Sort is needed to compare the output because ToSet is non-deterministic
    68  			if !reflect.DeepEqual(actual, tc.expected) {
    69  				t.Errorf("wrong output. Actual=%v, Expected=%v", actual, tc.expected)
    70  			}
    71  		})
    72  	}
    73  }
    74  

View as plain text