-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtest_types.rb
171 lines (124 loc) · 5.65 KB
/
test_types.rb
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
##
# to run use
# ruby -I ./lib -I ./test test/test_types.rb
require 'helper'
class TestTypes < MiniTest::Test
Type = ABI::Type
Tuple = ABI::Tuple
Parser = ABI::Type::Parser
ParseError = ABI::Type::ParseError
def test_parse_dims
assert_equal [], Parser._parse_dims( '' )
assert_equal [-1], Parser._parse_dims( '[]' )
assert_equal [-1,3], Parser._parse_dims( '[][3]' )
assert_equal [2,3], Parser._parse_dims( '[2][3]' )
end
def test_parse_base_type
assert_equal ['uint', 256, []], Parser._parse_base_type( 'uint256' )
assert_equal ['uint', 8, []], Parser._parse_base_type( 'uint8' )
assert_equal ['string', nil, []], Parser._parse_base_type( 'string' )
assert_equal ['uint', 256, [-1]], Parser._parse_base_type( 'uint256[]' )
assert_equal ['uint', 256, [-1,3]], Parser._parse_base_type( 'uint256[][3]' )
assert_equal ['string', nil, [-1]], Parser._parse_base_type( 'string[]' )
end
def test_base
t = Type.parse( 'uint256' )
assert_equal 'uint256', t.format
pp t
assert_equal ABI::Uint, t.class
assert_equal 256, t.bits # subscript of type (size in bits)
assert_equal false, t.dynamic?
assert_equal 32, t.size
t = Type.parse( 'string' )
assert_equal 'string', t.format
pp t
assert_equal ABI::String, t.class
assert_equal true, t.dynamic?
assert_nil t.size ## note: size always nil if type dynamic
end
def test_parse_tuple_type
assert_equal ['string', 'string', 'bool'], Parser._parse_tuple_type('string,string,bool')
assert_equal ['string', '(string,bool)'], Parser._parse_tuple_type('string,(string,bool)')
assert_equal ['string', '(string,(string,uint256[]))','address[4]'],
Parser._parse_tuple_type('string,(string,(string,uint256[])),address[4]')
end
def test_tuple
t = Type.parse( '(string,string,bool)' )
assert_equal '(string,string,bool)', t.format
pp t
assert_equal ABI::Tuple, t.class
assert_equal ['string', 'string', 'bool'], t.types.map {|c| c.format }
assert_equal true, t.dynamic? ## because it incl. string
assert_nil t.size ## note: size always nil if type dynamic
t = Type.parse('(string,(string,(string,uint256[])),address[4])' )
assert_equal '(string,(string,(string,uint256[])),address[4])', t.format
pp t
assert_equal ABI::Tuple, t.class
assert_equal ['string', '(string,(string,uint256[]))', 'address[4]'], t.types.map {|c| c.format }
assert_equal true, t.dynamic? ## because it incl. string
assert_nil t.size ## note: size always nil if type dynamic
end
####
## tests initially adapted from
## https://github.com/cryptape/ruby-ethereum-abi/blob/master/test/abi/type_test.rb
def test_type_parse
assert_equal ABI::Uint.new( 8 ), Type.parse("uint8")
assert_equal ABI::FixedBytes.new( 32 ), Type.parse("bytes32")
assert_equal ABI::FixedArray.new(
ABI::Uint.new( 256 ), 10 ), Type.parse("uint256[10]")
## assert_equal Type.new('fixed', [128,128]', [1,2,3,0]), Type.parse("fixed128x128[1][2][3][]")
end
def test_type_parse_validations
assert_raises(ParseError) { Type.parse("string8") }
assert_raises(ParseError) { Type.parse("bytes33") }
assert_raises(ParseError) { Type.parse('hash')}
assert_raises(ParseError) { Type.parse('address8') }
assert_raises(ParseError) { Type.parse('bool8') }
assert_raises(ParseError) { Type.parse('decimal') }
assert_raises(ParseError) { Type.parse("int") }
assert_raises(ParseError) { Type.parse("int2") }
assert_raises(ParseError) { Type.parse("int20") }
assert_raises(ParseError) { Type.parse("int512") }
assert_raises(ParseError) { Type.parse("fixed") }
assert_raises(ParseError) { Type.parse("fixed256") }
assert_raises(ParseError) { Type.parse("fixed2x2") }
assert_raises(ParseError) { Type.parse("fixed20x20") }
assert_raises(ParseError) { Type.parse("fixed256x256") }
end
def test_type_size
assert_nil Type.parse("string").size
assert_nil Type.parse("bytes").size
assert_nil Type.parse("uint256[]").size
assert_nil Type.parse("uint256[4][]").size
assert_equal 32, Type.parse("uint256").size
assert_equal 32, Type.parse("bool").size
assert_equal 64, Type.parse("uint256[2]").size
assert_equal 128, Type.parse("address[2][2]").size
assert_equal 32*2, Type.parse( "bytes3[2]" ).size
## assert_equal 32, Type.parse("fixed128x128").size
## assert_equal 1024, Type.parse("ufixed192x64[2][2][2][2][2]").size
end
def test_subtype_of_array
t = Type.parse("uint256[2][]")
puts "uint256[2][]:"
puts t.format
pp t
##
#<ABI::Array @subtype=
# <ABI::FixedArray @dim=2, @subtype=
# <ABI::Uint @bits=256>>>
assert_equal 2, t.subtype.dim
assert_equal ABI::Array, t.class ## note: outer most returns first
assert_equal ABI::FixedArray, t.subtype.class ## note: inner most returned last!!
assert_equal ABI::Uint, t.subtype.subtype.class
t = Type.parse("uint256[2][3]")
puts "uint256[2][3]:"
puts t.format
pp t
assert_equal 3, t.dim ## note: outer most returns first
assert_equal 2, t.subtype.dim ## note: inner most returned last!!
assert_equal ABI::FixedArray, t.class
assert_equal ABI::FixedArray, t.subtype.class
assert_equal ABI::Uint, t.subtype.subtype.class
end
end # class TestTypes