diff options
author | Jisi Liu <liujisi@google.com> | 2018-01-04 14:26:24 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-04 14:26:24 -0800 |
commit | a3868af12be724f808faa21a29e7b4931e30bed1 (patch) | |
tree | 3df23c4174287023b569108eb133db0f9c310646 /ruby/tests/encode_decode_test.rb | |
parent | eca1d2a7113b3d57f900f69661342c0078c956c4 (diff) | |
parent | ec826c5a592126ddb6b12b47e6e1fb6232ce340d (diff) | |
download | protobuf-a3868af12be724f808faa21a29e7b4931e30bed1.tar.gz protobuf-a3868af12be724f808faa21a29e7b4931e30bed1.tar.bz2 protobuf-a3868af12be724f808faa21a29e7b4931e30bed1.zip |
Merge pull request #4131 from pherl/merge
Merge 3.5.x branch into master
Diffstat (limited to 'ruby/tests/encode_decode_test.rb')
-rw-r--r-- | ruby/tests/encode_decode_test.rb | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/ruby/tests/encode_decode_test.rb b/ruby/tests/encode_decode_test.rb new file mode 100644 index 00000000..09581ab0 --- /dev/null +++ b/ruby/tests/encode_decode_test.rb @@ -0,0 +1,63 @@ +#!/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/unit' + +def hex2bin(s) + s.scan(/../).map { |x| x.hex.chr }.join +end + +class EncodeDecodeTest < Test::Unit::TestCase + def test_discard_unknown + # Test discard unknown in message. + unknown_msg = A::B::C::TestUnknown.new(:unknown_field => 1) + from = A::B::C::TestUnknown.encode(unknown_msg) + m = A::B::C::TestMessage.decode(from) + Google::Protobuf.discard_unknown(m) + to = A::B::C::TestMessage.encode(m) + assert_equal '', to + + # Test discard unknown for singular message field. + unknown_msg = A::B::C::TestUnknown.new( + :optional_unknown => + A::B::C::TestUnknown.new(:unknown_field => 1)) + from = A::B::C::TestUnknown.encode(unknown_msg) + m = A::B::C::TestMessage.decode(from) + Google::Protobuf.discard_unknown(m) + to = A::B::C::TestMessage.encode(m.optional_msg) + assert_equal '', to + + # Test discard unknown for repeated message field. + unknown_msg = A::B::C::TestUnknown.new( + :repeated_unknown => + [A::B::C::TestUnknown.new(:unknown_field => 1)]) + from = A::B::C::TestUnknown.encode(unknown_msg) + m = A::B::C::TestMessage.decode(from) + Google::Protobuf.discard_unknown(m) + to = A::B::C::TestMessage.encode(m.repeated_msg[0]) + assert_equal '', to + + # Test discard unknown for map value message field. + unknown_msg = A::B::C::TestUnknown.new( + :map_unknown => + {"" => A::B::C::TestUnknown.new(:unknown_field => 1)}) + from = A::B::C::TestUnknown.encode(unknown_msg) + m = A::B::C::TestMessage.decode(from) + Google::Protobuf.discard_unknown(m) + to = A::B::C::TestMessage.encode(m.map_string_msg['']) + assert_equal '', to + + # Test discard unknown for oneof message field. + unknown_msg = A::B::C::TestUnknown.new( + :oneof_unknown => + A::B::C::TestUnknown.new(:unknown_field => 1)) + from = A::B::C::TestUnknown.encode(unknown_msg) + m = A::B::C::TestMessage.decode(from) + Google::Protobuf.discard_unknown(m) + to = A::B::C::TestMessage.encode(m.oneof_msg) + assert_equal '', to + end +end |