• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/ruby
2
3# basic_test_pb.rb is in the same directory as this test.
4$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
5
6require 'basic_test_proto2_pb'
7require 'common_tests'
8require 'google/protobuf'
9require 'json'
10require 'test/unit'
11
12# ------------- generated code --------------
13
14module BasicTestProto2
15  pool = Google::Protobuf::DescriptorPool.new
16  pool.build do
17    add_file "test_proto2.proto", syntax: :proto2 do
18      add_message "BadFieldNames" do
19        optional :dup, :int32, 1
20        optional :class, :int32, 2
21      end
22    end
23  end
24
25  BadFieldNames = pool.lookup("BadFieldNames").msgclass
26
27# ------------ test cases ---------------
28
29  class MessageContainerTest < Test::Unit::TestCase
30    # Required by CommonTests module to resolve proto2 proto classes used in tests.
31    def proto_module
32      ::BasicTestProto2
33    end
34    include CommonTests
35
36    def test_has_field
37      m = TestMessage.new
38      assert !m.has_optional_int32?
39      assert !TestMessage.descriptor.lookup('optional_int32').has?(m)
40      assert !m.has_optional_int64?
41      assert !TestMessage.descriptor.lookup('optional_int64').has?(m)
42      assert !m.has_optional_uint32?
43      assert !TestMessage.descriptor.lookup('optional_uint32').has?(m)
44      assert !m.has_optional_uint64?
45      assert !TestMessage.descriptor.lookup('optional_uint64').has?(m)
46      assert !m.has_optional_bool?
47      assert !TestMessage.descriptor.lookup('optional_bool').has?(m)
48      assert !m.has_optional_float?
49      assert !TestMessage.descriptor.lookup('optional_float').has?(m)
50      assert !m.has_optional_double?
51      assert !TestMessage.descriptor.lookup('optional_double').has?(m)
52      assert !m.has_optional_string?
53      assert !TestMessage.descriptor.lookup('optional_string').has?(m)
54      assert !m.has_optional_bytes?
55      assert !TestMessage.descriptor.lookup('optional_bytes').has?(m)
56      assert !m.has_optional_enum?
57      assert !TestMessage.descriptor.lookup('optional_enum').has?(m)
58
59      m = TestMessage.new(:optional_int32 => nil)
60      assert !m.has_optional_int32?
61
62      assert_raise NoMethodError do
63        m.has_repeated_msg?
64      end
65      assert_raise ArgumentError do
66        TestMessage.descriptor.lookup('repeated_msg').has?(m)
67      end
68
69      m.optional_msg = TestMessage2.new
70      assert m.has_optional_msg?
71      assert TestMessage.descriptor.lookup('optional_msg').has?(m)
72
73      m = OneofMessage.new
74      assert !m.has_my_oneof?
75      m.a = "foo"
76      assert m.has_my_oneof?
77      assert_equal :a, m.my_oneof
78      assert m.has_a?
79      assert OneofMessage.descriptor.lookup('a').has?(m)
80      assert_equal "foo", m.a
81      assert !m.has_b?
82      assert !OneofMessage.descriptor.lookup('b').has?(m)
83      assert !m.has_c?
84      assert !OneofMessage.descriptor.lookup('c').has?(m)
85      assert !m.has_d?
86      assert !OneofMessage.descriptor.lookup('d').has?(m)
87
88      m = OneofMessage.new
89      m.b = 100
90      assert m.has_b?
91      assert_equal 100, m.b
92      assert m.has_my_oneof?
93      assert !m.has_a?
94      assert !m.has_c?
95      assert !m.has_d?
96
97      m = OneofMessage.new
98      m.c = TestMessage2.new
99      assert m.has_c?
100      assert_equal TestMessage2.new, m.c
101      assert m.has_my_oneof?
102      assert !m.has_a?
103      assert !m.has_b?
104      assert !m.has_d?
105
106      m = OneofMessage.new
107      m.d = :A
108      assert m.has_d?
109      assert_equal :A, m.d
110      assert m.has_my_oneof?
111      assert !m.has_a?
112      assert !m.has_b?
113      assert !m.has_c?
114    end
115
116    def test_defined_defaults
117      m = TestMessageDefaults.new
118      assert_equal 1, m.optional_int32
119      assert_equal 2, m.optional_int64
120      assert_equal 3, m.optional_uint32
121      assert_equal 4, m.optional_uint64
122      assert_equal true, m.optional_bool
123      assert_equal 6.0, m.optional_float
124      assert_equal 7.0, m.optional_double
125      assert_equal "Default Str", m.optional_string
126      assert_equal "\xCF\xA5s\xBD\xBA\xE6fubar".force_encoding("ASCII-8BIT"), m.optional_bytes
127      assert_equal :B2, m.optional_enum
128
129      assert !m.has_optional_int32?
130      assert !m.has_optional_int64?
131      assert !m.has_optional_uint32?
132      assert !m.has_optional_uint64?
133      assert !m.has_optional_bool?
134      assert !m.has_optional_float?
135      assert !m.has_optional_double?
136      assert !m.has_optional_string?
137      assert !m.has_optional_bytes?
138      assert !m.has_optional_enum?
139    end
140
141    def test_set_clear_defaults
142      m = TestMessageDefaults.new
143
144      m.optional_int32 = -42
145      assert_equal -42, m.optional_int32
146      assert m.has_optional_int32?
147      m.clear_optional_int32
148      assert_equal 1, m.optional_int32
149      assert !m.has_optional_int32?
150
151      m.optional_string = "foo bar"
152      assert_equal "foo bar", m.optional_string
153      assert m.has_optional_string?
154      m.clear_optional_string
155      assert_equal "Default Str", m.optional_string
156      assert !m.has_optional_string?
157
158      m.optional_msg = TestMessage2.new(:foo => 42)
159      assert_equal TestMessage2.new(:foo => 42), m.optional_msg
160      assert m.has_optional_msg?
161
162      m.clear_optional_msg
163      assert_equal nil, m.optional_msg
164      assert !m.has_optional_msg?
165
166      m.optional_msg = TestMessage2.new(:foo => 42)
167      assert_equal TestMessage2.new(:foo => 42), m.optional_msg
168      assert TestMessageDefaults.descriptor.lookup('optional_msg').has?(m)
169
170      TestMessageDefaults.descriptor.lookup('optional_msg').clear(m)
171      assert_equal nil, m.optional_msg
172      assert !TestMessageDefaults.descriptor.lookup('optional_msg').has?(m)
173
174      m = TestMessage.new
175      m.repeated_int32.push(1)
176      assert_equal [1], m.repeated_int32
177      m.clear_repeated_int32
178      assert_equal [], m.repeated_int32
179
180      m = OneofMessage.new
181      m.a = "foo"
182      assert_equal "foo", m.a
183      assert m.has_a?
184      m.clear_a
185      assert !m.has_a?
186
187      m = OneofMessage.new
188      m.a = "foobar"
189      assert m.has_my_oneof?
190      m.clear_my_oneof
191      assert !m.has_my_oneof?
192
193      m = OneofMessage.new
194      m.a = "bar"
195      assert_equal "bar", m.a
196      assert m.has_my_oneof?
197      OneofMessage.descriptor.lookup('a').clear(m)
198      assert !m.has_my_oneof?
199    end
200
201    def test_assign_nil
202      m = TestMessageDefaults.new
203      m.optional_msg = TestMessage2.new(:foo => 42)
204
205      assert_equal TestMessage2.new(:foo => 42), m.optional_msg
206      assert m.has_optional_msg?
207      m.optional_msg = nil
208      assert_equal nil, m.optional_msg
209      assert !m.has_optional_msg?
210    end
211
212    def test_initialization_map_errors
213      e = assert_raise ArgumentError do
214        TestMessage.new(:hello => "world")
215      end
216      assert_match(/hello/, e.message)
217
218      e = assert_raise ArgumentError do
219        TestMessage.new(:repeated_uint32 => "hello")
220      end
221      assert_equal e.message, "Expected array as initializer value for repeated field 'repeated_uint32' (given String)."
222    end
223
224
225    def test_to_h
226      m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'])
227      expected_result = {
228        :optional_bool=>true,
229        :optional_double=>-10.100001,
230        :optional_string=>"foo",
231        :repeated_string=>["bar1", "bar2"],
232      }
233      assert_equal expected_result, m.to_h
234
235      m = OneofMessage.new(:a => "foo")
236      expected_result = {:a => "foo"}
237      assert_equal expected_result, m.to_h
238    end
239
240    def test_map_keyword_disabled
241      pool = Google::Protobuf::DescriptorPool.new
242
243      e = assert_raise ArgumentError do
244        pool.build do
245          add_file 'test_file.proto', syntax: :proto2 do
246            add_message "MapMessage" do
247              map :map_string_int32, :string, :int32, 1
248              map :map_string_msg, :string, :message, 2, "TestMessage2"
249            end
250          end
251        end
252      end
253
254      assert_match(/Cannot add a native map/, e.message)
255    end
256
257    def test_respond_to
258      # This test fails with JRuby 1.7.23, likely because of an old JRuby bug.
259      return if RUBY_PLATFORM == "java"
260      msg = TestMessage.new
261      assert !msg.respond_to?(:bacon)
262    end
263
264    def test_file_descriptor
265      file_descriptor = TestMessage.descriptor.file_descriptor
266      assert nil != file_descriptor
267      assert_equal "tests/basic_test_proto2.proto", file_descriptor.name
268      assert_equal :proto2, file_descriptor.syntax
269
270      file_descriptor = TestEnum.descriptor.file_descriptor
271      assert nil != file_descriptor
272      assert_equal "tests/basic_test_proto2.proto", file_descriptor.name
273      assert_equal :proto2, file_descriptor.syntax
274    end
275  end
276end
277