adding some test cases and tweaking implementation to address them

This commit is contained in:
Kristin Laemmert 2018-05-25 16:30:50 -07:00 committed by Martin Atkins
parent 0cbcd75ebb
commit 498ffbf77b
2 changed files with 34 additions and 2 deletions

View File

@ -113,7 +113,7 @@ var CoalesceListFunc = function.New(&function.Spec{
Params: []function.Parameter{},
VarParam: &function.Parameter{
Name: "vals",
Type: cty.DynamicPseudoType,
Type: cty.List(cty.DynamicPseudoType),
AllowUnknown: true,
AllowDynamicType: true,
AllowNull: true,
@ -170,7 +170,6 @@ var CompactFunc = function.New(&function.Spec{
},
Type: function.StaticReturnType(cty.List(cty.String)),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
var outputList []cty.Value
for it := args[0].ElementIterator(); it.Next(); {
@ -180,6 +179,11 @@ var CompactFunc = function.New(&function.Spec{
}
outputList = append(outputList, v)
}
if len(outputList) == 0 {
return cty.ListValEmpty(cty.String), nil
}
return cty.ListVal(outputList), nil
},
})
@ -223,6 +227,10 @@ var IndexFunc = function.New(&function.Spec{
},
Type: function.StaticReturnType(cty.Number),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
if !(args[0].Type().IsListType() || args[0].Type().IsTupleType()) {
return cty.NilVal, fmt.Errorf("argument must be a list or tuple")
}
if args[0].LengthInt() == 0 { // Easy path
return cty.NilVal, fmt.Errorf("Cannot search an empty list")
}

View File

@ -299,6 +299,16 @@ func TestCoalesceList(t *testing.T) {
}),
false,
},
{
[]cty.Value{
cty.MapValEmpty(cty.DynamicPseudoType),
cty.ListVal([]cty.Value{
cty.StringVal("third"), cty.StringVal("fourth"),
}),
},
cty.NilVal,
true,
},
}
for _, test := range tests {
@ -339,6 +349,20 @@ func TestCompact(t *testing.T) {
}),
false,
},
{
cty.ListVal([]cty.Value{
cty.StringVal(""),
cty.StringVal(""),
cty.StringVal(""),
}),
cty.ListValEmpty(cty.String),
false,
},
{
cty.ListValEmpty(cty.String),
cty.ListValEmpty(cty.String),
false,
},
{
cty.ListVal([]cty.Value{
cty.StringVal("test"),