|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package float32validator_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 15 | + |
| 16 | + "github.com/hashicorp/terraform-plugin-framework-validators/float32validator" |
| 17 | +) |
| 18 | + |
| 19 | +func TestAllValidatorValidateFloat32(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + type testCase struct { |
| 23 | + val types.Float32 |
| 24 | + validators []validator.Float32 |
| 25 | + expected diag.Diagnostics |
| 26 | + } |
| 27 | + tests := map[string]testCase{ |
| 28 | + "invalid": { |
| 29 | + val: types.Float32Value(1.2), |
| 30 | + validators: []validator.Float32{ |
| 31 | + float32validator.AtLeast(3), |
| 32 | + float32validator.AtLeast(5), |
| 33 | + }, |
| 34 | + expected: diag.Diagnostics{ |
| 35 | + diag.NewAttributeErrorDiagnostic( |
| 36 | + path.Root("test"), |
| 37 | + "Invalid Attribute Value", |
| 38 | + "Attribute test value must be at least 3.000000, got: 1.200000", |
| 39 | + ), |
| 40 | + diag.NewAttributeErrorDiagnostic( |
| 41 | + path.Root("test"), |
| 42 | + "Invalid Attribute Value", |
| 43 | + "Attribute test value must be at least 5.000000, got: 1.200000", |
| 44 | + ), |
| 45 | + }, |
| 46 | + }, |
| 47 | + "valid": { |
| 48 | + val: types.Float32Value(1.2), |
| 49 | + validators: []validator.Float32{ |
| 50 | + float32validator.AtLeast(0), |
| 51 | + float32validator.AtLeast(1), |
| 52 | + }, |
| 53 | + expected: nil, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + for name, test := range tests { |
| 58 | + name, test := name, test |
| 59 | + t.Run(name, func(t *testing.T) { |
| 60 | + t.Parallel() |
| 61 | + request := validator.Float32Request{ |
| 62 | + Path: path.Root("test"), |
| 63 | + PathExpression: path.MatchRoot("test"), |
| 64 | + ConfigValue: test.val, |
| 65 | + } |
| 66 | + response := validator.Float32Response{} |
| 67 | + float32validator.All(test.validators...).ValidateFloat32(context.Background(), request, &response) |
| 68 | + |
| 69 | + if diff := cmp.Diff(response.Diagnostics, test.expected); diff != "" { |
| 70 | + t.Errorf("unexpected diagnostics difference: %s", diff) |
| 71 | + } |
| 72 | + }) |
| 73 | + } |
| 74 | +} |
0 commit comments