Debian Codegraph @

ruby/tests/test_interop.rb

raw
1 #!/usr/bin/env ruby
2
3 require 'test_tools'
4 require 'qpid_proton'
5
6 if ((RUBY_VERSION.split(".").map {|x| x.to_i} <=> [1, 9]) < 0)
7 require 'pathname'
8 class File
9 def self.absolute_path(name)
10 return Pathname.new(name).realpath
11 end
12 end
13 end
14
15 class InteropTest < MiniTest::Test
16 include Qpid::Proton
17
18 def setup
19 @data = Codec::Data.new
20 @message = Message.new
21 end
22
23 # Walk up the directory tree to find the tests directory.
24 def get_data(name)
25 path = File.join(File.dirname(__FILE__), "../../tests/interop/#{name}.amqp")
26 raise "Can't find test/interop directory from #{__FILE__}" unless File.exist?(path)
27 File.open(path, "rb") { |f| f.read }
28 end
29
30 # Decode encoded bytes as a Data object
31 def decode_data(encoded)
32 buffer = encoded
33 while buffer.size > 0
34 n = @data.decode(buffer)
35 buffer = buffer[n..-1]
36 end
37 @data.rewind
38 reencoded = @data.encode
39 # Test the round-trip re-encoding gives the same result.
40 assert_equal(encoded, reencoded)
41 end
42
43 def decode_data_file(name) decode_data(get_data(name)); end
44
45 def decode_message_file(name)
46 message = Message.new()
47 message.decode(self.get_data(name))
48 self.decode_data(message.body)
49 end
50
51 def assert_next(type, value)
52 assert @data.next
53 assert_equal(type, @data.type)
54 assert_equal(value, @data.object)
55 end
56
57 def assert_array_next(expected)
58 result = @data.next_object
59 assert_equal(expected, result)
60 end
61
62 def test_message
63 decode_message_file("message")
64 assert_next(Codec::STRING, "hello")
65 assert [email protected]
66 end
67
68 def test_primitives
69 decode_data_file("primitives")
70 assert_next(Codec::BOOL, true)
71 assert_next(Codec::BOOL, false)
72 assert_next(Codec::UBYTE, 42)
73 assert_next(Codec::USHORT, 42)
74 assert_next(Codec::SHORT, -42)
75 assert_next(Codec::UINT, 12345)
76 assert_next(Codec::INT, -12345)
77 assert_next(Codec::ULONG, 12345)
78 assert_next(Codec::LONG, -12345)
79 assert_next(Codec::FLOAT, 0.125)
80 assert_next(Codec::DOUBLE, 0.125)
81 assert [email protected]
82 end
83
84 def test_strings
85 decode_data_file("strings")
86 assert_next(Codec::BINARY, "abc\0defg")
87 assert_next(Codec::STRING, "abcdefg")
88 assert_next(Codec::SYMBOL, :abcdefg)
89 assert_next(Codec::BINARY, "")
90 assert_next(Codec::STRING, "")
91 assert_next(Codec::SYMBOL, :"")
92 assert [email protected]
93 end
94
95 def test_described
96 decode_data_file("described")
97 assert_next(Codec::DESCRIBED, Types::Described.new(:"foo-descriptor", "foo-value"))
98 assert(@data.described?)
99 assert_next(Codec::DESCRIBED, Types::Described.new(12, 13))
100 assert(@data.described?)
101 assert [email protected]
102 end
103
104 def test_described_array
105 decode_data_file("described_array")
106 assert_array_next(Types::UniformArray.new(Types::INT, (0...10).to_a, :"int-array"))
107 end
108
109 def test_arrays
110 decode_data_file("arrays")
111 assert_array_next(Types::UniformArray.new(Codec::INT, (0...100).to_a))
112 assert_array_next(Types::UniformArray.new(Codec::STRING, ["a", "b", "c"]))
113 assert_array_next(Types::UniformArray.new(Codec::INT))
114 assert [email protected]
115 end
116
117 def test_lists
118 decode_data_file("lists")
119 assert_next(Codec::LIST, [32, "foo", true])
120 assert_next(Codec::LIST, [])
121 assert [email protected]
122 end
123
124 def test_maps
125 decode_data_file("maps")
126 assert_next(Codec::MAP, {"one" => 1, "two" => 2, "three" => 3 })
127 assert_next(Codec::MAP, {1 => "one", 2 => "two", 3 => "three"})
128 assert_next(Codec::MAP, {})
129 assert [email protected]
130 end
131 end