generated from FrangipaneTeam/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 1
/
attrtypes_test.go
104 lines (81 loc) · 2.34 KB
/
attrtypes_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package supertypes_test
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
supertypes "github.com/FrangipaneTeam/terraform-plugin-framework-supertypes"
)
func TestAttributeTypes(t *testing.T) {
t.Parallel()
type struct1 struct{}
type struct2 struct {
Name types.String `tfsdk:"name"`
ID types.Int64 `tfsdk:"id"`
IncludeProperty types.Bool `tfsdk:"include_property"`
}
ctx := context.Background()
got := supertypes.AttributeTypesMust[struct1](ctx)
wanted := map[string]attr.Type{}
if diff := cmp.Diff(got, wanted); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
_, err := supertypes.AttributeTypes[int](ctx)
if err == nil {
t.Fatalf("expected error")
}
got, err = supertypes.AttributeTypes[struct2](ctx)
if err != nil {
t.Fatalf("unexpected error")
}
wanted = map[string]attr.Type{
"name": types.StringType,
"id": types.Int64Type,
"include_property": types.BoolType,
}
if diff := cmp.Diff(got, wanted); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
}
func TestElementType(t *testing.T) {
t.Parallel()
type String string
type Int64 int64
type Bool bool
type Float64 float64
type Invalid int
ctx := context.Background()
got, err := supertypes.ElementType[String](ctx)
if err != nil {
t.Fatalf("unexpected error")
}
if diff := cmp.Diff(got, supertypes.StringType{}); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
got, err = supertypes.ElementType[Int64](ctx)
if err != nil {
t.Fatalf("unexpected error")
}
if diff := cmp.Diff(got, supertypes.Int64Type{}); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
got, err = supertypes.ElementType[Bool](ctx)
if err != nil {
t.Fatalf("unexpected error")
}
if diff := cmp.Diff(got, supertypes.BoolType{}); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
got, err = supertypes.ElementType[Float64](ctx)
if err != nil {
t.Fatalf("unexpected error")
}
if diff := cmp.Diff(got, supertypes.Float64Type{}); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
_, err = supertypes.ElementType[Invalid](ctx)
if err == nil {
t.Fatalf("expected error")
}
}