@@ -248,29 +248,55 @@ terraform {
248
248
}
249
249
250
250
func Test_EvaluateExpr (t * testing.T ) {
251
- src := `
251
+ tests := []struct {
252
+ Name string
253
+ Src string
254
+ Want string
255
+ }{
256
+ {
257
+ Name : "string literal" ,
258
+ Src : `
252
259
resource "aws_instance" "foo" {
253
260
instance_type = "t2.micro"
254
- }`
255
-
256
- runner := TestRunner (t , map [string ]string {"main.tf" : src })
261
+ }` ,
262
+ Want : "t2.micro" ,
263
+ },
264
+ {
265
+ Name : "string interpolation" ,
266
+ Src : `
267
+ variable "instance_type" {
268
+ default = "t2.micro"
269
+ }
257
270
258
- resources , err := runner . GetResourceContent ( "aws_instance" , & hclext. BodySchema {
259
- Attributes : []hclext. AttributeSchema {{ Name : " instance_type" }},
260
- }, nil )
261
- if err != nil {
262
- t . Fatal ( err )
271
+ resource "aws_instance" "foo" {
272
+ instance_type = var.instance_type
273
+ }` ,
274
+ Want : "t2.micro" ,
275
+ },
263
276
}
264
277
265
- for _ , resource := range resources .Blocks {
266
- var instanceType string
267
- if err := runner .EvaluateExpr (resource .Body .Attributes ["instance_type" ].Expr , & instanceType , nil ); err != nil {
268
- t .Fatal (err )
269
- }
278
+ for _ , test := range tests {
279
+ t .Run (test .Name , func (t * testing.T ) {
280
+ runner := TestRunner (t , map [string ]string {"main.tf" : test .Src })
270
281
271
- if instanceType != "t2.micro" {
272
- t .Fatalf (`expected value is "t2.micro", but got "%s"` , instanceType )
273
- }
282
+ resources , err := runner .GetResourceContent ("aws_instance" , & hclext.BodySchema {
283
+ Attributes : []hclext.AttributeSchema {{Name : "instance_type" }},
284
+ }, nil )
285
+ if err != nil {
286
+ t .Fatal (err )
287
+ }
288
+
289
+ for _ , resource := range resources .Blocks {
290
+ var instanceType string
291
+ if err := runner .EvaluateExpr (resource .Body .Attributes ["instance_type" ].Expr , & instanceType , nil ); err != nil {
292
+ t .Fatal (err )
293
+ }
294
+
295
+ if instanceType != test .Want {
296
+ t .Fatalf (`"%s" is expected, but got "%s"` , test .Want , instanceType )
297
+ }
298
+ }
299
+ })
274
300
}
275
301
}
276
302
0 commit comments