1
0
Fork 0
secondly/confection_test.go

45 lines
953 B
Go
Raw Normal View History

2015-08-29 09:45:00 +00:00
package confection2
import "testing"
type testConf struct {
Foo string `json:"foo"`
Bar int `json:"bar"`
}
const (
goodJSON = `{"foo": "baz", "bar": 1}`
badJSON = `{"foo": "noooo...`
)
func TestIsStructPtr(t *testing.T) {
if ok := isStructPtr(1); ok {
t.Error("Integer recognized as a struct pointer")
}
if ok := isStructPtr(testConf{}); ok {
t.Error("Struct instance recognized as a struct pointer")
}
if ok := isStructPtr(&testConf{}); !ok {
t.Error("Struct pointer was not recognized")
}
}
func TestUnmarshal(t *testing.T) {
conf := testConf{}
var i interface{} = &conf
if err := unmarshal([]byte(badJSON), i); err == nil {
t.Error("Expected error")
}
if err := unmarshal([]byte(goodJSON), i); err != nil {
t.Error("Unexpected error")
}
if conf.Foo != "baz" {
t.Errorf("Expected Foo to equal %q, got %q", "baz", conf.Foo)
}
if conf.Bar != 1 {
t.Errorf("Expected Bar to equal %q, got %q", 1, conf.Bar)
}
}