Skip to content

Commit

Permalink
internal/reflection: Migrate reflection tests for struct tags to expo…
Browse files Browse the repository at this point in the history
…rted 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`
  • Loading branch information
austinvalle authored Jul 6, 2023
1 parent 9fe2ca5 commit 51c5272
Show file tree
Hide file tree
Showing 2 changed files with 294 additions and 209 deletions.
85 changes: 0 additions & 85 deletions internal/reflect/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
package reflect

import (
"context"
"fmt"
"reflect"
"testing"

"github.com/hashicorp/terraform-plugin-framework/path"
)

func TestTrueReflectValue(t *testing.T) {
Expand Down Expand Up @@ -96,88 +93,6 @@ func TestCommaSeparatedString(t *testing.T) {
}
}

func TestGetStructTags_success(t *testing.T) {
t.Parallel()

type testStruct struct {
ExportedAndTagged string `tfsdk:"exported_and_tagged"`
unexported string //nolint:structcheck,unused
unexportedAndTagged string `tfsdk:"unexported_and_tagged"`
ExportedAndExcluded string `tfsdk:"-"`
}

res, err := getStructTags(context.Background(), reflect.ValueOf(testStruct{}), path.Empty())
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if len(res) != 1 {
t.Errorf("Unexpected result: %v", res)
}
if res["exported_and_tagged"] != 0 {
t.Errorf("Unexpected result: %v", res)
}
}

func TestGetStructTags_untagged(t *testing.T) {
t.Parallel()
type testStruct struct {
ExportedAndUntagged string
}
_, err := getStructTags(context.Background(), reflect.ValueOf(testStruct{}), path.Empty())
if err == nil {
t.Error("Expected error, got nil")
}
expected := `: need a struct tag for "tfsdk" on ExportedAndUntagged`
if err.Error() != expected {
t.Errorf("Expected error to be %q, got %q", expected, err.Error())
}
}

func TestGetStructTags_invalidTag(t *testing.T) {
t.Parallel()
type testStruct struct {
InvalidTag string `tfsdk:"invalidTag"`
}
_, err := getStructTags(context.Background(), reflect.ValueOf(testStruct{}), path.Empty())
if err == nil {
t.Errorf("Expected error, got nil")
}
expected := `invalidTag: invalid field name, must only use lowercase letters, underscores, and numbers, and must start with a letter`
if err.Error() != expected {
t.Errorf("Expected error to be %q, got %q", expected, err.Error())
}
}

func TestGetStructTags_duplicateTag(t *testing.T) {
t.Parallel()
type testStruct struct {
Field1 string `tfsdk:"my_field"`
Field2 string `tfsdk:"my_field"`
}
_, err := getStructTags(context.Background(), reflect.ValueOf(testStruct{}), path.Empty())
if err == nil {
t.Errorf("Expected error, got nil")
}
expected := `my_field: can't use field name for both Field1 and Field2`
if err.Error() != expected {
t.Errorf("Expected error to be %q, got %q", expected, err.Error())
}
}

func TestGetStructTags_notAStruct(t *testing.T) {
t.Parallel()
var testStruct string

_, err := getStructTags(context.Background(), reflect.ValueOf(testStruct), path.Empty())
if err == nil {
t.Errorf("Expected error, got nil")
}
expected := `: can't get struct tags of string, is not a struct`
if err.Error() != expected {
t.Errorf("Expected error to be %q, got %q", expected, err.Error())
}
}

func TestIsValidFieldName(t *testing.T) {
t.Parallel()
tests := map[string]bool{
Expand Down
Loading

0 comments on commit 51c5272

Please sign in to comment.