aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/types_test.go
blob: 9ef7b8d389d79e6224b9a0f3eec568c9d7026a1e (plain) (blame)
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
package rpc

import (
    "bytes"
    "encoding/json"
    "math/big"
    "testing"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/types"
)

func TestInvalidTypeError(t *testing.T) {
    err := NewInvalidTypeError("testField", "not string")
    expected := "invalid type on field testField: not string"

    if err.Error() != expected {
        t.Error(err.Error())
    }
}

func TestInsufficientParamsError(t *testing.T) {
    err := NewInsufficientParamsError(0, 1)
    expected := "insufficient params, want 1 have 0"

    if err.Error() != expected {
        t.Error(err.Error())
    }
}

func TestNotImplementedError(t *testing.T) {
    err := NewNotImplementedError("foo")
    expected := "foo method not implemented"

    if err.Error() != expected {
        t.Error(err.Error())
    }
}

func TestDecodeParamError(t *testing.T) {
    err := NewDecodeParamError("foo")
    expected := "could not decode, foo"

    if err.Error() != expected {
        t.Error(err.Error())
    }
}

func TestValidationError(t *testing.T) {
    err := NewValidationError("foo", "should be `bar`")
    expected := "foo not valid, should be `bar`"

    if err.Error() != expected {
        t.Error(err.Error())
    }
}

func TestHexdataMarshalNil(t *testing.T) {
    hd := newHexData([]byte{})
    hd.isNil = true
    v, _ := json.Marshal(hd)
    if string(v) != "null" {
        t.Errorf("Expected null, got %s", v)
    }
}

func TestHexnumMarshalNil(t *testing.T) {
    hn := newHexNum([]byte{})
    hn.isNil = true
    v, _ := json.Marshal(hn)
    if string(v) != "null" {
        t.Errorf("Expected null, got %s", v)
    }
}

func TestHexdataNil(t *testing.T) {
    v := newHexData(nil)
    if v.isNil != true {
        t.Errorf("Expected isNil to be true, but is %v", v.isNil)
    }
}

func TestHexdataPtrHash(t *testing.T) {
    in := common.Hash{0, 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}
    v := newHexData(&in)
    if bytes.Compare(in.Bytes(), v.data) != 0 {
        t.Errorf("Got % x expected % x", in, v.data)
    }
}

func TestHexdataPtrHashNil(t *testing.T) {
    var in *common.Hash
    in = nil
    v := newHexData(in)
    if !v.isNil {
        t.Errorf("Expect isNil to be true, but is %v", v.isNil)
    }
}

func TestHexdataPtrAddress(t *testing.T) {
    in := common.Address{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
    v := newHexData(&in)
    if bytes.Compare(in.Bytes(), v.data) != 0 {
        t.Errorf("Got % x expected % x", in, v.data)
    }
}

func TestHexdataPtrAddressNil(t *testing.T) {
    var in *common.Address
    in = nil
    v := newHexData(in)
    if !v.isNil {
        t.Errorf("Expect isNil to be true, but is %v", v.isNil)
    }
}

func TestHexdataPtrBloom(t *testing.T) {
    in := types.Bloom{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
    v := newHexData(&in)
    if bytes.Compare(in.Bytes(), v.data) != 0 {
        t.Errorf("Got % x expected % x", in, v.data)
    }
}

func TestHexdataPtrBloomNil(t *testing.T) {
    var in *types.Bloom
    in = nil
    v := newHexData(in)
    if !v.isNil {
        t.Errorf("Expect isNil to be true, but is %v", v.isNil)
    }
}

func TestHexdataBigintNil(t *testing.T) {
    var in *big.Int
    in = nil
    v := newHexData(in)
    if !v.isNil {
        t.Errorf("Expect isNil to be true, but is %v", v.isNil)
    }
}

func TestHexdataUint(t *testing.T) {
    var in = uint(16)
    var expected = []byte{0x10}
    v := newHexData(in)
    if bytes.Compare(expected, v.data) != 0 {
        t.Errorf("Expected % x got % x", expected, v.data)
    }
}

func TestHexdataInt8(t *testing.T) {
    var in = int8(16)
    var expected = []byte{0x10}
    v := newHexData(in)
    if bytes.Compare(expected, v.data) != 0 {
        t.Errorf("Expected % x got % x", expected, v.data)
    }
}

func TestHexdataUint8(t *testing.T) {
    var in = uint8(16)
    var expected = []byte{0x10}
    v := newHexData(in)
    if bytes.Compare(expected, v.data) != 0 {
        t.Errorf("Expected % x got % x", expected, v.data)
    }
}

func TestHexdataInt16(t *testing.T) {
    var in = int16(16)
    var expected = []byte{0x10}
    v := newHexData(in)
    if bytes.Compare(expected, v.data) != 0 {
        t.Errorf("Expected % x got % x", expected, v.data)
    }
}

func TestHexdataUint16(t *testing.T) {
    var in = uint16(16)
    var expected = []byte{0x0, 0x10}
    v := newHexData(in)
    if bytes.Compare(expected, v.data) != 0 {
        t.Errorf("Expected % x got % x", expected, v.data)
    }
}

func TestHexdataInt32(t *testing.T) {
    var in = int32(16)
    var expected = []byte{0x10}
    v := newHexData(in)
    if bytes.Compare(expected, v.data) != 0 {
        t.Errorf("Expected % x got % x", expected, v.data)
    }
}

func TestHexdataUint32(t *testing.T) {
    var in = uint32(16)
    var expected = []byte{0x0, 0x0, 0x0, 0x10}
    v := newHexData(in)
    if bytes.Compare(expected, v.data) != 0 {
        t.Errorf("Expected % x got % x", expected, v.data)
    }
}