• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/ruby
2
3# generated_code.rb is in the same directory as this test.
4$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
5
6require 'generated_code_pb'
7require 'test/unit'
8
9def hex2bin(s)
10  s.scan(/../).map { |x| x.hex.chr }.join
11end
12
13class EncodeDecodeTest < Test::Unit::TestCase
14  def test_discard_unknown
15    # Test discard unknown in message.
16    unknown_msg = A::B::C::TestUnknown.new(:unknown_field => 1)
17    from = A::B::C::TestUnknown.encode(unknown_msg)
18    m = A::B::C::TestMessage.decode(from)
19    Google::Protobuf.discard_unknown(m)
20    to = A::B::C::TestMessage.encode(m)
21    assert_equal '', to
22
23    # Test discard unknown for singular message field.
24    unknown_msg = A::B::C::TestUnknown.new(
25            :optional_unknown =>
26            A::B::C::TestUnknown.new(:unknown_field => 1))
27    from = A::B::C::TestUnknown.encode(unknown_msg)
28    m = A::B::C::TestMessage.decode(from)
29    Google::Protobuf.discard_unknown(m)
30    to = A::B::C::TestMessage.encode(m.optional_msg)
31    assert_equal '', to
32
33    # Test discard unknown for repeated message field.
34    unknown_msg = A::B::C::TestUnknown.new(
35            :repeated_unknown =>
36            [A::B::C::TestUnknown.new(:unknown_field => 1)])
37    from = A::B::C::TestUnknown.encode(unknown_msg)
38    m = A::B::C::TestMessage.decode(from)
39    Google::Protobuf.discard_unknown(m)
40    to = A::B::C::TestMessage.encode(m.repeated_msg[0])
41    assert_equal '', to
42
43    # Test discard unknown for map value message field.
44    unknown_msg = A::B::C::TestUnknown.new(
45            :map_unknown =>
46            {"" => A::B::C::TestUnknown.new(:unknown_field => 1)})
47    from = A::B::C::TestUnknown.encode(unknown_msg)
48    m = A::B::C::TestMessage.decode(from)
49    Google::Protobuf.discard_unknown(m)
50    to = A::B::C::TestMessage.encode(m.map_string_msg[''])
51    assert_equal '', to
52
53    # Test discard unknown for oneof message field.
54    unknown_msg = A::B::C::TestUnknown.new(
55            :oneof_unknown =>
56            A::B::C::TestUnknown.new(:unknown_field => 1))
57    from = A::B::C::TestUnknown.encode(unknown_msg)
58    m = A::B::C::TestMessage.decode(from)
59    Google::Protobuf.discard_unknown(m)
60    to = A::B::C::TestMessage.encode(m.oneof_msg)
61    assert_equal '', to
62  end
63
64  def test_encode_json
65    msg = A::B::C::TestMessage.new({ optional_int32: 22 })
66    json = msg.to_json
67
68    to = A::B::C::TestMessage.decode_json(json)
69    assert_equal to.optional_int32, 22
70
71    msg = A::B::C::TestMessage.new({ optional_int32: 22 })
72    json = msg.to_json({ preserve_proto_fieldnames: true })
73
74    assert_match 'optional_int32', json
75
76    to = A::B::C::TestMessage.decode_json(json)
77    assert_equal 22, to.optional_int32
78
79    msg = A::B::C::TestMessage.new({ optional_int32: 22 })
80    json = A::B::C::TestMessage.encode_json(
81      msg,
82      { preserve_proto_fieldnames: true, emit_defaults: true }
83    )
84
85    assert_match 'optional_int32', json
86  end
87
88  def test_encode_wrong_msg
89    assert_raise ::ArgumentError do
90      m = A::B::C::TestMessage.new(
91          :optional_int32 => 1,
92      )
93      Google::Protobuf::Any.encode(m)
94    end
95  end
96
97end
98