From 442e079677780f5551cb0f5ef5a78f35e5ee2f5f Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 27 Dec 2023 21:05:28 -0800 Subject: [PATCH] Removed Ruby compatibility tests. In Ruby protobuf 4.26.0 we are intentionally breaking compatibility with our generated code from Protobuf 3.0.0. So these tests are now obsolete. PiperOrigin-RevId: 594157323 --- ruby/compatibility_tests/v3.0.0/BUILD.bazel | 34 - ruby/compatibility_tests/v3.0.0/README.md | 5 - ruby/compatibility_tests/v3.0.0/Rakefile | 26 - ruby/compatibility_tests/v3.0.0/test.sh | 17 - .../v3.0.0/tests/BUILD.bazel | 64 - .../compatibility_tests/v3.0.0/tests/basic.rb | 1133 ----------------- .../v3.0.0/tests/generated_code.proto | 67 - .../v3.0.0/tests/generated_code_test.rb | 19 - .../v3.0.0/tests/repeated_field_test.rb | 647 ---------- .../v3.0.0/tests/stress.rb | 38 - .../v3.0.0/tests/test_import.proto | 5 - 11 files changed, 2055 deletions(-) delete mode 100644 ruby/compatibility_tests/v3.0.0/BUILD.bazel delete mode 100644 ruby/compatibility_tests/v3.0.0/README.md delete mode 100644 ruby/compatibility_tests/v3.0.0/Rakefile delete mode 100755 ruby/compatibility_tests/v3.0.0/test.sh delete mode 100644 ruby/compatibility_tests/v3.0.0/tests/BUILD.bazel delete mode 100755 ruby/compatibility_tests/v3.0.0/tests/basic.rb delete mode 100644 ruby/compatibility_tests/v3.0.0/tests/generated_code.proto delete mode 100755 ruby/compatibility_tests/v3.0.0/tests/generated_code_test.rb delete mode 100755 ruby/compatibility_tests/v3.0.0/tests/repeated_field_test.rb delete mode 100755 ruby/compatibility_tests/v3.0.0/tests/stress.rb delete mode 100644 ruby/compatibility_tests/v3.0.0/tests/test_import.proto diff --git a/ruby/compatibility_tests/v3.0.0/BUILD.bazel b/ruby/compatibility_tests/v3.0.0/BUILD.bazel deleted file mode 100644 index 60c4985f39..0000000000 --- a/ruby/compatibility_tests/v3.0.0/BUILD.bazel +++ /dev/null @@ -1,34 +0,0 @@ -load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") - -################################################################################ -# Distribution files -############################################################################ - -genrule( - name = "protoc-compat-gen", - outs = ["protoc"], - cmd = """ - PROTOC_BINARY_NAME="protoc-3.0.0-linux-x86_64.exe" - if [ `uname` = "Darwin" ]; then - PROTOC_BINARY_NAME="protoc-3.0.0-osx-x86_64.exe" - fi - wget https://repo1.maven.org/maven2/com/google/protobuf/protoc/3.0.0/$${PROTOC_BINARY_NAME} -O protoc - chmod +x protoc - mv protoc $@ - """, - executable = True, - visibility = ["//ruby/compatibility_tests/v3.0.0:__subpackages__"], -) - -pkg_files( - name = "dist_files", - srcs = [ - "BUILD.bazel", - "README.md", - "Rakefile", - "test.sh", - "//ruby/compatibility_tests/v3.0.0/tests:dist_files", - ], - strip_prefix = strip_prefix.from_root(""), - visibility = ["//pkg:__pkg__"], -) diff --git a/ruby/compatibility_tests/v3.0.0/README.md b/ruby/compatibility_tests/v3.0.0/README.md deleted file mode 100644 index 06d981c90d..0000000000 --- a/ruby/compatibility_tests/v3.0.0/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Protobuf Ruby Compatibility Tests - -This directory contains a snapshot of protobuf ruby 3.0.0 unittest code and -test scripts used to verifies whether the latest version of protobuf is -still compatible with 3.0.0 generated code. diff --git a/ruby/compatibility_tests/v3.0.0/Rakefile b/ruby/compatibility_tests/v3.0.0/Rakefile deleted file mode 100644 index fee72b415c..0000000000 --- a/ruby/compatibility_tests/v3.0.0/Rakefile +++ /dev/null @@ -1,26 +0,0 @@ -require "rake/testtask" - -# Proto for tests. -genproto_output = [] - -genproto_output << "tests/generated_code.rb" -genproto_output << "tests/test_import.rb" -file "tests/generated_code.rb" => "tests/generated_code.proto" do |file_task| - sh "./protoc --ruby_out=. tests/generated_code.proto" -end - -file "tests/test_import.rb" => "tests/test_import.proto" do |file_task| - sh "./protoc --ruby_out=. tests/test_import.proto" -end - -task :genproto => genproto_output - -task :clean do - sh "rm -f #{genproto_output.join(' ')}" -end - -Rake::TestTask.new(:test => :genproto) do |t| - t.test_files = FileList["tests/*.rb"] -end - -task :default => [:test] diff --git a/ruby/compatibility_tests/v3.0.0/test.sh b/ruby/compatibility_tests/v3.0.0/test.sh deleted file mode 100755 index 1a9b798422..0000000000 --- a/ruby/compatibility_tests/v3.0.0/test.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -set -ex - -# Change to the script's directory -cd $(dirname $0) - -# Download 3.0.0 version of protoc -PROTOC_BINARY_NAME="protoc-3.0.0-linux-x86_64.exe" -if [ `uname` = "Darwin" ]; then - PROTOC_BINARY_NAME="protoc-3.0.0-osx-x86_64.exe" -fi -wget https://repo1.maven.org/maven2/com/google/protobuf/protoc/3.0.0/${PROTOC_BINARY_NAME} -O protoc -chmod +x protoc - -# Run tests -RUBYLIB=../../lib:. rake test diff --git a/ruby/compatibility_tests/v3.0.0/tests/BUILD.bazel b/ruby/compatibility_tests/v3.0.0/tests/BUILD.bazel deleted file mode 100644 index 697f876991..0000000000 --- a/ruby/compatibility_tests/v3.0.0/tests/BUILD.bazel +++ /dev/null @@ -1,64 +0,0 @@ -load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") -load("@rules_ruby//ruby:defs.bzl", "ruby_test") -load("//ruby:defs.bzl", "internal_ruby_proto_library") - -internal_ruby_proto_library( - name = "test_ruby_protos", - srcs = glob(["*.proto"]), - includes = ["."], - protoc = "//ruby/compatibility_tests/v3.0.0:protoc", -) - -ruby_test( - name = "basic", - srcs = ["basic.rb"], - deps = [ - ":test_ruby_protos", - "//ruby:protobuf", - "@protobuf_bundle//:test-unit", - ], -) - -ruby_test( - name = "generated_code_test", - srcs = ["generated_code_test.rb"], - deps = [ - ":test_ruby_protos", - "//ruby:protobuf", - "@protobuf_bundle//:test-unit", - ], -) - -ruby_test( - name = "repeated_field_test", - srcs = ["repeated_field_test.rb"], - deps = [ - ":test_ruby_protos", - "//ruby:protobuf", - "@protobuf_bundle//:test-unit", - ], -) - -ruby_test( - name = "stress", - srcs = ["stress.rb"], - deps = [ - ":test_ruby_protos", - "//ruby:protobuf", - "@protobuf_bundle//:test-unit", - ], -) - -################################################################################ -# Distribution files -############################################################################ - -pkg_files( - name = "dist_files", - srcs = glob([ - "**/*.rb", - "**/*.proto", - ]), - strip_prefix = strip_prefix.from_root(""), - visibility = ["//ruby/compatibility_tests/v3.0.0:__pkg__"], -) diff --git a/ruby/compatibility_tests/v3.0.0/tests/basic.rb b/ruby/compatibility_tests/v3.0.0/tests/basic.rb deleted file mode 100755 index b59193e000..0000000000 --- a/ruby/compatibility_tests/v3.0.0/tests/basic.rb +++ /dev/null @@ -1,1133 +0,0 @@ -#!/usr/bin/ruby - -require 'google/protobuf' -require 'test/unit' - -# ------------- generated code -------------- - -module BasicTest - pool = Google::Protobuf::DescriptorPool.new - pool.build do - add_message "Foo" do - optional :bar, :message, 1, "Bar" - repeated :baz, :message, 2, "Baz" - end - - add_message "Bar" do - optional :msg, :string, 1 - end - - add_message "Baz" do - optional :msg, :string, 1 - end - - add_message "TestMessage" do - optional :optional_int32, :int32, 1 - optional :optional_int64, :int64, 2 - optional :optional_uint32, :uint32, 3 - optional :optional_uint64, :uint64, 4 - optional :optional_bool, :bool, 5 - optional :optional_float, :float, 6 - optional :optional_double, :double, 7 - optional :optional_string, :string, 8 - optional :optional_bytes, :bytes, 9 - optional :optional_msg, :message, 10, "TestMessage2" - optional :optional_enum, :enum, 11, "TestEnum" - - repeated :repeated_int32, :int32, 12 - repeated :repeated_int64, :int64, 13 - repeated :repeated_uint32, :uint32, 14 - repeated :repeated_uint64, :uint64, 15 - repeated :repeated_bool, :bool, 16 - repeated :repeated_float, :float, 17 - repeated :repeated_double, :double, 18 - repeated :repeated_string, :string, 19 - repeated :repeated_bytes, :bytes, 20 - repeated :repeated_msg, :message, 21, "TestMessage2" - repeated :repeated_enum, :enum, 22, "TestEnum" - end - add_message "TestMessage2" do - optional :foo, :int32, 1 - end - - add_message "Recursive1" do - optional :foo, :message, 1, "Recursive2" - end - add_message "Recursive2" do - optional :foo, :message, 1, "Recursive1" - end - - add_enum "TestEnum" do - value :Default, 0 - value :A, 1 - value :B, 2 - value :C, 3 - end - - add_message "BadFieldNames" do - optional :dup, :int32, 1 - optional :class, :int32, 2 - end - - add_message "MapMessage" do - map :map_string_int32, :string, :int32, 1 - map :map_string_msg, :string, :message, 2, "TestMessage2" - end - add_message "MapMessageWireEquiv" do - repeated :map_string_int32, :message, 1, "MapMessageWireEquiv_entry1" - repeated :map_string_msg, :message, 2, "MapMessageWireEquiv_entry2" - end - add_message "MapMessageWireEquiv_entry1" do - optional :key, :string, 1 - optional :value, :int32, 2 - end - add_message "MapMessageWireEquiv_entry2" do - optional :key, :string, 1 - optional :value, :message, 2, "TestMessage2" - end - - add_message "OneofMessage" do - oneof :my_oneof do - optional :a, :string, 1 - optional :b, :int32, 2 - optional :c, :message, 3, "TestMessage2" - optional :d, :enum, 4, "TestEnum" - end - end - end - - Foo = pool.lookup("Foo").msgclass - Bar = pool.lookup("Bar").msgclass - Baz = pool.lookup("Baz").msgclass - TestMessage = pool.lookup("TestMessage").msgclass - TestMessage2 = pool.lookup("TestMessage2").msgclass - Recursive1 = pool.lookup("Recursive1").msgclass - Recursive2 = pool.lookup("Recursive2").msgclass - TestEnum = pool.lookup("TestEnum").enummodule - BadFieldNames = pool.lookup("BadFieldNames").msgclass - MapMessage = pool.lookup("MapMessage").msgclass - MapMessageWireEquiv = pool.lookup("MapMessageWireEquiv").msgclass - MapMessageWireEquiv_entry1 = - pool.lookup("MapMessageWireEquiv_entry1").msgclass - MapMessageWireEquiv_entry2 = - pool.lookup("MapMessageWireEquiv_entry2").msgclass - OneofMessage = pool.lookup("OneofMessage").msgclass - -# ------------ test cases --------------- - - class MessageContainerTest < Test::Unit::TestCase - - def test_defaults - m = TestMessage.new - assert_equal 0, m.optional_int32 - assert_equal 0, m.optional_int64 - assert_equal 0, m.optional_uint32 - assert_equal 0, m.optional_uint64 - refute m.optional_bool - assert_equal 0.0, m.optional_float - assert_equal 0.0, m.optional_double - assert_empty m.optional_string - assert_empty m.optional_bytes - assert_nil m.optional_msg - assert_equal :Default, m.optional_enum - end - - def test_setters - m = TestMessage.new - m.optional_int32 = -42 - assert_equal -42, m.optional_int32 - m.optional_int64 = -0x1_0000_0000 - assert_equal -0x1_0000_0000, m.optional_int64 - m.optional_uint32 = 0x9000_0000 - assert_equal 0x9000_0000, m.optional_uint32 - m.optional_uint64 = 0x9000_0000_0000_0000 - assert_equal 0x9000_0000_0000_0000, m.optional_uint64 - m.optional_bool = true - assert m.optional_bool - m.optional_float = 0.5 - assert_equal 0.5, m.optional_float - m.optional_double = 0.5 - assert_equal 0.5, m.optional_double - m.optional_string = "hello" - assert_equal "hello", m.optional_string - m.optional_bytes = "world".encode!('ASCII-8BIT') - assert_equal "world", m.optional_bytes - m.optional_msg = TestMessage2.new(:foo => 42) - assert_equal m.optional_msg, TestMessage2.new(:foo => 42) - m.optional_msg = nil - assert_nil m.optional_msg - end - - def test_ctor_args - m = TestMessage.new(:optional_int32 => -42, - :optional_msg => TestMessage2.new, - :optional_enum => :C, - :repeated_string => ["hello", "there", "world"]) - assert_equal -42, m.optional_int32 - assert_instance_of TestMessage2, m.optional_msg - assert_equal 3, m.repeated_string.length - assert_equal :C, m.optional_enum - assert_equal "hello", m.repeated_string[0] - assert_equal "there", m.repeated_string[1] - assert_equal "world", m.repeated_string[2] - end - - def test_inspect - m = TestMessage.new(:optional_int32 => -42, - :optional_enum => :A, - :optional_msg => TestMessage2.new, - :repeated_string => ["hello", "there", "world"]) - expected = ', 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: []>' - assert_equal expected, m.inspect - end - - def test_hash - m1 = TestMessage.new(:optional_int32 => 42) - m2 = TestMessage.new(:optional_int32 => 102) - refute_equal 0, m1.hash - refute_equal 0, m2.hash - # relying on the randomness here -- if hash function changes and we are - # unlucky enough to get a collision, then change the values above. - refute_equal m1.hash, m2.hash - end - - def test_unknown_field_errors - e = assert_raises NoMethodError do - TestMessage.new.hello - end - assert_match(/hello/, e.message) - - e = assert_raises NoMethodError do - TestMessage.new.hello = "world" - end - assert_match(/hello/, e.message) - end - - def test_initialization_map_errors - e = assert_raises ArgumentError do - TestMessage.new(:hello => "world") - end - assert_match(/hello/, e.message) - - e = assert_raises ArgumentError do - MapMessage.new(:map_string_int32 => "hello") - end - assert_equal "Expected Hash object as initializer value for map field 'map_string_int32' (given String).", e.message - e = assert_raises ArgumentError do - TestMessage.new(:repeated_uint32 => "hello") - end - assert_equal "Expected array as initializer value for repeated field 'repeated_uint32' (given String).", e.message - end - - def test_type_errors - m = TestMessage.new - - assert_raises Google::Protobuf::TypeError do - m.optional_int32 = "hello" - end - - assert_raises Google::Protobuf::TypeError do - m.optional_string = nil - end - - assert_raises Google::Protobuf::TypeError do - m.optional_bool = 42 - end - - assert_raises Google::Protobuf::TypeError do - m.optional_msg = TestMessage.new # expects TestMessage2 - end - - assert_raises Google::Protobuf::TypeError do - m.repeated_int32 = [] # needs RepeatedField - end - - assert_raises Google::Protobuf::TypeError do - m.repeated_msg.push TestMessage.new - end - end - - def test_string_encoding - m = TestMessage.new - - # Assigning a normal (ASCII or UTF8) string to a bytes field, or - # ASCII-8BIT to a string field will convert to the proper encoding. - m.optional_bytes = "Test string ASCII".encode!('ASCII') - assert m.optional_bytes.frozen? - assert_equal Encoding::ASCII_8BIT, m.optional_bytes.encoding - assert_equal "Test string ASCII", m.optional_bytes - - assert_raises Encoding::UndefinedConversionError do - m.optional_bytes = "Test string UTF-8 \u0100".encode!('UTF-8') - end - - assert_raises Encoding::UndefinedConversionError do - m.optional_string = ["FFFF"].pack('H*') - end - - # "Ordinary" use case. - m.optional_bytes = ["FFFF"].pack('H*') - m.optional_string = "\u0100" - - # strings are immutable so we can't do this, but serialize should catch it. - m.optional_string = "asdf".encode!('UTF-8') - assert_raises do - m.optional_string.encode!('ASCII-8BIT') - end - end - - def test_rptfield_int32 - l = Google::Protobuf::RepeatedField.new(:int32) - assert_equal 0, l.count - l = Google::Protobuf::RepeatedField.new(:int32, [1, 2, 3]) - assert_equal 3, l.count - assert_equal [1, 2, 3], l - assert_equal [1, 2, 3], l - l.push 4 - assert_equal [1, 2, 3, 4], l - dst_list = [] - l.each { |val| dst_list.push val } - assert_equal [1, 2, 3, 4], dst_list - assert_equal [1, 2, 3, 4], l.to_a - assert_equal 1, l[0] - assert_equal 4, l[3] - l[0] = 5 - assert_equal [5, 2, 3, 4], l - l2 = l.dup - assert_equal l, l2 - refute_same l, l2 - l2.push 6 - assert_equal 4, l.count - assert_equal 5, l2.count - assert_equal '[5, 2, 3, 4]', l.inspect - l.concat([7, 8, 9]) - assert_equal [5, 2, 3, 4, 7, 8, 9], l - assert_equal 9, l.pop - assert_equal [5, 2, 3, 4, 7, 8], l - m = TestMessage.new - assert_raises Google::Protobuf::TypeError do - l.push m - end - - m.repeated_int32 = l - assert_equal [5, 2, 3, 4, 7, 8], m.repeated_int32 - assert_same m.repeated_int32, l - l.push 42 - assert_equal 42, m.repeated_int32.pop - l3 = l + l.dup - assert_equal l.count * 2, l3.count - l.count.times do |i| - assert_equal l[i], l3[i] - assert_equal l[i], l3[l.count + i] - end - - l.clear - assert_equal 0, l.count - l += [1, 2, 3, 4] - l.replace([5, 6, 7, 8]) - assert_equal [5, 6, 7, 8], l - l4 = Google::Protobuf::RepeatedField.new(:int32) - l4[5] = 42 - assert_equal [0, 0, 0, 0, 0, 42], l4 - l4 << 100 - assert_equal [0, 0, 0, 0, 0, 42, 100], l4 - l4 << 101 << 102 - assert_equal [0, 0, 0, 0, 0, 42, 100, 101, 102], l4 - end - - def test_parent_rptfield - #make sure we set the RepeatedField and can add to it - m = TestMessage.new - assert_empty m.repeated_string - m.repeated_string << 'ok' - m.repeated_string.push('ok2') - assert_equal ['ok', 'ok2'], m.repeated_string - m.repeated_string += ['ok3'] - assert_equal ['ok', 'ok2', 'ok3'], m.repeated_string - end - - def test_rptfield_msg - l = Google::Protobuf::RepeatedField.new(:message, TestMessage) - l.push TestMessage.new - assert_equal 1, l.count - assert_raises Google::Protobuf::TypeError do - l.push TestMessage2.new - end - assert_raises Google::Protobuf::TypeError do - l.push 42 - end - - l2 = l.dup - assert_equal l[0], l2[0] - assert_same l2[0], l[0] - l2 = Google::Protobuf.deep_copy(l) - assert_equal l[0], l2[0] - refute_same l2[0], l[0] - l3 = l + l2 - assert_equal 2, l3.count - assert_equal l[0], l3[0] - assert_equal l2[0], l3[1] - l3[0].optional_int32 = 1000 - assert_equal 1000, l[0].optional_int32 - new_msg = TestMessage.new(:optional_int32 => 200) - l4 = l + [new_msg] - assert_equal 2, l4.count - new_msg.optional_int32 = 1000 - assert_equal 1000, l4[1].optional_int32 - end - - def test_rptfield_enum - l = Google::Protobuf::RepeatedField.new(:enum, TestEnum) - l.push :A - l.push :B - l.push :C - assert_equal 3, l.count - assert_raises RangeError do - l.push :D - end - assert_equal :A, l[0] - l.push 4 - assert_equal 4, l[3] - end - - def test_rptfield_initialize - assert_raises ArgumentError do - l = Google::Protobuf::RepeatedField.new - end - assert_raises ArgumentError do - l = Google::Protobuf::RepeatedField.new(:message) - end - assert_raises ArgumentError do - l = Google::Protobuf::RepeatedField.new([1, 2, 3]) - end - assert_raises ArgumentError do - l = Google::Protobuf::RepeatedField.new(:message, [TestMessage2.new]) - end - end - - def test_rptfield_array_ducktyping - l = Google::Protobuf::RepeatedField.new(:int32) - length_methods = %w(count length size) - length_methods.each do |lm| - assert_equal 0, l.send(lm) - end - # out of bounds returns a nil - assert_nil l[0] - assert_nil l[1] - assert_nil l[-1] - l.push 4 - length_methods.each do |lm| - assert_equal 1, l.send(lm) - end - assert_equal 4, l[0] - assert_nil l[1] - assert_equal 4, l[-1] - assert_nil l[-2] - l.push 2 - length_methods.each do |lm| - assert_equal 2, l.send(lm) - end - assert_equal 4, l[0] - assert_equal 2, l[1] - assert_nil l[2] - assert_equal 2, l[-1] - assert_equal 4, l[-2] - assert_nil l[-3] - #adding out of scope will backfill with empty objects - end - - def test_map_basic - # allowed key types: - # :int32, :int64, :uint32, :uint64, :bool, :string, :bytes. - - m = Google::Protobuf::Map.new(:string, :int32) - m["asdf"] = 1 - assert_equal 1, m["asdf"] - m["jkl;"] = 42 - assert_equal({ "jkl;" => 42, "asdf" => 1 }, m.to_h) - assert_includes m.to_h, "asdf" - refute_includes m, "qwerty" - assert_equal 2, m.length - m2 = m.dup - assert_equal m, m2 - refute_equal 0, m.hash - assert_equal m.hash, m2.hash - collected = {} - m.each { |k,v| collected[v] = k } - assert_equal({ 42 => "jkl;", 1 => "asdf" }, collected) - assert_equal 1, m.delete("asdf") - refute_includes m, "asdf" - assert_nil m["asdf"] - refute_includes m, "asdf" - - # We only assert on inspect value when there is one map entry because the - # order in which elements appear is unspecified (depends on the internal - # hash function). We don't want a brittle test. - assert_equal "{\"jkl;\"=>42}", m.inspect - assert_equal ["jkl;"], m.keys - assert_equal [42], m.values - m.clear - assert_equal 0, m.length - assert_empty m.to_h - assert_raises Google::Protobuf::TypeError do - m[1] = 1 - end - - assert_raises RangeError do - m["asdf"] = 0x1_0000_0000 - end - end - - def test_map_ctor - m = Google::Protobuf::Map.new(:string, :int32, - {"a" => 1, "b" => 2, "c" => 3}) - assert_equal({"a" => 1, "c" => 3, "b" => 2}, m.to_h) - end - - def test_map_keytypes - m = Google::Protobuf::Map.new(:int32, :int32) - m[1] = 42 - m[-1] = 42 - assert_raises RangeError do - m[0x8000_0000] = 1 - end - - assert_raises Google::Protobuf::TypeError do - m["asdf"] = 1 - end - - m = Google::Protobuf::Map.new(:int64, :int32) - m[0x1000_0000_0000_0000] = 1 - assert_raises RangeError do - m[0x1_0000_0000_0000_0000] = 1 - end - - assert_raises Google::Protobuf::TypeError do - m["asdf"] = 1 - end - - m = Google::Protobuf::Map.new(:uint32, :int32) - m[0x8000_0000] = 1 - assert_raises RangeError do - m[0x1_0000_0000] = 1 - end - assert_raises RangeError do - m[-1] = 1 - end - - m = Google::Protobuf::Map.new(:uint64, :int32) - m[0x8000_0000_0000_0000] = 1 - assert_raises RangeError do - m[0x1_0000_0000_0000_0000] = 1 - end - assert_raises RangeError do - m[-1] = 1 - end - - m = Google::Protobuf::Map.new(:bool, :int32) - m[true] = 1 - m[false] = 2 - - assert_raises Google::Protobuf::TypeError do - m[1] = 1 - end - - assert_raises Google::Protobuf::TypeError do - m["asdf"] = 1 - end - - m = Google::Protobuf::Map.new(:string, :int32) - m["asdf"] = 1 - assert_raises Google::Protobuf::TypeError do - m[1] = 1 - end - bytestring = ["FFFF"].pack("H*") - assert_raises Encoding::UndefinedConversionError do - m[bytestring] = 1 - end - - m = Google::Protobuf::Map.new(:bytes, :int32) - bytestring = ["FFFF"].pack("H*") - m[bytestring] = 1 - # Allowed -- we will automatically convert to ASCII-8BIT. - m["asdf"] = 1 - assert_raises Google::Protobuf::TypeError do - m[1] = 1 - end - end - - def test_map_msg_enum_valuetypes - m = Google::Protobuf::Map.new(:string, :message, TestMessage) - m["asdf"] = TestMessage.new - assert_raises Google::Protobuf::TypeError do - m["jkl;"] = TestMessage2.new - end - - m = Google::Protobuf::Map.new( - :string, :message, TestMessage, - { "a" => TestMessage.new(:optional_int32 => 42), - "b" => TestMessage.new(:optional_int32 => 84) }) - assert_equal 2, m.length - assert_equal [42, 84], m.values.map{|msg| msg.optional_int32}.sort - m = Google::Protobuf::Map.new(:string, :enum, TestEnum, - { "x" => :A, "y" => :B, "z" => :C }) - assert_equal 3, m.length - assert_equal :C, m["z"] - m["z"] = 2 - assert_equal :B, m["z"] - m["z"] = 5 - assert_equal 5, m["z"] - assert_raises RangeError do - m["z"] = :Z - end - assert_raises RangeError do - m["z"] = "z" - end - end - - def test_map_dup_deep_copy - m = Google::Protobuf::Map.new( - :string, :message, TestMessage, - { "a" => TestMessage.new(:optional_int32 => 42), - "b" => TestMessage.new(:optional_int32 => 84) }) - - m2 = m.dup - assert_equal m, m2 - refute_same m, m2 - assert_same m["a"], m2["a"] - assert_same m["b"], m2["b"] - m2 = Google::Protobuf.deep_copy(m) - assert_equal m, m2 - refute_same m, m2 - refute_same m["a"], m2["a"] - refute_same m["b"], m2["b"] - end - - def test_map_field - m = MapMessage.new - assert_empty m.map_string_int32.to_h - assert_empty m.map_string_msg.to_h - m = MapMessage.new( - :map_string_int32 => {"a" => 1, "b" => 2}, - :map_string_msg => {"a" => TestMessage2.new(:foo => 1), - "b" => TestMessage2.new(:foo => 2)}) - assert_equal ["a", "b"], m.map_string_int32.keys.sort - assert_equal 1, m.map_string_int32["a"] - assert_equal 2, m.map_string_msg["b"].foo - m.map_string_int32["c"] = 3 - assert_equal 3, m.map_string_int32["c"] - m.map_string_msg["c"] = TestMessage2.new(:foo => 3) - assert_equal TestMessage2.new(:foo => 3), m.map_string_msg["c"] - m.map_string_msg.delete("b") - m.map_string_msg.delete("c") - assert_equal({ "a" => TestMessage2.new(:foo => 1).to_h }, m.map_string_msg.to_h) - assert_raises Google::Protobuf::TypeError do - m.map_string_msg["e"] = TestMessage.new # wrong value type - end - # ensure nothing was added by the above - assert_equal({ "a" => TestMessage2.new(:foo => 1).to_h }, m.map_string_msg.to_h) - m.map_string_int32 = Google::Protobuf::Map.new(:string, :int32) - assert_raises Google::Protobuf::TypeError do - m.map_string_int32 = Google::Protobuf::Map.new(:string, :int64) - end - assert_raises Google::Protobuf::TypeError do - m.map_string_int32 = {} - end - assert_raises Google::Protobuf::TypeError do - m = MapMessage.new(:map_string_int32 => { 1 => "I am not a number" }) - end - end - - def test_map_encode_decode - m = MapMessage.new( - :map_string_int32 => {"a" => 1, "b" => 2}, - :map_string_msg => {"a" => TestMessage2.new(:foo => 1), - "b" => TestMessage2.new(:foo => 2)}) - m2 = MapMessage.decode(MapMessage.encode(m)) - assert_equal m, m2 - m3 = MapMessageWireEquiv.decode(MapMessage.encode(m)) - assert_equal 2, m3.map_string_int32.length - kv = {} - m3.map_string_int32.map { |msg| kv[msg.key] = msg.value } - assert_equal({"a" => 1, "b" => 2}, kv) - kv = {} - m3.map_string_msg.map { |msg| kv[msg.key] = msg.value } - assert_equal({"a" => TestMessage2.new(:foo => 1), - "b" => TestMessage2.new(:foo => 2)}, kv) - end - - def test_oneof_descriptors - d = OneofMessage.descriptor - o = d.lookup_oneof("my_oneof") - refute_nil o - assert_instance_of Google::Protobuf::OneofDescriptor, o - assert_equal "my_oneof", o.name - oneof_count = 0 - d.each_oneof{ |oneof| - oneof_count += 1 - assert_equal o, oneof - } - assert_equal 1, oneof_count - assert_equal 4, o.count - field_names = o.map{|f| f.name}.sort - assert_equal ["a", "b", "c", "d"], field_names - end - - def test_oneof - d = OneofMessage.new - assert_empty d.a - assert_equal 0, d.b - assert_nil d.c - assert_equal :Default, d.d - assert_nil d.my_oneof - d.a = "hi" - assert_equal "hi", d.a - assert_equal 0, d.b - assert_nil d.c - assert_equal :Default, d.d - assert_equal :a, d.my_oneof - d.b = 42 - assert_empty d.a - assert_equal 42, d.b - assert_nil d.c - assert_equal :Default, d.d - assert_equal :b, d.my_oneof - d.c = TestMessage2.new(:foo => 100) - assert_empty d.a - assert_equal 0, d.b - assert_equal 100, d.c.foo - assert_equal :Default, d.d - assert_equal :c, d.my_oneof - d.d = :C - assert_empty d.a - assert_equal 0, d.b - assert_nil d.c - assert_equal :C, d.d - assert_equal :d, d.my_oneof - d2 = OneofMessage.decode(OneofMessage.encode(d)) - assert_equal d2, d - encoded_field_a = OneofMessage.encode(OneofMessage.new(:a => "string")) - encoded_field_b = OneofMessage.encode(OneofMessage.new(:b => 1000)) - encoded_field_c = OneofMessage.encode( - OneofMessage.new(:c => TestMessage2.new(:foo => 1))) - encoded_field_d = OneofMessage.encode(OneofMessage.new(:d => :B)) - - d3 = OneofMessage.decode( - encoded_field_c + encoded_field_a + encoded_field_d) - assert_empty d3.a - assert_equal 0, d3.b - assert_nil d3.c - assert_equal :B, d3.d - d4 = OneofMessage.decode( - encoded_field_c + encoded_field_a + encoded_field_d + - encoded_field_c) - assert_empty d4.a - assert_equal 0, d4.b - assert_equal 1, d4.c.foo - assert_equal :Default, d4.d - d5 = OneofMessage.new(:a => "hello") - assert_equal "hello", d5.a - d5.a = nil - assert_empty d5.a - assert_empty OneofMessage.encode(d5) - assert_nil d5.my_oneof - end - - def test_enum_field - m = TestMessage.new - assert_equal :Default, m.optional_enum - m.optional_enum = :A - assert_equal :A, m.optional_enum - assert_raises RangeError do - m.optional_enum = :ASDF - end - m.optional_enum = 1 - assert_equal :A, m.optional_enum - m.optional_enum = 100 - assert_equal 100, m.optional_enum - end - - def test_dup - m = TestMessage.new - m.optional_string = "hello" - m.optional_int32 = 42 - tm1 = TestMessage2.new(:foo => 100) - tm2 = TestMessage2.new(:foo => 200) - m.repeated_msg.push tm1 - assert_equal m.repeated_msg[-1], tm1 - m.repeated_msg.push tm2 - assert_equal m.repeated_msg[-1], tm2 - m2 = m.dup - assert_equal m, m2 - m.optional_int32 += 1 - refute_equal m2, m - assert_equal m.repeated_msg[0], m2.repeated_msg[0] - assert_same m.repeated_msg[0], m2.repeated_msg[0] - end - - def test_deep_copy - m = TestMessage.new(:optional_int32 => 42, - :repeated_msg => [TestMessage2.new(:foo => 100)]) - m2 = Google::Protobuf.deep_copy(m) - assert_equal m, m2 - assert_equal m.repeated_msg, m2.repeated_msg - refute_same m.repeated_msg, m2.repeated_msg - refute_same m.repeated_msg[0], m2.repeated_msg[0] - end - - def test_eq - m = TestMessage.new(:optional_int32 => 42, - :repeated_int32 => [1, 2, 3]) - m2 = TestMessage.new(:optional_int32 => 43, - :repeated_int32 => [1, 2, 3]) - refute_equal m2, m - end - - def test_enum_lookup - assert_equal 1, TestEnum::A - assert_equal 2, TestEnum::B - assert_equal 3, TestEnum::C - assert_equal :A, TestEnum::lookup(1) - assert_equal :B, TestEnum::lookup(2) - assert_equal :C, TestEnum::lookup(3) - assert_equal 1, TestEnum::resolve(:A) - assert_equal 2, TestEnum::resolve(:B) - assert_equal 3, TestEnum::resolve(:C) - end - - def test_parse_serialize - m = TestMessage.new(:optional_int32 => 42, - :optional_string => "hello world", - :optional_enum => :B, - :repeated_string => ["a", "b", "c"], - :repeated_int32 => [42, 43, 44], - :repeated_enum => [:A, :B, :C, 100], - :repeated_msg => [TestMessage2.new(:foo => 1), - TestMessage2.new(:foo => 2)]) - data = TestMessage.encode m - m2 = TestMessage.decode data - assert_equal m, m2 - data = Google::Protobuf.encode m - m2 = Google::Protobuf.decode(TestMessage, data) - assert_equal m, m2 - end - - def test_encode_decode_helpers - m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) - assert_equal 'foo', m.optional_string - assert_equal ['bar1', 'bar2'], m.repeated_string - - json = m.to_json - m2 = TestMessage.decode_json(json) - assert_equal 'foo', m2.optional_string - assert_equal ['bar1', 'bar2'], m2.repeated_string - if RUBY_PLATFORM != "java" - assert m2.optional_string.frozen? - assert m2.repeated_string[0].frozen? - end - - proto = m.to_proto - m2 = TestMessage.decode(proto) - assert_equal 'foo', m2.optional_string - assert_equal ['bar1', 'bar2'], m2.repeated_string - end - - def test_protobuf_encode_decode_helpers - m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) - encoded_msg = Google::Protobuf.encode(m) - assert_equal m.to_proto, encoded_msg - - decoded_msg = Google::Protobuf.decode(TestMessage, encoded_msg) - assert_equal TestMessage.decode(m.to_proto), decoded_msg - end - - def test_protobuf_encode_decode_json_helpers - m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) - encoded_msg = Google::Protobuf.encode_json(m) - assert_equal m.to_json, encoded_msg - - decoded_msg = Google::Protobuf.decode_json(TestMessage, encoded_msg) - assert_equal TestMessage.decode_json(m.to_json), decoded_msg - end - - def test_to_h - m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) - expected_result = { - :optional_bool=>true, - :optional_bytes=>"", - :optional_double=>-10.100001, - :optional_enum=>:Default, - :optional_float=>0.0, - :optional_int32=>0, - :optional_int64=>0, - :optional_msg=>nil, - :optional_string=>"foo", - :optional_uint32=>0, - :optional_uint64=>0, - :repeated_bool=>[], - :repeated_bytes=>[], - :repeated_double=>[], - :repeated_enum=>[], - :repeated_float=>[], - :repeated_int32=>[], - :repeated_int64=>[], - :repeated_msg=>[], - :repeated_string=>["bar1", "bar2"], - :repeated_uint32=>[], - :repeated_uint64=>[] - } - assert_equal expected_result, m.to_h - end - - - def test_def_errors - s = Google::Protobuf::DescriptorPool.new - assert_raises Google::Protobuf::TypeError do - s.build do - # enum with no default (integer value 0) - add_enum "MyEnum" do - value :A, 1 - end - end - end - assert_raises Google::Protobuf::TypeError do - s.build do - # message with required field (unsupported in proto3) - add_message "MyMessage" do - required :foo, :int32, 1 - end - end - end - end - - def test_corecursive - # just be sure that we can instantiate types with corecursive field-type - # references. - m = Recursive1.new(:foo => Recursive2.new(:foo => Recursive1.new)) - assert_equal Recursive2.descriptor, Recursive1.descriptor.lookup("foo").subtype - assert_equal Recursive1.descriptor, Recursive2.descriptor.lookup("foo").subtype - serialized = Recursive1.encode(m) - m2 = Recursive1.decode(serialized) - assert_equal m, m2 - end - - def test_serialize_cycle - m = Recursive1.new(:foo => Recursive2.new) - m.foo.foo = m - assert_raises RuntimeError do - Recursive1.encode(m) - end - end - - def test_bad_field_names - m = BadFieldNames.new(:dup => 1, :class => 2) - m2 = m.dup - assert_equal m, m2 - assert_equal 1, m['dup'] - assert_equal 2, m['class'] - m['dup'] = 3 - assert_equal 3, m['dup'] - end - - def test_int_ranges - m = TestMessage.new - - m.optional_int32 = 0 - m.optional_int32 = -0x8000_0000 - m.optional_int32 = +0x7fff_ffff - m.optional_int32 = 1.0 - m.optional_int32 = -1.0 - m.optional_int32 = 2e9 - assert_raises RangeError do - m.optional_int32 = -0x8000_0001 - end - assert_raises RangeError do - m.optional_int32 = +0x8000_0000 - end - assert_raises RangeError do - m.optional_int32 = +0x1000_0000_0000_0000_0000_0000 # force Bignum - end - assert_raises RangeError do - m.optional_int32 = 1e12 - end - assert_raises RangeError do - m.optional_int32 = 1.5 - end - - m.optional_uint32 = 0 - m.optional_uint32 = +0xffff_ffff - m.optional_uint32 = 1.0 - m.optional_uint32 = 4e9 - assert_raises RangeError do - m.optional_uint32 = -1 - end - assert_raises RangeError do - m.optional_uint32 = -1.5 - end - assert_raises RangeError do - m.optional_uint32 = -1.5e12 - end - assert_raises RangeError do - m.optional_uint32 = -0x1000_0000_0000_0000 - end - assert_raises RangeError do - m.optional_uint32 = +0x1_0000_0000 - end - assert_raises RangeError do - m.optional_uint32 = +0x1000_0000_0000_0000_0000_0000 # force Bignum - end - assert_raises RangeError do - m.optional_uint32 = 1e12 - end - assert_raises RangeError do - m.optional_uint32 = 1.5 - end - - m.optional_int64 = 0 - m.optional_int64 = -0x8000_0000_0000_0000 - m.optional_int64 = +0x7fff_ffff_ffff_ffff - m.optional_int64 = 1.0 - m.optional_int64 = -1.0 - m.optional_int64 = 8e18 - m.optional_int64 = -8e18 - assert_raises RangeError do - m.optional_int64 = -0x8000_0000_0000_0001 - end - assert_raises RangeError do - m.optional_int64 = +0x8000_0000_0000_0000 - end - assert_raises RangeError do - m.optional_int64 = +0x1000_0000_0000_0000_0000_0000 # force Bignum - end - assert_raises RangeError do - m.optional_int64 = 1e50 - end - assert_raises RangeError do - m.optional_int64 = 1.5 - end - - m.optional_uint64 = 0 - m.optional_uint64 = +0xffff_ffff_ffff_ffff - m.optional_uint64 = 1.0 - m.optional_uint64 = 16e18 - assert_raises RangeError do - m.optional_uint64 = -1 - end - assert_raises RangeError do - m.optional_uint64 = -1.5 - end - assert_raises RangeError do - m.optional_uint64 = -1.5e12 - end - assert_raises RangeError do - m.optional_uint64 = -0x1_0000_0000_0000_0000 - end - assert_raises RangeError do - m.optional_uint64 = +0x1_0000_0000_0000_0000 - end - assert_raises RangeError do - m.optional_uint64 = +0x1000_0000_0000_0000_0000_0000 # force Bignum - end - assert_raises RangeError do - m.optional_uint64 = 1e50 - end - assert_raises RangeError do - m.optional_uint64 = 1.5 - end - end - - def test_stress_test - m = TestMessage.new - m.optional_int32 = 42 - m.optional_int64 = 0x100000000 - m.optional_string = "hello world" - 10.times do m.repeated_msg.push TestMessage2.new(:foo => 42) end - 10.times do m.repeated_string.push "hello world" end - - data = TestMessage.encode(m) - - l = 0 - 10_000.times do - m = TestMessage.decode(data) - data_new = TestMessage.encode(m) - assert_equal data, data_new - data = data_new - end - end - - def test_reflection - m = TestMessage.new(:optional_int32 => 1234) - msgdef = m.class.descriptor - assert_instance_of Google::Protobuf::Descriptor, msgdef - assert msgdef.any? {|field| field.name == "optional_int32"} - optional_int32 = msgdef.lookup "optional_int32" - assert_instance_of Google::Protobuf::FieldDescriptor, optional_int32 - refute_nil optional_int32 - assert_equal "optional_int32", optional_int32.name - assert_equal :int32, optional_int32.type - optional_int32.set(m, 5678) - assert_equal 5678, m.optional_int32 - m.optional_int32 = 1000 - assert_equal 1000, optional_int32.get(m) - optional_msg = msgdef.lookup "optional_msg" - assert_equal TestMessage2.descriptor, optional_msg.subtype - optional_msg.set(m, optional_msg.subtype.msgclass.new) - - assert_equal TestMessage, msgdef.msgclass - optional_enum = msgdef.lookup "optional_enum" - assert_equal TestEnum.descriptor, optional_enum.subtype - assert_instance_of Google::Protobuf::EnumDescriptor, optional_enum.subtype - optional_enum.subtype.each do |k, v| - # set with integer, check resolution to symbolic name - optional_enum.set(m, v) - assert_equal k, optional_enum.get(m) - end - end - - def test_json - # TODO: Fix JSON in JRuby version. - return if RUBY_PLATFORM == "java" - m = TestMessage.new(:optional_int32 => 1234, - :optional_int64 => -0x1_0000_0000, - :optional_uint32 => 0x8000_0000, - :optional_uint64 => 0xffff_ffff_ffff_ffff, - :optional_bool => true, - :optional_float => 1.0, - :optional_double => -1e100, - :optional_string => "Test string", - :optional_bytes => ["FFFFFFFF"].pack('H*'), - :optional_msg => TestMessage2.new(:foo => 42), - :repeated_int32 => [1, 2, 3, 4], - :repeated_string => ["a", "b", "c"], - :repeated_bool => [true, false, true, false], - :repeated_msg => [TestMessage2.new(:foo => 1), - TestMessage2.new(:foo => 2)]) - - json_text = TestMessage.encode_json(m) - m2 = TestMessage.decode_json(json_text) - assert_equal m, m2 - # Crash case from GitHub issue 283. - bar = Bar.new(msg: "bar") - baz1 = Baz.new(msg: "baz") - baz2 = Baz.new(msg: "quux") - Foo.encode_json(Foo.new) - Foo.encode_json(Foo.new(bar: bar)) - Foo.encode_json(Foo.new(bar: bar, baz: [baz1, baz2])) - end - - def test_json_maps - # TODO: Fix JSON in JRuby version. - return if RUBY_PLATFORM == "java" - m = MapMessage.new(:map_string_int32 => {"a" => 1}) - expected = '{"mapStringInt32":{"a":1},"mapStringMsg":{}}' - expected_preserve = '{"map_string_int32":{"a":1},"map_string_msg":{}}' - assert_equal expected, MapMessage.encode_json(m, :emit_defaults => true) - - json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true, :emit_defaults => true) - assert_equal expected_preserve, json - - m2 = MapMessage.decode_json(MapMessage.encode_json(m)) - assert_equal m, m2 - end - end -end diff --git a/ruby/compatibility_tests/v3.0.0/tests/generated_code.proto b/ruby/compatibility_tests/v3.0.0/tests/generated_code.proto deleted file mode 100644 index 62fd83ed89..0000000000 --- a/ruby/compatibility_tests/v3.0.0/tests/generated_code.proto +++ /dev/null @@ -1,67 +0,0 @@ -syntax = "proto3"; - -package a.b.c; - -message TestMessage { - int32 optional_int32 = 1; - int64 optional_int64 = 2; - uint32 optional_uint32 = 3; - uint64 optional_uint64 = 4; - bool optional_bool = 5; - double optional_double = 6; - float optional_float = 7; - string optional_string = 8; - bytes optional_bytes = 9; - TestEnum optional_enum = 10; - TestMessage optional_msg = 11; - - repeated int32 repeated_int32 = 21; - repeated int64 repeated_int64 = 22; - repeated uint32 repeated_uint32 = 23; - repeated uint64 repeated_uint64 = 24; - repeated bool repeated_bool = 25; - repeated double repeated_double = 26; - repeated float repeated_float = 27; - repeated string repeated_string = 28; - repeated bytes repeated_bytes = 29; - repeated TestEnum repeated_enum = 30; - repeated TestMessage repeated_msg = 31; - - oneof my_oneof { - int32 oneof_int32 = 41; - int64 oneof_int64 = 42; - uint32 oneof_uint32 = 43; - uint64 oneof_uint64 = 44; - bool oneof_bool = 45; - double oneof_double = 46; - float oneof_float = 47; - string oneof_string = 48; - bytes oneof_bytes = 49; - TestEnum oneof_enum = 50; - TestMessage oneof_msg = 51; - } - - map map_int32_string = 61; - map map_int64_string = 62; - map map_uint32_string = 63; - map map_uint64_string = 64; - map map_bool_string = 65; - map map_string_string = 66; - map map_string_msg = 67; - map map_string_enum = 68; - map map_string_int32 = 69; - map map_string_bool = 70; - - message NestedMessage { - int32 foo = 1; - } - - NestedMessage nested_message = 80; -} - -enum TestEnum { - Default = 0; - A = 1; - B = 2; - C = 3; -} diff --git a/ruby/compatibility_tests/v3.0.0/tests/generated_code_test.rb b/ruby/compatibility_tests/v3.0.0/tests/generated_code_test.rb deleted file mode 100755 index b92b0462d1..0000000000 --- a/ruby/compatibility_tests/v3.0.0/tests/generated_code_test.rb +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/ruby - -# generated_code.rb is in the same directory as this test. -$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) - -require 'generated_code_pb' -require 'test_import_pb' -require 'test/unit' - -class GeneratedCodeTest < Test::Unit::TestCase - def test_generated_msg - # just test that we can instantiate the message. The purpose of this test - # is to ensure that the output of the code generator is valid Ruby and - # successfully creates message definitions and classes, not to test every - # aspect of the extension (basic.rb is for that). - m = A::B::C::TestMessage.new() - m2 = FooBar::TestImportedMessage.new() - end -end diff --git a/ruby/compatibility_tests/v3.0.0/tests/repeated_field_test.rb b/ruby/compatibility_tests/v3.0.0/tests/repeated_field_test.rb deleted file mode 100755 index ed12eaa8de..0000000000 --- a/ruby/compatibility_tests/v3.0.0/tests/repeated_field_test.rb +++ /dev/null @@ -1,647 +0,0 @@ -#!/usr/bin/ruby - -require 'google/protobuf' -require 'test/unit' - -class RepeatedFieldTest < Test::Unit::TestCase - - def test_acts_like_enumerator - m = TestMessage.new - (Enumerable.instance_methods - TestMessage.new.repeated_string.methods).each do |method_name| - assert_respond_to m.repeated_string, method_name - end - end - - def test_acts_like_an_array - m = TestMessage.new - arr_methods = ([].methods - TestMessage.new.repeated_string.methods) - # jRuby additions to the Array class that we can ignore - arr_methods -= [ :indices, :iter_for_each, :iter_for_each_index, - :iter_for_each_with_index, :dimensions, :copy_data, :copy_data_simple, - :nitems, :iter_for_reverse_each, :indexes, :append, :prepend] - arr_methods -= [:union, :difference, :filter!] - # ruby 2.7 methods we can ignore - arr_methods -= [:intersection, :deconstruct, :resolve_feature_path] - # ruby 3.1 methods we can ignore - arr_methods -= [:intersect?] - arr_methods.each do |method_name| - assert_respond_to m.repeated_string, method_name - end - end - - def test_first - m = TestMessage.new - repeated_field_names(TestMessage).each do |field_name| - assert_nil m.send(field_name).first - end - fill_test_msg(m) - assert_equal -10, m.repeated_int32.first - assert_equal -1_000_000, m.repeated_int64.first - assert_equal 10, m.repeated_uint32.first - assert_equal 1_000_000, m.repeated_uint64.first - assert m.repeated_bool.first - assert_equal -1.01, m.repeated_float.first.round(2) - assert_equal -1.0000000000001, m.repeated_double.first - assert_equal 'foo', m.repeated_string.first - assert_equal "bar".encode!('ASCII-8BIT'), m.repeated_bytes.first - assert_equal TestMessage2.new(:foo => 1), m.repeated_msg.first - assert_equal :A, m.repeated_enum.first - end - - - def test_last - m = TestMessage.new - repeated_field_names(TestMessage).each do |field_name| - assert_nil m.send(field_name).first - end - fill_test_msg(m) - assert_equal -11, m.repeated_int32.last - assert_equal -1_000_001, m.repeated_int64.last - assert_equal 11, m.repeated_uint32.last - assert_equal 1_000_001, m.repeated_uint64.last - refute m.repeated_bool.last - assert_equal -1.02, m.repeated_float.last.round(2) - assert_equal -1.0000000000002, m.repeated_double.last - assert_equal 'bar', m.repeated_string.last - assert_equal "foo".encode!('ASCII-8BIT'), m.repeated_bytes.last - assert_equal TestMessage2.new(:foo => 2), m.repeated_msg.last - assert_equal :B, m.repeated_enum.last - end - - - def test_pop - m = TestMessage.new - repeated_field_names(TestMessage).each do |field_name| - assert_nil m.send(field_name).pop - end - fill_test_msg(m) - - assert_equal -11, m.repeated_int32.pop - assert_equal -10, m.repeated_int32.pop - assert_equal -1_000_001, m.repeated_int64.pop - assert_equal -1_000_000, m.repeated_int64.pop - assert_equal 11, m.repeated_uint32.pop - assert_equal 10, m.repeated_uint32.pop - assert_equal 1_000_001, m.repeated_uint64.pop - assert_equal 1_000_000, m.repeated_uint64.pop - refute m.repeated_bool.pop - assert m.repeated_bool.pop - assert_equal -1.02, m.repeated_float.pop.round(2) - assert_equal -1.01, m.repeated_float.pop.round(2) - assert_equal -1.0000000000002, m.repeated_double.pop - assert_equal -1.0000000000001, m.repeated_double.pop - assert_equal 'bar', m.repeated_string.pop - assert_equal 'foo', m.repeated_string.pop - assert_equal "foo".encode!('ASCII-8BIT'), m.repeated_bytes.pop - assert_equal "bar".encode!('ASCII-8BIT'), m.repeated_bytes.pop - assert_equal TestMessage2.new(:foo => 2), m.repeated_msg.pop - assert_equal TestMessage2.new(:foo => 1), m.repeated_msg.pop - assert_equal :B, m.repeated_enum.pop - assert_equal :A, m.repeated_enum.pop - repeated_field_names(TestMessage).each do |field_name| - assert_nil m.send(field_name).pop - end - - fill_test_msg(m) - assert_equal ['bar', 'foo'], m.repeated_string.pop(2) - assert_nil m.repeated_string.pop - end - - - def test_each - m = TestMessage.new - 5.times{|i| m.repeated_string << 'string' } - count = 0 - m.repeated_string.each do |val| - assert_equal 'string', val - count += 1 - end - assert_equal 5, count - result = m.repeated_string.each{|val| val + '_junk'} - assert_equal ['string'] * 5, result - end - - - def test_each_index - m = TestMessage.new - 5.times{|i| m.repeated_string << 'string' } - - expected = 0 - m.repeated_string.each_index do |idx| - assert_equal expected, idx - expected += 1 - assert_equal 'string', m.repeated_string[idx] - end - assert_equal 5, expected - end - - - def test_empty? - m = TestMessage.new - assert_empty m.repeated_string - m.repeated_string << 'foo' - refute_empty m.repeated_string - m.repeated_string << 'bar' - refute_empty m.repeated_string - end - - def test_array_accessor - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr[1] - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr[-2] - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr[20] - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr[1, 2] - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr[0..2] - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr[-1, 1] - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr[10, 12] - end - end - - def test_array_settor - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr[1] = 'junk' - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr[-2] = 'snappy' - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr[3] = '' - end - # slight deviation; we are strongly typed, and nil is not allowed - # for string types; - m.repeated_string[5] = 'spacious' - assert_equal ["foo", "snappy", "baz", "", "", "spacious"], m.repeated_string - - #make sure it sests the default types for other fields besides strings - %w(repeated_int32 repeated_int64 repeated_uint32 repeated_uint64).each do |field_name| - m.send(field_name)[3] = 10 - assert_equal [0,0,0,10], m.send(field_name) - end - m.repeated_float[3] = 10.1 - #wonky mri float handling - assert_equal [0,0,0], m.repeated_float.to_a[0..2] - assert_equal 10.1, m.repeated_float[3].round(1) - m.repeated_double[3] = 10.1 - assert_equal [0,0,0,10.1], m.repeated_double - m.repeated_bool[3] = true - assert_equal [false, false, false, true], m.repeated_bool - m.repeated_bytes[3] = "bar".encode!('ASCII-8BIT') - assert_equal ['', '', '', "bar".encode!('ASCII-8BIT')], m.repeated_bytes - m.repeated_msg[3] = TestMessage2.new(:foo => 1) - assert_equal [nil, nil, nil, TestMessage2.new(:foo => 1)], m.repeated_msg - m.repeated_enum[3] = :A - assert_equal [:Default, :Default, :Default, :A], m.repeated_enum - - # check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - # arr[20] = 'spacious' - # end - # TODO: accessor doesn't allow other ruby-like methods - # check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - # arr[1, 2] = 'fizz' - # end - # check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - # arr[0..2] = 'buzz' - # end - end - - def test_push - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.push('fizz') - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr << 'fizz' - end - #TODO: push should support multiple - # check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - # arr.push('fizz', 'buzz') - # end - end - - def test_clear - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.clear - end - end - - def test_concat - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - m.repeated_string.concat(['fizz', 'buzz']) - assert_equal %w(foo bar baz fizz buzz), m.repeated_string - #TODO: concat should return the orig array - # check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - # arr.concat(['fizz', 'buzz']) - # end - end - - def test_equal - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - assert_equal reference_arr, m.repeated_string - reference_arr << 'fizz' - refute_equal reference_arr, m.repeated_string - m.repeated_string << 'fizz' - assert_equal reference_arr, m.repeated_string - end - - def test_hash - # just a sanity check - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - assert m.repeated_string.hash.is_a?(Integer) - hash = m.repeated_string.hash - assert_equal hash, m.repeated_string.hash - m.repeated_string << 'j' - refute_equal hash, m.repeated_string.hash - end - - def test_plus - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr + ['fizz', 'buzz'] - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr += ['fizz', 'buzz'] - end - end - - def test_replace - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.replace(['fizz', 'buzz']) - end - end - - def test_to_a - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.to_a - end - end - - def test_to_ary - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.to_ary - end - end - - # emulate Array behavior - ########################## - - def test_collect! - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.collect!{|x| x + "!" } - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.collect!.with_index{|x, i| x[0...i] } - end - end - - def test_delete - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.delete('bar') - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.delete('nope') - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.delete('nope'){'within'} - end - end - - def test_delete_at - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.delete_at(2) - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.delete_at(10) - end - end - - def test_fill - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.fill("x") - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.fill("z", 2, 2) - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.fill("y", 0..1) - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.fill { |i| (i*i).to_s } - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.fill(-2) { |i| (i*i*i).to_s } - end - end - - def test_flatten! - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.flatten! - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.flatten!(1) - end - end - - def test_insert - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.insert(2, 'fizz') - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.insert(3, 'fizz', 'buzz', 'bazz') - end - end - - def test_inspect - m = TestMessage.new - assert_equal '[]', m.repeated_string.inspect - m.repeated_string << 'foo' - assert_equal m.repeated_string.to_a.inspect, m.repeated_string.inspect - m.repeated_string << 'bar' - assert_equal m.repeated_string.to_a.inspect, m.repeated_string.inspect - end - - def test_reverse! - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.reverse! - end - end - - def test_rotate! - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.rotate! - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.rotate!(2) - end - end - - def test_select! - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.select! { |v| v =~ /[aeiou]/ } - end - end - - def test_shift - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - # should return an element - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.shift - end - # should return an array - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.shift(2) - end - # should return nil - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.shift - end - end - - def test_shuffle! - m = TestMessage.new - m.repeated_string += %w(foo bar baz) - orig_repeated_string = m.repeated_string.clone - result = m.repeated_string.shuffle! - assert_equal m.repeated_string, result - # NOTE: sometimes it doesn't change the order... - # refute_equal m.repeated_string.to_a, orig_repeated_string.to_a - end - - def test_slice! - m = TestMessage.new - reference_arr = %w(foo bar baz bar fizz buzz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.slice!(2) - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.slice!(1,2) - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.slice!(0..1) - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.slice!(10) - end - end - - def test_sort! - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.sort! - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.sort! { |x,y| y <=> x } - end - end - - def test_sort_by! - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.sort_by! - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.sort_by!(&:hash) - end - end - - def test_uniq! - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.uniq! - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.uniq!{|s| s[0] } - end - end - - def test_unshift - m = TestMessage.new - reference_arr = %w(foo bar baz) - m.repeated_string += reference_arr.clone - - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.unshift('1') - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.unshift('a', 'b') - end - check_self_modifying_method(m.repeated_string, reference_arr) do |arr| - arr.unshift('') - end - end - - - ##### HELPER METHODS - - def check_self_modifying_method(repeated_field, ref_array) - expected_result = yield(ref_array) - actual_result = yield(repeated_field) - if expected_result.is_a?(Enumerator) - assert_equal expected_result.to_a, actual_result.to_a - else - assert_equal expected_result, actual_result - end - assert_equal ref_array, repeated_field - end - - - def repeated_field_names(klass) - klass.descriptor.find_all{|f| f.label == :repeated}.map(&:name) - end - - - def fill_test_msg(test_msg) - test_msg.repeated_int32 += [-10, -11] - test_msg.repeated_int64 += [-1_000_000, -1_000_001] - test_msg.repeated_uint32 += [10, 11] - test_msg.repeated_uint64 += [1_000_000, 1_000_001] - test_msg.repeated_bool += [true, false] - test_msg.repeated_float += [-1.01, -1.02] - test_msg.repeated_double += [-1.0000000000001, -1.0000000000002] - test_msg.repeated_string += %w(foo bar) - test_msg.repeated_bytes += ["bar".encode!('ASCII-8BIT'), "foo".encode!('ASCII-8BIT')] - test_msg.repeated_msg << TestMessage2.new(:foo => 1) - test_msg.repeated_msg << TestMessage2.new(:foo => 2) - test_msg.repeated_enum << :A - test_msg.repeated_enum << :B - end - - - pool = Google::Protobuf::DescriptorPool.new - pool.build do - - add_message "TestMessage" do - optional :optional_int32, :int32, 1 - optional :optional_int64, :int64, 2 - optional :optional_uint32, :uint32, 3 - optional :optional_uint64, :uint64, 4 - optional :optional_bool, :bool, 5 - optional :optional_float, :float, 6 - optional :optional_double, :double, 7 - optional :optional_string, :string, 8 - optional :optional_bytes, :bytes, 9 - optional :optional_msg, :message, 10, "TestMessage2" - optional :optional_enum, :enum, 11, "TestEnum" - - repeated :repeated_int32, :int32, 12 - repeated :repeated_int64, :int64, 13 - repeated :repeated_uint32, :uint32, 14 - repeated :repeated_uint64, :uint64, 15 - repeated :repeated_bool, :bool, 16 - repeated :repeated_float, :float, 17 - repeated :repeated_double, :double, 18 - repeated :repeated_string, :string, 19 - repeated :repeated_bytes, :bytes, 20 - repeated :repeated_msg, :message, 21, "TestMessage2" - repeated :repeated_enum, :enum, 22, "TestEnum" - end - add_message "TestMessage2" do - optional :foo, :int32, 1 - end - - add_enum "TestEnum" do - value :Default, 0 - value :A, 1 - value :B, 2 - value :C, 3 - end - end - - TestMessage = pool.lookup("TestMessage").msgclass - TestMessage2 = pool.lookup("TestMessage2").msgclass - TestEnum = pool.lookup("TestEnum").enummodule - - -end diff --git a/ruby/compatibility_tests/v3.0.0/tests/stress.rb b/ruby/compatibility_tests/v3.0.0/tests/stress.rb deleted file mode 100755 index b688e8b0bd..0000000000 --- a/ruby/compatibility_tests/v3.0.0/tests/stress.rb +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/ruby - -require 'google/protobuf' -require 'test/unit' - -module StressTest - pool = Google::Protobuf::DescriptorPool.new - pool.build do - add_message "TestMessage" do - optional :a, :int32, 1 - repeated :b, :message, 2, "M" - end - add_message "M" do - optional :foo, :string, 1 - end - end - - TestMessage = pool.lookup("TestMessage").msgclass - M = pool.lookup("M").msgclass - - class StressTest < Test::Unit::TestCase - def get_msg - TestMessage.new(:a => 1000, - :b => [M.new(:foo => "hello"), - M.new(:foo => "world")]) - end - def test_stress - m = get_msg - data = TestMessage.encode(m) - 100_000.times do - mnew = TestMessage.decode(data) - mnew = mnew.dup - assert_equal m.inspect, mnew.inspect - assert_equal data, TestMessage.encode(mnew) - end - end - end -end diff --git a/ruby/compatibility_tests/v3.0.0/tests/test_import.proto b/ruby/compatibility_tests/v3.0.0/tests/test_import.proto deleted file mode 100644 index 230484ee57..0000000000 --- a/ruby/compatibility_tests/v3.0.0/tests/test_import.proto +++ /dev/null @@ -1,5 +0,0 @@ -syntax = "proto3"; - -package foo_bar; - -message TestImportedMessage {}