github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/utils/maps/maps_test.go (about) 1 // Copyright © 2023 Alibaba Group Holding Ltd. 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 maps 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 ) 22 23 func Test_Merge(t *testing.T) { 24 src := map[string]string{ 25 "key1": "src1", 26 "key2": "src2", 27 "key3": "src3", 28 "src-key": "src-value", 29 } 30 31 dst := map[string]string{ 32 "key1": "v1", 33 "key2": "v2", 34 "key3": "v3", 35 "dst-key": "dst-value", 36 } 37 38 result := map[string]string{ 39 "key1": "v1", 40 "key2": "v2", 41 "key3": "v3", 42 "dst-key": "dst-value", 43 "src-key": "src-value", 44 } 45 46 nilDst := make(map[string]string) 47 48 type args struct { 49 src map[string]string 50 dst map[string]string 51 wanted map[string]string 52 } 53 54 var tests = []struct { 55 name string 56 args args 57 }{ 58 { 59 name: "nil dst want get src as result", 60 args: args{ 61 src: src, 62 dst: nilDst, 63 wanted: src, 64 }, 65 }, 66 { 67 name: "not nil dst want get overwriting dst with src as result", 68 args: args{ 69 src: src, 70 dst: dst, 71 wanted: result, 72 }, 73 }, 74 } 75 76 for _, tt := range tests { 77 t.Run(tt.name, func(t *testing.T) { 78 assert.Equal(t, tt.args.wanted, Merge(tt.args.dst, tt.args.src)) 79 }) 80 } 81 } 82 83 func Test_Copy(t *testing.T) { 84 src := map[string]string{ 85 "key1": "v1", 86 "key2": "v2", 87 "key3": "v3", 88 "dst-key": "dst-value", 89 "src-key": "src-value", 90 } 91 92 result := map[string]string{ 93 "key1": "v1", 94 "key2": "v2", 95 "key3": "v3", 96 "dst-key": "dst-value", 97 "src-key": "src-value", 98 } 99 100 type args struct { 101 src map[string]string 102 wanted map[string]string 103 } 104 105 var tests = []struct { 106 name string 107 args args 108 }{ 109 { 110 name: "nil src want get nil result", 111 args: args{ 112 src: nil, 113 wanted: nil, 114 }, 115 }, 116 { 117 name: "not nil src want same src as result", 118 args: args{ 119 src: src, 120 wanted: result, 121 }, 122 }, 123 } 124 125 for _, tt := range tests { 126 t.Run(tt.name, func(t *testing.T) { 127 assert.Equal(t, tt.args.wanted, Copy(tt.args.src)) 128 }) 129 } 130 }