-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ReadJSON from file test utility function that fails the test immediately if the file cannot be read or parsed into the provided data structure. cfg := Config{} test.ReadJSON(t, "testdata/config.json", &cfg) * test: Add ReadJSON from file test utility test/json.go | 28 ++++++++++++++++++++++++++++ test/json_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ test/testdata/invalid.json | 1 + test/testdata/profile.json | 4 ++++ 4 files changed, 78 insertions(+)
- Loading branch information
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package test | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"testing" | ||
) | ||
|
||
// ReadJSON decodes JSON from a file into a value. If it fails to open the given | ||
// filename or decode the contents of the file, ReadJSON will call t.Fatal with the | ||
// error. | ||
// | ||
// The type of the first parameter as testing.TB allows it to be used from both | ||
// tests and benchmarks, passing either a *testing.T or *testing.B. | ||
// | ||
// cfg := Config{} | ||
// test.ReadJSON(t, "testdata/config.json", &cfg) | ||
func ReadJSON(t testing.TB, fname string, v interface{}) { | ||
t.Helper() | ||
f, err := os.Open(fname) //nolint:gosec | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer f.Close() //nolint:errcheck,gosec | ||
if err := json.NewDecoder(f).Decode(v); err != nil { | ||
t.Fatalf("cannot decode %s: %v", fname, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type MockT struct { | ||
testing.TB | ||
errMsg string | ||
} | ||
|
||
func (t *MockT) Helper() {} | ||
func (t *MockT) Fatal(s ...interface{}) { | ||
t.errMsg = fmt.Sprintln(s...) | ||
} | ||
|
||
func (t *MockT) Fatalf(format string, args ...interface{}) { | ||
t.errMsg = fmt.Sprintf(format, args...) | ||
} | ||
|
||
func TestReadJSON(t *testing.T) { | ||
mt := &MockT{TB: t} | ||
got := map[string]string{} | ||
ReadJSON(mt, "testdata/profile.json", &got) | ||
want := map[string]string{"name": "Lee Sedol", "game": "Go"} | ||
require.Equal(t, want, got) | ||
require.Empty(t, mt.errMsg) | ||
} | ||
|
||
func TestReadJSONMissingFile(t *testing.T) { | ||
mt := &MockT{} | ||
got := map[string]string{} | ||
ReadJSON(mt, "testdata/MISSING_FILE.json", &got) | ||
require.NotEmpty(t, mt.errMsg) | ||
} | ||
|
||
func TestReadJSONInvalidJSON(t *testing.T) { | ||
mt := &MockT{} | ||
got := map[string]string{} | ||
ReadJSON(mt, "testdata/invalid.json", &got) | ||
require.NotEmpty(t, mt.errMsg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"truncate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"game": "Go", | ||
"name": "Lee Sedol" | ||
} |