Skip to content

Commit

Permalink
add modulus function support
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Jan 29, 2025
1 parent 0764ace commit 6bc2fa4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/functions/mod/function.tf
Original file line number Diff line number Diff line change
@@ -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
66 changes: 66 additions & 0 deletions stdlib/number/mod_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
})
}
}
1 change: 1 addition & 0 deletions stdlib/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down

0 comments on commit 6bc2fa4

Please sign in to comment.