diff --git a/examples/functions/mod/function.tf b/examples/functions/mod/function.tf new file mode 100644 index 0000000..af248b6 --- /dev/null +++ b/examples/functions/mod/function.tf @@ -0,0 +1,11 @@ +# Return the remainder of 4 / 2: +provider::stdlib::mod(4, 2) +# result: 0 + +# Return the remainder of 5 / 3: +provider::stdlib::mod(5, 3) +# result: 2 + +# Return the remainder of 10 / 4.75: +provider::stdlib::mod(10, 4.75) +# result: 0.5 \ No newline at end of file diff --git a/stdlib/number/mod_test.go b/stdlib/number/mod_test.go new file mode 100644 index 0000000..ca89e3c --- /dev/null +++ b/stdlib/number/mod_test.go @@ -0,0 +1,66 @@ +package numberfunc_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" + + numberfunc "github.com/mschuchard/terraform-provider-stdlib/stdlib/number" +) + +func TestModFunction(test *testing.T) { + test.Parallel() + + standardTestCases := map[string]struct { + request function.RunRequest + expected function.RunResponse + }{ + "zero": { + request: function.RunRequest{ + Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(4), types.Float64Value(2)}), + }, + expected: function.RunResponse{ + Result: function.NewResultData(types.Float64Value(0)), + }, + }, + "integer": { + request: function.RunRequest{ + Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(5), types.Float64Value(3)}), + }, + expected: function.RunResponse{ + Result: function.NewResultData(types.Float64Value(2)), + }, + }, + "float": { + request: function.RunRequest{ + Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(10), types.Float64Value(4.75)}), + }, + expected: function.RunResponse{ + Result: function.NewResultData(types.Float64Value(0.5)), + }, + }, + } + + for name, testCase := range standardTestCases { + test.Run(name, func(test *testing.T) { + // initialize result + result := function.RunResponse{Result: function.NewResultData(types.Float64Unknown())} + + // execute function and store result + numberfunc.NewModFunction().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: %+q", testCase.expected.Result.Value()) + test.Errorf("actual value: %+q", result.Result.Value()) + } + }) + } +} diff --git a/stdlib/provider.go b/stdlib/provider.go index d0d21d0..7292e92 100644 --- a/stdlib/provider.go +++ b/stdlib/provider.go @@ -89,6 +89,7 @@ func (*stdlibProvider) DataSources(_ context.Context) []func() datasource.DataSo func (*stdlibProvider) Functions(_ context.Context) []func() function.Function { return []func() function.Function{ numberfunc.NewExpFunction, + numberfunc.NewModFunction, stringfunc.NewCutFunction, stringfunc.NewLastCharFunction, }