-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
message_fields_test.go
163 lines (120 loc) · 4.04 KB
/
message_fields_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
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
package sse
import (
"testing"
"github.com/tmaxmax/go-sse/internal/tests"
)
func mustMessageField(tb testing.TB, value string) messageField { //nolint:unparam // May receive other values.
tb.Helper()
f, err := newMessageField(value)
if err != nil {
panic(err)
}
return f
}
func TestNewMessageField(t *testing.T) {
t.Parallel()
id, err := newMessageField("")
tests.Equal(t, err, nil, "field evaluated as invalid")
tests.Expect(t, id.IsSet(), "field is not set")
tests.Equal(t, id.String(), "", "field incorrectly set")
id, err = newMessageField("in\nvalid")
tests.Expect(t, err != nil, "field evaluated as valid")
tests.Expect(t, !id.IsSet() && id.String() == "", "field isn't unset")
}
func TestMessageField_UnmarshalJSON(t *testing.T) {
t.Parallel()
type test struct {
name string
input []byte
output messageField
expectErr bool
}
tt := []test{
{name: "Valid input", input: []byte("\"\""), output: mustMessageField(t, "")},
{name: "Null input", input: []byte("null")},
{name: "Invalid JSON value", input: []byte("525482"), expectErr: true},
{name: "Invalid input", input: []byte("\"multi\\nline\""), expectErr: true},
}
for _, test := range tt {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
id := messageField{}
err := id.UnmarshalJSON(test.input)
if test.expectErr {
tests.Expect(t, err != nil, "expected error")
} else {
tests.Equal(t, err, nil, "unexpected error")
}
tests.Equal(t, id, test.output, "unexpected unmarshal result")
})
}
}
func TestMessageField_UnmarshalText(t *testing.T) {
t.Parallel()
var id messageField
err := id.UnmarshalText([]byte(""))
tests.Equal(t, id, mustMessageField(t, ""), "unexpected unmarshal result")
tests.Equal(t, err, nil, "unexpected error")
err = id.UnmarshalText([]byte("in\nvalid"))
tests.Expect(t, err != nil, "expected error")
tests.Expect(t, !id.IsSet() && id.String() == "", "ID is not unset after invalid unmarshal")
}
func TestMessageField_MarshalJSON(t *testing.T) {
t.Parallel()
var id messageField
v, err := id.MarshalJSON()
tests.Equal(t, err, nil, "unexpected error")
tests.Equal(t, string(v), "null", "invalid JSON result")
id = mustMessageField(t, "")
v, err = id.MarshalJSON()
tests.Equal(t, err, nil, "unexpected error")
tests.Equal(t, string(v), "\"\"", "invalid JSON result")
}
func TestMessageField_MarshalText(t *testing.T) {
t.Parallel()
var id messageField
v, err := id.MarshalText()
tests.Expect(t, err != nil, "expected error")
tests.DeepEqual(t, v, nil, "invalid result")
id = mustMessageField(t, "")
v, err = id.MarshalText()
tests.Equal(t, err, nil, "unexpected error")
tests.DeepEqual(t, v, []byte{}, "unexpected result")
}
func TestMessageField_Scan(t *testing.T) {
t.Parallel()
var id messageField
err := id.Scan(nil)
tests.Equal(t, err, nil, "unexpected error")
tests.Equal(t, id, messageField{}, "unexpected result")
err = id.Scan("")
tests.Equal(t, err, nil, "unexpected error")
tests.Equal(t, id, mustMessageField(t, ""), "unexpected result")
err = id.Scan([]byte(""))
tests.Equal(t, err, nil, "unexpected error")
tests.Equal(t, id, mustMessageField(t, ""), "unexpected result")
err = id.Scan(5)
tests.Expect(t, err != nil, "expected error")
tests.Equal(t, id, messageField{}, "invalid result")
}
func TestMessageField_Value(t *testing.T) {
t.Parallel()
var id messageField
v, err := id.Value()
tests.Equal(t, err, nil, "unexpected error")
tests.Equal(t, v, nil, "unexpected value")
id = mustMessageField(t, "")
v, err = id.Value()
tests.Equal(t, err, nil, "unexpected error")
tests.Equal(t, v, "", "unexpected value")
}
func TestFieldConstructors(t *testing.T) {
t.Parallel()
_, err := NewID("a\nb")
tests.Equal(t, err.Error(), "invalid event ID: input is multiline", "unexpected error message")
_, err = NewType("a\nb")
tests.Equal(t, err.Error(), "invalid event type: input is multiline", "unexpected error message")
tests.Panics(t, func() { ID("a\nb") }, "id creation should panic")
tests.Panics(t, func() { Type("a\nb") }, "id creation should panic")
}