-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4eb4eb
commit 7efdea9
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
}) | ||
} | ||
} |