Skip to content

Commit 51c5272

Browse files
authored
internal/reflection: Migrate reflection tests for struct tags to exported methods (#790)
* refactored error tests into table driven * moved struct tags ignore test * migrated all `getStructTags` error tests to table test, except `TestGetStructTags_notAStruct` * add tests for FromStruct method * add struct ignore tests for `FromStruct`
1 parent 9fe2ca5 commit 51c5272

File tree

2 files changed

+294
-209
lines changed

2 files changed

+294
-209
lines changed

internal/reflect/helpers_test.go

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
package reflect
55

66
import (
7-
"context"
87
"fmt"
98
"reflect"
109
"testing"
11-
12-
"github.com/hashicorp/terraform-plugin-framework/path"
1310
)
1411

1512
func TestTrueReflectValue(t *testing.T) {
@@ -96,88 +93,6 @@ func TestCommaSeparatedString(t *testing.T) {
9693
}
9794
}
9895

99-
func TestGetStructTags_success(t *testing.T) {
100-
t.Parallel()
101-
102-
type testStruct struct {
103-
ExportedAndTagged string `tfsdk:"exported_and_tagged"`
104-
unexported string //nolint:structcheck,unused
105-
unexportedAndTagged string `tfsdk:"unexported_and_tagged"`
106-
ExportedAndExcluded string `tfsdk:"-"`
107-
}
108-
109-
res, err := getStructTags(context.Background(), reflect.ValueOf(testStruct{}), path.Empty())
110-
if err != nil {
111-
t.Errorf("Unexpected error: %s", err)
112-
}
113-
if len(res) != 1 {
114-
t.Errorf("Unexpected result: %v", res)
115-
}
116-
if res["exported_and_tagged"] != 0 {
117-
t.Errorf("Unexpected result: %v", res)
118-
}
119-
}
120-
121-
func TestGetStructTags_untagged(t *testing.T) {
122-
t.Parallel()
123-
type testStruct struct {
124-
ExportedAndUntagged string
125-
}
126-
_, err := getStructTags(context.Background(), reflect.ValueOf(testStruct{}), path.Empty())
127-
if err == nil {
128-
t.Error("Expected error, got nil")
129-
}
130-
expected := `: need a struct tag for "tfsdk" on ExportedAndUntagged`
131-
if err.Error() != expected {
132-
t.Errorf("Expected error to be %q, got %q", expected, err.Error())
133-
}
134-
}
135-
136-
func TestGetStructTags_invalidTag(t *testing.T) {
137-
t.Parallel()
138-
type testStruct struct {
139-
InvalidTag string `tfsdk:"invalidTag"`
140-
}
141-
_, err := getStructTags(context.Background(), reflect.ValueOf(testStruct{}), path.Empty())
142-
if err == nil {
143-
t.Errorf("Expected error, got nil")
144-
}
145-
expected := `invalidTag: invalid field name, must only use lowercase letters, underscores, and numbers, and must start with a letter`
146-
if err.Error() != expected {
147-
t.Errorf("Expected error to be %q, got %q", expected, err.Error())
148-
}
149-
}
150-
151-
func TestGetStructTags_duplicateTag(t *testing.T) {
152-
t.Parallel()
153-
type testStruct struct {
154-
Field1 string `tfsdk:"my_field"`
155-
Field2 string `tfsdk:"my_field"`
156-
}
157-
_, err := getStructTags(context.Background(), reflect.ValueOf(testStruct{}), path.Empty())
158-
if err == nil {
159-
t.Errorf("Expected error, got nil")
160-
}
161-
expected := `my_field: can't use field name for both Field1 and Field2`
162-
if err.Error() != expected {
163-
t.Errorf("Expected error to be %q, got %q", expected, err.Error())
164-
}
165-
}
166-
167-
func TestGetStructTags_notAStruct(t *testing.T) {
168-
t.Parallel()
169-
var testStruct string
170-
171-
_, err := getStructTags(context.Background(), reflect.ValueOf(testStruct), path.Empty())
172-
if err == nil {
173-
t.Errorf("Expected error, got nil")
174-
}
175-
expected := `: can't get struct tags of string, is not a struct`
176-
if err.Error() != expected {
177-
t.Errorf("Expected error to be %q, got %q", expected, err.Error())
178-
}
179-
}
180-
18196
func TestIsValidFieldName(t *testing.T) {
18297
t.Parallel()
18398
tests := map[string]bool{

0 commit comments

Comments
 (0)