1ffe3c632Sopenharmony_ci#!/usr/bin/ruby 2ffe3c632Sopenharmony_ci 3ffe3c632Sopenharmony_cirequire 'google/protobuf' 4ffe3c632Sopenharmony_cirequire 'test/unit' 5ffe3c632Sopenharmony_ci 6ffe3c632Sopenharmony_ci# ------------- generated code -------------- 7ffe3c632Sopenharmony_ci 8ffe3c632Sopenharmony_cimodule BasicTest 9ffe3c632Sopenharmony_ci pool = Google::Protobuf::DescriptorPool.new 10ffe3c632Sopenharmony_ci pool.build do 11ffe3c632Sopenharmony_ci add_message "Foo" do 12ffe3c632Sopenharmony_ci optional :bar, :message, 1, "Bar" 13ffe3c632Sopenharmony_ci repeated :baz, :message, 2, "Baz" 14ffe3c632Sopenharmony_ci end 15ffe3c632Sopenharmony_ci 16ffe3c632Sopenharmony_ci add_message "Bar" do 17ffe3c632Sopenharmony_ci optional :msg, :string, 1 18ffe3c632Sopenharmony_ci end 19ffe3c632Sopenharmony_ci 20ffe3c632Sopenharmony_ci add_message "Baz" do 21ffe3c632Sopenharmony_ci optional :msg, :string, 1 22ffe3c632Sopenharmony_ci end 23ffe3c632Sopenharmony_ci 24ffe3c632Sopenharmony_ci add_message "TestMessage" do 25ffe3c632Sopenharmony_ci optional :optional_int32, :int32, 1 26ffe3c632Sopenharmony_ci optional :optional_int64, :int64, 2 27ffe3c632Sopenharmony_ci optional :optional_uint32, :uint32, 3 28ffe3c632Sopenharmony_ci optional :optional_uint64, :uint64, 4 29ffe3c632Sopenharmony_ci optional :optional_bool, :bool, 5 30ffe3c632Sopenharmony_ci optional :optional_float, :float, 6 31ffe3c632Sopenharmony_ci optional :optional_double, :double, 7 32ffe3c632Sopenharmony_ci optional :optional_string, :string, 8 33ffe3c632Sopenharmony_ci optional :optional_bytes, :bytes, 9 34ffe3c632Sopenharmony_ci optional :optional_msg, :message, 10, "TestMessage2" 35ffe3c632Sopenharmony_ci optional :optional_enum, :enum, 11, "TestEnum" 36ffe3c632Sopenharmony_ci 37ffe3c632Sopenharmony_ci repeated :repeated_int32, :int32, 12 38ffe3c632Sopenharmony_ci repeated :repeated_int64, :int64, 13 39ffe3c632Sopenharmony_ci repeated :repeated_uint32, :uint32, 14 40ffe3c632Sopenharmony_ci repeated :repeated_uint64, :uint64, 15 41ffe3c632Sopenharmony_ci repeated :repeated_bool, :bool, 16 42ffe3c632Sopenharmony_ci repeated :repeated_float, :float, 17 43ffe3c632Sopenharmony_ci repeated :repeated_double, :double, 18 44ffe3c632Sopenharmony_ci repeated :repeated_string, :string, 19 45ffe3c632Sopenharmony_ci repeated :repeated_bytes, :bytes, 20 46ffe3c632Sopenharmony_ci repeated :repeated_msg, :message, 21, "TestMessage2" 47ffe3c632Sopenharmony_ci repeated :repeated_enum, :enum, 22, "TestEnum" 48ffe3c632Sopenharmony_ci end 49ffe3c632Sopenharmony_ci add_message "TestMessage2" do 50ffe3c632Sopenharmony_ci optional :foo, :int32, 1 51ffe3c632Sopenharmony_ci end 52ffe3c632Sopenharmony_ci 53ffe3c632Sopenharmony_ci add_message "Recursive1" do 54ffe3c632Sopenharmony_ci optional :foo, :message, 1, "Recursive2" 55ffe3c632Sopenharmony_ci end 56ffe3c632Sopenharmony_ci add_message "Recursive2" do 57ffe3c632Sopenharmony_ci optional :foo, :message, 1, "Recursive1" 58ffe3c632Sopenharmony_ci end 59ffe3c632Sopenharmony_ci 60ffe3c632Sopenharmony_ci add_enum "TestEnum" do 61ffe3c632Sopenharmony_ci value :Default, 0 62ffe3c632Sopenharmony_ci value :A, 1 63ffe3c632Sopenharmony_ci value :B, 2 64ffe3c632Sopenharmony_ci value :C, 3 65ffe3c632Sopenharmony_ci end 66ffe3c632Sopenharmony_ci 67ffe3c632Sopenharmony_ci add_message "BadFieldNames" do 68ffe3c632Sopenharmony_ci optional :dup, :int32, 1 69ffe3c632Sopenharmony_ci optional :class, :int32, 2 70ffe3c632Sopenharmony_ci end 71ffe3c632Sopenharmony_ci 72ffe3c632Sopenharmony_ci add_message "MapMessage" do 73ffe3c632Sopenharmony_ci map :map_string_int32, :string, :int32, 1 74ffe3c632Sopenharmony_ci map :map_string_msg, :string, :message, 2, "TestMessage2" 75ffe3c632Sopenharmony_ci end 76ffe3c632Sopenharmony_ci add_message "MapMessageWireEquiv" do 77ffe3c632Sopenharmony_ci repeated :map_string_int32, :message, 1, "MapMessageWireEquiv_entry1" 78ffe3c632Sopenharmony_ci repeated :map_string_msg, :message, 2, "MapMessageWireEquiv_entry2" 79ffe3c632Sopenharmony_ci end 80ffe3c632Sopenharmony_ci add_message "MapMessageWireEquiv_entry1" do 81ffe3c632Sopenharmony_ci optional :key, :string, 1 82ffe3c632Sopenharmony_ci optional :value, :int32, 2 83ffe3c632Sopenharmony_ci end 84ffe3c632Sopenharmony_ci add_message "MapMessageWireEquiv_entry2" do 85ffe3c632Sopenharmony_ci optional :key, :string, 1 86ffe3c632Sopenharmony_ci optional :value, :message, 2, "TestMessage2" 87ffe3c632Sopenharmony_ci end 88ffe3c632Sopenharmony_ci 89ffe3c632Sopenharmony_ci add_message "OneofMessage" do 90ffe3c632Sopenharmony_ci oneof :my_oneof do 91ffe3c632Sopenharmony_ci optional :a, :string, 1 92ffe3c632Sopenharmony_ci optional :b, :int32, 2 93ffe3c632Sopenharmony_ci optional :c, :message, 3, "TestMessage2" 94ffe3c632Sopenharmony_ci optional :d, :enum, 4, "TestEnum" 95ffe3c632Sopenharmony_ci end 96ffe3c632Sopenharmony_ci end 97ffe3c632Sopenharmony_ci end 98ffe3c632Sopenharmony_ci 99ffe3c632Sopenharmony_ci Foo = pool.lookup("Foo").msgclass 100ffe3c632Sopenharmony_ci Bar = pool.lookup("Bar").msgclass 101ffe3c632Sopenharmony_ci Baz = pool.lookup("Baz").msgclass 102ffe3c632Sopenharmony_ci TestMessage = pool.lookup("TestMessage").msgclass 103ffe3c632Sopenharmony_ci TestMessage2 = pool.lookup("TestMessage2").msgclass 104ffe3c632Sopenharmony_ci Recursive1 = pool.lookup("Recursive1").msgclass 105ffe3c632Sopenharmony_ci Recursive2 = pool.lookup("Recursive2").msgclass 106ffe3c632Sopenharmony_ci TestEnum = pool.lookup("TestEnum").enummodule 107ffe3c632Sopenharmony_ci BadFieldNames = pool.lookup("BadFieldNames").msgclass 108ffe3c632Sopenharmony_ci MapMessage = pool.lookup("MapMessage").msgclass 109ffe3c632Sopenharmony_ci MapMessageWireEquiv = pool.lookup("MapMessageWireEquiv").msgclass 110ffe3c632Sopenharmony_ci MapMessageWireEquiv_entry1 = 111ffe3c632Sopenharmony_ci pool.lookup("MapMessageWireEquiv_entry1").msgclass 112ffe3c632Sopenharmony_ci MapMessageWireEquiv_entry2 = 113ffe3c632Sopenharmony_ci pool.lookup("MapMessageWireEquiv_entry2").msgclass 114ffe3c632Sopenharmony_ci OneofMessage = pool.lookup("OneofMessage").msgclass 115ffe3c632Sopenharmony_ci 116ffe3c632Sopenharmony_ci# ------------ test cases --------------- 117ffe3c632Sopenharmony_ci 118ffe3c632Sopenharmony_ci class MessageContainerTest < Test::Unit::TestCase 119ffe3c632Sopenharmony_ci 120ffe3c632Sopenharmony_ci def test_defaults 121ffe3c632Sopenharmony_ci m = TestMessage.new 122ffe3c632Sopenharmony_ci assert m.optional_int32 == 0 123ffe3c632Sopenharmony_ci assert m.optional_int64 == 0 124ffe3c632Sopenharmony_ci assert m.optional_uint32 == 0 125ffe3c632Sopenharmony_ci assert m.optional_uint64 == 0 126ffe3c632Sopenharmony_ci assert m.optional_bool == false 127ffe3c632Sopenharmony_ci assert m.optional_float == 0.0 128ffe3c632Sopenharmony_ci assert m.optional_double == 0.0 129ffe3c632Sopenharmony_ci assert m.optional_string == "" 130ffe3c632Sopenharmony_ci assert m.optional_bytes == "" 131ffe3c632Sopenharmony_ci assert m.optional_msg == nil 132ffe3c632Sopenharmony_ci assert m.optional_enum == :Default 133ffe3c632Sopenharmony_ci end 134ffe3c632Sopenharmony_ci 135ffe3c632Sopenharmony_ci def test_setters 136ffe3c632Sopenharmony_ci m = TestMessage.new 137ffe3c632Sopenharmony_ci m.optional_int32 = -42 138ffe3c632Sopenharmony_ci assert m.optional_int32 == -42 139ffe3c632Sopenharmony_ci m.optional_int64 = -0x1_0000_0000 140ffe3c632Sopenharmony_ci assert m.optional_int64 == -0x1_0000_0000 141ffe3c632Sopenharmony_ci m.optional_uint32 = 0x9000_0000 142ffe3c632Sopenharmony_ci assert m.optional_uint32 == 0x9000_0000 143ffe3c632Sopenharmony_ci m.optional_uint64 = 0x9000_0000_0000_0000 144ffe3c632Sopenharmony_ci assert m.optional_uint64 == 0x9000_0000_0000_0000 145ffe3c632Sopenharmony_ci m.optional_bool = true 146ffe3c632Sopenharmony_ci assert m.optional_bool == true 147ffe3c632Sopenharmony_ci m.optional_float = 0.5 148ffe3c632Sopenharmony_ci assert m.optional_float == 0.5 149ffe3c632Sopenharmony_ci m.optional_double = 0.5 150ffe3c632Sopenharmony_ci m.optional_string = "hello" 151ffe3c632Sopenharmony_ci assert m.optional_string == "hello" 152ffe3c632Sopenharmony_ci m.optional_bytes = "world".encode!('ASCII-8BIT') 153ffe3c632Sopenharmony_ci assert m.optional_bytes == "world" 154ffe3c632Sopenharmony_ci m.optional_msg = TestMessage2.new(:foo => 42) 155ffe3c632Sopenharmony_ci assert m.optional_msg == TestMessage2.new(:foo => 42) 156ffe3c632Sopenharmony_ci m.optional_msg = nil 157ffe3c632Sopenharmony_ci assert m.optional_msg == nil 158ffe3c632Sopenharmony_ci end 159ffe3c632Sopenharmony_ci 160ffe3c632Sopenharmony_ci def test_ctor_args 161ffe3c632Sopenharmony_ci m = TestMessage.new(:optional_int32 => -42, 162ffe3c632Sopenharmony_ci :optional_msg => TestMessage2.new, 163ffe3c632Sopenharmony_ci :optional_enum => :C, 164ffe3c632Sopenharmony_ci :repeated_string => ["hello", "there", "world"]) 165ffe3c632Sopenharmony_ci assert m.optional_int32 == -42 166ffe3c632Sopenharmony_ci assert m.optional_msg.class == TestMessage2 167ffe3c632Sopenharmony_ci assert m.repeated_string.length == 3 168ffe3c632Sopenharmony_ci assert m.optional_enum == :C 169ffe3c632Sopenharmony_ci assert m.repeated_string[0] == "hello" 170ffe3c632Sopenharmony_ci assert m.repeated_string[1] == "there" 171ffe3c632Sopenharmony_ci assert m.repeated_string[2] == "world" 172ffe3c632Sopenharmony_ci end 173ffe3c632Sopenharmony_ci 174ffe3c632Sopenharmony_ci def test_inspect 175ffe3c632Sopenharmony_ci m = TestMessage.new(:optional_int32 => -42, 176ffe3c632Sopenharmony_ci :optional_enum => :A, 177ffe3c632Sopenharmony_ci :optional_msg => TestMessage2.new, 178ffe3c632Sopenharmony_ci :repeated_string => ["hello", "there", "world"]) 179ffe3c632Sopenharmony_ci expected = '<BasicTest::TestMessage: optional_int32: -42, optional_int64: 0, optional_uint32: 0, optional_uint64: 0, optional_bool: false, optional_float: 0.0, optional_double: 0.0, optional_string: "", optional_bytes: "", optional_msg: <BasicTest::TestMessage2: foo: 0>, optional_enum: :A, repeated_int32: [], repeated_int64: [], repeated_uint32: [], repeated_uint64: [], repeated_bool: [], repeated_float: [], repeated_double: [], repeated_string: ["hello", "there", "world"], repeated_bytes: [], repeated_msg: [], repeated_enum: []>' 180ffe3c632Sopenharmony_ci assert_equal expected, m.inspect 181ffe3c632Sopenharmony_ci end 182ffe3c632Sopenharmony_ci 183ffe3c632Sopenharmony_ci def test_hash 184ffe3c632Sopenharmony_ci m1 = TestMessage.new(:optional_int32 => 42) 185ffe3c632Sopenharmony_ci m2 = TestMessage.new(:optional_int32 => 102) 186ffe3c632Sopenharmony_ci assert m1.hash != 0 187ffe3c632Sopenharmony_ci assert m2.hash != 0 188ffe3c632Sopenharmony_ci # relying on the randomness here -- if hash function changes and we are 189ffe3c632Sopenharmony_ci # unlucky enough to get a collision, then change the values above. 190ffe3c632Sopenharmony_ci assert m1.hash != m2.hash 191ffe3c632Sopenharmony_ci end 192ffe3c632Sopenharmony_ci 193ffe3c632Sopenharmony_ci def test_unknown_field_errors 194ffe3c632Sopenharmony_ci e = assert_raise NoMethodError do 195ffe3c632Sopenharmony_ci TestMessage.new.hello 196ffe3c632Sopenharmony_ci end 197ffe3c632Sopenharmony_ci assert_match(/hello/, e.message) 198ffe3c632Sopenharmony_ci 199ffe3c632Sopenharmony_ci e = assert_raise NoMethodError do 200ffe3c632Sopenharmony_ci TestMessage.new.hello = "world" 201ffe3c632Sopenharmony_ci end 202ffe3c632Sopenharmony_ci assert_match(/hello/, e.message) 203ffe3c632Sopenharmony_ci end 204ffe3c632Sopenharmony_ci 205ffe3c632Sopenharmony_ci def test_initialization_map_errors 206ffe3c632Sopenharmony_ci e = assert_raise ArgumentError do 207ffe3c632Sopenharmony_ci TestMessage.new(:hello => "world") 208ffe3c632Sopenharmony_ci end 209ffe3c632Sopenharmony_ci assert_match(/hello/, e.message) 210ffe3c632Sopenharmony_ci 211ffe3c632Sopenharmony_ci e = assert_raise ArgumentError do 212ffe3c632Sopenharmony_ci MapMessage.new(:map_string_int32 => "hello") 213ffe3c632Sopenharmony_ci end 214ffe3c632Sopenharmony_ci assert_equal e.message, "Expected Hash object as initializer value for map field 'map_string_int32' (given String)." 215ffe3c632Sopenharmony_ci 216ffe3c632Sopenharmony_ci e = assert_raise ArgumentError do 217ffe3c632Sopenharmony_ci TestMessage.new(:repeated_uint32 => "hello") 218ffe3c632Sopenharmony_ci end 219ffe3c632Sopenharmony_ci assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32' (given String)." 220ffe3c632Sopenharmony_ci end 221ffe3c632Sopenharmony_ci 222ffe3c632Sopenharmony_ci def test_type_errors 223ffe3c632Sopenharmony_ci m = TestMessage.new 224ffe3c632Sopenharmony_ci 225ffe3c632Sopenharmony_ci # Use rescue to allow subclasses of error 226ffe3c632Sopenharmony_ci success = false 227ffe3c632Sopenharmony_ci begin 228ffe3c632Sopenharmony_ci m.optional_int32 = "hello" 229ffe3c632Sopenharmony_ci rescue TypeError 230ffe3c632Sopenharmony_ci success = true 231ffe3c632Sopenharmony_ci end 232ffe3c632Sopenharmony_ci assert(success) 233ffe3c632Sopenharmony_ci 234ffe3c632Sopenharmony_ci success = false 235ffe3c632Sopenharmony_ci begin 236ffe3c632Sopenharmony_ci m.optional_string = nil 237ffe3c632Sopenharmony_ci rescue TypeError 238ffe3c632Sopenharmony_ci success = true 239ffe3c632Sopenharmony_ci end 240ffe3c632Sopenharmony_ci assert(success) 241ffe3c632Sopenharmony_ci 242ffe3c632Sopenharmony_ci success = false 243ffe3c632Sopenharmony_ci begin 244ffe3c632Sopenharmony_ci m.optional_bool = 42 245ffe3c632Sopenharmony_ci rescue TypeError 246ffe3c632Sopenharmony_ci success = true 247ffe3c632Sopenharmony_ci end 248ffe3c632Sopenharmony_ci assert(success) 249ffe3c632Sopenharmony_ci 250ffe3c632Sopenharmony_ci success = false 251ffe3c632Sopenharmony_ci begin 252ffe3c632Sopenharmony_ci m.optional_msg = TestMessage.new # expects TestMessage2 253ffe3c632Sopenharmony_ci rescue TypeError 254ffe3c632Sopenharmony_ci success = true 255ffe3c632Sopenharmony_ci end 256ffe3c632Sopenharmony_ci assert(success) 257ffe3c632Sopenharmony_ci 258ffe3c632Sopenharmony_ci success = false 259ffe3c632Sopenharmony_ci begin 260ffe3c632Sopenharmony_ci m.repeated_int32 = [] # needs RepeatedField 261ffe3c632Sopenharmony_ci rescue TypeError 262ffe3c632Sopenharmony_ci success = true 263ffe3c632Sopenharmony_ci end 264ffe3c632Sopenharmony_ci assert(success) 265ffe3c632Sopenharmony_ci 266ffe3c632Sopenharmony_ci success = false 267ffe3c632Sopenharmony_ci begin 268ffe3c632Sopenharmony_ci m.repeated_msg.push TestMessage.new 269ffe3c632Sopenharmony_ci rescue TypeError 270ffe3c632Sopenharmony_ci success = true 271ffe3c632Sopenharmony_ci end 272ffe3c632Sopenharmony_ci assert(success) 273ffe3c632Sopenharmony_ci end 274ffe3c632Sopenharmony_ci 275ffe3c632Sopenharmony_ci def test_string_encoding 276ffe3c632Sopenharmony_ci m = TestMessage.new 277ffe3c632Sopenharmony_ci 278ffe3c632Sopenharmony_ci # Assigning a normal (ASCII or UTF8) string to a bytes field, or 279ffe3c632Sopenharmony_ci # ASCII-8BIT to a string field will convert to the proper encoding. 280ffe3c632Sopenharmony_ci m.optional_bytes = "Test string ASCII".encode!('ASCII') 281ffe3c632Sopenharmony_ci assert m.optional_bytes.frozen? 282ffe3c632Sopenharmony_ci assert_equal Encoding::ASCII_8BIT, m.optional_bytes.encoding 283ffe3c632Sopenharmony_ci assert_equal "Test string ASCII", m.optional_bytes 284ffe3c632Sopenharmony_ci 285ffe3c632Sopenharmony_ci assert_raise Encoding::UndefinedConversionError do 286ffe3c632Sopenharmony_ci m.optional_bytes = "Test string UTF-8 \u0100".encode!('UTF-8') 287ffe3c632Sopenharmony_ci end 288ffe3c632Sopenharmony_ci 289ffe3c632Sopenharmony_ci assert_raise Encoding::UndefinedConversionError do 290ffe3c632Sopenharmony_ci m.optional_string = ["FFFF"].pack('H*') 291ffe3c632Sopenharmony_ci end 292ffe3c632Sopenharmony_ci 293ffe3c632Sopenharmony_ci # "Ordinary" use case. 294ffe3c632Sopenharmony_ci m.optional_bytes = ["FFFF"].pack('H*') 295ffe3c632Sopenharmony_ci m.optional_string = "\u0100" 296ffe3c632Sopenharmony_ci 297ffe3c632Sopenharmony_ci # strings are immutable so we can't do this, but serialize should catch it. 298ffe3c632Sopenharmony_ci m.optional_string = "asdf".encode!('UTF-8') 299ffe3c632Sopenharmony_ci assert_raise do 300ffe3c632Sopenharmony_ci m.optional_string.encode!('ASCII-8BIT') 301ffe3c632Sopenharmony_ci end 302ffe3c632Sopenharmony_ci end 303ffe3c632Sopenharmony_ci 304ffe3c632Sopenharmony_ci def test_rptfield_int32 305ffe3c632Sopenharmony_ci l = Google::Protobuf::RepeatedField.new(:int32) 306ffe3c632Sopenharmony_ci assert l.count == 0 307ffe3c632Sopenharmony_ci l = Google::Protobuf::RepeatedField.new(:int32, [1, 2, 3]) 308ffe3c632Sopenharmony_ci assert l.count == 3 309ffe3c632Sopenharmony_ci assert_equal [1, 2, 3], l 310ffe3c632Sopenharmony_ci assert_equal l, [1, 2, 3] 311ffe3c632Sopenharmony_ci l.push 4 312ffe3c632Sopenharmony_ci assert l == [1, 2, 3, 4] 313ffe3c632Sopenharmony_ci dst_list = [] 314ffe3c632Sopenharmony_ci l.each { |val| dst_list.push val } 315ffe3c632Sopenharmony_ci assert dst_list == [1, 2, 3, 4] 316ffe3c632Sopenharmony_ci assert l.to_a == [1, 2, 3, 4] 317ffe3c632Sopenharmony_ci assert l[0] == 1 318ffe3c632Sopenharmony_ci assert l[3] == 4 319ffe3c632Sopenharmony_ci l[0] = 5 320ffe3c632Sopenharmony_ci assert l == [5, 2, 3, 4] 321ffe3c632Sopenharmony_ci 322ffe3c632Sopenharmony_ci l2 = l.dup 323ffe3c632Sopenharmony_ci assert l == l2 324ffe3c632Sopenharmony_ci assert l.object_id != l2.object_id 325ffe3c632Sopenharmony_ci l2.push 6 326ffe3c632Sopenharmony_ci assert l.count == 4 327ffe3c632Sopenharmony_ci assert l2.count == 5 328ffe3c632Sopenharmony_ci 329ffe3c632Sopenharmony_ci assert l.inspect == '[5, 2, 3, 4]' 330ffe3c632Sopenharmony_ci 331ffe3c632Sopenharmony_ci l.concat([7, 8, 9]) 332ffe3c632Sopenharmony_ci assert l == [5, 2, 3, 4, 7, 8, 9] 333ffe3c632Sopenharmony_ci assert l.pop == 9 334ffe3c632Sopenharmony_ci assert l == [5, 2, 3, 4, 7, 8] 335ffe3c632Sopenharmony_ci 336ffe3c632Sopenharmony_ci success = false 337ffe3c632Sopenharmony_ci begin 338ffe3c632Sopenharmony_ci m = TestMessage.new 339ffe3c632Sopenharmony_ci l.push m 340ffe3c632Sopenharmony_ci rescue TypeError 341ffe3c632Sopenharmony_ci success = true 342ffe3c632Sopenharmony_ci end 343ffe3c632Sopenharmony_ci assert(success) 344ffe3c632Sopenharmony_ci 345ffe3c632Sopenharmony_ci m = TestMessage.new 346ffe3c632Sopenharmony_ci m.repeated_int32 = l 347ffe3c632Sopenharmony_ci assert m.repeated_int32 == [5, 2, 3, 4, 7, 8] 348ffe3c632Sopenharmony_ci assert m.repeated_int32.object_id == l.object_id 349ffe3c632Sopenharmony_ci l.push 42 350ffe3c632Sopenharmony_ci assert m.repeated_int32.pop == 42 351ffe3c632Sopenharmony_ci 352ffe3c632Sopenharmony_ci l3 = l + l.dup 353ffe3c632Sopenharmony_ci assert l3.count == l.count * 2 354ffe3c632Sopenharmony_ci l.count.times do |i| 355ffe3c632Sopenharmony_ci assert l3[i] == l[i] 356ffe3c632Sopenharmony_ci assert l3[l.count + i] == l[i] 357ffe3c632Sopenharmony_ci end 358ffe3c632Sopenharmony_ci 359ffe3c632Sopenharmony_ci l.clear 360ffe3c632Sopenharmony_ci assert l.count == 0 361ffe3c632Sopenharmony_ci l += [1, 2, 3, 4] 362ffe3c632Sopenharmony_ci l.replace([5, 6, 7, 8]) 363ffe3c632Sopenharmony_ci assert l == [5, 6, 7, 8] 364ffe3c632Sopenharmony_ci 365ffe3c632Sopenharmony_ci l4 = Google::Protobuf::RepeatedField.new(:int32) 366ffe3c632Sopenharmony_ci l4[5] = 42 367ffe3c632Sopenharmony_ci assert l4 == [0, 0, 0, 0, 0, 42] 368ffe3c632Sopenharmony_ci 369ffe3c632Sopenharmony_ci l4 << 100 370ffe3c632Sopenharmony_ci assert l4 == [0, 0, 0, 0, 0, 42, 100] 371ffe3c632Sopenharmony_ci l4 << 101 << 102 372ffe3c632Sopenharmony_ci assert l4 == [0, 0, 0, 0, 0, 42, 100, 101, 102] 373ffe3c632Sopenharmony_ci end 374ffe3c632Sopenharmony_ci 375ffe3c632Sopenharmony_ci def test_parent_rptfield 376ffe3c632Sopenharmony_ci #make sure we set the RepeatedField and can add to it 377ffe3c632Sopenharmony_ci m = TestMessage.new 378ffe3c632Sopenharmony_ci assert m.repeated_string == [] 379ffe3c632Sopenharmony_ci m.repeated_string << 'ok' 380ffe3c632Sopenharmony_ci m.repeated_string.push('ok2') 381ffe3c632Sopenharmony_ci assert m.repeated_string == ['ok', 'ok2'] 382ffe3c632Sopenharmony_ci m.repeated_string += ['ok3'] 383ffe3c632Sopenharmony_ci assert m.repeated_string == ['ok', 'ok2', 'ok3'] 384ffe3c632Sopenharmony_ci end 385ffe3c632Sopenharmony_ci 386ffe3c632Sopenharmony_ci def test_rptfield_msg 387ffe3c632Sopenharmony_ci l = Google::Protobuf::RepeatedField.new(:message, TestMessage) 388ffe3c632Sopenharmony_ci l.push TestMessage.new 389ffe3c632Sopenharmony_ci assert l.count == 1 390ffe3c632Sopenharmony_ci 391ffe3c632Sopenharmony_ci success = false 392ffe3c632Sopenharmony_ci begin 393ffe3c632Sopenharmony_ci l.push TestMessage2.new 394ffe3c632Sopenharmony_ci rescue TypeError 395ffe3c632Sopenharmony_ci success = true 396ffe3c632Sopenharmony_ci end 397ffe3c632Sopenharmony_ci assert(success) 398ffe3c632Sopenharmony_ci 399ffe3c632Sopenharmony_ci success = false 400ffe3c632Sopenharmony_ci begin 401ffe3c632Sopenharmony_ci l.push 42 402ffe3c632Sopenharmony_ci rescue TypeError 403ffe3c632Sopenharmony_ci success = true 404ffe3c632Sopenharmony_ci end 405ffe3c632Sopenharmony_ci assert(success) 406ffe3c632Sopenharmony_ci 407ffe3c632Sopenharmony_ci l2 = l.dup 408ffe3c632Sopenharmony_ci assert l2[0] == l[0] 409ffe3c632Sopenharmony_ci assert l2[0].object_id == l[0].object_id 410ffe3c632Sopenharmony_ci 411ffe3c632Sopenharmony_ci l2 = Google::Protobuf.deep_copy(l) 412ffe3c632Sopenharmony_ci assert l2[0] == l[0] 413ffe3c632Sopenharmony_ci assert l2[0].object_id != l[0].object_id 414ffe3c632Sopenharmony_ci 415ffe3c632Sopenharmony_ci l3 = l + l2 416ffe3c632Sopenharmony_ci assert l3.count == 2 417ffe3c632Sopenharmony_ci assert l3[0] == l[0] 418ffe3c632Sopenharmony_ci assert l3[1] == l2[0] 419ffe3c632Sopenharmony_ci l3[0].optional_int32 = 1000 420ffe3c632Sopenharmony_ci assert l[0].optional_int32 == 1000 421ffe3c632Sopenharmony_ci 422ffe3c632Sopenharmony_ci new_msg = TestMessage.new(:optional_int32 => 200) 423ffe3c632Sopenharmony_ci l4 = l + [new_msg] 424ffe3c632Sopenharmony_ci assert l4.count == 2 425ffe3c632Sopenharmony_ci new_msg.optional_int32 = 1000 426ffe3c632Sopenharmony_ci assert l4[1].optional_int32 == 1000 427ffe3c632Sopenharmony_ci end 428ffe3c632Sopenharmony_ci 429ffe3c632Sopenharmony_ci def test_rptfield_enum 430ffe3c632Sopenharmony_ci l = Google::Protobuf::RepeatedField.new(:enum, TestEnum) 431ffe3c632Sopenharmony_ci l.push :A 432ffe3c632Sopenharmony_ci l.push :B 433ffe3c632Sopenharmony_ci l.push :C 434ffe3c632Sopenharmony_ci assert l.count == 3 435ffe3c632Sopenharmony_ci assert_raise RangeError do 436ffe3c632Sopenharmony_ci l.push :D 437ffe3c632Sopenharmony_ci end 438ffe3c632Sopenharmony_ci assert l[0] == :A 439ffe3c632Sopenharmony_ci 440ffe3c632Sopenharmony_ci l.push 4 441ffe3c632Sopenharmony_ci assert l[3] == 4 442ffe3c632Sopenharmony_ci end 443ffe3c632Sopenharmony_ci 444ffe3c632Sopenharmony_ci def test_rptfield_initialize 445ffe3c632Sopenharmony_ci assert_raise ArgumentError do 446ffe3c632Sopenharmony_ci l = Google::Protobuf::RepeatedField.new 447ffe3c632Sopenharmony_ci end 448ffe3c632Sopenharmony_ci assert_raise ArgumentError do 449ffe3c632Sopenharmony_ci l = Google::Protobuf::RepeatedField.new(:message) 450ffe3c632Sopenharmony_ci end 451ffe3c632Sopenharmony_ci assert_raise ArgumentError do 452ffe3c632Sopenharmony_ci l = Google::Protobuf::RepeatedField.new([1, 2, 3]) 453ffe3c632Sopenharmony_ci end 454ffe3c632Sopenharmony_ci assert_raise ArgumentError do 455ffe3c632Sopenharmony_ci l = Google::Protobuf::RepeatedField.new(:message, [TestMessage2.new]) 456ffe3c632Sopenharmony_ci end 457ffe3c632Sopenharmony_ci end 458ffe3c632Sopenharmony_ci 459ffe3c632Sopenharmony_ci def test_rptfield_array_ducktyping 460ffe3c632Sopenharmony_ci l = Google::Protobuf::RepeatedField.new(:int32) 461ffe3c632Sopenharmony_ci length_methods = %w(count length size) 462ffe3c632Sopenharmony_ci length_methods.each do |lm| 463ffe3c632Sopenharmony_ci assert l.send(lm) == 0 464ffe3c632Sopenharmony_ci end 465ffe3c632Sopenharmony_ci # out of bounds returns a nil 466ffe3c632Sopenharmony_ci assert l[0] == nil 467ffe3c632Sopenharmony_ci assert l[1] == nil 468ffe3c632Sopenharmony_ci assert l[-1] == nil 469ffe3c632Sopenharmony_ci l.push 4 470ffe3c632Sopenharmony_ci length_methods.each do |lm| 471ffe3c632Sopenharmony_ci assert l.send(lm) == 1 472ffe3c632Sopenharmony_ci end 473ffe3c632Sopenharmony_ci assert l[0] == 4 474ffe3c632Sopenharmony_ci assert l[1] == nil 475ffe3c632Sopenharmony_ci assert l[-1] == 4 476ffe3c632Sopenharmony_ci assert l[-2] == nil 477ffe3c632Sopenharmony_ci 478ffe3c632Sopenharmony_ci l.push 2 479ffe3c632Sopenharmony_ci length_methods.each do |lm| 480ffe3c632Sopenharmony_ci assert l.send(lm) == 2 481ffe3c632Sopenharmony_ci end 482ffe3c632Sopenharmony_ci assert l[0] == 4 483ffe3c632Sopenharmony_ci assert l[1] == 2 484ffe3c632Sopenharmony_ci assert l[2] == nil 485ffe3c632Sopenharmony_ci assert l[-1] == 2 486ffe3c632Sopenharmony_ci assert l[-2] == 4 487ffe3c632Sopenharmony_ci assert l[-3] == nil 488ffe3c632Sopenharmony_ci 489ffe3c632Sopenharmony_ci #adding out of scope will backfill with empty objects 490ffe3c632Sopenharmony_ci end 491ffe3c632Sopenharmony_ci 492ffe3c632Sopenharmony_ci def test_map_basic 493ffe3c632Sopenharmony_ci # allowed key types: 494ffe3c632Sopenharmony_ci # :int32, :int64, :uint32, :uint64, :bool, :string, :bytes. 495ffe3c632Sopenharmony_ci 496ffe3c632Sopenharmony_ci m = Google::Protobuf::Map.new(:string, :int32) 497ffe3c632Sopenharmony_ci m["asdf"] = 1 498ffe3c632Sopenharmony_ci assert m["asdf"] == 1 499ffe3c632Sopenharmony_ci m["jkl;"] = 42 500ffe3c632Sopenharmony_ci assert m == { "jkl;" => 42, "asdf" => 1 } 501ffe3c632Sopenharmony_ci assert m.has_key?("asdf") 502ffe3c632Sopenharmony_ci assert !m.has_key?("qwerty") 503ffe3c632Sopenharmony_ci assert m.length == 2 504ffe3c632Sopenharmony_ci 505ffe3c632Sopenharmony_ci m2 = m.dup 506ffe3c632Sopenharmony_ci assert m == m2 507ffe3c632Sopenharmony_ci assert m.hash != 0 508ffe3c632Sopenharmony_ci assert m.hash == m2.hash 509ffe3c632Sopenharmony_ci 510ffe3c632Sopenharmony_ci collected = {} 511ffe3c632Sopenharmony_ci m.each { |k,v| collected[v] = k } 512ffe3c632Sopenharmony_ci assert collected == { 42 => "jkl;", 1 => "asdf" } 513ffe3c632Sopenharmony_ci 514ffe3c632Sopenharmony_ci assert m.delete("asdf") == 1 515ffe3c632Sopenharmony_ci assert !m.has_key?("asdf") 516ffe3c632Sopenharmony_ci assert m["asdf"] == nil 517ffe3c632Sopenharmony_ci assert !m.has_key?("asdf") 518ffe3c632Sopenharmony_ci 519ffe3c632Sopenharmony_ci # We only assert on inspect value when there is one map entry because the 520ffe3c632Sopenharmony_ci # order in which elements appear is unspecified (depends on the internal 521ffe3c632Sopenharmony_ci # hash function). We don't want a brittle test. 522ffe3c632Sopenharmony_ci assert m.inspect == "{\"jkl;\"=>42}" 523ffe3c632Sopenharmony_ci 524ffe3c632Sopenharmony_ci assert m.keys == ["jkl;"] 525ffe3c632Sopenharmony_ci assert m.values == [42] 526ffe3c632Sopenharmony_ci 527ffe3c632Sopenharmony_ci m.clear 528ffe3c632Sopenharmony_ci assert m.length == 0 529ffe3c632Sopenharmony_ci assert m == {} 530ffe3c632Sopenharmony_ci 531ffe3c632Sopenharmony_ci success = false 532ffe3c632Sopenharmony_ci begin 533ffe3c632Sopenharmony_ci m[1] = 1 534ffe3c632Sopenharmony_ci rescue TypeError 535ffe3c632Sopenharmony_ci success = true 536ffe3c632Sopenharmony_ci end 537ffe3c632Sopenharmony_ci assert(success) 538ffe3c632Sopenharmony_ci 539ffe3c632Sopenharmony_ci assert_raise RangeError do 540ffe3c632Sopenharmony_ci m["asdf"] = 0x1_0000_0000 541ffe3c632Sopenharmony_ci end 542ffe3c632Sopenharmony_ci end 543ffe3c632Sopenharmony_ci 544ffe3c632Sopenharmony_ci def test_map_ctor 545ffe3c632Sopenharmony_ci m = Google::Protobuf::Map.new(:string, :int32, 546ffe3c632Sopenharmony_ci {"a" => 1, "b" => 2, "c" => 3}) 547ffe3c632Sopenharmony_ci assert m == {"a" => 1, "c" => 3, "b" => 2} 548ffe3c632Sopenharmony_ci end 549ffe3c632Sopenharmony_ci 550ffe3c632Sopenharmony_ci def test_map_keytypes 551ffe3c632Sopenharmony_ci m = Google::Protobuf::Map.new(:int32, :int32) 552ffe3c632Sopenharmony_ci m[1] = 42 553ffe3c632Sopenharmony_ci m[-1] = 42 554ffe3c632Sopenharmony_ci assert_raise RangeError do 555ffe3c632Sopenharmony_ci m[0x8000_0000] = 1 556ffe3c632Sopenharmony_ci end 557ffe3c632Sopenharmony_ci 558ffe3c632Sopenharmony_ci success = false 559ffe3c632Sopenharmony_ci begin 560ffe3c632Sopenharmony_ci m["asdf"] = 1 561ffe3c632Sopenharmony_ci rescue TypeError 562ffe3c632Sopenharmony_ci success = true 563ffe3c632Sopenharmony_ci end 564ffe3c632Sopenharmony_ci assert(success) 565ffe3c632Sopenharmony_ci 566ffe3c632Sopenharmony_ci m = Google::Protobuf::Map.new(:int64, :int32) 567ffe3c632Sopenharmony_ci m[0x1000_0000_0000_0000] = 1 568ffe3c632Sopenharmony_ci assert_raise RangeError do 569ffe3c632Sopenharmony_ci m[0x1_0000_0000_0000_0000] = 1 570ffe3c632Sopenharmony_ci end 571ffe3c632Sopenharmony_ci 572ffe3c632Sopenharmony_ci success = false 573ffe3c632Sopenharmony_ci begin 574ffe3c632Sopenharmony_ci m["asdf"] = 1 575ffe3c632Sopenharmony_ci rescue TypeError 576ffe3c632Sopenharmony_ci success = true 577ffe3c632Sopenharmony_ci end 578ffe3c632Sopenharmony_ci assert(success) 579ffe3c632Sopenharmony_ci 580ffe3c632Sopenharmony_ci m = Google::Protobuf::Map.new(:uint32, :int32) 581ffe3c632Sopenharmony_ci m[0x8000_0000] = 1 582ffe3c632Sopenharmony_ci assert_raise RangeError do 583ffe3c632Sopenharmony_ci m[0x1_0000_0000] = 1 584ffe3c632Sopenharmony_ci end 585ffe3c632Sopenharmony_ci assert_raise RangeError do 586ffe3c632Sopenharmony_ci m[-1] = 1 587ffe3c632Sopenharmony_ci end 588ffe3c632Sopenharmony_ci 589ffe3c632Sopenharmony_ci m = Google::Protobuf::Map.new(:uint64, :int32) 590ffe3c632Sopenharmony_ci m[0x8000_0000_0000_0000] = 1 591ffe3c632Sopenharmony_ci assert_raise RangeError do 592ffe3c632Sopenharmony_ci m[0x1_0000_0000_0000_0000] = 1 593ffe3c632Sopenharmony_ci end 594ffe3c632Sopenharmony_ci assert_raise RangeError do 595ffe3c632Sopenharmony_ci m[-1] = 1 596ffe3c632Sopenharmony_ci end 597ffe3c632Sopenharmony_ci 598ffe3c632Sopenharmony_ci m = Google::Protobuf::Map.new(:bool, :int32) 599ffe3c632Sopenharmony_ci m[true] = 1 600ffe3c632Sopenharmony_ci m[false] = 2 601ffe3c632Sopenharmony_ci 602ffe3c632Sopenharmony_ci success = false 603ffe3c632Sopenharmony_ci begin 604ffe3c632Sopenharmony_ci m[1] = 1 605ffe3c632Sopenharmony_ci rescue TypeError 606ffe3c632Sopenharmony_ci success = true 607ffe3c632Sopenharmony_ci end 608ffe3c632Sopenharmony_ci assert(success) 609ffe3c632Sopenharmony_ci 610ffe3c632Sopenharmony_ci success = false 611ffe3c632Sopenharmony_ci begin 612ffe3c632Sopenharmony_ci m["asdf"] = 1 613ffe3c632Sopenharmony_ci rescue TypeError 614ffe3c632Sopenharmony_ci success = true 615ffe3c632Sopenharmony_ci end 616ffe3c632Sopenharmony_ci assert(success) 617ffe3c632Sopenharmony_ci 618ffe3c632Sopenharmony_ci m = Google::Protobuf::Map.new(:string, :int32) 619ffe3c632Sopenharmony_ci m["asdf"] = 1 620ffe3c632Sopenharmony_ci success = false 621ffe3c632Sopenharmony_ci begin 622ffe3c632Sopenharmony_ci m[1] = 1 623ffe3c632Sopenharmony_ci rescue TypeError 624ffe3c632Sopenharmony_ci success = true 625ffe3c632Sopenharmony_ci end 626ffe3c632Sopenharmony_ci assert(success) 627ffe3c632Sopenharmony_ci assert_raise Encoding::UndefinedConversionError do 628ffe3c632Sopenharmony_ci bytestring = ["FFFF"].pack("H*") 629ffe3c632Sopenharmony_ci m[bytestring] = 1 630ffe3c632Sopenharmony_ci end 631ffe3c632Sopenharmony_ci 632ffe3c632Sopenharmony_ci m = Google::Protobuf::Map.new(:bytes, :int32) 633ffe3c632Sopenharmony_ci bytestring = ["FFFF"].pack("H*") 634ffe3c632Sopenharmony_ci m[bytestring] = 1 635ffe3c632Sopenharmony_ci # Allowed -- we will automatically convert to ASCII-8BIT. 636ffe3c632Sopenharmony_ci m["asdf"] = 1 637ffe3c632Sopenharmony_ci success = false 638ffe3c632Sopenharmony_ci begin 639ffe3c632Sopenharmony_ci m[1] = 1 640ffe3c632Sopenharmony_ci rescue TypeError 641ffe3c632Sopenharmony_ci success = true 642ffe3c632Sopenharmony_ci end 643ffe3c632Sopenharmony_ci assert(success) 644ffe3c632Sopenharmony_ci end 645ffe3c632Sopenharmony_ci 646ffe3c632Sopenharmony_ci def test_map_msg_enum_valuetypes 647ffe3c632Sopenharmony_ci m = Google::Protobuf::Map.new(:string, :message, TestMessage) 648ffe3c632Sopenharmony_ci m["asdf"] = TestMessage.new 649ffe3c632Sopenharmony_ci success = false 650ffe3c632Sopenharmony_ci begin 651ffe3c632Sopenharmony_ci m["jkl;"] = TestMessage2.new 652ffe3c632Sopenharmony_ci rescue TypeError 653ffe3c632Sopenharmony_ci success = true 654ffe3c632Sopenharmony_ci end 655ffe3c632Sopenharmony_ci assert(success) 656ffe3c632Sopenharmony_ci 657ffe3c632Sopenharmony_ci m = Google::Protobuf::Map.new( 658ffe3c632Sopenharmony_ci :string, :message, TestMessage, 659ffe3c632Sopenharmony_ci { "a" => TestMessage.new(:optional_int32 => 42), 660ffe3c632Sopenharmony_ci "b" => TestMessage.new(:optional_int32 => 84) }) 661ffe3c632Sopenharmony_ci assert m.length == 2 662ffe3c632Sopenharmony_ci assert m.values.map{|msg| msg.optional_int32}.sort == [42, 84] 663ffe3c632Sopenharmony_ci 664ffe3c632Sopenharmony_ci m = Google::Protobuf::Map.new(:string, :enum, TestEnum, 665ffe3c632Sopenharmony_ci { "x" => :A, "y" => :B, "z" => :C }) 666ffe3c632Sopenharmony_ci assert m.length == 3 667ffe3c632Sopenharmony_ci assert m["z"] == :C 668ffe3c632Sopenharmony_ci m["z"] = 2 669ffe3c632Sopenharmony_ci assert m["z"] == :B 670ffe3c632Sopenharmony_ci m["z"] = 4 671ffe3c632Sopenharmony_ci assert m["z"] == 4 672ffe3c632Sopenharmony_ci assert_raise RangeError do 673ffe3c632Sopenharmony_ci m["z"] = :Z 674ffe3c632Sopenharmony_ci end 675ffe3c632Sopenharmony_ci assert_raise RangeError do 676ffe3c632Sopenharmony_ci m["z"] = "z" 677ffe3c632Sopenharmony_ci end 678ffe3c632Sopenharmony_ci end 679ffe3c632Sopenharmony_ci 680ffe3c632Sopenharmony_ci def test_map_dup_deep_copy 681ffe3c632Sopenharmony_ci m = Google::Protobuf::Map.new( 682ffe3c632Sopenharmony_ci :string, :message, TestMessage, 683ffe3c632Sopenharmony_ci { "a" => TestMessage.new(:optional_int32 => 42), 684ffe3c632Sopenharmony_ci "b" => TestMessage.new(:optional_int32 => 84) }) 685ffe3c632Sopenharmony_ci 686ffe3c632Sopenharmony_ci m2 = m.dup 687ffe3c632Sopenharmony_ci assert m == m2 688ffe3c632Sopenharmony_ci assert m.object_id != m2.object_id 689ffe3c632Sopenharmony_ci assert m["a"].object_id == m2["a"].object_id 690ffe3c632Sopenharmony_ci assert m["b"].object_id == m2["b"].object_id 691ffe3c632Sopenharmony_ci 692ffe3c632Sopenharmony_ci m2 = Google::Protobuf.deep_copy(m) 693ffe3c632Sopenharmony_ci assert m == m2 694ffe3c632Sopenharmony_ci assert m.object_id != m2.object_id 695ffe3c632Sopenharmony_ci assert m["a"].object_id != m2["a"].object_id 696ffe3c632Sopenharmony_ci assert m["b"].object_id != m2["b"].object_id 697ffe3c632Sopenharmony_ci end 698ffe3c632Sopenharmony_ci 699ffe3c632Sopenharmony_ci def test_map_field 700ffe3c632Sopenharmony_ci m = MapMessage.new 701ffe3c632Sopenharmony_ci assert m.map_string_int32 == {} 702ffe3c632Sopenharmony_ci assert m.map_string_msg == {} 703ffe3c632Sopenharmony_ci 704ffe3c632Sopenharmony_ci m = MapMessage.new( 705ffe3c632Sopenharmony_ci :map_string_int32 => {"a" => 1, "b" => 2}, 706ffe3c632Sopenharmony_ci :map_string_msg => {"a" => TestMessage2.new(:foo => 1), 707ffe3c632Sopenharmony_ci "b" => TestMessage2.new(:foo => 2)}) 708ffe3c632Sopenharmony_ci assert m.map_string_int32.keys.sort == ["a", "b"] 709ffe3c632Sopenharmony_ci assert m.map_string_int32["a"] == 1 710ffe3c632Sopenharmony_ci assert m.map_string_msg["b"].foo == 2 711ffe3c632Sopenharmony_ci 712ffe3c632Sopenharmony_ci m.map_string_int32["c"] = 3 713ffe3c632Sopenharmony_ci assert m.map_string_int32["c"] == 3 714ffe3c632Sopenharmony_ci m.map_string_msg["c"] = TestMessage2.new(:foo => 3) 715ffe3c632Sopenharmony_ci assert m.map_string_msg["c"] == TestMessage2.new(:foo => 3) 716ffe3c632Sopenharmony_ci m.map_string_msg.delete("b") 717ffe3c632Sopenharmony_ci m.map_string_msg.delete("c") 718ffe3c632Sopenharmony_ci assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) } 719ffe3c632Sopenharmony_ci 720ffe3c632Sopenharmony_ci success = false 721ffe3c632Sopenharmony_ci begin 722ffe3c632Sopenharmony_ci m.map_string_msg["e"] = TestMessage.new # wrong value type 723ffe3c632Sopenharmony_ci rescue TypeError 724ffe3c632Sopenharmony_ci success = true 725ffe3c632Sopenharmony_ci end 726ffe3c632Sopenharmony_ci assert(success) 727ffe3c632Sopenharmony_ci # ensure nothing was added by the above 728ffe3c632Sopenharmony_ci assert m.map_string_msg == { "a" => TestMessage2.new(:foo => 1) } 729ffe3c632Sopenharmony_ci 730ffe3c632Sopenharmony_ci m.map_string_int32 = Google::Protobuf::Map.new(:string, :int32) 731ffe3c632Sopenharmony_ci success = false 732ffe3c632Sopenharmony_ci begin 733ffe3c632Sopenharmony_ci m.map_string_int32 = Google::Protobuf::Map.new(:string, :int64) 734ffe3c632Sopenharmony_ci rescue TypeError 735ffe3c632Sopenharmony_ci success = true 736ffe3c632Sopenharmony_ci end 737ffe3c632Sopenharmony_ci assert(success) 738ffe3c632Sopenharmony_ci success = false 739ffe3c632Sopenharmony_ci begin 740ffe3c632Sopenharmony_ci m.map_string_int32 = {} 741ffe3c632Sopenharmony_ci rescue TypeError 742ffe3c632Sopenharmony_ci success = true 743ffe3c632Sopenharmony_ci end 744ffe3c632Sopenharmony_ci assert(success) 745ffe3c632Sopenharmony_ci 746ffe3c632Sopenharmony_ci success = false 747ffe3c632Sopenharmony_ci begin 748ffe3c632Sopenharmony_ci m = MapMessage.new(:map_string_int32 => { 1 => "I am not a number" }) 749ffe3c632Sopenharmony_ci rescue TypeError 750ffe3c632Sopenharmony_ci success = true 751ffe3c632Sopenharmony_ci end 752ffe3c632Sopenharmony_ci assert(success) 753ffe3c632Sopenharmony_ci end 754ffe3c632Sopenharmony_ci 755ffe3c632Sopenharmony_ci def test_map_encode_decode 756ffe3c632Sopenharmony_ci m = MapMessage.new( 757ffe3c632Sopenharmony_ci :map_string_int32 => {"a" => 1, "b" => 2}, 758ffe3c632Sopenharmony_ci :map_string_msg => {"a" => TestMessage2.new(:foo => 1), 759ffe3c632Sopenharmony_ci "b" => TestMessage2.new(:foo => 2)}) 760ffe3c632Sopenharmony_ci m2 = MapMessage.decode(MapMessage.encode(m)) 761ffe3c632Sopenharmony_ci assert m == m2 762ffe3c632Sopenharmony_ci 763ffe3c632Sopenharmony_ci m3 = MapMessageWireEquiv.decode(MapMessage.encode(m)) 764ffe3c632Sopenharmony_ci assert m3.map_string_int32.length == 2 765ffe3c632Sopenharmony_ci 766ffe3c632Sopenharmony_ci kv = {} 767ffe3c632Sopenharmony_ci m3.map_string_int32.map { |msg| kv[msg.key] = msg.value } 768ffe3c632Sopenharmony_ci assert kv == {"a" => 1, "b" => 2} 769ffe3c632Sopenharmony_ci 770ffe3c632Sopenharmony_ci kv = {} 771ffe3c632Sopenharmony_ci m3.map_string_msg.map { |msg| kv[msg.key] = msg.value } 772ffe3c632Sopenharmony_ci assert kv == {"a" => TestMessage2.new(:foo => 1), 773ffe3c632Sopenharmony_ci "b" => TestMessage2.new(:foo => 2)} 774ffe3c632Sopenharmony_ci end 775ffe3c632Sopenharmony_ci 776ffe3c632Sopenharmony_ci def test_oneof_descriptors 777ffe3c632Sopenharmony_ci d = OneofMessage.descriptor 778ffe3c632Sopenharmony_ci o = d.lookup_oneof("my_oneof") 779ffe3c632Sopenharmony_ci assert o != nil 780ffe3c632Sopenharmony_ci assert o.class == Google::Protobuf::OneofDescriptor 781ffe3c632Sopenharmony_ci assert o.name == "my_oneof" 782ffe3c632Sopenharmony_ci oneof_count = 0 783ffe3c632Sopenharmony_ci d.each_oneof{ |oneof| 784ffe3c632Sopenharmony_ci oneof_count += 1 785ffe3c632Sopenharmony_ci assert oneof == o 786ffe3c632Sopenharmony_ci } 787ffe3c632Sopenharmony_ci assert oneof_count == 1 788ffe3c632Sopenharmony_ci assert o.count == 4 789ffe3c632Sopenharmony_ci field_names = o.map{|f| f.name}.sort 790ffe3c632Sopenharmony_ci assert field_names == ["a", "b", "c", "d"] 791ffe3c632Sopenharmony_ci end 792ffe3c632Sopenharmony_ci 793ffe3c632Sopenharmony_ci def test_oneof 794ffe3c632Sopenharmony_ci d = OneofMessage.new 795ffe3c632Sopenharmony_ci assert d.a == "" 796ffe3c632Sopenharmony_ci assert d.b == 0 797ffe3c632Sopenharmony_ci assert d.c == nil 798ffe3c632Sopenharmony_ci assert d.d == :Default 799ffe3c632Sopenharmony_ci assert d.my_oneof == nil 800ffe3c632Sopenharmony_ci 801ffe3c632Sopenharmony_ci d.a = "hi" 802ffe3c632Sopenharmony_ci assert d.a == "hi" 803ffe3c632Sopenharmony_ci assert d.b == 0 804ffe3c632Sopenharmony_ci assert d.c == nil 805ffe3c632Sopenharmony_ci assert d.d == :Default 806ffe3c632Sopenharmony_ci assert d.my_oneof == :a 807ffe3c632Sopenharmony_ci 808ffe3c632Sopenharmony_ci d.b = 42 809ffe3c632Sopenharmony_ci assert d.a == "" 810ffe3c632Sopenharmony_ci assert d.b == 42 811ffe3c632Sopenharmony_ci assert d.c == nil 812ffe3c632Sopenharmony_ci assert d.d == :Default 813ffe3c632Sopenharmony_ci assert d.my_oneof == :b 814ffe3c632Sopenharmony_ci 815ffe3c632Sopenharmony_ci d.c = TestMessage2.new(:foo => 100) 816ffe3c632Sopenharmony_ci assert d.a == "" 817ffe3c632Sopenharmony_ci assert d.b == 0 818ffe3c632Sopenharmony_ci assert d.c.foo == 100 819ffe3c632Sopenharmony_ci assert d.d == :Default 820ffe3c632Sopenharmony_ci assert d.my_oneof == :c 821ffe3c632Sopenharmony_ci 822ffe3c632Sopenharmony_ci d.d = :C 823ffe3c632Sopenharmony_ci assert d.a == "" 824ffe3c632Sopenharmony_ci assert d.b == 0 825ffe3c632Sopenharmony_ci assert d.c == nil 826ffe3c632Sopenharmony_ci assert d.d == :C 827ffe3c632Sopenharmony_ci assert d.my_oneof == :d 828ffe3c632Sopenharmony_ci 829ffe3c632Sopenharmony_ci d2 = OneofMessage.decode(OneofMessage.encode(d)) 830ffe3c632Sopenharmony_ci assert d2 == d 831ffe3c632Sopenharmony_ci 832ffe3c632Sopenharmony_ci encoded_field_a = OneofMessage.encode(OneofMessage.new(:a => "string")) 833ffe3c632Sopenharmony_ci encoded_field_b = OneofMessage.encode(OneofMessage.new(:b => 1000)) 834ffe3c632Sopenharmony_ci encoded_field_c = OneofMessage.encode( 835ffe3c632Sopenharmony_ci OneofMessage.new(:c => TestMessage2.new(:foo => 1))) 836ffe3c632Sopenharmony_ci encoded_field_d = OneofMessage.encode(OneofMessage.new(:d => :B)) 837ffe3c632Sopenharmony_ci 838ffe3c632Sopenharmony_ci d3 = OneofMessage.decode( 839ffe3c632Sopenharmony_ci encoded_field_c + encoded_field_a + encoded_field_d) 840ffe3c632Sopenharmony_ci assert d3.a == "" 841ffe3c632Sopenharmony_ci assert d3.b == 0 842ffe3c632Sopenharmony_ci assert d3.c == nil 843ffe3c632Sopenharmony_ci assert d3.d == :B 844ffe3c632Sopenharmony_ci 845ffe3c632Sopenharmony_ci d4 = OneofMessage.decode( 846ffe3c632Sopenharmony_ci encoded_field_c + encoded_field_a + encoded_field_d + 847ffe3c632Sopenharmony_ci encoded_field_c) 848ffe3c632Sopenharmony_ci assert d4.a == "" 849ffe3c632Sopenharmony_ci assert d4.b == 0 850ffe3c632Sopenharmony_ci assert d4.c.foo == 1 851ffe3c632Sopenharmony_ci assert d4.d == :Default 852ffe3c632Sopenharmony_ci 853ffe3c632Sopenharmony_ci d5 = OneofMessage.new(:a => "hello") 854ffe3c632Sopenharmony_ci assert d5.a == "hello" 855ffe3c632Sopenharmony_ci d5.a = nil 856ffe3c632Sopenharmony_ci assert d5.a == "" 857ffe3c632Sopenharmony_ci assert OneofMessage.encode(d5) == '' 858ffe3c632Sopenharmony_ci assert d5.my_oneof == nil 859ffe3c632Sopenharmony_ci end 860ffe3c632Sopenharmony_ci 861ffe3c632Sopenharmony_ci def test_enum_field 862ffe3c632Sopenharmony_ci m = TestMessage.new 863ffe3c632Sopenharmony_ci assert m.optional_enum == :Default 864ffe3c632Sopenharmony_ci m.optional_enum = :A 865ffe3c632Sopenharmony_ci assert m.optional_enum == :A 866ffe3c632Sopenharmony_ci assert_raise RangeError do 867ffe3c632Sopenharmony_ci m.optional_enum = :ASDF 868ffe3c632Sopenharmony_ci end 869ffe3c632Sopenharmony_ci m.optional_enum = 1 870ffe3c632Sopenharmony_ci assert m.optional_enum == :A 871ffe3c632Sopenharmony_ci m.optional_enum = 100 872ffe3c632Sopenharmony_ci assert m.optional_enum == 100 873ffe3c632Sopenharmony_ci end 874ffe3c632Sopenharmony_ci 875ffe3c632Sopenharmony_ci def test_dup 876ffe3c632Sopenharmony_ci m = TestMessage.new 877ffe3c632Sopenharmony_ci m.optional_string = "hello" 878ffe3c632Sopenharmony_ci m.optional_int32 = 42 879ffe3c632Sopenharmony_ci tm1 = TestMessage2.new(:foo => 100) 880ffe3c632Sopenharmony_ci tm2 = TestMessage2.new(:foo => 200) 881ffe3c632Sopenharmony_ci m.repeated_msg.push tm1 882ffe3c632Sopenharmony_ci assert m.repeated_msg[-1] == tm1 883ffe3c632Sopenharmony_ci m.repeated_msg.push tm2 884ffe3c632Sopenharmony_ci assert m.repeated_msg[-1] == tm2 885ffe3c632Sopenharmony_ci m2 = m.dup 886ffe3c632Sopenharmony_ci assert m == m2 887ffe3c632Sopenharmony_ci m.optional_int32 += 1 888ffe3c632Sopenharmony_ci assert m != m2 889ffe3c632Sopenharmony_ci assert m.repeated_msg[0] == m2.repeated_msg[0] 890ffe3c632Sopenharmony_ci assert m.repeated_msg[0].object_id == m2.repeated_msg[0].object_id 891ffe3c632Sopenharmony_ci end 892ffe3c632Sopenharmony_ci 893ffe3c632Sopenharmony_ci def test_deep_copy 894ffe3c632Sopenharmony_ci m = TestMessage.new(:optional_int32 => 42, 895ffe3c632Sopenharmony_ci :repeated_msg => [TestMessage2.new(:foo => 100)]) 896ffe3c632Sopenharmony_ci m2 = Google::Protobuf.deep_copy(m) 897ffe3c632Sopenharmony_ci assert m == m2 898ffe3c632Sopenharmony_ci assert m.repeated_msg == m2.repeated_msg 899ffe3c632Sopenharmony_ci assert m.repeated_msg.object_id != m2.repeated_msg.object_id 900ffe3c632Sopenharmony_ci assert m.repeated_msg[0].object_id != m2.repeated_msg[0].object_id 901ffe3c632Sopenharmony_ci end 902ffe3c632Sopenharmony_ci 903ffe3c632Sopenharmony_ci def test_eq 904ffe3c632Sopenharmony_ci m = TestMessage.new(:optional_int32 => 42, 905ffe3c632Sopenharmony_ci :repeated_int32 => [1, 2, 3]) 906ffe3c632Sopenharmony_ci m2 = TestMessage.new(:optional_int32 => 43, 907ffe3c632Sopenharmony_ci :repeated_int32 => [1, 2, 3]) 908ffe3c632Sopenharmony_ci assert m != m2 909ffe3c632Sopenharmony_ci end 910ffe3c632Sopenharmony_ci 911ffe3c632Sopenharmony_ci def test_enum_lookup 912ffe3c632Sopenharmony_ci assert TestEnum::A == 1 913ffe3c632Sopenharmony_ci assert TestEnum::B == 2 914ffe3c632Sopenharmony_ci assert TestEnum::C == 3 915ffe3c632Sopenharmony_ci 916ffe3c632Sopenharmony_ci assert TestEnum::lookup(1) == :A 917ffe3c632Sopenharmony_ci assert TestEnum::lookup(2) == :B 918ffe3c632Sopenharmony_ci assert TestEnum::lookup(3) == :C 919ffe3c632Sopenharmony_ci 920ffe3c632Sopenharmony_ci assert TestEnum::resolve(:A) == 1 921ffe3c632Sopenharmony_ci assert TestEnum::resolve(:B) == 2 922ffe3c632Sopenharmony_ci assert TestEnum::resolve(:C) == 3 923ffe3c632Sopenharmony_ci end 924ffe3c632Sopenharmony_ci 925ffe3c632Sopenharmony_ci def test_parse_serialize 926ffe3c632Sopenharmony_ci m = TestMessage.new(:optional_int32 => 42, 927ffe3c632Sopenharmony_ci :optional_string => "hello world", 928ffe3c632Sopenharmony_ci :optional_enum => :B, 929ffe3c632Sopenharmony_ci :repeated_string => ["a", "b", "c"], 930ffe3c632Sopenharmony_ci :repeated_int32 => [42, 43, 44], 931ffe3c632Sopenharmony_ci :repeated_enum => [:A, :B, :C, 100], 932ffe3c632Sopenharmony_ci :repeated_msg => [TestMessage2.new(:foo => 1), 933ffe3c632Sopenharmony_ci TestMessage2.new(:foo => 2)]) 934ffe3c632Sopenharmony_ci data = TestMessage.encode m 935ffe3c632Sopenharmony_ci m2 = TestMessage.decode data 936ffe3c632Sopenharmony_ci assert m == m2 937ffe3c632Sopenharmony_ci 938ffe3c632Sopenharmony_ci data = Google::Protobuf.encode m 939ffe3c632Sopenharmony_ci m2 = Google::Protobuf.decode(TestMessage, data) 940ffe3c632Sopenharmony_ci assert m == m2 941ffe3c632Sopenharmony_ci end 942ffe3c632Sopenharmony_ci 943ffe3c632Sopenharmony_ci def test_encode_decode_helpers 944ffe3c632Sopenharmony_ci m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) 945ffe3c632Sopenharmony_ci assert_equal 'foo', m.optional_string 946ffe3c632Sopenharmony_ci assert_equal ['bar1', 'bar2'], m.repeated_string 947ffe3c632Sopenharmony_ci 948ffe3c632Sopenharmony_ci json = m.to_json 949ffe3c632Sopenharmony_ci m2 = TestMessage.decode_json(json) 950ffe3c632Sopenharmony_ci assert_equal 'foo', m2.optional_string 951ffe3c632Sopenharmony_ci assert_equal ['bar1', 'bar2'], m2.repeated_string 952ffe3c632Sopenharmony_ci if RUBY_PLATFORM != "java" 953ffe3c632Sopenharmony_ci assert m2.optional_string.frozen? 954ffe3c632Sopenharmony_ci assert m2.repeated_string[0].frozen? 955ffe3c632Sopenharmony_ci end 956ffe3c632Sopenharmony_ci 957ffe3c632Sopenharmony_ci proto = m.to_proto 958ffe3c632Sopenharmony_ci m2 = TestMessage.decode(proto) 959ffe3c632Sopenharmony_ci assert_equal 'foo', m2.optional_string 960ffe3c632Sopenharmony_ci assert_equal ['bar1', 'bar2'], m2.repeated_string 961ffe3c632Sopenharmony_ci end 962ffe3c632Sopenharmony_ci 963ffe3c632Sopenharmony_ci def test_protobuf_encode_decode_helpers 964ffe3c632Sopenharmony_ci m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) 965ffe3c632Sopenharmony_ci encoded_msg = Google::Protobuf.encode(m) 966ffe3c632Sopenharmony_ci assert_equal m.to_proto, encoded_msg 967ffe3c632Sopenharmony_ci 968ffe3c632Sopenharmony_ci decoded_msg = Google::Protobuf.decode(TestMessage, encoded_msg) 969ffe3c632Sopenharmony_ci assert_equal TestMessage.decode(m.to_proto), decoded_msg 970ffe3c632Sopenharmony_ci end 971ffe3c632Sopenharmony_ci 972ffe3c632Sopenharmony_ci def test_protobuf_encode_decode_json_helpers 973ffe3c632Sopenharmony_ci m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) 974ffe3c632Sopenharmony_ci encoded_msg = Google::Protobuf.encode_json(m) 975ffe3c632Sopenharmony_ci assert_equal m.to_json, encoded_msg 976ffe3c632Sopenharmony_ci 977ffe3c632Sopenharmony_ci decoded_msg = Google::Protobuf.decode_json(TestMessage, encoded_msg) 978ffe3c632Sopenharmony_ci assert_equal TestMessage.decode_json(m.to_json), decoded_msg 979ffe3c632Sopenharmony_ci end 980ffe3c632Sopenharmony_ci 981ffe3c632Sopenharmony_ci def test_to_h 982ffe3c632Sopenharmony_ci m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) 983ffe3c632Sopenharmony_ci expected_result = { 984ffe3c632Sopenharmony_ci :optional_bool=>true, 985ffe3c632Sopenharmony_ci :optional_bytes=>"", 986ffe3c632Sopenharmony_ci :optional_double=>-10.100001, 987ffe3c632Sopenharmony_ci :optional_enum=>:Default, 988ffe3c632Sopenharmony_ci :optional_float=>0.0, 989ffe3c632Sopenharmony_ci :optional_int32=>0, 990ffe3c632Sopenharmony_ci :optional_int64=>0, 991ffe3c632Sopenharmony_ci :optional_msg=>nil, 992ffe3c632Sopenharmony_ci :optional_string=>"foo", 993ffe3c632Sopenharmony_ci :optional_uint32=>0, 994ffe3c632Sopenharmony_ci :optional_uint64=>0, 995ffe3c632Sopenharmony_ci :repeated_bool=>[], 996ffe3c632Sopenharmony_ci :repeated_bytes=>[], 997ffe3c632Sopenharmony_ci :repeated_double=>[], 998ffe3c632Sopenharmony_ci :repeated_enum=>[], 999ffe3c632Sopenharmony_ci :repeated_float=>[], 1000ffe3c632Sopenharmony_ci :repeated_int32=>[], 1001ffe3c632Sopenharmony_ci :repeated_int64=>[], 1002ffe3c632Sopenharmony_ci :repeated_msg=>[], 1003ffe3c632Sopenharmony_ci :repeated_string=>["bar1", "bar2"], 1004ffe3c632Sopenharmony_ci :repeated_uint32=>[], 1005ffe3c632Sopenharmony_ci :repeated_uint64=>[] 1006ffe3c632Sopenharmony_ci } 1007ffe3c632Sopenharmony_ci assert_equal expected_result, m.to_h 1008ffe3c632Sopenharmony_ci end 1009ffe3c632Sopenharmony_ci 1010ffe3c632Sopenharmony_ci 1011ffe3c632Sopenharmony_ci def test_def_errors 1012ffe3c632Sopenharmony_ci s = Google::Protobuf::DescriptorPool.new 1013ffe3c632Sopenharmony_ci success = false 1014ffe3c632Sopenharmony_ci begin 1015ffe3c632Sopenharmony_ci s.build do 1016ffe3c632Sopenharmony_ci # enum with no default (integer value 0) 1017ffe3c632Sopenharmony_ci add_enum "MyEnum" do 1018ffe3c632Sopenharmony_ci value :A, 1 1019ffe3c632Sopenharmony_ci end 1020ffe3c632Sopenharmony_ci end 1021ffe3c632Sopenharmony_ci rescue TypeError 1022ffe3c632Sopenharmony_ci success = true 1023ffe3c632Sopenharmony_ci end 1024ffe3c632Sopenharmony_ci assert(success) 1025ffe3c632Sopenharmony_ci success = false 1026ffe3c632Sopenharmony_ci begin 1027ffe3c632Sopenharmony_ci s.build do 1028ffe3c632Sopenharmony_ci # message with required field (unsupported in proto3) 1029ffe3c632Sopenharmony_ci add_message "MyMessage" do 1030ffe3c632Sopenharmony_ci required :foo, :int32, 1 1031ffe3c632Sopenharmony_ci end 1032ffe3c632Sopenharmony_ci end 1033ffe3c632Sopenharmony_ci rescue TypeError 1034ffe3c632Sopenharmony_ci success = true 1035ffe3c632Sopenharmony_ci end 1036ffe3c632Sopenharmony_ci assert(success) 1037ffe3c632Sopenharmony_ci end 1038ffe3c632Sopenharmony_ci 1039ffe3c632Sopenharmony_ci def test_corecursive 1040ffe3c632Sopenharmony_ci # just be sure that we can instantiate types with corecursive field-type 1041ffe3c632Sopenharmony_ci # references. 1042ffe3c632Sopenharmony_ci m = Recursive1.new(:foo => Recursive2.new(:foo => Recursive1.new)) 1043ffe3c632Sopenharmony_ci assert Recursive1.descriptor.lookup("foo").subtype == 1044ffe3c632Sopenharmony_ci Recursive2.descriptor 1045ffe3c632Sopenharmony_ci assert Recursive2.descriptor.lookup("foo").subtype == 1046ffe3c632Sopenharmony_ci Recursive1.descriptor 1047ffe3c632Sopenharmony_ci 1048ffe3c632Sopenharmony_ci serialized = Recursive1.encode(m) 1049ffe3c632Sopenharmony_ci m2 = Recursive1.decode(serialized) 1050ffe3c632Sopenharmony_ci assert m == m2 1051ffe3c632Sopenharmony_ci end 1052ffe3c632Sopenharmony_ci 1053ffe3c632Sopenharmony_ci def test_serialize_cycle 1054ffe3c632Sopenharmony_ci m = Recursive1.new(:foo => Recursive2.new) 1055ffe3c632Sopenharmony_ci m.foo.foo = m 1056ffe3c632Sopenharmony_ci assert_raise RuntimeError do 1057ffe3c632Sopenharmony_ci serialized = Recursive1.encode(m) 1058ffe3c632Sopenharmony_ci end 1059ffe3c632Sopenharmony_ci end 1060ffe3c632Sopenharmony_ci 1061ffe3c632Sopenharmony_ci def test_bad_field_names 1062ffe3c632Sopenharmony_ci m = BadFieldNames.new(:dup => 1, :class => 2) 1063ffe3c632Sopenharmony_ci m2 = m.dup 1064ffe3c632Sopenharmony_ci assert m == m2 1065ffe3c632Sopenharmony_ci assert m['dup'] == 1 1066ffe3c632Sopenharmony_ci assert m['class'] == 2 1067ffe3c632Sopenharmony_ci m['dup'] = 3 1068ffe3c632Sopenharmony_ci assert m['dup'] == 3 1069ffe3c632Sopenharmony_ci end 1070ffe3c632Sopenharmony_ci 1071ffe3c632Sopenharmony_ci def test_int_ranges 1072ffe3c632Sopenharmony_ci m = TestMessage.new 1073ffe3c632Sopenharmony_ci 1074ffe3c632Sopenharmony_ci m.optional_int32 = 0 1075ffe3c632Sopenharmony_ci m.optional_int32 = -0x8000_0000 1076ffe3c632Sopenharmony_ci m.optional_int32 = +0x7fff_ffff 1077ffe3c632Sopenharmony_ci m.optional_int32 = 1.0 1078ffe3c632Sopenharmony_ci m.optional_int32 = -1.0 1079ffe3c632Sopenharmony_ci m.optional_int32 = 2e9 1080ffe3c632Sopenharmony_ci assert_raise RangeError do 1081ffe3c632Sopenharmony_ci m.optional_int32 = -0x8000_0001 1082ffe3c632Sopenharmony_ci end 1083ffe3c632Sopenharmony_ci assert_raise RangeError do 1084ffe3c632Sopenharmony_ci m.optional_int32 = +0x8000_0000 1085ffe3c632Sopenharmony_ci end 1086ffe3c632Sopenharmony_ci assert_raise RangeError do 1087ffe3c632Sopenharmony_ci m.optional_int32 = +0x1000_0000_0000_0000_0000_0000 # force Bignum 1088ffe3c632Sopenharmony_ci end 1089ffe3c632Sopenharmony_ci assert_raise RangeError do 1090ffe3c632Sopenharmony_ci m.optional_int32 = 1e12 1091ffe3c632Sopenharmony_ci end 1092ffe3c632Sopenharmony_ci assert_raise RangeError do 1093ffe3c632Sopenharmony_ci m.optional_int32 = 1.5 1094ffe3c632Sopenharmony_ci end 1095ffe3c632Sopenharmony_ci 1096ffe3c632Sopenharmony_ci m.optional_uint32 = 0 1097ffe3c632Sopenharmony_ci m.optional_uint32 = +0xffff_ffff 1098ffe3c632Sopenharmony_ci m.optional_uint32 = 1.0 1099ffe3c632Sopenharmony_ci m.optional_uint32 = 4e9 1100ffe3c632Sopenharmony_ci assert_raise RangeError do 1101ffe3c632Sopenharmony_ci m.optional_uint32 = -1 1102ffe3c632Sopenharmony_ci end 1103ffe3c632Sopenharmony_ci assert_raise RangeError do 1104ffe3c632Sopenharmony_ci m.optional_uint32 = -1.5 1105ffe3c632Sopenharmony_ci end 1106ffe3c632Sopenharmony_ci assert_raise RangeError do 1107ffe3c632Sopenharmony_ci m.optional_uint32 = -1.5e12 1108ffe3c632Sopenharmony_ci end 1109ffe3c632Sopenharmony_ci assert_raise RangeError do 1110ffe3c632Sopenharmony_ci m.optional_uint32 = -0x1000_0000_0000_0000 1111ffe3c632Sopenharmony_ci end 1112ffe3c632Sopenharmony_ci assert_raise RangeError do 1113ffe3c632Sopenharmony_ci m.optional_uint32 = +0x1_0000_0000 1114ffe3c632Sopenharmony_ci end 1115ffe3c632Sopenharmony_ci assert_raise RangeError do 1116ffe3c632Sopenharmony_ci m.optional_uint32 = +0x1000_0000_0000_0000_0000_0000 # force Bignum 1117ffe3c632Sopenharmony_ci end 1118ffe3c632Sopenharmony_ci assert_raise RangeError do 1119ffe3c632Sopenharmony_ci m.optional_uint32 = 1e12 1120ffe3c632Sopenharmony_ci end 1121ffe3c632Sopenharmony_ci assert_raise RangeError do 1122ffe3c632Sopenharmony_ci m.optional_uint32 = 1.5 1123ffe3c632Sopenharmony_ci end 1124ffe3c632Sopenharmony_ci 1125ffe3c632Sopenharmony_ci m.optional_int64 = 0 1126ffe3c632Sopenharmony_ci m.optional_int64 = -0x8000_0000_0000_0000 1127ffe3c632Sopenharmony_ci m.optional_int64 = +0x7fff_ffff_ffff_ffff 1128ffe3c632Sopenharmony_ci m.optional_int64 = 1.0 1129ffe3c632Sopenharmony_ci m.optional_int64 = -1.0 1130ffe3c632Sopenharmony_ci m.optional_int64 = 8e18 1131ffe3c632Sopenharmony_ci m.optional_int64 = -8e18 1132ffe3c632Sopenharmony_ci assert_raise RangeError do 1133ffe3c632Sopenharmony_ci m.optional_int64 = -0x8000_0000_0000_0001 1134ffe3c632Sopenharmony_ci end 1135ffe3c632Sopenharmony_ci assert_raise RangeError do 1136ffe3c632Sopenharmony_ci m.optional_int64 = +0x8000_0000_0000_0000 1137ffe3c632Sopenharmony_ci end 1138ffe3c632Sopenharmony_ci assert_raise RangeError do 1139ffe3c632Sopenharmony_ci m.optional_int64 = +0x1000_0000_0000_0000_0000_0000 # force Bignum 1140ffe3c632Sopenharmony_ci end 1141ffe3c632Sopenharmony_ci assert_raise RangeError do 1142ffe3c632Sopenharmony_ci m.optional_int64 = 1e50 1143ffe3c632Sopenharmony_ci end 1144ffe3c632Sopenharmony_ci assert_raise RangeError do 1145ffe3c632Sopenharmony_ci m.optional_int64 = 1.5 1146ffe3c632Sopenharmony_ci end 1147ffe3c632Sopenharmony_ci 1148ffe3c632Sopenharmony_ci m.optional_uint64 = 0 1149ffe3c632Sopenharmony_ci m.optional_uint64 = +0xffff_ffff_ffff_ffff 1150ffe3c632Sopenharmony_ci m.optional_uint64 = 1.0 1151ffe3c632Sopenharmony_ci m.optional_uint64 = 16e18 1152ffe3c632Sopenharmony_ci assert_raise RangeError do 1153ffe3c632Sopenharmony_ci m.optional_uint64 = -1 1154ffe3c632Sopenharmony_ci end 1155ffe3c632Sopenharmony_ci assert_raise RangeError do 1156ffe3c632Sopenharmony_ci m.optional_uint64 = -1.5 1157ffe3c632Sopenharmony_ci end 1158ffe3c632Sopenharmony_ci assert_raise RangeError do 1159ffe3c632Sopenharmony_ci m.optional_uint64 = -1.5e12 1160ffe3c632Sopenharmony_ci end 1161ffe3c632Sopenharmony_ci assert_raise RangeError do 1162ffe3c632Sopenharmony_ci m.optional_uint64 = -0x1_0000_0000_0000_0000 1163ffe3c632Sopenharmony_ci end 1164ffe3c632Sopenharmony_ci assert_raise RangeError do 1165ffe3c632Sopenharmony_ci m.optional_uint64 = +0x1_0000_0000_0000_0000 1166ffe3c632Sopenharmony_ci end 1167ffe3c632Sopenharmony_ci assert_raise RangeError do 1168ffe3c632Sopenharmony_ci m.optional_uint64 = +0x1000_0000_0000_0000_0000_0000 # force Bignum 1169ffe3c632Sopenharmony_ci end 1170ffe3c632Sopenharmony_ci assert_raise RangeError do 1171ffe3c632Sopenharmony_ci m.optional_uint64 = 1e50 1172ffe3c632Sopenharmony_ci end 1173ffe3c632Sopenharmony_ci assert_raise RangeError do 1174ffe3c632Sopenharmony_ci m.optional_uint64 = 1.5 1175ffe3c632Sopenharmony_ci end 1176ffe3c632Sopenharmony_ci end 1177ffe3c632Sopenharmony_ci 1178ffe3c632Sopenharmony_ci def test_stress_test 1179ffe3c632Sopenharmony_ci m = TestMessage.new 1180ffe3c632Sopenharmony_ci m.optional_int32 = 42 1181ffe3c632Sopenharmony_ci m.optional_int64 = 0x100000000 1182ffe3c632Sopenharmony_ci m.optional_string = "hello world" 1183ffe3c632Sopenharmony_ci 10.times do m.repeated_msg.push TestMessage2.new(:foo => 42) end 1184ffe3c632Sopenharmony_ci 10.times do m.repeated_string.push "hello world" end 1185ffe3c632Sopenharmony_ci 1186ffe3c632Sopenharmony_ci data = TestMessage.encode(m) 1187ffe3c632Sopenharmony_ci 1188ffe3c632Sopenharmony_ci l = 0 1189ffe3c632Sopenharmony_ci 10_000.times do 1190ffe3c632Sopenharmony_ci m = TestMessage.decode(data) 1191ffe3c632Sopenharmony_ci data_new = TestMessage.encode(m) 1192ffe3c632Sopenharmony_ci assert data_new == data 1193ffe3c632Sopenharmony_ci data = data_new 1194ffe3c632Sopenharmony_ci end 1195ffe3c632Sopenharmony_ci end 1196ffe3c632Sopenharmony_ci 1197ffe3c632Sopenharmony_ci def test_reflection 1198ffe3c632Sopenharmony_ci m = TestMessage.new(:optional_int32 => 1234) 1199ffe3c632Sopenharmony_ci msgdef = m.class.descriptor 1200ffe3c632Sopenharmony_ci assert msgdef.class == Google::Protobuf::Descriptor 1201ffe3c632Sopenharmony_ci assert msgdef.any? {|field| field.name == "optional_int32"} 1202ffe3c632Sopenharmony_ci optional_int32 = msgdef.lookup "optional_int32" 1203ffe3c632Sopenharmony_ci assert optional_int32.class == Google::Protobuf::FieldDescriptor 1204ffe3c632Sopenharmony_ci assert optional_int32 != nil 1205ffe3c632Sopenharmony_ci assert optional_int32.name == "optional_int32" 1206ffe3c632Sopenharmony_ci assert optional_int32.type == :int32 1207ffe3c632Sopenharmony_ci optional_int32.set(m, 5678) 1208ffe3c632Sopenharmony_ci assert m.optional_int32 == 5678 1209ffe3c632Sopenharmony_ci m.optional_int32 = 1000 1210ffe3c632Sopenharmony_ci assert optional_int32.get(m) == 1000 1211ffe3c632Sopenharmony_ci 1212ffe3c632Sopenharmony_ci optional_msg = msgdef.lookup "optional_msg" 1213ffe3c632Sopenharmony_ci assert optional_msg.subtype == TestMessage2.descriptor 1214ffe3c632Sopenharmony_ci 1215ffe3c632Sopenharmony_ci optional_msg.set(m, optional_msg.subtype.msgclass.new) 1216ffe3c632Sopenharmony_ci 1217ffe3c632Sopenharmony_ci assert msgdef.msgclass == TestMessage 1218ffe3c632Sopenharmony_ci 1219ffe3c632Sopenharmony_ci optional_enum = msgdef.lookup "optional_enum" 1220ffe3c632Sopenharmony_ci assert optional_enum.subtype == TestEnum.descriptor 1221ffe3c632Sopenharmony_ci assert optional_enum.subtype.class == Google::Protobuf::EnumDescriptor 1222ffe3c632Sopenharmony_ci optional_enum.subtype.each do |k, v| 1223ffe3c632Sopenharmony_ci # set with integer, check resolution to symbolic name 1224ffe3c632Sopenharmony_ci optional_enum.set(m, v) 1225ffe3c632Sopenharmony_ci assert optional_enum.get(m) == k 1226ffe3c632Sopenharmony_ci end 1227ffe3c632Sopenharmony_ci end 1228ffe3c632Sopenharmony_ci 1229ffe3c632Sopenharmony_ci def test_json 1230ffe3c632Sopenharmony_ci # TODO: Fix JSON in JRuby version. 1231ffe3c632Sopenharmony_ci return if RUBY_PLATFORM == "java" 1232ffe3c632Sopenharmony_ci m = TestMessage.new(:optional_int32 => 1234, 1233ffe3c632Sopenharmony_ci :optional_int64 => -0x1_0000_0000, 1234ffe3c632Sopenharmony_ci :optional_uint32 => 0x8000_0000, 1235ffe3c632Sopenharmony_ci :optional_uint64 => 0xffff_ffff_ffff_ffff, 1236ffe3c632Sopenharmony_ci :optional_bool => true, 1237ffe3c632Sopenharmony_ci :optional_float => 1.0, 1238ffe3c632Sopenharmony_ci :optional_double => -1e100, 1239ffe3c632Sopenharmony_ci :optional_string => "Test string", 1240ffe3c632Sopenharmony_ci :optional_bytes => ["FFFFFFFF"].pack('H*'), 1241ffe3c632Sopenharmony_ci :optional_msg => TestMessage2.new(:foo => 42), 1242ffe3c632Sopenharmony_ci :repeated_int32 => [1, 2, 3, 4], 1243ffe3c632Sopenharmony_ci :repeated_string => ["a", "b", "c"], 1244ffe3c632Sopenharmony_ci :repeated_bool => [true, false, true, false], 1245ffe3c632Sopenharmony_ci :repeated_msg => [TestMessage2.new(:foo => 1), 1246ffe3c632Sopenharmony_ci TestMessage2.new(:foo => 2)]) 1247ffe3c632Sopenharmony_ci 1248ffe3c632Sopenharmony_ci json_text = TestMessage.encode_json(m) 1249ffe3c632Sopenharmony_ci m2 = TestMessage.decode_json(json_text) 1250ffe3c632Sopenharmony_ci assert m == m2 1251ffe3c632Sopenharmony_ci 1252ffe3c632Sopenharmony_ci # Crash case from GitHub issue 283. 1253ffe3c632Sopenharmony_ci bar = Bar.new(msg: "bar") 1254ffe3c632Sopenharmony_ci baz1 = Baz.new(msg: "baz") 1255ffe3c632Sopenharmony_ci baz2 = Baz.new(msg: "quux") 1256ffe3c632Sopenharmony_ci Foo.encode_json(Foo.new) 1257ffe3c632Sopenharmony_ci Foo.encode_json(Foo.new(bar: bar)) 1258ffe3c632Sopenharmony_ci Foo.encode_json(Foo.new(bar: bar, baz: [baz1, baz2])) 1259ffe3c632Sopenharmony_ci end 1260ffe3c632Sopenharmony_ci 1261ffe3c632Sopenharmony_ci def test_json_maps 1262ffe3c632Sopenharmony_ci # TODO: Fix JSON in JRuby version. 1263ffe3c632Sopenharmony_ci return if RUBY_PLATFORM == "java" 1264ffe3c632Sopenharmony_ci m = MapMessage.new(:map_string_int32 => {"a" => 1}) 1265ffe3c632Sopenharmony_ci expected = '{"mapStringInt32":{"a":1},"mapStringMsg":{}}' 1266ffe3c632Sopenharmony_ci expected_preserve = '{"map_string_int32":{"a":1},"map_string_msg":{}}' 1267ffe3c632Sopenharmony_ci assert MapMessage.encode_json(m) == expected 1268ffe3c632Sopenharmony_ci 1269ffe3c632Sopenharmony_ci json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true) 1270ffe3c632Sopenharmony_ci assert json == expected_preserve 1271ffe3c632Sopenharmony_ci 1272ffe3c632Sopenharmony_ci m2 = MapMessage.decode_json(MapMessage.encode_json(m)) 1273ffe3c632Sopenharmony_ci assert m == m2 1274ffe3c632Sopenharmony_ci end 1275ffe3c632Sopenharmony_ci end 1276ffe3c632Sopenharmony_ciend 1277