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

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

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

Powered by Google App Engine
This is Rietveld 408576698