package main import ( "encoding/json" "testing" "github.com/alecthomas/assert" "github.com/davecgh/go-spew/spew" ) var dataOne = `{"foo":"bar", "description":"dataOne"}` var dataTwo = `{"qux":"baz", "description":"dataTwo", "anint":1, "snarf":true}` type dataOneType struct { Foo string `json:"foo"` Description string `json:"description"` } type dataTwoType struct { Qux string `json:"qux"` Description string `json:"description"` AnInt int `json:"anint"` Snarf bool `json:"snarf"` } func TestHappy(t *testing.T) { d1 := &dataOneType{} d2 := &dataTwoType{} errd1 := json.Unmarshal([]byte(dataOne), d1) assert.NoError(t, errd1) spew.Dump(d1) errd2 := json.Unmarshal([]byte(dataTwo), d2) spew.Dump(d2) assert.NoError(t, errd2) } func TestWTF(t *testing.T) { d1 := &dataOneType{} d2 := &dataTwoType{} errd1 := json.Unmarshal([]byte(dataOne), d2) spew.Dump(d2) assert.Error(t, errd1) errd2 := json.Unmarshal([]byte(dataTwo), d1) spew.Dump(d1) assert.Error(t, errd2) }