-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathany_test.go
More file actions
85 lines (76 loc) · 2.53 KB
/
any_test.go
File metadata and controls
85 lines (76 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright IBM Corp. 2022, 2026
// SPDX-License-Identifier: MPL-2.0
package stringvalidator_test
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework-validators/internal/testvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
)
func TestAnyValidatorValidateString(t *testing.T) {
t.Parallel()
type testCase struct {
val types.String
validators []validator.String
expected diag.Diagnostics
}
tests := map[string]testCase{
"invalid": {
val: types.StringValue("one"),
validators: []validator.String{
stringvalidator.LengthAtLeast(4),
stringvalidator.LengthAtLeast(5),
},
expected: diag.Diagnostics{
diag.NewAttributeErrorDiagnostic(
path.Root("test"),
"Invalid Attribute Value Length",
"Attribute test string length must be at least 4, got: 3",
),
diag.NewAttributeErrorDiagnostic(
path.Root("test"),
"Invalid Attribute Value Length",
"Attribute test string length must be at least 5, got: 3",
),
},
},
"valid": {
val: types.StringValue("test"),
validators: []validator.String{
stringvalidator.LengthAtLeast(5),
stringvalidator.LengthAtLeast(3),
},
expected: diag.Diagnostics{},
},
"valid with warning": {
val: types.StringValue("test"),
validators: []validator.String{
stringvalidator.All(stringvalidator.LengthAtLeast(5), testvalidator.WarningString("failing warning summary", "failing warning details")),
stringvalidator.All(stringvalidator.LengthAtLeast(2), testvalidator.WarningString("passing warning summary", "passing warning details")),
},
expected: diag.Diagnostics{
diag.NewWarningDiagnostic("passing warning summary", "passing warning details"),
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
request := validator.StringRequest{
Path: path.Root("test"),
PathExpression: path.MatchRoot("test"),
ConfigValue: test.val,
}
response := validator.StringResponse{}
stringvalidator.Any(test.validators...).ValidateString(context.Background(), request, &response)
if diff := cmp.Diff(response.Diagnostics, test.expected); diff != "" {
t.Errorf("unexpected diagnostics difference: %s", diff)
}
})
}
}