Skip to content

Commit

Permalink
feat(testdata): put special treatment for maps behind a struct tag op…
Browse files Browse the repository at this point in the history
…tion (explode) (#8)
  • Loading branch information
dominicbarnes authored Sep 18, 2024
1 parent 1fdf850 commit f66cc4f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
4 changes: 2 additions & 2 deletions testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func loadDir(inputs []string, output any) error {
func loadDirInput(input string, tag *structtag.Tag, field reflect.StructField, value reflect.Value) error {
file := filepath.Join(input, tag.Name)

if isMap(field.Type) {
if isMap(field.Type) && tag.HasOption("explode") {
matches, err := filepath.Glob(file)
if err != nil {
return fmt.Errorf("%s: failed to list files %s: %w", field.Name, file, err)
Expand Down Expand Up @@ -270,7 +270,7 @@ func saveDir(dir string, input any) error {
}

func saveDirField(dir string, tag *structtag.Tag, field reflect.StructField, value reflect.Value) error {
if isMap(field.Type) {
if isMap(field.Type) && tag.HasOption("explode") {
iter := value.MapRange()

for iter.Next() {
Expand Down
30 changes: 24 additions & 6 deletions testdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,19 @@ func TestLoad(t *testing.T) {
})

t.Run("maps", func(t *testing.T) {
t.Run("raw json", func(t *testing.T) {
type test struct {
Input map[string]any `testdata:"input.json"`
}

testLoadOne(t, "json", new(test), &test{
Input: map[string]any{"hello": "world"},
})
})

t.Run("expand glob", func(t *testing.T) {
type test struct {
Multiple map[string]string `testdata:"*.txt"`
Multiple map[string]string `testdata:"*.txt,explode"`
}

testLoadOne(t, "multiple", new(test), &test{
Expand All @@ -132,7 +142,7 @@ func TestLoad(t *testing.T) {

t.Run("single file", func(t *testing.T) {
type test struct {
Multiple map[string]string `testdata:"a.txt"`
Multiple map[string]string `testdata:"a.txt,explode"`
}

testLoadOne(t, "multiple", new(test), &test{
Expand All @@ -144,7 +154,7 @@ func TestLoad(t *testing.T) {

t.Run("bytes", func(t *testing.T) {
type test struct {
Multiple map[string][]byte `testdata:"a.txt"`
Multiple map[string][]byte `testdata:"a.txt,explode"`
}

testLoadOne(t, "multiple", new(test), &test{
Expand All @@ -156,7 +166,7 @@ func TestLoad(t *testing.T) {

t.Run("glob without matches", func(t *testing.T) {
type test struct {
Multiple map[string]string `testdata:"*.log"`
Multiple map[string]string `testdata:"*.log,explode"`
}

testLoadOne(t, "multiple", new(test), &test{
Expand Down Expand Up @@ -366,9 +376,17 @@ func TestAssert(t *testing.T) {
},
},
{
name: "map",
name: "map json",
expected: &struct {
Input map[string]string `testdata:"input.json"`
}{
Input: map[string]string{"hello": "world"},
},
},
{
name: "map explode",
expected: &struct {
Files map[string]string `testdata:"*.txt"`
Files map[string]string `testdata:"*.txt,explode"`
}{
Files: map[string]string{"a.txt": "A", "b.txt": "B"},
},
Expand Down

0 comments on commit f66cc4f

Please sign in to comment.