github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/general/list_test.go (about) 1 /* 2 Copyright 2022 The Katalyst 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 general 18 19 import ( 20 "testing" 21 ) 22 23 func TestSliceContains(t *testing.T) { 24 t.Parallel() 25 26 type args struct { 27 list interface{} 28 elem interface{} 29 } 30 type myType struct { 31 name string 32 id int 33 } 34 tests := []struct { 35 name string 36 args args 37 want bool 38 }{ 39 { 40 name: "int - true", 41 args: args{ 42 list: []int{1, 2, 3}, 43 elem: 1, 44 }, 45 want: true, 46 }, 47 { 48 name: "int - false", 49 args: args{ 50 list: []int{1, 2, 3}, 51 elem: 4, 52 }, 53 want: false, 54 }, 55 { 56 name: "string - true", 57 args: args{ 58 list: []string{"1", "2", "3"}, 59 elem: "1", 60 }, 61 want: true, 62 }, 63 { 64 name: "string - false", 65 args: args{ 66 list: []string{"1", "2", "3"}, 67 elem: "4", 68 }, 69 want: false, 70 }, 71 { 72 name: "string - false", 73 args: args{ 74 list: []string{"1", "2", "3"}, 75 elem: 4, 76 }, 77 want: false, 78 }, 79 { 80 name: "string - true", 81 args: args{ 82 list: []string{"1", "2", "3"}, 83 elem: "1", 84 }, 85 want: true, 86 }, 87 { 88 name: "myType - true", 89 args: args{ 90 list: []myType{ 91 { 92 name: "name", 93 id: 1, 94 }, 95 { 96 name: "name", 97 id: 2, 98 }, 99 }, 100 elem: myType{ 101 name: "name", 102 id: 2, 103 }, 104 }, 105 want: true, 106 }, 107 { 108 name: "myType - false", 109 args: args{ 110 list: []myType{ 111 { 112 name: "name", 113 id: 1, 114 }, 115 { 116 name: "name", 117 id: 2, 118 }, 119 }, 120 elem: myType{ 121 name: "name-1", 122 id: 2, 123 }, 124 }, 125 want: false, 126 }, 127 { 128 name: "list - nil", 129 args: args{ 130 elem: 1, 131 }, 132 want: false, 133 }, 134 { 135 name: "elem - nil", 136 args: args{ 137 list: []int{1, 2, 3}, 138 }, 139 want: false, 140 }, 141 } 142 for _, tt := range tests { 143 tt := tt 144 t.Run(tt.name, func(t *testing.T) { 145 t.Parallel() 146 if got := SliceContains(tt.args.list, tt.args.elem); got != tt.want { 147 t.Errorf("SliceContains() = %v, want %v", got, tt.want) 148 } 149 }) 150 } 151 }