|
| 1 | +package float64validator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 10 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 11 | +) |
| 12 | + |
| 13 | +func TestValidateFloat(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + testCases := map[string]struct { |
| 17 | + request tfsdk.ValidateAttributeRequest |
| 18 | + expectedFloat64 float64 |
| 19 | + expectedOk bool |
| 20 | + }{ |
| 21 | + "invalid-type": { |
| 22 | + request: tfsdk.ValidateAttributeRequest{ |
| 23 | + AttributeConfig: types.Bool{Value: true}, |
| 24 | + AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), |
| 25 | + }, |
| 26 | + expectedFloat64: 0.0, |
| 27 | + expectedOk: false, |
| 28 | + }, |
| 29 | + "float64-null": { |
| 30 | + request: tfsdk.ValidateAttributeRequest{ |
| 31 | + AttributeConfig: types.Float64{Null: true}, |
| 32 | + AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), |
| 33 | + }, |
| 34 | + expectedFloat64: 0.0, |
| 35 | + expectedOk: false, |
| 36 | + }, |
| 37 | + "float64-value": { |
| 38 | + request: tfsdk.ValidateAttributeRequest{ |
| 39 | + AttributeConfig: types.Float64{Value: 1.2}, |
| 40 | + AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), |
| 41 | + }, |
| 42 | + expectedFloat64: 1.2, |
| 43 | + expectedOk: true, |
| 44 | + }, |
| 45 | + "float64-unknown": { |
| 46 | + request: tfsdk.ValidateAttributeRequest{ |
| 47 | + AttributeConfig: types.Float64{Unknown: true}, |
| 48 | + AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), |
| 49 | + }, |
| 50 | + expectedFloat64: 0.0, |
| 51 | + expectedOk: false, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for name, testCase := range testCases { |
| 56 | + name, testCase := name, testCase |
| 57 | + |
| 58 | + t.Run(name, func(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + |
| 61 | + gotFloat64, gotOk := validateFloat(context.Background(), testCase.request, &tfsdk.ValidateAttributeResponse{}) |
| 62 | + |
| 63 | + if diff := cmp.Diff(gotFloat64, testCase.expectedFloat64); diff != "" { |
| 64 | + t.Errorf("unexpected float64 difference: %s", diff) |
| 65 | + } |
| 66 | + |
| 67 | + if diff := cmp.Diff(gotOk, testCase.expectedOk); diff != "" { |
| 68 | + t.Errorf("unexpected ok difference: %s", diff) |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
0 commit comments