Repository navigation
Expand file tree
/
Copy pathstring_test.go
More file actions
198 lines (174 loc) · 4.21 KB
/
Copy pathstring_test.go
File metadata and controls
198 lines (174 loc) · 4.21 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package opt
import (
"encoding/json"
"testing"
)
func TestStringFrom(t *testing.T) {
s := StringFrom("hello")
if s.V != "hello" || !s.Valid {
t.Errorf("StringFrom: got {%v, %v}, want {\"hello\", true}", s.V, s.Valid)
}
}
func TestStringFromPtr(t *testing.T) {
str := "hello"
s := StringFromPtr(&str)
if s.V != "hello" || !s.Valid {
t.Errorf("StringFromPtr: got {%v, %v}, want {\"hello\", true}", s.V, s.Valid)
}
null := StringFromPtr(nil)
if null.Valid {
t.Error("StringFromPtr(nil) should be invalid")
}
}
func TestStringMarshalJSON(t *testing.T) {
tests := []struct {
name string
val String
want string
}{
{"valid", StringFrom("hello"), `"hello"`},
{"empty", StringFrom(""), `""`},
{"null", NewString("", false), "null"},
{"special chars", StringFrom(`"quotes" & <tags>`), `"\"quotes\" \u0026 \u003ctags\u003e"`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(tt.val)
if err != nil {
t.Fatal(err)
}
if string(data) != tt.want {
t.Errorf("got %s, want %s", data, tt.want)
}
})
}
}
func TestStringUnmarshalJSON(t *testing.T) {
tests := []struct {
name string
input string
want String
wantErr bool
}{
{"string", `"hello"`, StringFrom("hello"), false},
{"empty", `""`, StringFrom(""), false},
{"null", "null", NewString("", false), false},
{"number", "42", String{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var s String
err := json.Unmarshal([]byte(tt.input), &s)
if (err != nil) != tt.wantErr {
t.Fatalf("err = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr {
if s.Valid != tt.want.Valid || (s.Valid && s.V != tt.want.V) {
t.Errorf("got {%v, %v}, want {%v, %v}", s.V, s.Valid, tt.want.V, tt.want.Valid)
}
}
})
}
}
func TestStringMarshalText(t *testing.T) {
s := StringFrom("hello")
data, err := s.MarshalText()
if err != nil {
t.Fatal(err)
}
if string(data) != "hello" {
t.Errorf("got %s, want hello", data)
}
null := NewString("", false)
data, err = null.MarshalText()
if err != nil {
t.Fatal(err)
}
if len(data) != 0 {
t.Errorf("null MarshalText should return empty, got %s", data)
}
}
func TestStringUnmarshalText(t *testing.T) {
var s String
if err := s.UnmarshalText([]byte("hello")); err != nil {
t.Fatal(err)
}
if s.V != "hello" || !s.Valid {
t.Errorf("got {%v, %v}, want {\"hello\", true}", s.V, s.Valid)
}
var empty String
if err := empty.UnmarshalText([]byte("")); err != nil {
t.Fatal(err)
}
if empty.Valid {
t.Error("empty text should produce invalid String")
}
}
func TestStringEqual(t *testing.T) {
a := StringFrom("hello")
b := StringFrom("hello")
if !a.Equal(b) {
t.Error("equal strings should be Equal")
}
c := StringFrom("world")
if a.Equal(c) {
t.Error("different strings should not be Equal")
}
null1 := NewString("", false)
null2 := NewString("", false)
if !null1.Equal(null2) {
t.Error("two null strings should be Equal")
}
if a.Equal(null1) {
t.Error("valid and null should not be Equal")
}
}
func TestStringOr(t *testing.T) {
s := StringFrom("hello")
if s.Or("default") != "hello" {
t.Error("valid Or should return value")
}
null := NewString("", false)
if null.Or("default") != "default" {
t.Error("null Or should return fallback")
}
}
func TestStringIsZero(t *testing.T) {
s := StringFrom("hello")
if s.IsZero() {
t.Error("valid string should not be IsZero")
}
empty := StringFrom("")
if empty.IsZero() {
t.Error("valid empty string should not be IsZero")
}
null := NewString("", false)
if !null.IsZero() {
t.Error("null string should be IsZero")
}
}
func TestStringJSONRoundtrip(t *testing.T) {
type user struct {
Name String `json:"name"`
Email String `json:"email"`
}
original := user{
Name: StringFrom("John"),
Email: NewString("", false),
}
data, err := json.Marshal(original)
if err != nil {
t.Fatal(err)
}
want := `{"name":"John","email":null}`
if string(data) != want {
t.Errorf("marshal: got %s, want %s", data, want)
}
var decoded user
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatal(err)
}
if !decoded.Name.Equal(original.Name) || !decoded.Email.Equal(original.Email) {
t.Error("roundtrip failed")
}
}