github.com/hashicorp/hcl/v2@v2.20.0/hclsyntax/variables_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package hclsyntax 5 6 import ( 7 "fmt" 8 "reflect" 9 "testing" 10 11 "github.com/hashicorp/hcl/v2" 12 "github.com/kr/pretty" 13 "github.com/zclconf/go-cty/cty" 14 ) 15 16 func TestVariables(t *testing.T) { 17 tests := []struct { 18 Expr Expression 19 Want []hcl.Traversal 20 }{ 21 { 22 &LiteralValueExpr{ 23 Val: cty.True, 24 }, 25 nil, 26 }, 27 { 28 &ScopeTraversalExpr{ 29 Traversal: hcl.Traversal{ 30 hcl.TraverseRoot{ 31 Name: "foo", 32 }, 33 }, 34 }, 35 []hcl.Traversal{ 36 { 37 hcl.TraverseRoot{ 38 Name: "foo", 39 }, 40 }, 41 }, 42 }, 43 { 44 &BinaryOpExpr{ 45 LHS: &ScopeTraversalExpr{ 46 Traversal: hcl.Traversal{ 47 hcl.TraverseRoot{ 48 Name: "foo", 49 }, 50 }, 51 }, 52 Op: OpAdd, 53 RHS: &ScopeTraversalExpr{ 54 Traversal: hcl.Traversal{ 55 hcl.TraverseRoot{ 56 Name: "bar", 57 }, 58 }, 59 }, 60 }, 61 []hcl.Traversal{ 62 { 63 hcl.TraverseRoot{ 64 Name: "foo", 65 }, 66 }, 67 { 68 hcl.TraverseRoot{ 69 Name: "bar", 70 }, 71 }, 72 }, 73 }, 74 { 75 &UnaryOpExpr{ 76 Val: &ScopeTraversalExpr{ 77 Traversal: hcl.Traversal{ 78 hcl.TraverseRoot{ 79 Name: "foo", 80 }, 81 }, 82 }, 83 Op: OpNegate, 84 }, 85 []hcl.Traversal{ 86 { 87 hcl.TraverseRoot{ 88 Name: "foo", 89 }, 90 }, 91 }, 92 }, 93 { 94 &ConditionalExpr{ 95 Condition: &ScopeTraversalExpr{ 96 Traversal: hcl.Traversal{ 97 hcl.TraverseRoot{ 98 Name: "foo", 99 }, 100 }, 101 }, 102 TrueResult: &ScopeTraversalExpr{ 103 Traversal: hcl.Traversal{ 104 hcl.TraverseRoot{ 105 Name: "bar", 106 }, 107 }, 108 }, 109 FalseResult: &ScopeTraversalExpr{ 110 Traversal: hcl.Traversal{ 111 hcl.TraverseRoot{ 112 Name: "baz", 113 }, 114 }, 115 }, 116 }, 117 []hcl.Traversal{ 118 { 119 hcl.TraverseRoot{ 120 Name: "foo", 121 }, 122 }, 123 { 124 hcl.TraverseRoot{ 125 Name: "bar", 126 }, 127 }, 128 { 129 hcl.TraverseRoot{ 130 Name: "baz", 131 }, 132 }, 133 }, 134 }, 135 { 136 &ForExpr{ 137 KeyVar: "k", 138 ValVar: "v", 139 140 CollExpr: &ScopeTraversalExpr{ 141 Traversal: hcl.Traversal{ 142 hcl.TraverseRoot{ 143 Name: "foo", 144 }, 145 }, 146 }, 147 KeyExpr: &BinaryOpExpr{ 148 LHS: &ScopeTraversalExpr{ 149 Traversal: hcl.Traversal{ 150 hcl.TraverseRoot{ 151 Name: "k", 152 }, 153 }, 154 }, 155 Op: OpAdd, 156 RHS: &ScopeTraversalExpr{ 157 Traversal: hcl.Traversal{ 158 hcl.TraverseRoot{ 159 Name: "bar", 160 }, 161 }, 162 }, 163 }, 164 ValExpr: &BinaryOpExpr{ 165 LHS: &ScopeTraversalExpr{ 166 Traversal: hcl.Traversal{ 167 hcl.TraverseRoot{ 168 Name: "v", 169 }, 170 }, 171 }, 172 Op: OpAdd, 173 RHS: &ScopeTraversalExpr{ 174 Traversal: hcl.Traversal{ 175 hcl.TraverseRoot{ 176 Name: "baz", 177 }, 178 }, 179 }, 180 }, 181 CondExpr: &BinaryOpExpr{ 182 LHS: &ScopeTraversalExpr{ 183 Traversal: hcl.Traversal{ 184 hcl.TraverseRoot{ 185 Name: "k", 186 }, 187 }, 188 }, 189 Op: OpLessThan, 190 RHS: &ScopeTraversalExpr{ 191 Traversal: hcl.Traversal{ 192 hcl.TraverseRoot{ 193 Name: "limit", 194 }, 195 }, 196 }, 197 }, 198 }, 199 []hcl.Traversal{ 200 { 201 hcl.TraverseRoot{ 202 Name: "foo", 203 }, 204 }, 205 { 206 hcl.TraverseRoot{ 207 Name: "bar", 208 }, 209 }, 210 { 211 hcl.TraverseRoot{ 212 Name: "baz", 213 }, 214 }, 215 { 216 hcl.TraverseRoot{ 217 Name: "limit", 218 }, 219 }, 220 }, 221 }, 222 { 223 &ScopeTraversalExpr{ 224 Traversal: hcl.Traversal{ 225 hcl.TraverseRoot{ 226 Name: "data", 227 }, 228 hcl.TraverseAttr{ 229 Name: "null_data_source", 230 }, 231 hcl.TraverseAttr{ 232 Name: "multi", 233 }, 234 hcl.TraverseIndex{ 235 Key: cty.NumberFloatVal(0), 236 }, 237 }, 238 }, 239 []hcl.Traversal{ 240 { 241 hcl.TraverseRoot{ 242 Name: "data", 243 }, 244 hcl.TraverseAttr{ 245 Name: "null_data_source", 246 }, 247 hcl.TraverseAttr{ 248 Name: "multi", 249 }, 250 hcl.TraverseIndex{ 251 Key: cty.NumberFloatVal(0), 252 }, 253 }, 254 }, 255 }, 256 { 257 &RelativeTraversalExpr{ 258 Source: &FunctionCallExpr{ 259 Name: "sort", 260 Args: []Expression{ 261 &ScopeTraversalExpr{ 262 Traversal: hcl.Traversal{ 263 hcl.TraverseRoot{ 264 Name: "data", 265 }, 266 hcl.TraverseAttr{ 267 Name: "null_data_source", 268 }, 269 hcl.TraverseAttr{ 270 Name: "multi", 271 }, 272 }, 273 }, 274 }, 275 }, 276 Traversal: hcl.Traversal{ 277 hcl.TraverseIndex{ 278 Key: cty.NumberFloatVal(0), 279 }, 280 }, 281 }, 282 []hcl.Traversal{ 283 { 284 hcl.TraverseRoot{ 285 Name: "data", 286 }, 287 hcl.TraverseAttr{ 288 Name: "null_data_source", 289 }, 290 hcl.TraverseAttr{ 291 Name: "multi", 292 }, 293 }, 294 }, 295 }, 296 } 297 298 for _, test := range tests { 299 t.Run(fmt.Sprintf("%#v", test.Expr), func(t *testing.T) { 300 got := Variables(test.Expr) 301 302 if !reflect.DeepEqual(got, test.Want) { 303 t.Errorf("wrong result\ngot: %s\nwant: %s", pretty.Sprint(got), pretty.Sprint(test.Want)) 304 } 305 }) 306 } 307 }