Skip to content

Commit ac2ce0a

Browse files
authored
knownvalue: Add Int32Exact and Float32Exact checks (#356)
* Add `float32exact` and `int32exact` types * Update website documentation * Add changelog entries * Update changelog wording
1 parent 1589250 commit ac2ce0a

File tree

13 files changed

+361
-1
lines changed

13 files changed

+361
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: ENHANCEMENTS
2+
body: 'knownvalue: Add `Int32Exact` check for int32 value testing.'
3+
time: 2024-06-26T16:32:40.387821-04:00
4+
custom:
5+
Issue: "356"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: ENHANCEMENTS
2+
body: 'knownvalue: Add `Float32Exact` check for float32 value testing.'
3+
time: 2024-06-26T16:33:11.495969-04:00
4+
custom:
5+
Issue: "356"

knownvalue/float32.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package knownvalue
5+
6+
import (
7+
"encoding/json"
8+
"fmt"
9+
"strconv"
10+
)
11+
12+
var _ Check = float32Exact{}
13+
14+
type float32Exact struct {
15+
value float32
16+
}
17+
18+
// CheckValue determines whether the passed value is of type float32, and
19+
// contains a matching float32 value.
20+
func (v float32Exact) CheckValue(other any) error {
21+
jsonNum, ok := other.(json.Number)
22+
23+
if !ok {
24+
return fmt.Errorf("expected json.Number value for Float32Exact check, got: %T", other)
25+
}
26+
27+
otherVal, err := strconv.ParseFloat(string(jsonNum), 32)
28+
29+
if err != nil {
30+
return fmt.Errorf("expected json.Number to be parseable as float32 value for Float32Exact check: %s", err)
31+
}
32+
33+
if float32(otherVal) != v.value {
34+
return fmt.Errorf("expected value %s for Float32Exact check, got: %s", v.String(), strconv.FormatFloat(otherVal, 'f', -1, 32))
35+
}
36+
37+
return nil
38+
}
39+
40+
// String returns the string representation of the float32 value.
41+
func (v float32Exact) String() string {
42+
return strconv.FormatFloat(float64(v.value), 'f', -1, 32)
43+
}
44+
45+
// Float32Exact returns a Check for asserting equality between the
46+
// supplied float32 and the value passed to the CheckValue method.
47+
func Float32Exact(value float32) float32Exact {
48+
return float32Exact{
49+
value: value,
50+
}
51+
}

knownvalue/float32_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package knownvalue_test
5+
6+
import (
7+
"encoding/json"
8+
"fmt"
9+
"testing"
10+
11+
"github.com/google/go-cmp/cmp"
12+
13+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
14+
)
15+
16+
func TestFloat32Value_CheckValue(t *testing.T) {
17+
t.Parallel()
18+
19+
testCases := map[string]struct {
20+
self knownvalue.Check
21+
other any
22+
expectedError error
23+
}{
24+
"zero-nil": {
25+
self: knownvalue.Float32Exact(0),
26+
expectedError: fmt.Errorf("expected json.Number value for Float32Exact check, got: <nil>"),
27+
},
28+
"zero-other": {
29+
self: knownvalue.Float32Exact(0),
30+
other: json.Number("0.0"), // checking against the underlying value field zero-value
31+
},
32+
"nil": {
33+
self: knownvalue.Float32Exact(1.234),
34+
expectedError: fmt.Errorf("expected json.Number value for Float32Exact check, got: <nil>"),
35+
},
36+
"wrong-type": {
37+
self: knownvalue.Float32Exact(1.234),
38+
other: json.Number("str"),
39+
expectedError: fmt.Errorf("expected json.Number to be parseable as float32 value for Float32Exact check: strconv.ParseFloat: parsing \"str\": invalid syntax"),
40+
},
41+
"not-equal": {
42+
self: knownvalue.Float32Exact(1.234),
43+
other: json.Number("4.321"),
44+
expectedError: fmt.Errorf("expected value 1.234 for Float32Exact check, got: 4.321"),
45+
},
46+
"equal": {
47+
self: knownvalue.Float32Exact(1.234),
48+
other: json.Number("1.234"),
49+
},
50+
}
51+
52+
for name, testCase := range testCases {
53+
name, testCase := name, testCase
54+
55+
t.Run(name, func(t *testing.T) {
56+
t.Parallel()
57+
58+
got := testCase.self.CheckValue(testCase.other)
59+
60+
if diff := cmp.Diff(got, testCase.expectedError, equateErrorMessage); diff != "" {
61+
t.Errorf("unexpected difference: %s", diff)
62+
}
63+
})
64+
}
65+
}
66+
67+
func TestFloat32Value_String(t *testing.T) {
68+
t.Parallel()
69+
70+
got := knownvalue.Float32Exact(1.234567890123e+03).String()
71+
72+
if diff := cmp.Diff(got, "1234.5679"); diff != "" {
73+
t.Errorf("unexpected difference: %s", diff)
74+
}
75+
}

knownvalue/int32.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package knownvalue
5+
6+
import (
7+
"encoding/json"
8+
"fmt"
9+
"strconv"
10+
)
11+
12+
var _ Check = int32Exact{}
13+
14+
type int32Exact struct {
15+
value int32
16+
}
17+
18+
// CheckValue determines whether the passed value is of type int32, and
19+
// contains a matching int32 value.
20+
func (v int32Exact) CheckValue(other any) error {
21+
jsonNum, ok := other.(json.Number)
22+
23+
if !ok {
24+
return fmt.Errorf("expected json.Number value for Int32Exact check, got: %T", other)
25+
}
26+
27+
otherVal, err := strconv.ParseInt(string(jsonNum), 10, 32)
28+
29+
if err != nil {
30+
return fmt.Errorf("expected json.Number to be parseable as int32 value for Int32Exact check: %s", err)
31+
}
32+
33+
if int32(otherVal) != v.value {
34+
return fmt.Errorf("expected value %d for Int32Exact check, got: %d", v.value, otherVal)
35+
}
36+
37+
return nil
38+
}
39+
40+
// String returns the string representation of the int32 value.
41+
func (v int32Exact) String() string {
42+
return strconv.FormatInt(int64(v.value), 10)
43+
}
44+
45+
// Int32Exact returns a Check for asserting equality between the
46+
// supplied int32 and the value passed to the CheckValue method.
47+
func Int32Exact(value int32) int32Exact {
48+
return int32Exact{
49+
value: value,
50+
}
51+
}

knownvalue/int32_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package knownvalue_test
5+
6+
import (
7+
"encoding/json"
8+
"fmt"
9+
"testing"
10+
11+
"github.com/google/go-cmp/cmp"
12+
13+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
14+
)
15+
16+
func TestInt32Value_CheckValue(t *testing.T) {
17+
t.Parallel()
18+
19+
testCases := map[string]struct {
20+
self knownvalue.Check
21+
other any
22+
expectedError error
23+
}{
24+
"zero-nil": {
25+
self: knownvalue.Int32Exact(0),
26+
expectedError: fmt.Errorf("expected json.Number value for Int32Exact check, got: <nil>"),
27+
},
28+
"zero-other": {
29+
self: knownvalue.Int32Exact(0),
30+
other: json.Number("0"), // checking against the underlying value field zero-value
31+
},
32+
"nil": {
33+
self: knownvalue.Int32Exact(1234),
34+
expectedError: fmt.Errorf("expected json.Number value for Int32Exact check, got: <nil>"),
35+
},
36+
"wrong-type": {
37+
self: knownvalue.Int32Exact(1234),
38+
other: json.Number("str"),
39+
expectedError: fmt.Errorf("expected json.Number to be parseable as int32 value for Int32Exact check: strconv.ParseInt: parsing \"str\": invalid syntax"),
40+
},
41+
"not-equal": {
42+
self: knownvalue.Int32Exact(1234),
43+
other: json.Number("4321"),
44+
expectedError: fmt.Errorf("expected value 1234 for Int32Exact check, got: 4321"),
45+
},
46+
"equal": {
47+
self: knownvalue.Int32Exact(1234),
48+
other: json.Number("1234"),
49+
},
50+
}
51+
52+
for name, testCase := range testCases {
53+
name, testCase := name, testCase
54+
55+
t.Run(name, func(t *testing.T) {
56+
t.Parallel()
57+
58+
got := testCase.self.CheckValue(testCase.other)
59+
60+
if diff := cmp.Diff(got, testCase.expectedError, equateErrorMessage); diff != "" {
61+
t.Errorf("unexpected difference: %s", diff)
62+
}
63+
})
64+
}
65+
}
66+
67+
func TestInt32Value_String(t *testing.T) {
68+
t.Parallel()
69+
70+
got := knownvalue.Int32Exact(123456789).String()
71+
72+
if diff := cmp.Diff(got, "123456789"); diff != "" {
73+
t.Errorf("unexpected difference: %s", diff)
74+
}
75+
}

website/data/plugin-testing-nav-data.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,18 @@
8686
"title": "Custom",
8787
"path": "acceptance-tests/known-value-checks/custom"
8888
},
89+
{
90+
"title": "Float32",
91+
"path": "acceptance-tests/known-value-checks/float32"
92+
},
8993
{
9094
"title": "Float64",
9195
"path": "acceptance-tests/known-value-checks/float64"
9296
},
97+
{
98+
"title": "Int32",
99+
"path": "acceptance-tests/known-value-checks/int32"
100+
},
93101
{
94102
"title": "Int64",
95103
"path": "acceptance-tests/known-value-checks/int64"

website/docs/plugin/testing/acceptance-tests/known-value-checks/custom.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ The `other` parameter passed to the `CheckValue` method is one of the following
6363
Refer to the following built-in known value checks for implementations that handle the different types that can be passed to the `CheckValue` method in the `other` parameter:
6464

6565
* [Bool](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Bool)
66+
* [Float32Exact](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Float32Exact)
6667
* [Float64Exact](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Float64Exact)
68+
* [Int32Exact](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Int32Exact)
6769
* [Int64Exact](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Int64Exact)
6870
* [ListExact](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#ListExact)
6971
* [MapExact](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#MapExact)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
page_title: 'Plugin Development - Acceptance Testing: Known Values'
3+
description: >-
4+
Float32 Value Checks for use with Plan Checks.
5+
---
6+
7+
# Float32 Known Value Checks
8+
9+
The known value checks that are available for float32 values are:
10+
11+
* [Float32Exact](/terraform/plugin/testing/acceptance-tests/known-value-checks/float32#float32exact-check)
12+
13+
## `Float32Exact` Check
14+
15+
The [Float32Exact](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Float32Exact) check tests that a resource attribute, or output value has an exactly matching float32 value.
16+
17+
Example usage of [Float32Exact](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Float32Exact) in an [ExpectKnownValue](/terraform/plugin/testing/acceptance-tests/plan-checks/resource) plan check.
18+
19+
```go
20+
func TestExpectKnownValue_CheckPlan_Float32(t *testing.T) {
21+
t.Parallel()
22+
23+
resource.Test(t, resource.TestCase{
24+
// Provider definition omitted.
25+
Steps: []resource.TestStep{
26+
{
27+
// Example resource containing a computed float32 attribute named "computed_attribute"
28+
Config: `resource "test_resource" "one" {}`,
29+
ConfigPlanChecks: resource.ConfigPlanChecks{
30+
PreApply: []plancheck.PlanCheck{
31+
plancheck.ExpectKnownValue(
32+
"test_resource.one",
33+
tfjsonpath.New("computed_attribute"),
34+
knownvalue.Float32Exact(1.23),
35+
),
36+
},
37+
},
38+
},
39+
},
40+
})
41+
}
42+
```

website/docs/plugin/testing/acceptance-tests/known-value-checks/index.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ The following table shows the correspondence between [knownvalue.Check](https://
3939
| Known Value Check Type | Framework Attribute Type | SDKv2 Attribute Type |
4040
|------------------------------------------------------------------------------------------------------|---------------------------|----------------------|
4141
| [Bool Known Value Checks](/terraform/plugin/testing/acceptance-tests/known-value-checks/bool) | `schema.BoolAttribute` | `schema.TypeBool` |
42+
| [Float32 Known Value Checks](/terraform/plugin/testing/acceptance-tests/known-value-checks/float32) | `schema.Float32Attribute` | `schema.TypeFloat` |
4243
| [Float64 Known Value Checks](/terraform/plugin/testing/acceptance-tests/known-value-checks/float64) | `schema.Float64Attribute` | `schema.TypeFloat` |
44+
| [Int32 Known Value Checks](/terraform/plugin/testing/acceptance-tests/known-value-checks/int32) | `schema.Int32Attribute` | `schema.TypeInt` |
4345
| [Int64 Known Value Checks](/terraform/plugin/testing/acceptance-tests/known-value-checks/int64) | `schema.Int64Attribute` | `schema.TypeInt` |
4446
| [List Known Value Checks](/terraform/plugin/testing/acceptance-tests/known-value-checks/list) | `schema.ListAttribute` | `schema.TypeList` |
4547
| [Map Known Value Checks](/terraform/plugin/testing/acceptance-tests/known-value-checks/map) | `schema.MapAttribute` | `schema.TypeMap` |
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
page_title: 'Plugin Development - Acceptance Testing: Known Values'
3+
description: >-
4+
Int32 Value Checks for use with Plan Checks.
5+
---
6+
7+
# Int32 Known Value Checks
8+
9+
The known value checks that are available for int32 values are:
10+
11+
* [Int32Exact](/terraform/plugin/testing/acceptance-tests/known-value-checks/int32#int32exact-check)
12+
13+
## `Int32Exact` Check
14+
15+
The [Int32Exact](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Int32Exact) check tests that a resource attribute, or output value has an exactly matching int32 value.
16+
17+
Example usage of [Int32Exact](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/knownvalue#Int32Exact) in an [ExpectKnownValue](/terraform/plugin/testing/acceptance-tests/plan-checks/resource) plan check.
18+
19+
```go
20+
func TestExpectKnownValue_CheckPlan_Int32(t *testing.T) {
21+
t.Parallel()
22+
23+
resource.Test(t, resource.TestCase{
24+
// Provider definition omitted.
25+
Steps: []resource.TestStep{
26+
{
27+
// Example resource containing a computed int32 attribute named "computed_attribute"
28+
Config: `resource "test_resource" "one" {}`,
29+
ConfigPlanChecks: resource.ConfigPlanChecks{
30+
PreApply: []plancheck.PlanCheck{
31+
plancheck.ExpectKnownValue(
32+
"test_resource.one",
33+
tfjsonpath.New("computed_attribute"),
34+
knownvalue.Int32Exact(123),
35+
),
36+
},
37+
},
38+
},
39+
},
40+
})
41+
}
42+
```

0 commit comments

Comments
 (0)