/
attribute_path_step_test.go
83 lines (72 loc) · 2.47 KB
/
attribute_path_step_test.go
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
package fromtftypes_test
import (
"context"
"fmt"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/internal/fromtftypes"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
func TestAttributePathStep(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
tfType tftypes.AttributePathStep
attrType attr.Type
expected path.PathStep
expectedError error
}{
"nil": {
tfType: nil,
expected: nil,
expectedError: fmt.Errorf("unknown tftypes.AttributePathStep: <nil>"),
},
"PathStepAttributeName": {
tfType: tftypes.AttributeName("test"),
expected: path.PathStepAttributeName("test"),
},
"PathStepElementKeyInt": {
tfType: tftypes.ElementKeyInt(1),
expected: path.PathStepElementKeyInt(1),
},
"PathStepElementKeyString": {
tfType: tftypes.ElementKeyString("test"),
expected: path.PathStepElementKeyString("test"),
},
"PathStepElementKeyValue": {
tfType: tftypes.ElementKeyValue(tftypes.NewValue(tftypes.String, "test")),
attrType: types.StringType,
expected: path.PathStepElementKeyValue{Value: types.StringValue("test")},
},
"PathStepElementKeyValue-error": {
tfType: tftypes.ElementKeyValue(tftypes.NewValue(tftypes.String, "test")),
attrType: types.BoolType,
expected: nil,
expectedError: fmt.Errorf("unable to create PathStepElementKeyValue from tftypes.Value: unable to convert tftypes.Value (tftypes.String<\"test\">) to attr.Value: can't unmarshal tftypes.String into *bool, expected boolean"),
},
}
for name, testCase := range testCases {
name, testCase := name, testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
got, err := fromtftypes.AttributePathStep(context.Background(), testCase.tfType, testCase.attrType)
if err != nil {
if testCase.expectedError == nil {
t.Fatalf("expected no error, got: %s", err)
}
if !strings.Contains(err.Error(), testCase.expectedError.Error()) {
t.Fatalf("expected error %q, got: %s", testCase.expectedError, err)
}
}
if err == nil && testCase.expectedError != nil {
t.Fatalf("got no error, tfType: %s", testCase.expectedError)
}
if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}