forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 4
/
field_map_test.go
139 lines (108 loc) · 2.82 KB
/
field_map_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
package quickfix
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFieldMap_Clear(t *testing.T) {
var fMap FieldMap
fMap.init()
fMap.SetField(1, FIXString("hello"))
fMap.SetField(2, FIXString("world"))
fMap.Clear()
if fMap.Has(1) || fMap.Has(2) {
t.Error("All fields should be cleared")
}
}
func TestFieldMap_DeleteTag(t *testing.T) {
var fMap FieldMap
fMap.init()
fMap.SetField(1, FIXString("hello"))
fMap.SetField(2, FIXString("world"))
fMap.DeleteTag(1)
assert.False(t, fMap.Has(1))
assert.True(t, fMap.Has(2))
}
func TestFieldMap_SetAndGet(t *testing.T) {
var fMap FieldMap
fMap.init()
fMap.SetField(1, FIXString("hello"))
fMap.SetField(2, FIXString("world"))
var testCases = []struct {
tag Tag
expectErr bool
expectValue string
}{
{tag: 1, expectValue: "hello"},
{tag: 2, expectValue: "world"},
{tag: 44, expectErr: true},
}
for _, tc := range testCases {
var testField FIXString
err := fMap.GetField(tc.tag, &testField)
if tc.expectErr {
assert.NotNil(t, err, "Expected Error")
continue
}
assert.Nil(t, err, "Unexpected error")
assert.Equal(t, tc.expectValue, string(testField))
}
}
func TestFieldMap_Length(t *testing.T) {
var fMap FieldMap
fMap.init()
fMap.SetField(1, FIXString("hello"))
fMap.SetField(2, FIXString("world"))
fMap.SetField(8, FIXString("FIX.4.4"))
fMap.SetField(9, FIXInt(100))
fMap.SetField(10, FIXString("100"))
assert.Equal(t, 16, fMap.length(), "Length should include all fields but beginString, bodyLength, and checkSum")
}
func TestFieldMap_Total(t *testing.T) {
var fMap FieldMap
fMap.init()
fMap.SetField(1, FIXString("hello"))
fMap.SetField(2, FIXString("world"))
fMap.SetField(8, FIXString("FIX.4.4"))
fMap.SetField(Tag(9), FIXInt(100))
fMap.SetField(10, FIXString("100"))
assert.Equal(t, 2116, fMap.total(), "Total should includes all fields but checkSum")
}
func TestFieldMap_TypedSetAndGet(t *testing.T) {
var fMap FieldMap
fMap.init()
fMap.SetString(1, "hello")
fMap.SetInt(2, 256)
s, err := fMap.GetString(1)
assert.Nil(t, err)
assert.Equal(t, "hello", s)
i, err := fMap.GetInt(2)
assert.Nil(t, err)
assert.Equal(t, 256, i)
_, err = fMap.GetInt(1)
assert.NotNil(t, err, "Type mismatch should occur error")
s, err = fMap.GetString(2)
assert.Nil(t, err)
assert.Equal(t, "256", s)
b, err := fMap.GetBytes(1)
assert.Nil(t, err)
assert.True(t, bytes.Equal([]byte("hello"), b))
}
func TestFieldMap_BoolTypedSetAndGet(t *testing.T) {
var fMap FieldMap
fMap.init()
fMap.SetBool(1, true)
v, err := fMap.GetBool(1)
assert.Nil(t, err)
assert.True(t, v)
s, err := fMap.GetString(1)
assert.Nil(t, err)
assert.Equal(t, "Y", s)
fMap.SetBool(2, false)
v, err = fMap.GetBool(2)
assert.Nil(t, err)
assert.False(t, v)
s, err = fMap.GetString(2)
assert.Nil(t, err)
assert.Equal(t, "N", s)
}