Skip to content

Commit

Permalink
add compare_list function support
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Feb 25, 2025
1 parent b4eb4eb commit 7efdea9
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/functions/compare_list/function.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Returns a comparison between two lists (final element of first list is less than final element of second list):
provider::stdlib::compare_list(["foo", "bar", "b"], ["foo", "bar", "baz"])
# result: -1

# Returns a comparison between two lists (lists are equal):
provider::stdlib::compare_list(["pizza", "cake"], ["pizza", "cake"])
# result: 0

# Returns a comparison between two lists (second element of first list is greater than second element of second list):
provider::stdlib::compare_list(["super", "hyper", "turbo"], ["pizza", "cake", "punch"])
# result: 1

# Returns a comparison between two lists (lists are equal until first list has more elements):
provider::stdlib::compare_list(["pizza", "cake", "punch"], ["pizza", "cake"])
# result: 1
1 change: 1 addition & 0 deletions stdlib/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (*stdlibProvider) Functions(_ context.Context) []func() function.Function {
numberfunc.NewModFunction,
numberfunc.NewRoundFunction,
numberfunc.NewSqrtFunction,
slicefunc.NewCompareListFunction,
stringfunc.NewCutFunction,
stringfunc.NewLastCharFunction,
}
Expand Down
86 changes: 86 additions & 0 deletions stdlib/slice/compare_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package slicefunc_test

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/function"
"github.com/hashicorp/terraform-plugin-framework/types"

slicefunc "github.com/mschuchard/terraform-provider-stdlib/stdlib/slice"
)

func TestCutFunction(test *testing.T) {
test.Parallel()

standardTestCases := map[string]struct {
request function.RunRequest
expected function.RunResponse
}{
"lesser": {
request: function.RunRequest{
Arguments: function.NewArgumentsData([]attr.Value{
types.ListValueMust(types.StringType, []attr.Value{types.StringValue("foo"), types.StringValue("bar"), types.StringValue("b")}),
types.ListValueMust(types.StringType, []attr.Value{types.StringValue("foo"), types.StringValue("bar"), types.StringValue("baz")}),
}),
},
expected: function.RunResponse{
Result: function.NewResultData(types.Int32Value(-1)),
},
},
"equal": {
request: function.RunRequest{
Arguments: function.NewArgumentsData([]attr.Value{
types.ListValueMust(types.StringType, []attr.Value{types.StringValue("pizza"), types.StringValue("cake")}),
types.ListValueMust(types.StringType, []attr.Value{types.StringValue("pizza"), types.StringValue("cake")}),
}),
},
expected: function.RunResponse{
Result: function.NewResultData(types.Int32Value(0)),
},
},
"greater": {
request: function.RunRequest{
Arguments: function.NewArgumentsData([]attr.Value{
types.ListValueMust(types.StringType, []attr.Value{types.StringValue("super"), types.StringValue("hyper"), types.StringValue("turbo")}),
types.ListValueMust(types.StringType, []attr.Value{types.StringValue("pizza"), types.StringValue("cake"), types.StringValue("punch")}),
}),
},
expected: function.RunResponse{
Result: function.NewResultData(types.Int32Value(1)),
},
},
"length": {
request: function.RunRequest{
Arguments: function.NewArgumentsData([]attr.Value{
types.ListValueMust(types.StringType, []attr.Value{types.StringValue("pizza"), types.StringValue("cake"), types.StringValue("punch")}),
types.ListValueMust(types.StringType, []attr.Value{types.StringValue("pizza"), types.StringValue("cake")}),
}),
},
expected: function.RunResponse{
Result: function.NewResultData(types.Int32Value(1)),
},
},
}

for name, testCase := range standardTestCases {
test.Run(name, func(test *testing.T) {
// initialize result
result := function.RunResponse{Result: function.NewResultData(types.Int32Unknown())}

// execute function and store result
slicefunc.NewCompareListFunction().Run(context.Background(), testCase.request, &result)

// compare results
if !result.Error.Equal(testCase.expected.Error) {
test.Errorf("expected value: %s", testCase.expected.Error)
test.Errorf("actual value: %s", result.Error)
}
if !result.Result.Equal(testCase.expected.Result) {
test.Errorf("expected value: %d", testCase.expected.Result.Value())
test.Errorf("actual value: %d", result.Result.Value())
}
})
}
}

0 comments on commit 7efdea9

Please sign in to comment.