github.com/GoogleCloudPlatform/compute-image-tools/cli_tools@v0.0.0-20240516224744-de2dabc4ed1b/common/utils/collections/collections_utils_test.go (about) 1 // Copyright 2020 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package collections 16 17 import ( 18 "reflect" 19 "sort" 20 "testing" 21 ) 22 23 func TestReverseMap(t *testing.T) { 24 tests := []struct { 25 name string 26 input map[string]string 27 want map[string]string 28 expectSuccess bool 29 }{ 30 {"nil map", nil, map[string]string{}, true}, 31 {"empty map", map[string]string{}, map[string]string{}, true}, 32 {"single item map", map[string]string{"k1": "v1"}, map[string]string{"v1": "k1"}, true}, 33 {"multiple items map", map[string]string{"k1": "v1", "k2": "v2"}, map[string]string{"v1": "k1", "v2": "k2"}, true}, 34 {"dup values", map[string]string{"k1": "v1", "k2": "v1"}, nil, false}, 35 } 36 37 for _, test := range tests { 38 m, ok := ReverseMap(test.input) 39 if test.expectSuccess != ok { 40 t.Errorf("[%v] Expected success: %v, actual: %v", test.name, test.expectSuccess, ok) 41 } else if test.expectSuccess && !reflect.DeepEqual(m, test.want) { 42 t.Errorf("[%v] Expected map '%v' != actual map '%v'", test.name, test.want, m) 43 } 44 } 45 } 46 47 func TestGetKeys(t *testing.T) { 48 tests := []struct { 49 name string 50 input map[string]string 51 want []string 52 }{ 53 {"nil map", nil, []string{}}, 54 {"empty map", map[string]string{}, []string{}}, 55 {"single item map", map[string]string{"k1": "v1"}, []string{"k1"}}, 56 {"multiple items map", map[string]string{"k1": "v1", "k2": "v2"}, []string{"k1", "k2"}}, 57 } 58 59 for _, test := range tests { 60 keys := GetKeys(test.input) 61 sort.Strings(keys) 62 sort.Strings(test.want) 63 if !reflect.DeepEqual(keys, test.want) { 64 t.Errorf("[%v] Expected keys '%v' != actual keys '%v'", test.name, test.want, keys) 65 } 66 } 67 }