encode_decode_test.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/ruby
  2. # generated_code.rb is in the same directory as this test.
  3. $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
  4. require 'generated_code_pb'
  5. require 'test/unit'
  6. def hex2bin(s)
  7. s.scan(/../).map { |x| x.hex.chr }.join
  8. end
  9. class EncodeDecodeTest < Test::Unit::TestCase
  10. def test_discard_unknown
  11. # Test discard unknown in message.
  12. unknown_msg = A::B::C::TestUnknown.new(:unknown_field => 1)
  13. from = A::B::C::TestUnknown.encode(unknown_msg)
  14. m = A::B::C::TestMessage.decode(from)
  15. Google::Protobuf.discard_unknown(m)
  16. to = A::B::C::TestMessage.encode(m)
  17. assert_equal '', to
  18. # Test discard unknown for singular message field.
  19. unknown_msg = A::B::C::TestUnknown.new(
  20. :optional_unknown =>
  21. A::B::C::TestUnknown.new(:unknown_field => 1))
  22. from = A::B::C::TestUnknown.encode(unknown_msg)
  23. m = A::B::C::TestMessage.decode(from)
  24. Google::Protobuf.discard_unknown(m)
  25. to = A::B::C::TestMessage.encode(m.optional_msg)
  26. assert_equal '', to
  27. # Test discard unknown for repeated message field.
  28. unknown_msg = A::B::C::TestUnknown.new(
  29. :repeated_unknown =>
  30. [A::B::C::TestUnknown.new(:unknown_field => 1)])
  31. from = A::B::C::TestUnknown.encode(unknown_msg)
  32. m = A::B::C::TestMessage.decode(from)
  33. Google::Protobuf.discard_unknown(m)
  34. to = A::B::C::TestMessage.encode(m.repeated_msg[0])
  35. assert_equal '', to
  36. # Test discard unknown for map value message field.
  37. unknown_msg = A::B::C::TestUnknown.new(
  38. :map_unknown =>
  39. {"" => A::B::C::TestUnknown.new(:unknown_field => 1)})
  40. from = A::B::C::TestUnknown.encode(unknown_msg)
  41. m = A::B::C::TestMessage.decode(from)
  42. Google::Protobuf.discard_unknown(m)
  43. to = A::B::C::TestMessage.encode(m.map_string_msg[''])
  44. assert_equal '', to
  45. # Test discard unknown for oneof message field.
  46. unknown_msg = A::B::C::TestUnknown.new(
  47. :oneof_unknown =>
  48. A::B::C::TestUnknown.new(:unknown_field => 1))
  49. from = A::B::C::TestUnknown.encode(unknown_msg)
  50. m = A::B::C::TestMessage.decode(from)
  51. Google::Protobuf.discard_unknown(m)
  52. to = A::B::C::TestMessage.encode(m.oneof_msg)
  53. assert_equal '', to
  54. end
  55. end