Skip to content

Commit

Permalink
test(raw): refactor for table tests (#58)
Browse files Browse the repository at this point in the history
Co-authored-by: Neal <[email protected]>
  • Loading branch information
jbrockopp and Neal authored Apr 23, 2020
1 parent 338fccc commit 15807f9
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 317 deletions.
301 changes: 122 additions & 179 deletions raw/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,194 +5,137 @@
package raw

import (
"encoding/json"
"io/ioutil"
"reflect"
"testing"

yaml "gopkg.in/yaml.v2"
)

func TestRaw_StringSliceMap_UnmarshalJSON_String(t *testing.T) {
// setup types
want := &StringSliceMap{"foo": "bar"}
got := new(StringSliceMap)

// run test
b, err := ioutil.ReadFile("testdata/string_map.json")
if err != nil {
t.Errorf("Reading file for UnmarshalYAML returned err: %v", err)
}

err = json.Unmarshal(b, got)
if err != nil {
t.Errorf("UnmarshalJSON returned err: %v", err)
}

if !reflect.DeepEqual(got, want) {
t.Errorf("UnmarshalJSON is %v, want %v", got, want)
}
}

func TestRaw_StringSliceMap_UnmarshalJSON_Slice(t *testing.T) {
// setup types
want := &StringSliceMap{"foo": "bar"}
got := new(StringSliceMap)

// run test
b, err := ioutil.ReadFile("testdata/slice_map.json")
if err != nil {
t.Errorf("Reading file for UnmarshalYAML returned err: %v", err)
}

err = json.Unmarshal(b, got)
if err != nil {
t.Errorf("UnmarshalJSON returned err: %v", err)
}

if !reflect.DeepEqual(got, want) {
t.Errorf("UnmarshalJSON is %v, want %v", got, want)
}
}

func TestRaw_StringSliceMap_UnmarshalJSON_Map(t *testing.T) {
// setup types
want := &StringSliceMap{"foo": "bar"}
got := new(StringSliceMap)

// run test
b, err := ioutil.ReadFile("testdata/map.json")
if err != nil {
t.Errorf("Reading file for UnmarshalYAML returned err: %v", err)
}

err = json.Unmarshal(b, got)
if err != nil {
t.Errorf("UnmarshalJSON returned err: %v", err)
}

if !reflect.DeepEqual(got, want) {
t.Errorf("UnmarshalJSON is %v, want %v", got, want)
}
}

func TestRaw_StringSliceMap_UnmarshalJSON_Empty(t *testing.T) {
// setup types
want := new(StringSliceMap)
got := new(StringSliceMap)

// run test
err := got.UnmarshalJSON([]byte(""))
if err != nil {
t.Errorf("UnmarshalJSON returned err: %v", err)
}

if !reflect.DeepEqual(got, want) {
t.Errorf("UnmarshalJSON is %v, want %v", got, want)
}
}

func TestRaw_StringSliceMap_UnmarshalJSON_Invalid(t *testing.T) {
// setup types
want := new(StringSliceMap)
got := new(StringSliceMap)

// run test
b, err := ioutil.ReadFile("testdata/invalid.json")
if err != nil {
t.Errorf("Reading file for UnmarshalYAML returned err: %v", err)
}

err = json.Unmarshal(b, got)
if err == nil {
t.Errorf("UnmarshalJSON should have returned err")
}

if !reflect.DeepEqual(got, want) {
t.Errorf("UnmarshalJSON is %v, want %v", got, want)
}
}

func TestRaw_StringSliceMap_UnmarshalYAML_String(t *testing.T) {
// setup types
want := &StringSliceMap{"foo": "bar"}
got := new(StringSliceMap)

// run test
b, err := ioutil.ReadFile("testdata/string_map.yml")
if err != nil {
t.Errorf("Reading file for UnmarshalYAML returned err: %v", err)
}

err = yaml.Unmarshal(b, got)
if err != nil {
t.Errorf("UnmarshalYAML returned err: %v", err)
}

if !reflect.DeepEqual(got, want) {
t.Errorf("UnmarshalYAML is %v, want %v", got, want)
}
}

func TestRaw_StringSliceMap_UnmarshalYAML_Slice(t *testing.T) {
// setup types
want := &StringSliceMap{"foo": "bar"}
got := new(StringSliceMap)

// run test
b, err := ioutil.ReadFile("testdata/slice_map.yml")
if err != nil {
t.Errorf("Reading file for UnmarshalYAML returned err: %v", err)
}

err = yaml.Unmarshal(b, got)
if err != nil {
t.Errorf("UnmarshalYAML returned err: %v", err)
}

if !reflect.DeepEqual(got, want) {
t.Errorf("UnmarshalYAML is %v, want %v", got, want)
}
}

func TestRaw_StringSliceMap_UnmarshalYAML_Map(t *testing.T) {
// setup types
want := &StringSliceMap{"foo": "bar"}
got := new(StringSliceMap)

// run test
b, err := ioutil.ReadFile("testdata/map.yml")
if err != nil {
t.Errorf("Reading file for UnmarshalYAML returned err: %v", err)
}

err = yaml.Unmarshal(b, got)
if err != nil {
t.Errorf("UnmarshalYAML returned err: %v", err)
}

if !reflect.DeepEqual(got, want) {
t.Errorf("UnmarshalYAML is %v, want %v", got, want)
func TestRaw_StringSliceMap_UnmarshalJSON(t *testing.T) {
// setup tests
tests := []struct {
failure bool
file string
want *StringSliceMap
}{
{
failure: false,
file: "testdata/string_map.json",
want: &StringSliceMap{"foo": "bar"},
},
{
failure: false,
file: "testdata/slice_map.json",
want: &StringSliceMap{"foo": "bar"},
},
{
failure: false,
file: "testdata/map.json",
want: &StringSliceMap{"foo": "bar"},
},
{
failure: false,
file: "",
want: new(StringSliceMap),
},
{
failure: true,
file: "testdata/invalid.json",
want: nil,
},
}

// run tests
for _, test := range tests {
var (
err error

b = []byte{}
got = new(StringSliceMap)
)

if len(test.file) > 0 {
b, err = ioutil.ReadFile(test.file)
if err != nil {
t.Errorf("unable to read %s file: %v", test.file, err)
}
}

err = got.UnmarshalJSON(b)

if test.failure {
if err == nil {
t.Errorf("UnmarshalJSON should have returned err")
}

continue
}

if err != nil {
t.Errorf("UnmarshalJSON returned err: %v", err)
}

if !reflect.DeepEqual(got, test.want) {
t.Errorf("UnmarshalJSON is %v, want %v", got, test.want)
}
}
}

func TestRaw_StringSliceMap_UnmarshalYAML_Invalid(t *testing.T) {
// setup types
want := new(StringSliceMap)
got := new(StringSliceMap)

// run test
b, err := ioutil.ReadFile("testdata/invalid.yml")
if err != nil {
t.Errorf("Reading file for UnmarshalYAML returned err: %v", err)
}

err = yaml.Unmarshal(b, got)
if err == nil {
t.Errorf("UnmarshalYAML should have returned err")
}

if !reflect.DeepEqual(got, want) {
t.Errorf("UnmarshalYAML is %v, want %v", got, want)
func TestRaw_StringSliceMap_UnmarshalYAML(t *testing.T) {
// setup tests
tests := []struct {
failure bool
file string
want *StringSliceMap
}{
{
failure: false,
file: "testdata/string_map.yml",
want: &StringSliceMap{"foo": "bar"},
},
{
failure: false,
file: "testdata/slice_map.yml",
want: &StringSliceMap{"foo": "bar"},
},
{
failure: false,
file: "testdata/map.yml",
want: &StringSliceMap{"foo": "bar"},
},
{
failure: true,
file: "testdata/invalid.yml",
want: nil,
},
}

// run tests
for _, test := range tests {
got := new(StringSliceMap)

b, err := ioutil.ReadFile(test.file)
if err != nil {
t.Errorf("unable to read %s file: %v", test.file, err)
}

err = yaml.Unmarshal(b, got)

if test.failure {
if err == nil {
t.Errorf("UnmarshalYAML should have returned err")
}

continue
}

if err != nil {
t.Errorf("UnmarshalYAML returned err: %v", err)
}

if !reflect.DeepEqual(got, test.want) {
t.Errorf("UnmarshalYAML is %v, want %v", got, test.want)
}
}
}
Loading

0 comments on commit 15807f9

Please sign in to comment.