-
Notifications
You must be signed in to change notification settings - Fork 2
/
common_test.go
83 lines (68 loc) · 2.57 KB
/
common_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
package goscale
import (
"bytes"
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
type encodableType struct{}
func (e encodableType) Encode(buffer *bytes.Buffer) error {
return errors.New("error")
}
func (e encodableType) Bytes() []byte {
return []byte{}
}
func Test_EncodeEach(t *testing.T) {
buffer := &bytes.Buffer{}
err := EncodeEach(buffer, Bool(true), U8(127), I16(-128))
assert.NoError(t, err)
assert.Equal(t, []byte{0x01, 0x7f, 0x80, 0xff}, buffer.Bytes())
}
func Test_EncodeEach_Error(t *testing.T) {
buffer := &bytes.Buffer{}
err := EncodeEach(buffer, Bool(true), encodableType{}, U8(127))
assert.Error(t, err)
assert.Equal(t, []byte{0x01}, buffer.Bytes())
}
func Test_ToCompact(t *testing.T) {
var examples = []struct {
label string
input interface{}
expect Compact
}{
{label: "ToCompact(int)", input: 1, expect: Compact{NewU128(1)}},
{label: "ToCompact(uint)", input: uint(2), expect: Compact{NewU128(2)}},
{label: "ToCompact(int8)", input: int8(3), expect: Compact{NewU8(3)}},
{label: "ToCompact(I8)", input: I8(4), expect: Compact{NewU8(4)}},
{label: "ToCompact(uint8)", input: uint8(5), expect: Compact{NewU8(5)}},
{label: "ToCompact(U8)", input: U8(6), expect: Compact{NewU8(6)}},
{label: "ToCompact(int16)", input: int16(7), expect: Compact{NewU16(7)}},
{label: "ToCompact(I16)", input: I16(8), expect: Compact{NewU16(8)}},
{label: "ToCompact(uint16)", input: uint16(9), expect: Compact{NewU16(9)}},
{label: "ToCompact(U16)", input: U16(10), expect: Compact{NewU16(10)}},
{label: "ToCompact(int32)", input: int32(11), expect: Compact{NewU32(11)}},
{label: "ToCompact(I32)", input: I32(12), expect: Compact{NewU32(12)}},
{label: "ToCompact(uint32)", input: uint32(13), expect: Compact{NewU32(13)}},
{label: "ToCompact(U32)", input: U32(14), expect: Compact{NewU32(14)}},
{label: "ToCompact(int64)", input: int64(15), expect: Compact{NewU64(15)}},
{label: "ToCompact(I64)", input: I64(16), expect: Compact{NewU64(16)}},
{label: "ToCompact(uint64)", input: uint64(17), expect: Compact{NewU64(17)}},
{label: "ToCompact(U64)", input: U64(18), expect: Compact{NewU64(18)}},
{label: "ToCompact(I128)", input: NewI128(19), expect: Compact{NewU128(19)}},
{label: "ToCompact(U128)", input: NewU128(20), expect: Compact{NewU128(20)}},
}
for _, e := range examples {
t.Run(e.label, func(t *testing.T) {
result := ToCompact(e.input)
assert.Equal(t, e.expect, result)
})
}
}
func Test_ToCompact_Panics(t *testing.T) {
assert.PanicsWithValue(t,
"invalid numeric type in ToCompact()",
func() {
ToCompact([]byte{})
},
)
}