2015-08-29 12:41:20 +00:00
|
|
|
package secondly
|
2015-08-29 09:45:00 +00:00
|
|
|
|
2015-08-29 11:00:06 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
)
|
2015-08-29 09:45:00 +00:00
|
|
|
|
|
|
|
type testConf struct {
|
2015-08-29 12:25:47 +00:00
|
|
|
AppName string `json:"app_name"`
|
|
|
|
Version float32 `json:"version"`
|
|
|
|
Database testDatabaseConf `json:"database"`
|
|
|
|
}
|
2015-08-29 12:29:56 +00:00
|
|
|
|
2015-08-29 12:25:47 +00:00
|
|
|
type testDatabaseConf struct {
|
|
|
|
Adapter string `json:"adapter"`
|
|
|
|
Host string `json:"host"`
|
|
|
|
Port int `json:"port"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
2015-08-29 09:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2015-08-29 12:41:20 +00:00
|
|
|
goodJSON = `{"app_name": "Secondly", "version": 1}`
|
2015-08-29 12:25:47 +00:00
|
|
|
badJSON = `{"app_name": "noooo...`
|
2015-08-29 09:45:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-08-29 11:00:06 +00:00
|
|
|
if err := json.Unmarshal([]byte(badJSON), i); err == nil {
|
2015-08-29 09:45:00 +00:00
|
|
|
t.Error("Expected error")
|
|
|
|
}
|
|
|
|
|
2015-08-29 11:00:06 +00:00
|
|
|
if err := json.Unmarshal([]byte(goodJSON), i); err != nil {
|
2015-08-29 09:45:00 +00:00
|
|
|
t.Error("Unexpected error")
|
|
|
|
}
|
2015-08-29 12:41:20 +00:00
|
|
|
if conf.AppName != "Secondly" {
|
|
|
|
t.Errorf("Expected Foo to equal %q, got %q", "Secondly", conf.AppName)
|
2015-08-29 09:45:00 +00:00
|
|
|
}
|
2015-08-29 12:25:47 +00:00
|
|
|
if conf.Version != 1 {
|
|
|
|
t.Errorf("Expected Bar to equal %q, got %q", 1, conf.Version)
|
2015-08-29 09:45:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-29 09:55:18 +00:00
|
|
|
|
|
|
|
func TestDuplicate(t *testing.T) {
|
|
|
|
var i interface{} = &testConf{}
|
|
|
|
|
|
|
|
dupe := duplicate(i)
|
|
|
|
if _, ok := dupe.(*testConf); !ok {
|
|
|
|
t.Error("Duplication failed")
|
|
|
|
}
|
|
|
|
}
|