-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarshal_test.go
379 lines (320 loc) · 9.51 KB
/
marshal_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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package marshal_test
import (
"bytes"
"testing"
"time"
"github.com/quickfixgo/marshal/fix50sp1/instrument"
"github.com/quickfixgo/marshal/fix50sp1/marketdatasnapshotfullrefresh"
"github.com/quickfixgo/marshal/fix50sp1/mdfullgrp"
"github.com/quickfixgo/quickfix"
"github.com/quickfixgo/quickfix/enum"
"github.com/quickfixgo/quickfix/field"
)
func TestMarshal_FIXMsgType(t *testing.T) {
type Message struct {
FIXMsgType string `fix:"0"`
}
var m Message
fixMsg := quickfix.Marshal(m)
fixMsg.Build()
var msgType field.MsgTypeField
fixMsg.Header.Get(&msgType)
if msgType.FIXString != "0" {
t.Errorf("Expected %v got %v", "1", msgType)
}
}
func TestMarshal_Literals(t *testing.T) {
type Message struct {
FIXMsgType string `fix:"0"`
StringField string `fix:"114"`
IntField int `fix:"115"`
UnsetIntField int `fix:"116"`
BoolField bool `fix:"117"`
FloatField float64 `fix:"118"`
UTCTimestampField time.Time `fix:"119"`
}
tVal, _ := time.Parse("2006-Jan-02", "2014-Jun-16")
m := Message{
StringField: "world",
IntField: 5,
BoolField: true,
FloatField: 100.02,
UTCTimestampField: tVal,
}
fixMsg := quickfix.Marshal(m)
var tests = []struct {
tag quickfix.Tag
fieldValue quickfix.FieldValue
expected []byte
}{
{quickfix.Tag(114), new(quickfix.FIXString), []byte("world")},
{quickfix.Tag(115), new(quickfix.FIXInt), []byte("5")},
{quickfix.Tag(116), new(quickfix.FIXInt), []byte("0")},
{quickfix.Tag(117), new(quickfix.FIXBoolean), []byte("Y")},
{quickfix.Tag(118), new(quickfix.FIXFloat), []byte("100.02")},
{quickfix.Tag(119), new(quickfix.FIXUTCTimestamp), []byte("20140616-00:00:00.000")},
}
for _, test := range tests {
if err := fixMsg.Body.GetField(test.tag, test.fieldValue); err != nil {
t.Error("Unexpected error", err)
}
if !bytes.Equal(test.expected, test.fieldValue.Write()) {
t.Errorf("Expected %s got %s", test.expected, test.fieldValue.Write())
}
}
}
func TestMarshal_LiteralsOptional(t *testing.T) {
type Message struct {
FIXMsgType string `fix:"0"`
StringPtrField *string `fix:"112"`
UnsetStringPtrField *string `fix:"113"`
IntPtrField *int `fix:"115"`
BoolPtrField *bool `fix:"117"`
FloatPtrField *float64 `fix:"119"`
}
strVal := "hello"
intVal := 42
boolVal := false
floatVal := 456.123
m := Message{
StringPtrField: &strVal,
IntPtrField: &intVal,
BoolPtrField: &boolVal,
FloatPtrField: &floatVal,
}
fixMsg := quickfix.Marshal(m)
var tests = []struct {
tag quickfix.Tag
fieldValue quickfix.FieldValue
fieldSet bool
expected []byte
}{
{quickfix.Tag(112), new(quickfix.FIXString), true, []byte("hello")},
{quickfix.Tag(113), new(quickfix.FIXString), false, []byte{}},
{quickfix.Tag(115), new(quickfix.FIXInt), true, []byte("42")},
{quickfix.Tag(117), new(quickfix.FIXBoolean), true, []byte("N")},
{quickfix.Tag(119), new(quickfix.FIXFloat), true, []byte("456.123")},
}
for _, test := range tests {
if !test.fieldSet {
if fixMsg.Body.Has(test.tag) {
t.Error("Unexpected field for tag", test.tag)
}
continue
}
if err := fixMsg.Body.GetField(test.tag, test.fieldValue); err != nil {
t.Error("Unexpected error", err)
}
if !bytes.Equal(test.expected, test.fieldValue.Write()) {
t.Errorf("Expected %s got %s", test.expected, test.fieldValue.Write())
}
}
}
func TestMarshal_Components(t *testing.T) {
type Component1 struct {
StringField string `fix:"8"`
}
type Component2 struct {
IntField int `fix:"1"`
}
type Message struct {
FIXMsgType string `fix:"0"`
Component1
StringField string `fix:"112"`
*Component2
}
m := Message{
Component1: Component1{StringField: "hello"},
StringField: "world",
}
fixMsg := quickfix.Marshal(m)
var tests = []struct {
tag quickfix.Tag
fieldValue quickfix.FieldValue
fieldSet bool
expected []byte
}{
{quickfix.Tag(8), new(quickfix.FIXString), true, []byte("hello")},
{quickfix.Tag(1), new(quickfix.FIXInt), false, []byte{}},
{quickfix.Tag(112), new(quickfix.FIXString), true, []byte("world")},
}
for _, test := range tests {
if !test.fieldSet {
if fixMsg.Body.Has(test.tag) {
t.Error("Unexpected field for tag", test.tag)
}
continue
}
if err := fixMsg.Body.GetField(test.tag, test.fieldValue); err != nil {
t.Error("Unexpected error", err)
continue
}
if !bytes.Equal(test.expected, test.fieldValue.Write()) {
t.Errorf("Expected %s got %s", test.expected, test.fieldValue.Write())
}
}
}
func TestMarshal_RepeatingGroups(t *testing.T) {
type Group1 struct {
StringField1 string `fix:"8"`
StringField2 *string `fix:"9"`
}
type AnonymousGroup struct {
IntField3 int `fix:"3"`
}
type GroupComponent1 struct {
}
type GroupComponent2 struct {
}
type Group2 struct {
IntField1 int `fix:"1"`
IntField2 int `fix:"2, omitempty"`
AnonymousGroup
GroupComponent1
OptionalComponent *GroupComponent2
}
type Message struct {
FIXMsgType string `fix:"0"`
GroupField1 []Group1 `fix:"100"`
StringField string `fix:"112"`
GroupField2 []Group2 `fix:"101"`
}
s := "world"
m := Message{
GroupField1: []Group1{{StringField1: "hello", StringField2: &s}, {StringField1: "goodbye"}, {StringField1: "OHHAI", StringField2: &s}},
StringField: "world",
GroupField2: []Group2{{IntField1: 1, IntField2: 42, AnonymousGroup: AnonymousGroup{IntField3: 44}}},
}
fixMsg := quickfix.Marshal(m)
group1Template := quickfix.GroupTemplate{
quickfix.GroupElement(quickfix.Tag(8)),
quickfix.GroupElement(quickfix.Tag(9)),
}
group2Template := quickfix.GroupTemplate{
quickfix.GroupElement(quickfix.Tag(1)),
quickfix.GroupElement(quickfix.Tag(2)),
quickfix.GroupElement(quickfix.Tag(3)),
}
var tests = []struct {
groupTag quickfix.Tag
expectedGroupSize int
template quickfix.GroupTemplate
}{
{quickfix.Tag(100), 3, group1Template},
{quickfix.Tag(101), 1, group2Template},
}
for _, test := range tests {
if !fixMsg.Body.Has(test.groupTag) {
t.Errorf("Expected tag %v", test.groupTag)
continue
}
groupField := quickfix.NewRepeatingGroup(test.groupTag, test.template)
if err := fixMsg.Body.GetGroup(groupField); err != nil {
t.Error("Unexpected error", err)
continue
}
if groupField.Len() != test.expectedGroupSize {
t.Errorf("Expected group %v to have size %v, got %v", test.groupTag, test.expectedGroupSize, groupField.Len())
}
}
}
func TestMarshal_Default(t *testing.T) {
type Message struct {
FIXMsgType string `fix:"0"`
StringField1 string `fix:"10,default=hello"`
StringField2 *string `fix:"11,default=world"`
}
m := Message{}
fixMsg := quickfix.Marshal(m)
var tests = []struct {
quickfix.Tag
expected string
}{
{quickfix.Tag(10), "hello"},
{quickfix.Tag(11), "world"},
}
for _, test := range tests {
var f quickfix.FIXString
if err := fixMsg.Body.GetField(test.Tag, &f); err != nil {
t.Errorf("Unexpected Error for tag %v: %v", test.Tag, err)
}
if string(f) != test.expected {
t.Errorf("Expected default value %v got %v", test.expected, f)
}
}
}
func TestMarshal_OmitEmpty(t *testing.T) {
type Group1 struct {
StringField1 string `fix:"8"`
StringField2 *string `fix:"9"`
}
type Group2 struct {
IntField1 int `fix:"1"`
IntField2 int `fix:"2"`
}
type Message struct {
FIXMsgType string `fix:"0"`
GroupField1 []Group1 `fix:"100"`
StringField string `fix:"112"`
GroupField2 []Group2 `fix:"101,omitempty"`
}
m := Message{}
fixMsg := quickfix.Marshal(m)
if fixMsg.Body.Has(quickfix.Tag(101)) {
t.Error("Unexpected tag for empty group")
}
f := new(quickfix.FIXInt)
if err := fixMsg.Body.GetField(quickfix.Tag(100), f); err != nil {
t.Errorf("Unexpected Error %v", err)
}
if *f != 0 {
t.Errorf("Expected 0 got %v", *f)
}
}
func TestMarshal_HeaderTrailer(t *testing.T) {
type Header struct {
StringField string `fix:"49"`
}
type Trailer struct {
IntField int `fix:"89"`
}
type Message struct {
FIXMsgType string `fix:"0"`
StringField string `fix:"112"`
Header
Trailer
}
m := Message{
Header: Header{StringField: "hello"},
StringField: "world",
Trailer: Trailer{IntField: 3},
}
fixMsg := quickfix.Marshal(m)
var tests = []struct {
tag quickfix.Tag
fieldValue quickfix.FieldValue
fixMsgPart quickfix.FieldMap
expected []byte
}{
{quickfix.Tag(35), new(quickfix.FIXString), fixMsg.Header.FieldMap, []byte("0")},
{quickfix.Tag(49), new(quickfix.FIXString), fixMsg.Header.FieldMap, []byte("hello")},
{quickfix.Tag(112), new(quickfix.FIXString), fixMsg.Body.FieldMap, []byte("world")},
{quickfix.Tag(89), new(quickfix.FIXInt), fixMsg.Trailer.FieldMap, []byte("3")},
}
for _, test := range tests {
if err := test.fixMsgPart.GetField(test.tag, test.fieldValue); err != nil {
t.Error("Unexpected error", err)
}
if !bytes.Equal(test.expected, test.fieldValue.Write()) {
t.Errorf("Expected '%s' got '%s'", test.expected, test.fieldValue.Write())
}
}
}
// [GH 109] https://github.com/quickfixgo/quickfix/issues/109
func TestGH109_MarshalWillNotRecurseIntoTaggedStructFieldsAndCausePanic(t *testing.T) {
// When a message contains a non-component struct field (i.e. NoMDEntries.ExpireTime has type *time.Time)
grp := mdfullgrp.New([]mdfullgrp.NoMDEntries{{MDEntryType: enum.MDEntryType_BID}})
msg := marketdatasnapshotfullrefresh.New(instrument.Instrument{}, *grp)
// Then parseStructTag should not cause `panic: strconv.ParseInt: parsing "": invalid syntax` when marshalling
msg.Marshal()
}