Skip to content

Commit

Permalink
change to numeric functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bschaatsbergen committed Apr 21, 2024
1 parent 27ebc18 commit 5872f71
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 173 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,9 @@
## 0.5.0 (April 21, 2024)

FEATURES:

* change `between`, `contains`, `equal`, `not_equal` to numeric functions.

## 0.4.0 (April 5, 2024)

FEATURES:
Expand Down
2 changes: 1 addition & 1 deletion docs/functions/contains.md
@@ -1,6 +1,6 @@
---
page_title: "contains function - terraform-provider-assert"
subcategory: "Value Comparison Functions"
subcategory: "List Functions"
description: |-
Checks whether an element is in a list
---
Expand Down
10 changes: 5 additions & 5 deletions docs/functions/equal.md
@@ -1,8 +1,8 @@
---
page_title: "equal function - terraform-provider-assert"
subcategory: "Value Comparison Functions"
subcategory: "Numeric Functions"
description: |-
Checks whether an element is equal to another element
Checks whether a number is equal to another number
---

# function: equal
Expand Down Expand Up @@ -39,14 +39,14 @@ variable "number_of_glue_job_workers" {

<!-- signature generated by tfplugindocs -->
```text
equal(element string, compare_against string) bool
equal(number number, compare_against number) bool
```

## Arguments

<!-- arguments generated by tfplugindocs -->
1. `element` (String, Nullable) The element to compare
1. `compare_against` (String) The element to compare against
1. `number` (Number, Nullable) The number to compare
1. `compare_against` (Number) The number to compare against


## Return Type
Expand Down
2 changes: 1 addition & 1 deletion docs/functions/less_or_equal.md
Expand Up @@ -45,7 +45,7 @@ less_or_equal(number number, compare_against number) bool
## Arguments

<!-- arguments generated by tfplugindocs -->
1. `number` (Number, Nullable) The number to check
1. `number` (Number) The number to check
1. `compare_against` (Number) The number to compare against


Expand Down
10 changes: 5 additions & 5 deletions docs/functions/not_equal.md
@@ -1,8 +1,8 @@
---
page_title: "not_equal function - terraform-provider-assert"
subcategory: "Value Comparison Functions"
subcategory: "Numeric Functions"
description: |-
Checks whether an element is not equal to another element
Checks whether a number is not equal to another number
---

# function: not_equal
Expand Down Expand Up @@ -39,14 +39,14 @@ variable "rds_global_cluster_deletion_protection" {

<!-- signature generated by tfplugindocs -->
```text
not_equal(number string, compare_against string) bool
not_equal(number number, compare_against number) bool
```

## Arguments

<!-- arguments generated by tfplugindocs -->
1. `number` (String, Nullable) The element to compare
1. `compare_against` (String) The element to compare against
1. `number` (Number, Nullable) The number to compare
1. `compare_against` (Number) The number to compare against


## Return Type
Expand Down
21 changes: 11 additions & 10 deletions internal/provider/equal_function.go
Expand Up @@ -5,6 +5,7 @@ package provider

import (
"context"
"math/big"

"github.com/hashicorp/terraform-plugin-framework/function"
)
Expand All @@ -25,18 +26,18 @@ func (r EqualFunction) Metadata(_ context.Context, req function.MetadataRequest,

func (r EqualFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
Summary: "Checks whether an element is equal to another element",
Summary: "Checks whether a number is equal to another number",
Parameters: []function.Parameter{
function.StringParameter{
function.NumberParameter{
AllowNullValue: true,
AllowUnknownValues: true,
Description: "The element to compare",
Name: "element",
Description: "The number to compare",
Name: "number",
},
function.StringParameter{
function.NumberParameter{
AllowNullValue: false,
AllowUnknownValues: false,
Description: "The element to compare against",
Description: "The number to compare against",
Name: "compare_against",
},
},
Expand All @@ -45,12 +46,12 @@ func (r EqualFunction) Definition(_ context.Context, _ function.DefinitionReques
}

func (r EqualFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
var element string
var compareAgainst string
var number *big.Float
var compareAgainst *big.Float

resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &element, &compareAgainst))
resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &number, &compareAgainst))
if resp.Error != nil {
return
}
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, element == compareAgainst))
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, number.Cmp(compareAgainst) == 0))
}
108 changes: 3 additions & 105 deletions internal/provider/equal_function_test.go
Expand Up @@ -34,75 +34,6 @@ output "test" {
})
}

func TestEqualFunction_plainBool(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::assert::equal(true, true)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "true"),
),
},
},
})
}

func TestEqualFunction_comparison(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
locals {
comparison = 15 * 2 == 30
}
output "test" {
value = provider::assert::equal(local.comparison, true)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "true"),
),
},
},
})
}

func TestEqualFunction_string(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::assert::equal("hello", "hello")
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "true"),
),
},
},
})
}

func TestEqualFunction_float(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
Expand All @@ -125,28 +56,6 @@ output "test" {
})
}

func TestEqualFunction_minus(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::assert::equal(-20, -20)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "true"),
),
},
},
})
}

func TestEqualFunction_minusFloat(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
Expand Down Expand Up @@ -190,17 +99,6 @@ output "test" {
},
{
Config: `
# bool
output "test" {
value = provider::assert::equal(true, false)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "false"),
),
},
{
Config: `
# object
locals {
obj = {
Expand All @@ -212,7 +110,7 @@ output "test" {
value = provider::assert::equal(local.obj, local.obj)
}
`,
ExpectError: regexp.MustCompile("Invalid value for \"element\" parameter: string required."),
ExpectError: regexp.MustCompile("Invalid value for \"number\" parameter: number required."),
},
{
Config: `
Expand All @@ -221,7 +119,7 @@ output "test" {
value = provider::assert::equal([1, 2, 3], [1, 2, 3])
}
`,
ExpectError: regexp.MustCompile("Invalid value for \"element\" parameter: string required."),
ExpectError: regexp.MustCompile("Invalid value for \"number\" parameter: number required."),
},
{
Config: `
Expand All @@ -230,7 +128,7 @@ output "test" {
value = provider::assert::equal({ key1 = "value1", key2 = "value2" }, { key1 = "value1", key2 = "value2" })
}
`,
ExpectError: regexp.MustCompile("Invalid value for \"element\" parameter: string required."),
ExpectError: regexp.MustCompile("Invalid value for \"number\" parameter: number required."),
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/less_or_equal_function.go
Expand Up @@ -29,14 +29,14 @@ func (r LessOrEqualFunction) Definition(_ context.Context, _ function.Definition
Summary: "Checks whether a number is less than or equal to a given number",
Parameters: []function.Parameter{
function.NumberParameter{
AllowNullValue: true,
AllowNullValue: false,
AllowUnknownValues: true,
Description: "The number to check",
Name: "number",
},
function.NumberParameter{
AllowNullValue: false,
AllowUnknownValues: false,
AllowUnknownValues: true,
Description: "The number to compare against",
Name: "compare_against",
},
Expand Down
19 changes: 10 additions & 9 deletions internal/provider/not_equal_function.go
Expand Up @@ -5,6 +5,7 @@ package provider

import (
"context"
"math/big"

"github.com/hashicorp/terraform-plugin-framework/function"
)
Expand All @@ -25,18 +26,18 @@ func (r NotEqualFunction) Metadata(_ context.Context, req function.MetadataReque

func (r NotEqualFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
Summary: "Checks whether an element is not equal to another element",
Summary: "Checks whether a number is not equal to another number",
Parameters: []function.Parameter{
function.StringParameter{
function.NumberParameter{
AllowNullValue: true,
AllowUnknownValues: true,
Description: "The element to compare",
Description: "The number to compare",
Name: "number",
},
function.StringParameter{
function.NumberParameter{
AllowNullValue: false,
AllowUnknownValues: false,
Description: "The element to compare against",
Description: "The number to compare against",
Name: "compare_against",
},
},
Expand All @@ -45,12 +46,12 @@ func (r NotEqualFunction) Definition(_ context.Context, _ function.DefinitionReq
}

func (r NotEqualFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
var element string
var compareAgainst string
var number *big.Float
var compareAgainst *big.Float

resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &element, &compareAgainst))
resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &number, &compareAgainst))
if resp.Error != nil {
return
}
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, element != compareAgainst))
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, number.Cmp(compareAgainst) != 0))
}
32 changes: 0 additions & 32 deletions internal/provider/not_equal_function_test.go
Expand Up @@ -99,28 +99,6 @@ output "test" {
})
}

func TestNotEqualFunction_bool(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::assert::not_equal(true, false)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "true"),
),
},
},
})
}

func TestNotEqualFunction_falseCases(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
Expand All @@ -143,16 +121,6 @@ output "test" {
Config: `
output "test" {
value = provider::assert::not_equal(10.43234, 10.43234)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "false"),
),
},
{
Config: `
output "test" {
value = provider::assert::not_equal(true, true)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
Expand Down

0 comments on commit 5872f71

Please sign in to comment.