Debian Codegraph @

ruby/tests/test_data.rb

raw
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17
18
19 require 'test_tools'
20 require "securerandom"
21 require 'qpid_proton'
22
23 class TestData < MiniTest::Test
24 include Qpid::Proton
25
26 def assert_from_to(*values)
27 d = Codec::Data.new
28 values.each do |x|
29 Codec::Data.from_object(d.impl, x)
30 assert_equal x, Codec::Data.to_object(d.impl)
31 end
32 end
33
34 def test_from_to
35 assert_from_to({ 1 => :one, 2=>:two })
36 assert_from_to([{:a => 1, "b" => 2}, 3, 4.4, :five])
37 assert_from_to(Types::UniformArray.new(Types::INT, [1, 2, 3, 4]))
38 end
39
40 def rnum(*arg) SecureRandom.random_number(*arg); end
41 def rstr(*arg) SecureRandom.base64(*arg); end
42
43 def test_nil()
44 assert_nil((Codec::Data.new << nil).object)
45 end
46
47 def assert_convert(*values)
48 values.each { |x| assert_equal x, ((Codec::Data.new << x).object) }
49 end
50
51 def test_bool()
52 assert_convert(true, false)
53 end
54
55 def test_float()
56 assert_convert(0.0, 1.0, -1.0, 1.23e123, rnum(), rnum(), rnum(), rnum())
57 end
58
59 def test_string()
60 assert_convert("", "foo", rstr(100000), rstr(rnum(1000)), rstr(rnum(1000)))
61 end
62
63 def test_symbol()
64 assert_convert(:"", :foo, rstr(256).to_sym)
65 end
66 end