-
Notifications
You must be signed in to change notification settings - Fork 4
/
string_test.go
71 lines (53 loc) · 1.61 KB
/
string_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package null
import (
"database/sql"
"encoding/json"
"testing"
)
type testString struct {
Value String `json:"value"`
}
func TestNewString(t *testing.T) {
value := NewString("test", true)
if value.String != "test" || !value.Valid {
t.Fatal("New String must have value and be valid")
}
}
func TestMarshalString(t *testing.T) {
value := String{sql.NullString{String: "test", Valid: true}}
if data, err := json.Marshal(value); err != nil || string(data) != "\"test\"" {
t.Fatalf("String must be marshalled to value, but was %v %v", err, string(data))
}
value.Valid = false
if data, err := json.Marshal(value); err != nil || string(data) != "null" {
t.Fatalf("String must be marshalled to null, but was %v %v", err, string(data))
}
}
func TestUnmarshalString(t *testing.T) {
str := `{"value": "test"}`
var value testString
if err := json.Unmarshal([]byte(str), &value); err != nil {
t.Fatalf("String must be unmarshalled to value, but was %v", err)
}
if !value.Value.Valid || value.Value.String != "test" {
t.Fatalf("Unmarshalled null string must be valid, but was %v", value.Value)
}
str = `{"value": null}`
if err := json.Unmarshal([]byte(str), &value); err != nil {
t.Fatalf("String must be unmarshalled to null, but was %v", err)
}
if value.Value.Valid {
t.Fatal("Unmarshalled null string must be invalid")
}
}
func TestGettersSettersString(t *testing.T) {
value := NewString("test", true)
value.SetNil()
if value.String != "" || value.Valid {
t.Fatal("String must be nil")
}
value.SetValid("test")
if value.String != "test" || !value.Valid {
t.Fatal("String must be valid")
}
}