Skip to content

Instantly share code, notes, and snippets.

@lusis lusis/t_test.go
Created Jan 9, 2018

What would you like to do?
fml golang json.Unmarshal
package main
import (
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)
errd2 := json.Unmarshal([]byte(dataTwo), d2)
assert.NoError(t, errd2)
func TestWTF(t *testing.T) {
d1 := &dataOneType{}
d2 := &dataTwoType{}
errd1 := json.Unmarshal([]byte(dataOne), d2)
assert.Error(t, errd1)
errd2 := json.Unmarshal([]byte(dataTwo), d1)
assert.Error(t, errd2)
== RUN TestHappy
Foo: (string) (len=3) "bar",
Description: (string) (len=7) "dataOne"
Qux: (string) (len=3) "baz",
Description: (string) (len=7) "dataTwo",
AnInt: (int) 1,
Snarf: (bool) true
--- PASS: TestHappy (0.00s)
=== RUN TestWTF
Qux: (string) "",
Description: (string) (len=7) "dataOne",
AnInt: (int) 0,
Snarf: (bool) false
--- FAIL: TestWTF (0.00s)
Error Trace: t_test.go:44
Error: Expected value not to be nil.
Messages: An error is expected but got nil.
exit status 1
FAIL 0.003s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.