lbry.go/string_test.go

187 lines
4.1 KiB
Go
Raw Normal View History

2014-08-28 17:11:18 +02:00
package null
import (
"encoding/json"
"testing"
)
var (
2014-08-29 03:29:17 +02:00
stringJSON = []byte(`"test"`)
blankStringJSON = []byte(`""`)
nullStringJSON = []byte(`{"String":"test","Valid":true}`)
nullJSON = []byte(`null`)
2014-08-28 17:11:18 +02:00
)
2014-08-29 03:29:17 +02:00
type stringInStruct struct {
Test String `json:"test,omitempty"`
}
2014-08-28 17:11:18 +02:00
func TestStringFrom(t *testing.T) {
2014-08-28 17:13:09 +02:00
str := StringFrom("test")
2014-08-30 16:53:25 +02:00
assertStr(t, str, "StringFrom() string")
2014-08-28 17:11:18 +02:00
zero := StringFrom("")
if !zero.Valid {
t.Error("StringFrom(0)", "is invalid, but should be valid")
}
}
func TestStringFromPtr(t *testing.T) {
s := "test"
sptr := &s
str := StringFromPtr(sptr)
assertStr(t, str, "StringFromPtr() string")
null := StringFromPtr(nil)
assertNullStr(t, null, "StringFromPtr(nil)")
2014-08-28 17:11:18 +02:00
}
func TestUnmarshalString(t *testing.T) {
var str String
err := json.Unmarshal(stringJSON, &str)
maybePanic(err)
2014-08-30 16:53:25 +02:00
assertStr(t, str, "string json")
2014-08-28 17:11:18 +02:00
var ns String
err = json.Unmarshal(nullStringJSON, &ns)
maybePanic(err)
2014-08-30 16:53:25 +02:00
assertStr(t, ns, "sql.NullString json")
2014-08-28 17:11:18 +02:00
2014-08-29 03:29:17 +02:00
var blank String
err = json.Unmarshal(blankStringJSON, &blank)
maybePanic(err)
2014-08-30 16:53:25 +02:00
assertNullStr(t, blank, "blank string json")
2014-08-29 03:29:17 +02:00
2014-08-28 17:11:18 +02:00
var null String
err = json.Unmarshal(nullJSON, &null)
maybePanic(err)
2014-08-30 16:53:25 +02:00
assertNullStr(t, null, "null json")
var badType String
err = json.Unmarshal(boolJSON, &badType)
if err == nil {
panic("err should not be nil")
}
assertNullStr(t, badType, "wrong type json")
2014-08-28 17:11:18 +02:00
}
2014-08-29 03:50:02 +02:00
func TestTextUnmarshalString(t *testing.T) {
var str String
err := str.UnmarshalText([]byte("test"))
maybePanic(err)
2014-08-30 16:53:25 +02:00
assertStr(t, str, "UnmarshalText() string")
2014-08-29 03:50:02 +02:00
var null String
err = null.UnmarshalText([]byte(""))
maybePanic(err)
2014-08-30 16:53:25 +02:00
assertNullStr(t, null, "UnmarshalText() empty string")
2014-08-29 03:50:02 +02:00
}
2014-08-28 17:11:18 +02:00
func TestMarshalString(t *testing.T) {
2014-08-28 17:13:09 +02:00
str := StringFrom("test")
2014-08-28 17:11:18 +02:00
data, err := json.Marshal(str)
maybePanic(err)
assertJSONEquals(t, data, `"test"`, "non-empty json marshal")
// empty values should be encoded as an empty string
zero := StringFrom("")
data, err = json.Marshal(zero)
2014-08-28 17:11:18 +02:00
maybePanic(err)
2014-08-29 03:29:17 +02:00
assertJSONEquals(t, data, `""`, "empty json marshal")
null := StringFromPtr(nil)
data, err = json.Marshal(null)
maybePanic(err)
assertJSONEquals(t, data, `null`, "null json marshal")
2014-08-28 17:11:18 +02:00
}
2014-08-29 03:29:17 +02:00
// Tests omitempty... broken until Go 1.4
// func TestMarshalStringInStruct(t *testing.T) {
// obj := stringInStruct{Test: StringFrom("")}
// data, err := json.Marshal(obj)
// maybePanic(err)
// assertJSONEquals(t, data, `{}`, "null string in struct")
// }
2014-08-30 16:53:25 +02:00
func TestStringPointer(t *testing.T) {
2014-08-28 17:13:09 +02:00
str := StringFrom("test")
2014-08-31 10:28:34 +02:00
ptr := str.Ptr()
2014-08-28 17:11:18 +02:00
if *ptr != "test" {
t.Errorf("bad %s string: %#v ≠ %s\n", "pointer", ptr, "test")
}
null := NewString("", false)
2014-08-31 10:28:34 +02:00
ptr = null.Ptr()
if ptr != nil {
t.Errorf("bad %s string: %#v ≠ %s\n", "nil pointer", ptr, "nil")
}
}
2014-08-30 16:53:25 +02:00
func TestStringIsZero(t *testing.T) {
str := StringFrom("test")
if str.IsZero() {
t.Errorf("IsZero() should be false")
}
blank := StringFrom("")
if blank.IsZero() {
t.Errorf("IsZero() should be false")
}
empty := NewString("", true)
if empty.IsZero() {
t.Errorf("IsZero() should be false")
}
null := StringFromPtr(nil)
if !null.IsZero() {
t.Errorf("IsZero() should be true")
}
}
func TestStringSetValid(t *testing.T) {
change := NewString("", false)
assertNullStr(t, change, "SetValid()")
change.SetValid("test")
assertStr(t, change, "SetValid()")
}
2014-08-30 16:53:25 +02:00
func TestStringScan(t *testing.T) {
2014-08-28 17:11:18 +02:00
var str String
err := str.Scan("test")
maybePanic(err)
2014-08-30 16:53:25 +02:00
assertStr(t, str, "scanned string")
2014-08-28 17:11:18 +02:00
var null String
err = null.Scan(nil)
maybePanic(err)
2014-08-30 16:53:25 +02:00
assertNullStr(t, null, "scanned null")
2014-08-28 17:11:18 +02:00
}
func maybePanic(err error) {
if err != nil {
panic(err)
}
}
2014-08-30 16:53:25 +02:00
func assertStr(t *testing.T, s String, from string) {
2014-08-28 17:11:18 +02:00
if s.String != "test" {
t.Errorf("bad %s string: %s ≠ %s\n", from, s.String, "test")
}
if !s.Valid {
t.Error(from, "is invalid, but should be valid")
}
}
2014-08-30 16:53:25 +02:00
func assertNullStr(t *testing.T, s String, from string) {
2014-08-28 17:11:18 +02:00
if s.Valid {
t.Error(from, "is valid, but should be invalid")
}
}
func assertJSONEquals(t *testing.T, data []byte, cmp string, from string) {
if string(data) != cmp {
t.Errorf("bad %s data: %s ≠ %s\n", from, data, cmp)
}
}