Skip to content

Commit

Permalink
add error tests for slice funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Oct 10, 2024
1 parent b834333 commit 151db2a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 1.5.1 (Next)
- Refine attribute error messages.

### 1.5.0
- Do not coerce `number` type ID to `string` type ID (may cause superficial plan changes to existing states).
- Add `exp`, `mod`, `round`, and `sqrt` functions.
Expand Down
10 changes: 10 additions & 0 deletions stdlib/slice/insert_data_source_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slicefunc_test

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand All @@ -13,6 +14,15 @@ func TestAccInsert(test *testing.T) {
resource.ParallelTest(test, resource.TestCase{
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// test error for out of range index
{
Config: `data "stdlib_insert" "test" {
list_param = ["one", "two", "three"]
insert_values = ["zero"]
index = 5
}`,
ExpectError: regexp.MustCompile("The index at which to insert the values cannot be greater than"),
},
// test list values insert
{
Config: `data "stdlib_insert" "test" {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/slice/last_element_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (_ *lastElementDataSource) Read(ctx context.Context, req datasource.ReadReq
resp.Diagnostics.AddAttributeError(
path.Root("num_elements"),
"Invalid Value",
"The number of terminating characters to return must be fewer than the length of the input string parameter.",
"The number of terminating elements to return must be fewer than the length of the input list parameter.",
)
return
}
Expand Down
11 changes: 10 additions & 1 deletion stdlib/slice/last_element_data_source_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slicefunc_test

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand All @@ -13,6 +14,14 @@ func TestAccLastElement(test *testing.T) {
resource.ParallelTest(test, resource.TestCase{
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// test error on invalid number of terminating elements
{
Config: `data "stdlib_last_element" "test" {
param = ["h", "e", "l", "l", "o"]
num_elements = 10
}`,
ExpectError: regexp.MustCompile("The number of terminating elements to return must be fewer than"),
},
// test basic list slice last element
{
Config: `data "stdlib_last_element" "test" { param = ["h", "e", "l", "l", "o"] }`,
Expand All @@ -31,7 +40,7 @@ func TestAccLastElement(test *testing.T) {
Config: `data "stdlib_last_element" "test" {
param = ["h", "e", "l", "l", "o"]
num_elements = 3
}`,
}`,
Check: resource.ComposeAggregateTestCheckFunc(
// verify input param is stored correctly
resource.TestCheckResourceAttr("data.stdlib_last_element.test", "param.#", "5"),
Expand Down
2 changes: 1 addition & 1 deletion stdlib/slice/replace_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (_ *replaceDataSource) ValidateConfig(ctx context.Context, req datasource.V
resp.Diagnostics.AddAttributeError(
path.Root("endIndex"),
"Invalid Value",
"The index at which to replace the values added to the length of the replacement values cannot be greater than the length of the list where the values will be replaced as that would be out of range.",
"The index at which to replace the values added to the length of the replacement values (i.e. 'endIndex') cannot be greater than the length of the list where the values will be replaced as that would be out of range.",
)
}
}
Expand Down
10 changes: 10 additions & 0 deletions stdlib/slice/replace_data_source_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slicefunc_test

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand All @@ -13,6 +14,15 @@ func TestAccReplace(test *testing.T) {
resource.ParallelTest(test, resource.TestCase{
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// test error on out of range index
{
Config: `data "stdlib_replace" "test" {
list_param = ["foo", "bar", "two", "three"]
replace_values = ["zero", "one"]
index = 3
}`,
ExpectError: regexp.MustCompile("The index at which to replace the values added to the length"),
},
// test list values replace
{
Config: `data "stdlib_replace" "test" {
Expand Down

0 comments on commit 151db2a

Please sign in to comment.