-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtest_fixtures.rb
86 lines (64 loc) · 2.04 KB
/
test_fixtures.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
##
# to run use
# ruby -I ./lib -I ./test test/test_fixtures.rb
require 'helper'
class TestFixtures < MiniTest::Test
## little helper to (recursively) convert 0x hex strings to binary strings
## other option to use binary in yaml is using !!binary with base64 encoding
def hex_to_bin( arg )
if arg.is_a?( Array )
arg.map { |item| hex_to_bin( item ) }
elsif arg.is_a?( String ) && arg =~ /\A0x[0-9a-fA-F]{2,}\z/
hex = arg[2..-1]
bin = [hex].pack("H*")
puts " converting hex >#{arg}< to bin >#{bin.inspect}< #{bin.size} byte(s)"
bin
else
arg
end
end
def assert_test( test )
types = test['types']
args = test['args']
hex = test['data']
puts "==> testing #{types}...:"
args = hex_to_bin( args )
types.zip( args ).each_with_index do |(type,arg),i|
puts " [#{i}] #{type} => #{arg.inspect}"
## pp ABI::Type.parse( type )
end
=begin
## quick hack for bytes / bytes10 etc.
## change encoding to BINARAY / ASCII_8BIT
## not really possible with yaml fixtures
args = types.zip(args).map do |(type,arg)|
arg = arg.b if ['bytes', 'bytes10'].include?( type )
arg
end
=end
if hex
data = hex( hex ) ## convert hex string to binary string
bin = ABI.encode( types, args )
assert bin.encoding == Encoding::BINARY ## note: always check for BINARY encoding too
assert_equal data, bin
assert_equal args, ABI.decode( types, data )
end
assert_equal args, ABI.decode( types, ABI.encode( types, args ))
end
def test_basic
tests = read_yml( '../test/abicoder/basic.yml' )
puts " #{tests.size} test(s) in /test/abicoder/basic.yml"
pp tests
tests.each do |test|
assert_test test
end
end
def test_more
tests = read_yml( '../test/abicoder/more.yml' )
puts " #{tests.size} test(s) in /test/abicoder/more.yml"
pp tests
tests.each do |test|
assert_test test
end
end
end ## class TestFixtures