@@ -248,29 +248,55 @@ terraform {
248248}
249249
250250func 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 : `
252259resource "aws_instance" "foo" {
253260 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+ }
257270
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+ },
263276 }
264277
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 })
270281
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+ })
274300 }
275301}
276302
0 commit comments