Guest User

Untitled

a guest
Jan 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "encoding/json"
  5. "testing"
  6.  
  7. "github.com/alecthomas/assert"
  8. "github.com/davecgh/go-spew/spew"
  9. )
  10.  
  11. var dataOne = `{"foo":"bar", "description":"dataOne"}`
  12. var dataTwo = `{"qux":"baz", "description":"dataTwo", "anint":1, "snarf":true}`
  13.  
  14. type dataOneType struct {
  15. Foo string `json:"foo"`
  16. Description string `json:"description"`
  17. }
  18.  
  19. type dataTwoType struct {
  20. Qux string `json:"qux"`
  21. Description string `json:"description"`
  22. AnInt int `json:"anint"`
  23. Snarf bool `json:"snarf"`
  24. }
  25.  
  26. func TestHappy(t *testing.T) {
  27. d1 := &dataOneType{}
  28. d2 := &dataTwoType{}
  29.  
  30. errd1 := json.Unmarshal([]byte(dataOne), d1)
  31. assert.NoError(t, errd1)
  32. spew.Dump(d1)
  33. errd2 := json.Unmarshal([]byte(dataTwo), d2)
  34. spew.Dump(d2)
  35. assert.NoError(t, errd2)
  36. }
  37.  
  38. func TestWTF(t *testing.T) {
  39. d1 := &dataOneType{}
  40. d2 := &dataTwoType{}
  41.  
  42. errd1 := json.Unmarshal([]byte(dataOne), d2)
  43. spew.Dump(d2)
  44. assert.Error(t, errd1)
  45. errd2 := json.Unmarshal([]byte(dataTwo), d1)
  46. spew.Dump(d1)
  47. assert.Error(t, errd2)
  48. }
Add Comment
Please, Sign In to add comment