diff --git a/CHANGELOG.md b/CHANGELOG.md index 1592808..964f381 100644 --- a/CHANGELOG.md +++ b/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: diff --git a/docs/functions/contains.md b/docs/functions/contains.md index ec5b168..57c38cf 100644 --- a/docs/functions/contains.md +++ b/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 --- diff --git a/docs/functions/equal.md b/docs/functions/equal.md index 340656f..83ca623 100644 --- a/docs/functions/equal.md +++ b/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 @@ -39,14 +39,14 @@ variable "number_of_glue_job_workers" { ```text -equal(element string, compare_against string) bool +equal(number number, compare_against number) bool ``` ## Arguments -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 diff --git a/docs/functions/less_or_equal.md b/docs/functions/less_or_equal.md index c8c611f..ed68cf5 100644 --- a/docs/functions/less_or_equal.md +++ b/docs/functions/less_or_equal.md @@ -45,7 +45,7 @@ less_or_equal(number number, compare_against number) bool ## Arguments -1. `number` (Number, Nullable) The number to check +1. `number` (Number) The number to check 1. `compare_against` (Number) The number to compare against diff --git a/docs/functions/not_equal.md b/docs/functions/not_equal.md index c6eff60..6777edf 100644 --- a/docs/functions/not_equal.md +++ b/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 @@ -39,14 +39,14 @@ variable "rds_global_cluster_deletion_protection" { ```text -not_equal(number string, compare_against string) bool +not_equal(number number, compare_against number) bool ``` ## Arguments -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 diff --git a/internal/provider/equal_function.go b/internal/provider/equal_function.go index 2efa05f..1219a61 100644 --- a/internal/provider/equal_function.go +++ b/internal/provider/equal_function.go @@ -5,6 +5,7 @@ package provider import ( "context" + "math/big" "github.com/hashicorp/terraform-plugin-framework/function" ) @@ -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", }, }, @@ -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)) } diff --git a/internal/provider/equal_function_test.go b/internal/provider/equal_function_test.go index da24aa5..14ff816 100644 --- a/internal/provider/equal_function_test.go +++ b/internal/provider/equal_function_test.go @@ -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{ @@ -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{ @@ -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 = { @@ -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: ` @@ -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: ` @@ -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."), }, }, }) diff --git a/internal/provider/less_or_equal_function.go b/internal/provider/less_or_equal_function.go index b36ff75..b5559fa 100644 --- a/internal/provider/less_or_equal_function.go +++ b/internal/provider/less_or_equal_function.go @@ -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", }, diff --git a/internal/provider/not_equal_function.go b/internal/provider/not_equal_function.go index 3c6e3a6..e7daab6 100644 --- a/internal/provider/not_equal_function.go +++ b/internal/provider/not_equal_function.go @@ -5,6 +5,7 @@ package provider import ( "context" + "math/big" "github.com/hashicorp/terraform-plugin-framework/function" ) @@ -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", }, }, @@ -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)) } diff --git a/internal/provider/not_equal_function_test.go b/internal/provider/not_equal_function_test.go index 08a4f23..e9b2096 100644 --- a/internal/provider/not_equal_function_test.go +++ b/internal/provider/not_equal_function_test.go @@ -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{ @@ -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( diff --git a/templates/functions/contains.md.tmpl b/templates/functions/contains.md.tmpl index 9a14d88..77f4af6 100644 --- a/templates/functions/contains.md.tmpl +++ b/templates/functions/contains.md.tmpl @@ -1,6 +1,6 @@ --- page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}" -subcategory: "Value Comparison Functions" +subcategory: "List Functions" description: |- {{ .Summary | plainmarkdown | trimspace | prefixlines " " }} --- diff --git a/templates/functions/equal.md.tmpl b/templates/functions/equal.md.tmpl index 9a14d88..16f543b 100644 --- a/templates/functions/equal.md.tmpl +++ b/templates/functions/equal.md.tmpl @@ -1,6 +1,6 @@ --- page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}" -subcategory: "Value Comparison Functions" +subcategory: "Numeric Functions" description: |- {{ .Summary | plainmarkdown | trimspace | prefixlines " " }} --- diff --git a/templates/functions/not_equal.md.tmpl b/templates/functions/not_equal.md.tmpl index 9a14d88..16f543b 100644 --- a/templates/functions/not_equal.md.tmpl +++ b/templates/functions/not_equal.md.tmpl @@ -1,6 +1,6 @@ --- page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}" -subcategory: "Value Comparison Functions" +subcategory: "Numeric Functions" description: |- {{ .Summary | plainmarkdown | trimspace | prefixlines " " }} ---