Skip to content

Commit

Permalink
add round function support
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Sep 11, 2024
1 parent 9c052dd commit 9e639f5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### 1.5.0 (Next)
- Do not coerce `number` type ID to `string` type ID (may cause superficial plan changes to existing states).
- Add `exp` and `mod` functions.
- Add `exp`, `mod`, and `round` functions.

### 1.4.1
- Add `sorted` parameter to `list_index` function.
Expand Down
17 changes: 17 additions & 0 deletions examples/data-sources/stdlib_round/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Return the rounding of of 1.2
data "stdlib_round" "down" {
param = 1.2
}
# => 1

# Return the rounding of 1.8
data "stdlib_round" "up" {
param = 1.8
}
# => 2

# Return the rounding of 1.5
data "stdlib_round" "half" {
param = 1.5
}
# => 2
54 changes: 54 additions & 0 deletions stdlib/number/round_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package numberfunc_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"

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

func TestAccRound(test *testing.T) {
// invoke test
resource.ParallelTest(test, resource.TestCase{
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// test 1.2 round down
{
Config: `data "stdlib_round" "test" { param = 1.2 }`,
Check: resource.ComposeAggregateTestCheckFunc(
// verify input param is stored correctly
resource.TestCheckResourceAttr("data.stdlib_round.test", "param", "1.2"),
// verify rounding result is stored correctly
resource.TestCheckResourceAttr("data.stdlib_round.test", "result", "1"),
// verify id stored correctly
resource.TestCheckResourceAttr("data.stdlib_round.test", "id", "1.2"),
),
},
// test 1.8 round up
{
Config: `data "stdlib_round" "test" { param = 1.8 }`,
Check: resource.ComposeAggregateTestCheckFunc(
// verify input param is stored correctly
resource.TestCheckResourceAttr("data.stdlib_round.test", "param", "1.8"),
// verify rounding result is stored correctly
resource.TestCheckResourceAttr("data.stdlib_round.test", "result", "2"),
// verify id stored correctly
resource.TestCheckResourceAttr("data.stdlib_round.test", "id", "1.8"),
),
},
// test 1.5 round up
{
Config: `data "stdlib_round" "test" { param = 1.5 }`,
Check: resource.ComposeAggregateTestCheckFunc(
// verify input param is stored correctly
resource.TestCheckResourceAttr("data.stdlib_round.test", "param", "1.5"),
// verify rounding result is stored correctly
resource.TestCheckResourceAttr("data.stdlib_round.test", "result", "2"),
// verify id stored correctly
resource.TestCheckResourceAttr("data.stdlib_round.test", "id", "1.5"),
),
},
},
})
}
1 change: 1 addition & 0 deletions stdlib/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (_ *stdlibProvider) DataSources(_ context.Context) []func() datasource.Data
multiple.NewEmptyDataSource,
numberfunc.NewExpDataSource,
numberfunc.NewModDataSource,
numberfunc.NewRoundDataSource,
slicefunc.NewCompareListDataSource,
slicefunc.NewInsertDataSource,
slicefunc.NewLastElementDataSource,
Expand Down

0 comments on commit 9e639f5

Please sign in to comment.