github.com/verrazzano/verrazzano@v1.7.0/pkg/yaml/expand_test.go (about) 1 // Copyright (c) 2021, 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package yaml 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 // Simple name/value 13 const name1 = `aa` 14 const val1 = `val_1` 15 const expanded1 = `aa: val_1` 16 17 // Two level name/value 18 const name2 = `aa.bb` 19 const val2 = `val_2` 20 const expanded2 = `aa: 21 bb: val_2` 22 23 // Two level name/value with quoted final value segment 24 const name3 = `aa.bb."cc\.dd"` 25 const val3 = `val_3` 26 const expanded3 = `aa: 27 bb: 28 cc.dd: val_3` 29 30 // Name value with valuelist 31 const name4 = `aa.bb` 32 const val4a = `val_4a` 33 const val4b = `val_4b` 34 const val4c = `val_4c` 35 const expanded4 = `aa: 36 bb: 37 - val_4a 38 - val_4b 39 - val_4c` 40 41 // Name value with valuelist 42 const name5 = `aa.bb` 43 const val5 = `val_5a` 44 const expanded5 = `aa: 45 bb: 46 - val_5a` 47 48 // Name with list internal 49 const name6 = `aa.bb[0].cc` 50 const val6 = `val_6` 51 const expanded6 = `aa: 52 bb: 53 - cc: val_6` 54 55 // Name with multiple list internal 56 const name7 = `aa[0].bb[0].cc` 57 const val7 = `val_7` 58 const expanded7 = `aa: 59 - bb: 60 - cc: val_7` 61 62 // Final object list 63 const name8 = `aa[0].bb[0].cc[0]` 64 const val8 = `val_8` 65 const expanded8 = `aa: 66 - bb: 67 - cc: 68 - val_8` 69 70 // Escaped characters 71 const name9 = "aa\\.bb" 72 const val9 = `val_9` 73 const expanded9 = `aa.bb: val_9` 74 75 // Multiline value 76 const name10 = "aa" 77 const val10 = `val_10 78 val_10 79 val_10` 80 const expanded10 = `aa: | 81 val_10 82 val_10 83 val_10` 84 85 // Nested value 86 const name11 = "aa.bb" 87 const val11 = `val_11 88 val_11 89 val_11` 90 const expanded11 = `aa: 91 bb: | 92 val_11 93 val_11 94 val_11` 95 96 // TestExpand tests the Expand function 97 // GIVEN a set of dot separated names 98 // WHEN Expand is called 99 // THEN ensure that the expanded result is correct. 100 func TestExpand(t *testing.T) { 101 tests := []struct { 102 testName string 103 name string 104 forceList bool 105 values []string 106 expected string 107 }{ 108 { 109 testName: "1", 110 name: name1, 111 forceList: false, 112 values: []string{val1}, 113 expected: expanded1, 114 }, 115 { 116 testName: "2", 117 name: name2, 118 forceList: false, 119 values: []string{val2}, 120 expected: expanded2, 121 }, 122 { 123 testName: "3", 124 name: name3, 125 forceList: false, 126 values: []string{val3}, 127 expected: expanded3, 128 }, 129 { 130 testName: "4", 131 name: name4, 132 forceList: false, 133 values: []string{val4a, val4b, val4c}, 134 expected: expanded4, 135 }, 136 { 137 testName: "5", 138 name: name5, 139 forceList: true, 140 values: []string{val5}, 141 expected: expanded5, 142 }, 143 { 144 testName: "6", 145 name: name6, 146 forceList: false, 147 values: []string{val6}, 148 expected: expanded6, 149 }, 150 { 151 testName: "7", 152 name: name7, 153 forceList: false, 154 values: []string{val7}, 155 expected: expanded7, 156 }, 157 { 158 testName: "8", 159 name: name8, 160 forceList: false, 161 values: []string{val8}, 162 expected: expanded8, 163 }, 164 { 165 testName: "9", 166 name: name9, 167 forceList: false, 168 values: []string{val9}, 169 expected: expanded9, 170 }, 171 { 172 testName: "10", 173 name: name10, 174 forceList: false, 175 values: []string{val10}, 176 expected: expanded10, 177 }, 178 { 179 testName: "11", 180 name: name11, 181 forceList: false, 182 values: []string{val11}, 183 expected: expanded11, 184 }, 185 } 186 for _, test := range tests { 187 t.Run(test.testName, func(t *testing.T) { 188 assert := assert.New(t) 189 s, err := Expand(0, test.forceList, test.name, test.values...) 190 assert.NoError(err, s, "error merging profiles") 191 assert.Equal(test.expected, s, "Result does not match expected value") 192 }) 193 } 194 } 195 196 // Expanded results with a left margin of 4 197 const lmExpanded4 = ` aa: 198 bb: 199 - val_4a 200 - val_4b 201 - val_4c` 202 203 // TestLeftMargin tests the Expand function 204 // GIVEN a set of dot separated names 205 // WHEN Expand is called with a non-zero left margin 206 // THEN ensure that the expanded result is correct. 207 func TestLeftMargin(t *testing.T) { 208 tests := []struct { 209 testName string 210 name string 211 values []string 212 expected string 213 }{ 214 { 215 testName: "4", 216 name: name4, 217 values: []string{val4a, val4b, val4c}, 218 expected: lmExpanded4, 219 }, 220 } 221 for _, test := range tests { 222 t.Run(test.testName, func(t *testing.T) { 223 assert := assert.New(t) 224 s, err := Expand(4, false, test.name, test.values...) 225 assert.NoError(err, s, "error merging profiles") 226 assert.Equal(test.expected, s, "Result does not match expected value") 227 }) 228 } 229 }