-
Notifications
You must be signed in to change notification settings - Fork 217
/
encoder_test.go
90 lines (69 loc) · 2.09 KB
/
encoder_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
package eos
import (
"bytes"
"encoding/hex"
"math"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEncoder_Float64Inf(t *testing.T) {
buffer := bytes.NewBuffer(nil)
encoder := NewEncoder(buffer)
err := encoder.Encode(math.Inf(-1))
require.NoError(t, err)
out := hex.EncodeToString(buffer.Bytes())
require.Equal(t, "000000000000f0ff", out)
}
func TestEncoder_Float64NaN(t *testing.T) {
buffer := bytes.NewBuffer(nil)
encoder := NewEncoder(buffer)
err := encoder.Encode(math.NaN())
require.NoError(t, err)
out := hex.EncodeToString(buffer.Bytes())
require.Equal(t, "010000000000f87f", out)
}
func TestEncoder_MapStringString(t *testing.T) {
buffer := bytes.NewBuffer(nil)
encoder := NewEncoder(buffer)
data := map[string]string{
"a": "1",
"b": "2",
}
err := encoder.Encode(data)
require.NoError(t, err)
out := hex.EncodeToString(buffer.Bytes())
// Sadly, we cannot do much for map not ordered here, so let's check that it's either one or the other
expected1 := "020162013201610131"
expected2 := "020161013101620132"
if out != expected1 && out != expected2 {
require.Fail(t, "encoded map is invalid", "must be either %q or %q, got %q", expected1, expected2, out)
}
}
func Test_OptionalPrimitiveType(t *testing.T) {
type test struct {
ID uint64 `eos:"optional"`
}
out, err := MarshalBinary(test{ID: 0})
require.NoError(t, err)
assert.Equal(t, []byte{0x0}, out)
out, err = MarshalBinary(test{ID: 10})
require.NoError(t, err)
assert.Equal(t, []byte{0x1, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, out)
}
func Test_OptionalPointerToPrimitiveType(t *testing.T) {
type test struct {
ID *Uint64 `eos:"optional"`
}
out, err := MarshalBinary(test{ID: nil})
require.NoError(t, err)
assert.Equal(t, []byte{0x0}, out)
id := Uint64(0)
out, err = MarshalBinary(test{ID: &id})
require.NoError(t, err)
assert.Equal(t, []byte{0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, out)
id = Uint64(10)
out, err = MarshalBinary(test{ID: &id})
require.NoError(t, err)
assert.Equal(t, []byte{0x1, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, out)
}