Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(343)

Side by Side Diff: third_party/protobuf/ruby/tests/basic.rb

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Make chrome settings proto generated file a component Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/ruby 1 #!/usr/bin/ruby
2 2
3 require 'google/protobuf' 3 require 'google/protobuf'
4 require 'test/unit' 4 require 'test/unit'
5 5
6 # ------------- generated code -------------- 6 # ------------- generated code --------------
7 7
8 module BasicTest 8 module BasicTest
9 pool = Google::Protobuf::DescriptorPool.new 9 pool = Google::Protobuf::DescriptorPool.new
10 pool.build do 10 pool.build do
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 m = TestMessage.new(:optional_int32 => -42, 176 m = TestMessage.new(:optional_int32 => -42,
177 :optional_enum => :A, 177 :optional_enum => :A,
178 :optional_msg => TestMessage2.new, 178 :optional_msg => TestMessage2.new,
179 :repeated_string => ["hello", "there", "world"]) 179 :repeated_string => ["hello", "there", "world"])
180 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_ms g: <BasicTest::TestMessage2: foo: 0>, optional_enum: :A, repeated_int32: [], rep eated_int64: [], repeated_uint32: [], repeated_uint64: [], repeated_bool: [], re peated_float: [], repeated_double: [], repeated_string: ["hello", "there", "worl d"], repeated_bytes: [], repeated_msg: [], repeated_enum: []>' 180 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_ms g: <BasicTest::TestMessage2: foo: 0>, optional_enum: :A, repeated_int32: [], rep eated_int64: [], repeated_uint32: [], repeated_uint64: [], repeated_bool: [], re peated_float: [], repeated_double: [], repeated_string: ["hello", "there", "worl d"], repeated_bytes: [], repeated_msg: [], repeated_enum: []>'
181 assert_equal expected, m.inspect 181 assert_equal expected, m.inspect
182 end 182 end
183 183
184 def test_hash 184 def test_hash
185 m1 = TestMessage.new(:optional_int32 => 42) 185 m1 = TestMessage.new(:optional_int32 => 42)
186 m2 = TestMessage.new(:optional_int32 => 102) 186 m2 = TestMessage.new(:optional_int32 => 102, repeated_string: ['please', ' work', 'ok?'])
187 m3 = TestMessage.new(:optional_int32 => 102, repeated_string: ['please', ' work', 'ok?'])
187 assert m1.hash != 0 188 assert m1.hash != 0
188 assert m2.hash != 0 189 assert m2.hash != 0
190 assert m3.hash != 0
189 # relying on the randomness here -- if hash function changes and we are 191 # relying on the randomness here -- if hash function changes and we are
190 # unlucky enough to get a collision, then change the values above. 192 # unlucky enough to get a collision, then change the values above.
191 assert m1.hash != m2.hash 193 assert m1.hash != m2.hash
194 assert_equal m2.hash, m3.hash
192 end 195 end
193 196
194 def test_unknown_field_errors 197 def test_unknown_field_errors
195 e = assert_raise NoMethodError do 198 e = assert_raise NoMethodError do
196 TestMessage.new.hello 199 TestMessage.new.hello
197 end 200 end
198 assert_match(/hello/, e.message) 201 assert_match(/hello/, e.message)
199 202
200 e = assert_raise NoMethodError do 203 e = assert_raise NoMethodError do
201 TestMessage.new.hello = "world" 204 TestMessage.new.hello = "world"
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 251
249 assert_raise TypeError do 252 assert_raise TypeError do
250 m.repeated_msg.push TestMessage.new 253 m.repeated_msg.push TestMessage.new
251 end 254 end
252 end 255 end
253 256
254 def test_string_encoding 257 def test_string_encoding
255 m = TestMessage.new 258 m = TestMessage.new
256 259
257 # Assigning a normal (ASCII or UTF8) string to a bytes field, or 260 # Assigning a normal (ASCII or UTF8) string to a bytes field, or
258 # ASCII-8BIT to a string field, raises an error. 261 # ASCII-8BIT to a string field will convert to the proper encoding.
259 assert_raise TypeError do 262 m.optional_bytes = "Test string ASCII".encode!('ASCII')
260 m.optional_bytes = "Test string ASCII".encode!('ASCII') 263 assert m.optional_bytes.frozen?
261 end 264 assert_equal Encoding::ASCII_8BIT, m.optional_bytes.encoding
262 assert_raise TypeError do 265 assert_equal "Test string ASCII", m.optional_bytes
266
267 assert_raise Encoding::UndefinedConversionError do
263 m.optional_bytes = "Test string UTF-8 \u0100".encode!('UTF-8') 268 m.optional_bytes = "Test string UTF-8 \u0100".encode!('UTF-8')
264 end 269 end
265 assert_raise TypeError do 270
271 assert_raise Encoding::UndefinedConversionError do
266 m.optional_string = ["FFFF"].pack('H*') 272 m.optional_string = ["FFFF"].pack('H*')
267 end 273 end
268 274
269 # "Ordinary" use case. 275 # "Ordinary" use case.
270 m.optional_bytes = ["FFFF"].pack('H*') 276 m.optional_bytes = ["FFFF"].pack('H*')
271 m.optional_string = "\u0100" 277 m.optional_string = "\u0100"
272 278
273 # strings are mutable so we can do this, but serialize should catch it. 279 # strings are immutable so we can't do this, but serialize should catch it .
274 m.optional_string = "asdf".encode!('UTF-8') 280 m.optional_string = "asdf".encode!('UTF-8')
275 m.optional_string.encode!('ASCII-8BIT') 281 assert_raise RuntimeError do
276 assert_raise TypeError do 282 m.optional_string.encode!('ASCII-8BIT')
277 data = TestMessage.encode(m)
278 end 283 end
279 end 284 end
280 285
281 def test_rptfield_int32 286 def test_rptfield_int32
282 l = Google::Protobuf::RepeatedField.new(:int32) 287 l = Google::Protobuf::RepeatedField.new(:int32)
283 assert l.count == 0 288 assert l.count == 0
284 l = Google::Protobuf::RepeatedField.new(:int32, [1, 2, 3]) 289 l = Google::Protobuf::RepeatedField.new(:int32, [1, 2, 3])
285 assert l.count == 3 290 assert l.count == 3
286 assert_equal [1, 2, 3], l 291 assert_equal [1, 2, 3], l
287 assert_equal l, [1, 2, 3] 292 assert_equal l, [1, 2, 3]
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 m = Google::Protobuf::Map.new(:string, :int32) 464 m = Google::Protobuf::Map.new(:string, :int32)
460 m["asdf"] = 1 465 m["asdf"] = 1
461 assert m["asdf"] == 1 466 assert m["asdf"] == 1
462 m["jkl;"] = 42 467 m["jkl;"] = 42
463 assert m == { "jkl;" => 42, "asdf" => 1 } 468 assert m == { "jkl;" => 42, "asdf" => 1 }
464 assert m.has_key?("asdf") 469 assert m.has_key?("asdf")
465 assert !m.has_key?("qwerty") 470 assert !m.has_key?("qwerty")
466 assert m.length == 2 471 assert m.length == 2
467 472
468 m2 = m.dup 473 m2 = m.dup
469 assert m == m2 474 assert_equal m, m2
470 assert m.hash != 0 475 assert m.hash != 0
471 assert m.hash == m2.hash 476 assert_equal m.hash, m2.hash
472 477
473 collected = {} 478 collected = {}
474 m.each { |k,v| collected[v] = k } 479 m.each { |k,v| collected[v] = k }
475 assert collected == { 42 => "jkl;", 1 => "asdf" } 480 assert collected == { 42 => "jkl;", 1 => "asdf" }
476 481
477 assert m.delete("asdf") == 1 482 assert m.delete("asdf") == 1
478 assert !m.has_key?("asdf") 483 assert !m.has_key?("asdf")
479 assert m["asdf"] == nil 484 assert m["asdf"] == nil
480 assert !m.has_key?("asdf") 485 assert !m.has_key?("asdf")
481 486
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 end 556 end
552 assert_raise TypeError do 557 assert_raise TypeError do
553 m["asdf"] = 1 558 m["asdf"] = 1
554 end 559 end
555 560
556 m = Google::Protobuf::Map.new(:string, :int32) 561 m = Google::Protobuf::Map.new(:string, :int32)
557 m["asdf"] = 1 562 m["asdf"] = 1
558 assert_raise TypeError do 563 assert_raise TypeError do
559 m[1] = 1 564 m[1] = 1
560 end 565 end
561 assert_raise TypeError do 566 assert_raise Encoding::UndefinedConversionError do
562 bytestring = ["FFFF"].pack("H*") 567 bytestring = ["FFFF"].pack("H*")
563 m[bytestring] = 1 568 m[bytestring] = 1
564 end 569 end
565 570
566 m = Google::Protobuf::Map.new(:bytes, :int32) 571 m = Google::Protobuf::Map.new(:bytes, :int32)
567 bytestring = ["FFFF"].pack("H*") 572 bytestring = ["FFFF"].pack("H*")
568 m[bytestring] = 1 573 m[bytestring] = 1
569 assert_raise TypeError do 574 # Allowed -- we will automatically convert to ASCII-8BIT.
570 m["asdf"] = 1 575 m["asdf"] = 1
571 end
572 assert_raise TypeError do 576 assert_raise TypeError do
573 m[1] = 1 577 m[1] = 1
574 end 578 end
575 end 579 end
576 580
577 def test_map_msg_enum_valuetypes 581 def test_map_msg_enum_valuetypes
578 m = Google::Protobuf::Map.new(:string, :message, TestMessage) 582 m = Google::Protobuf::Map.new(:string, :message, TestMessage)
579 m["asdf"] = TestMessage.new 583 m["asdf"] = TestMessage.new
580 assert_raise TypeError do 584 assert_raise TypeError do
581 m["jkl;"] = TestMessage2.new 585 m["jkl;"] = TestMessage2.new
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
696 assert oneof == o 700 assert oneof == o
697 } 701 }
698 assert oneof_count == 1 702 assert oneof_count == 1
699 assert o.count == 4 703 assert o.count == 4
700 field_names = o.map{|f| f.name}.sort 704 field_names = o.map{|f| f.name}.sort
701 assert field_names == ["a", "b", "c", "d"] 705 assert field_names == ["a", "b", "c", "d"]
702 end 706 end
703 707
704 def test_oneof 708 def test_oneof
705 d = OneofMessage.new 709 d = OneofMessage.new
706 assert d.a == nil 710 assert d.a == ""
707 assert d.b == nil 711 assert d.b == 0
708 assert d.c == nil 712 assert d.c == nil
709 assert d.d == nil 713 assert d.d == :Default
710 assert d.my_oneof == nil 714 assert d.my_oneof == nil
711 715
712 d.a = "hi" 716 d.a = "hi"
713 assert d.a == "hi" 717 assert d.a == "hi"
714 assert d.b == nil 718 assert d.b == 0
715 assert d.c == nil 719 assert d.c == nil
716 assert d.d == nil 720 assert d.d == :Default
717 assert d.my_oneof == :a 721 assert d.my_oneof == :a
718 722
719 d.b = 42 723 d.b = 42
720 assert d.a == nil 724 assert d.a == ""
721 assert d.b == 42 725 assert d.b == 42
722 assert d.c == nil 726 assert d.c == nil
723 assert d.d == nil 727 assert d.d == :Default
724 assert d.my_oneof == :b 728 assert d.my_oneof == :b
725 729
726 d.c = TestMessage2.new(:foo => 100) 730 d.c = TestMessage2.new(:foo => 100)
727 assert d.a == nil 731 assert d.a == ""
728 assert d.b == nil 732 assert d.b == 0
729 assert d.c.foo == 100 733 assert d.c.foo == 100
730 assert d.d == nil 734 assert d.d == :Default
731 assert d.my_oneof == :c 735 assert d.my_oneof == :c
732 736
733 d.d = :C 737 d.d = :C
734 assert d.a == nil 738 assert d.a == ""
735 assert d.b == nil 739 assert d.b == 0
736 assert d.c == nil 740 assert d.c == nil
737 assert d.d == :C 741 assert d.d == :C
738 assert d.my_oneof == :d 742 assert d.my_oneof == :d
739 743
740 d2 = OneofMessage.decode(OneofMessage.encode(d)) 744 d2 = OneofMessage.decode(OneofMessage.encode(d))
741 assert d2 == d 745 assert d2 == d
742 746
743 encoded_field_a = OneofMessage.encode(OneofMessage.new(:a => "string")) 747 encoded_field_a = OneofMessage.encode(OneofMessage.new(:a => "string"))
744 encoded_field_b = OneofMessage.encode(OneofMessage.new(:b => 1000)) 748 encoded_field_b = OneofMessage.encode(OneofMessage.new(:b => 1000))
745 encoded_field_c = OneofMessage.encode( 749 encoded_field_c = OneofMessage.encode(
746 OneofMessage.new(:c => TestMessage2.new(:foo => 1))) 750 OneofMessage.new(:c => TestMessage2.new(:foo => 1)))
747 encoded_field_d = OneofMessage.encode(OneofMessage.new(:d => :B)) 751 encoded_field_d = OneofMessage.encode(OneofMessage.new(:d => :B))
748 752
749 d3 = OneofMessage.decode( 753 d3 = OneofMessage.decode(
750 encoded_field_c + encoded_field_a + encoded_field_d) 754 encoded_field_c + encoded_field_a + encoded_field_d)
751 assert d3.a == nil 755 assert d3.a == ""
752 assert d3.b == nil 756 assert d3.b == 0
753 assert d3.c == nil 757 assert d3.c == nil
754 assert d3.d == :B 758 assert d3.d == :B
755 759
756 d4 = OneofMessage.decode( 760 d4 = OneofMessage.decode(
757 encoded_field_c + encoded_field_a + encoded_field_d + 761 encoded_field_c + encoded_field_a + encoded_field_d +
758 encoded_field_c) 762 encoded_field_c)
759 assert d4.a == nil 763 assert d4.a == ""
760 assert d4.b == nil 764 assert d4.b == 0
761 assert d4.c.foo == 1 765 assert d4.c.foo == 1
762 assert d4.d == nil 766 assert d4.d == :Default
763 767
764 d5 = OneofMessage.new(:a => "hello") 768 d5 = OneofMessage.new(:a => "hello")
765 assert d5.a != nil 769 assert d5.a == "hello"
766 d5.a = nil 770 d5.a = nil
767 assert d5.a == nil 771 assert d5.a == ""
768 assert OneofMessage.encode(d5) == '' 772 assert OneofMessage.encode(d5) == ''
769 assert d5.my_oneof == nil 773 assert d5.my_oneof == nil
770 end 774 end
771 775
772 def test_enum_field 776 def test_enum_field
773 m = TestMessage.new 777 m = TestMessage.new
774 assert m.optional_enum == :Default 778 assert m.optional_enum == :Default
775 m.optional_enum = :A 779 m.optional_enum = :A
776 assert m.optional_enum == :A 780 assert m.optional_enum == :A
777 assert_raise RangeError do 781 assert_raise RangeError do
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
846 m2 = TestMessage.decode data 850 m2 = TestMessage.decode data
847 assert m == m2 851 assert m == m2
848 852
849 data = Google::Protobuf.encode m 853 data = Google::Protobuf.encode m
850 m2 = Google::Protobuf.decode(TestMessage, data) 854 m2 = Google::Protobuf.decode(TestMessage, data)
851 assert m == m2 855 assert m == m2
852 end 856 end
853 857
854 def test_encode_decode_helpers 858 def test_encode_decode_helpers
855 m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1' , 'bar2']) 859 m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1' , 'bar2'])
860 assert_equal 'foo', m.optional_string
861 assert_equal ['bar1', 'bar2'], m.repeated_string
862
856 json = m.to_json 863 json = m.to_json
857 m2 = TestMessage.decode_json(json) 864 m2 = TestMessage.decode_json(json)
858 assert m2.optional_string == 'foo' 865 assert_equal 'foo', m2.optional_string
859 assert m2.repeated_string == ['bar1', 'bar2'] 866 assert_equal ['bar1', 'bar2'], m2.repeated_string
867 if RUBY_PLATFORM != "java"
868 assert m2.optional_string.frozen?
869 assert m2.repeated_string[0].frozen?
870 end
860 871
861 proto = m.to_proto 872 proto = m.to_proto
862 m2 = TestMessage.decode(proto) 873 m2 = TestMessage.decode(proto)
863 assert m2.optional_string == 'foo' 874 assert_equal 'foo', m2.optional_string
864 assert m2.repeated_string == ['bar1', 'bar2'] 875 assert_equal ['bar1', 'bar2'], m2.repeated_string
865 end 876 end
866 877
867 def test_protobuf_encode_decode_helpers 878 def test_protobuf_encode_decode_helpers
868 m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1' , 'bar2']) 879 m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1' , 'bar2'])
869 encoded_msg = Google::Protobuf.encode(m) 880 encoded_msg = Google::Protobuf.encode(m)
870 assert_equal m.to_proto, encoded_msg 881 assert_equal m.to_proto, encoded_msg
871 882
872 decoded_msg = Google::Protobuf.decode(TestMessage, encoded_msg) 883 decoded_msg = Google::Protobuf.decode(TestMessage, encoded_msg)
873 assert_equal TestMessage.decode(m.to_proto), decoded_msg 884 assert_equal TestMessage.decode(m.to_proto), decoded_msg
874 end 885 end
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
1163 expected = '{"mapStringInt32":{"a":1},"mapStringMsg":{}}' 1174 expected = '{"mapStringInt32":{"a":1},"mapStringMsg":{}}'
1164 expected_preserve = '{"map_string_int32":{"a":1},"map_string_msg":{}}' 1175 expected_preserve = '{"map_string_int32":{"a":1},"map_string_msg":{}}'
1165 assert MapMessage.encode_json(m) == expected 1176 assert MapMessage.encode_json(m) == expected
1166 1177
1167 json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true) 1178 json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true)
1168 assert json == expected_preserve 1179 assert json == expected_preserve
1169 1180
1170 m2 = MapMessage.decode_json(MapMessage.encode_json(m)) 1181 m2 = MapMessage.decode_json(MapMessage.encode_json(m))
1171 assert m == m2 1182 assert m == m2
1172 end 1183 end
1184
1185 def test_comparison_with_arbitrary_object
1186 assert MapMessage.new != nil
1187 end
1188
1189 def test_respond_to
1190 # This test fails with JRuby 1.7.23, likely because of an old JRuby bug.
1191 return if RUBY_PLATFORM == "java"
1192 msg = MapMessage.new
1193 assert msg.respond_to?(:map_string_int32)
1194 assert !msg.respond_to?(:bacon)
1195 end
1173 end 1196 end
1174 end 1197 end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698