Skip to content

Commit

Permalink
add an ExprMap method to the mock expression
Browse files Browse the repository at this point in the history
Add an ExprMethod so that the mock expressions can be handled by the
hcl.ExprMap function.

Added basic tests for both ExprList and ExprMap
  • Loading branch information
jbardin committed Feb 8, 2021
1 parent 41e4082 commit 94baef9
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
18 changes: 18 additions & 0 deletions hcltest/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@ func (e mockExprLiteral) ExprList() []hcl.Expression {
return nil
}

// Implementation for hcl.ExprMap
func (e mockExprLiteral) ExprMap() []hcl.KeyValuePair {
v := e.V
ty := v.Type()
if v.IsKnown() && !v.IsNull() && (ty.IsObjectType() || ty.IsMapType()) {
ret := make([]hcl.KeyValuePair, 0, v.LengthInt())
for it := v.ElementIterator(); it.Next(); {
k, v := it.Element()
ret = append(ret, hcl.KeyValuePair{
Key: MockExprLiteral(k),
Value: MockExprLiteral(v),
})
}
return ret
}
return nil
}

// MockExprVariable returns a hcl.Expression that evaluates to the value of
// the variable with the given name.
func MockExprVariable(name string) hcl.Expression {
Expand Down
127 changes: 127 additions & 0 deletions hcltest/mock_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hcltest

import (
"strings"
"testing"

"reflect"
Expand Down Expand Up @@ -266,3 +267,129 @@ func TestMockBodyPartialContent(t *testing.T) {
})
}
}

func TestExprList(t *testing.T) {
tests := map[string]struct {
In hcl.Expression
Want []hcl.Expression
Diags string
}{
"as list": {
In: MockExprLiteral(cty.ListVal([]cty.Value{
cty.StringVal("foo"),
cty.StringVal("bar"),
})),
Want: []hcl.Expression{
MockExprLiteral(cty.StringVal("foo")),
MockExprLiteral(cty.StringVal("bar")),
},
},
"as tuple": {
In: MockExprLiteral(cty.TupleVal([]cty.Value{
cty.StringVal("foo"),
cty.StringVal("bar"),
})),
Want: []hcl.Expression{
MockExprLiteral(cty.StringVal("foo")),
MockExprLiteral(cty.StringVal("bar")),
},
},
"not list": {
In: MockExprLiteral(cty.ObjectVal(map[string]cty.Value{
"a": cty.StringVal("foo"),
"b": cty.StringVal("bar"),
})),
Want: nil,
Diags: "list expression is required",
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got, diags := hcl.ExprList(tc.In)
if tc.Diags != "" {
if diags.HasErrors() && !strings.Contains(diags.Error(), tc.Diags) {
t.Errorf("expected error %q, got %q", tc.Diags, diags)
}
if !diags.HasErrors() {
t.Errorf("expected diagnostic message %q", tc.Diags)
}
} else if diags.HasErrors() {
t.Error(diags)
}

if !reflect.DeepEqual(got, tc.Want) {
t.Errorf("incorrect expression,\ngot: %#v\nwant: %#v", got, tc.Want)
}
})
}
}

func TestExprMap(t *testing.T) {
tests := map[string]struct {
In hcl.Expression
Want []hcl.KeyValuePair
Diags string
}{
"as object": {
In: MockExprLiteral(cty.ObjectVal(map[string]cty.Value{
"name": cty.StringVal("test"),
"count": cty.NumberIntVal(2),
})),
Want: []hcl.KeyValuePair{
{
Key: MockExprLiteral(cty.StringVal("count")),
Value: MockExprLiteral(cty.NumberIntVal(2)),
},
{
Key: MockExprLiteral(cty.StringVal("name")),
Value: MockExprLiteral(cty.StringVal("test")),
},
},
},
"as map": {
In: MockExprLiteral(cty.MapVal(map[string]cty.Value{
"name": cty.StringVal("test"),
"version": cty.StringVal("2.0.0"),
})),
Want: []hcl.KeyValuePair{
{
Key: MockExprLiteral(cty.StringVal("name")),
Value: MockExprLiteral(cty.StringVal("test")),
},
{
Key: MockExprLiteral(cty.StringVal("version")),
Value: MockExprLiteral(cty.StringVal("2.0.0")),
},
},
},
"not map": {
In: MockExprLiteral(cty.ListVal([]cty.Value{
cty.StringVal("foo"),
cty.StringVal("bar"),
})),
Want: nil,
Diags: "map expression is required",
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got, diags := hcl.ExprMap(tc.In)
if tc.Diags != "" {
if diags.HasErrors() && !strings.Contains(diags.Error(), tc.Diags) {
t.Errorf("expected error %q, got %q", tc.Diags, diags)
}
if !diags.HasErrors() {
t.Errorf("expected diagnostic message %q", tc.Diags)
}
} else if diags.HasErrors() {
t.Error(diags)
}

if !reflect.DeepEqual(got, tc.Want) {
t.Errorf("incorrect expression,\ngot: %#v\nwant: %#v", got, tc.Want)
}
})
}
}

0 comments on commit 94baef9

Please sign in to comment.