• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Protocol Buffers - Google's data interchange format
2# Copyright 2008 Google Inc.  All rights reserved.
3# https://developers.google.com/protocol-buffers/
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9#     * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11#     * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15#     * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31"""Utilities for Python proto2 tests.
32
33This is intentionally modeled on C++ code in
34//google/protobuf/test_util.*.
35"""
36
37__author__ = 'robinson@google.com (Will Robinson)'
38
39import os.path
40
41import sys
42
43from google.protobuf import unittest_import_pb2
44from google.protobuf import unittest_pb2
45from google.protobuf import descriptor_pb2
46
47# Tests whether the given TestAllTypes message is proto2 or not.
48# This is used to gate several fields/features that only exist
49# for the proto2 version of the message.
50def IsProto2(message):
51  return message.DESCRIPTOR.syntax == "proto2"
52
53def SetAllNonLazyFields(message):
54  """Sets every non-lazy field in the message to a unique value.
55
56  Args:
57    message: A TestAllTypes instance.
58  """
59
60  #
61  # Optional fields.
62  #
63
64  message.optional_int32    = 101
65  message.optional_int64    = 102
66  message.optional_uint32   = 103
67  message.optional_uint64   = 104
68  message.optional_sint32   = 105
69  message.optional_sint64   = 106
70  message.optional_fixed32  = 107
71  message.optional_fixed64  = 108
72  message.optional_sfixed32 = 109
73  message.optional_sfixed64 = 110
74  message.optional_float    = 111
75  message.optional_double   = 112
76  message.optional_bool     = True
77  message.optional_string   = u'115'
78  message.optional_bytes    = b'116'
79
80  if IsProto2(message):
81    message.optionalgroup.a = 117
82  message.optional_nested_message.bb = 118
83  message.optional_foreign_message.c = 119
84  message.optional_import_message.d = 120
85  message.optional_public_import_message.e = 126
86
87  message.optional_nested_enum = unittest_pb2.TestAllTypes.BAZ
88  message.optional_foreign_enum = unittest_pb2.FOREIGN_BAZ
89  if IsProto2(message):
90    message.optional_import_enum = unittest_import_pb2.IMPORT_BAZ
91
92  message.optional_string_piece = u'124'
93  message.optional_cord = u'125'
94
95  #
96  # Repeated fields.
97  #
98
99  message.repeated_int32.append(201)
100  message.repeated_int64.append(202)
101  message.repeated_uint32.append(203)
102  message.repeated_uint64.append(204)
103  message.repeated_sint32.append(205)
104  message.repeated_sint64.append(206)
105  message.repeated_fixed32.append(207)
106  message.repeated_fixed64.append(208)
107  message.repeated_sfixed32.append(209)
108  message.repeated_sfixed64.append(210)
109  message.repeated_float.append(211)
110  message.repeated_double.append(212)
111  message.repeated_bool.append(True)
112  message.repeated_string.append(u'215')
113  message.repeated_bytes.append(b'216')
114
115  if IsProto2(message):
116    message.repeatedgroup.add().a = 217
117  message.repeated_nested_message.add().bb = 218
118  message.repeated_foreign_message.add().c = 219
119  message.repeated_import_message.add().d = 220
120  message.repeated_lazy_message.add().bb = 227
121
122  message.repeated_nested_enum.append(unittest_pb2.TestAllTypes.BAR)
123  message.repeated_foreign_enum.append(unittest_pb2.FOREIGN_BAR)
124  if IsProto2(message):
125    message.repeated_import_enum.append(unittest_import_pb2.IMPORT_BAR)
126
127  message.repeated_string_piece.append(u'224')
128  message.repeated_cord.append(u'225')
129
130  # Add a second one of each field.
131  message.repeated_int32.append(301)
132  message.repeated_int64.append(302)
133  message.repeated_uint32.append(303)
134  message.repeated_uint64.append(304)
135  message.repeated_sint32.append(305)
136  message.repeated_sint64.append(306)
137  message.repeated_fixed32.append(307)
138  message.repeated_fixed64.append(308)
139  message.repeated_sfixed32.append(309)
140  message.repeated_sfixed64.append(310)
141  message.repeated_float.append(311)
142  message.repeated_double.append(312)
143  message.repeated_bool.append(False)
144  message.repeated_string.append(u'315')
145  message.repeated_bytes.append(b'316')
146
147  if IsProto2(message):
148    message.repeatedgroup.add().a = 317
149  message.repeated_nested_message.add().bb = 318
150  message.repeated_foreign_message.add().c = 319
151  message.repeated_import_message.add().d = 320
152  message.repeated_lazy_message.add().bb = 327
153
154  message.repeated_nested_enum.append(unittest_pb2.TestAllTypes.BAZ)
155  message.repeated_foreign_enum.append(unittest_pb2.FOREIGN_BAZ)
156  if IsProto2(message):
157    message.repeated_import_enum.append(unittest_import_pb2.IMPORT_BAZ)
158
159  message.repeated_string_piece.append(u'324')
160  message.repeated_cord.append(u'325')
161
162  #
163  # Fields that have defaults.
164  #
165
166  if IsProto2(message):
167    message.default_int32 = 401
168    message.default_int64 = 402
169    message.default_uint32 = 403
170    message.default_uint64 = 404
171    message.default_sint32 = 405
172    message.default_sint64 = 406
173    message.default_fixed32 = 407
174    message.default_fixed64 = 408
175    message.default_sfixed32 = 409
176    message.default_sfixed64 = 410
177    message.default_float = 411
178    message.default_double = 412
179    message.default_bool = False
180    message.default_string = '415'
181    message.default_bytes = b'416'
182
183    message.default_nested_enum = unittest_pb2.TestAllTypes.FOO
184    message.default_foreign_enum = unittest_pb2.FOREIGN_FOO
185    message.default_import_enum = unittest_import_pb2.IMPORT_FOO
186
187    message.default_string_piece = '424'
188    message.default_cord = '425'
189
190  message.oneof_uint32 = 601
191  message.oneof_nested_message.bb = 602
192  message.oneof_string = '603'
193  message.oneof_bytes = b'604'
194
195
196def SetAllFields(message):
197  SetAllNonLazyFields(message)
198  message.optional_lazy_message.bb = 127
199
200
201def SetAllExtensions(message):
202  """Sets every extension in the message to a unique value.
203
204  Args:
205    message: A unittest_pb2.TestAllExtensions instance.
206  """
207
208  extensions = message.Extensions
209  pb2 = unittest_pb2
210  import_pb2 = unittest_import_pb2
211
212  #
213  # Optional fields.
214  #
215
216  extensions[pb2.optional_int32_extension] = 101
217  extensions[pb2.optional_int64_extension] = 102
218  extensions[pb2.optional_uint32_extension] = 103
219  extensions[pb2.optional_uint64_extension] = 104
220  extensions[pb2.optional_sint32_extension] = 105
221  extensions[pb2.optional_sint64_extension] = 106
222  extensions[pb2.optional_fixed32_extension] = 107
223  extensions[pb2.optional_fixed64_extension] = 108
224  extensions[pb2.optional_sfixed32_extension] = 109
225  extensions[pb2.optional_sfixed64_extension] = 110
226  extensions[pb2.optional_float_extension] = 111
227  extensions[pb2.optional_double_extension] = 112
228  extensions[pb2.optional_bool_extension] = True
229  extensions[pb2.optional_string_extension] = u'115'
230  extensions[pb2.optional_bytes_extension] = b'116'
231
232  extensions[pb2.optionalgroup_extension].a = 117
233  extensions[pb2.optional_nested_message_extension].bb = 118
234  extensions[pb2.optional_foreign_message_extension].c = 119
235  extensions[pb2.optional_import_message_extension].d = 120
236  extensions[pb2.optional_public_import_message_extension].e = 126
237  extensions[pb2.optional_lazy_message_extension].bb = 127
238
239  extensions[pb2.optional_nested_enum_extension] = pb2.TestAllTypes.BAZ
240  extensions[pb2.optional_nested_enum_extension] = pb2.TestAllTypes.BAZ
241  extensions[pb2.optional_foreign_enum_extension] = pb2.FOREIGN_BAZ
242  extensions[pb2.optional_import_enum_extension] = import_pb2.IMPORT_BAZ
243
244  extensions[pb2.optional_string_piece_extension] = u'124'
245  extensions[pb2.optional_cord_extension] = u'125'
246
247  #
248  # Repeated fields.
249  #
250
251  extensions[pb2.repeated_int32_extension].append(201)
252  extensions[pb2.repeated_int64_extension].append(202)
253  extensions[pb2.repeated_uint32_extension].append(203)
254  extensions[pb2.repeated_uint64_extension].append(204)
255  extensions[pb2.repeated_sint32_extension].append(205)
256  extensions[pb2.repeated_sint64_extension].append(206)
257  extensions[pb2.repeated_fixed32_extension].append(207)
258  extensions[pb2.repeated_fixed64_extension].append(208)
259  extensions[pb2.repeated_sfixed32_extension].append(209)
260  extensions[pb2.repeated_sfixed64_extension].append(210)
261  extensions[pb2.repeated_float_extension].append(211)
262  extensions[pb2.repeated_double_extension].append(212)
263  extensions[pb2.repeated_bool_extension].append(True)
264  extensions[pb2.repeated_string_extension].append(u'215')
265  extensions[pb2.repeated_bytes_extension].append(b'216')
266
267  extensions[pb2.repeatedgroup_extension].add().a = 217
268  extensions[pb2.repeated_nested_message_extension].add().bb = 218
269  extensions[pb2.repeated_foreign_message_extension].add().c = 219
270  extensions[pb2.repeated_import_message_extension].add().d = 220
271  extensions[pb2.repeated_lazy_message_extension].add().bb = 227
272
273  extensions[pb2.repeated_nested_enum_extension].append(pb2.TestAllTypes.BAR)
274  extensions[pb2.repeated_foreign_enum_extension].append(pb2.FOREIGN_BAR)
275  extensions[pb2.repeated_import_enum_extension].append(import_pb2.IMPORT_BAR)
276
277  extensions[pb2.repeated_string_piece_extension].append(u'224')
278  extensions[pb2.repeated_cord_extension].append(u'225')
279
280  # Append a second one of each field.
281  extensions[pb2.repeated_int32_extension].append(301)
282  extensions[pb2.repeated_int64_extension].append(302)
283  extensions[pb2.repeated_uint32_extension].append(303)
284  extensions[pb2.repeated_uint64_extension].append(304)
285  extensions[pb2.repeated_sint32_extension].append(305)
286  extensions[pb2.repeated_sint64_extension].append(306)
287  extensions[pb2.repeated_fixed32_extension].append(307)
288  extensions[pb2.repeated_fixed64_extension].append(308)
289  extensions[pb2.repeated_sfixed32_extension].append(309)
290  extensions[pb2.repeated_sfixed64_extension].append(310)
291  extensions[pb2.repeated_float_extension].append(311)
292  extensions[pb2.repeated_double_extension].append(312)
293  extensions[pb2.repeated_bool_extension].append(False)
294  extensions[pb2.repeated_string_extension].append(u'315')
295  extensions[pb2.repeated_bytes_extension].append(b'316')
296
297  extensions[pb2.repeatedgroup_extension].add().a = 317
298  extensions[pb2.repeated_nested_message_extension].add().bb = 318
299  extensions[pb2.repeated_foreign_message_extension].add().c = 319
300  extensions[pb2.repeated_import_message_extension].add().d = 320
301  extensions[pb2.repeated_lazy_message_extension].add().bb = 327
302
303  extensions[pb2.repeated_nested_enum_extension].append(pb2.TestAllTypes.BAZ)
304  extensions[pb2.repeated_foreign_enum_extension].append(pb2.FOREIGN_BAZ)
305  extensions[pb2.repeated_import_enum_extension].append(import_pb2.IMPORT_BAZ)
306
307  extensions[pb2.repeated_string_piece_extension].append(u'324')
308  extensions[pb2.repeated_cord_extension].append(u'325')
309
310  #
311  # Fields with defaults.
312  #
313
314  extensions[pb2.default_int32_extension] = 401
315  extensions[pb2.default_int64_extension] = 402
316  extensions[pb2.default_uint32_extension] = 403
317  extensions[pb2.default_uint64_extension] = 404
318  extensions[pb2.default_sint32_extension] = 405
319  extensions[pb2.default_sint64_extension] = 406
320  extensions[pb2.default_fixed32_extension] = 407
321  extensions[pb2.default_fixed64_extension] = 408
322  extensions[pb2.default_sfixed32_extension] = 409
323  extensions[pb2.default_sfixed64_extension] = 410
324  extensions[pb2.default_float_extension] = 411
325  extensions[pb2.default_double_extension] = 412
326  extensions[pb2.default_bool_extension] = False
327  extensions[pb2.default_string_extension] = u'415'
328  extensions[pb2.default_bytes_extension] = b'416'
329
330  extensions[pb2.default_nested_enum_extension] = pb2.TestAllTypes.FOO
331  extensions[pb2.default_foreign_enum_extension] = pb2.FOREIGN_FOO
332  extensions[pb2.default_import_enum_extension] = import_pb2.IMPORT_FOO
333
334  extensions[pb2.default_string_piece_extension] = u'424'
335  extensions[pb2.default_cord_extension] = '425'
336
337  extensions[pb2.oneof_uint32_extension] = 601
338  extensions[pb2.oneof_nested_message_extension].bb = 602
339  extensions[pb2.oneof_string_extension] = u'603'
340  extensions[pb2.oneof_bytes_extension] = b'604'
341
342
343def SetAllFieldsAndExtensions(message):
344  """Sets every field and extension in the message to a unique value.
345
346  Args:
347    message: A unittest_pb2.TestAllExtensions message.
348  """
349  message.my_int = 1
350  message.my_string = 'foo'
351  message.my_float = 1.0
352  message.Extensions[unittest_pb2.my_extension_int] = 23
353  message.Extensions[unittest_pb2.my_extension_string] = 'bar'
354
355
356def ExpectAllFieldsAndExtensionsInOrder(serialized):
357  """Ensures that serialized is the serialization we expect for a message
358  filled with SetAllFieldsAndExtensions().  (Specifically, ensures that the
359  serialization is in canonical, tag-number order).
360  """
361  my_extension_int = unittest_pb2.my_extension_int
362  my_extension_string = unittest_pb2.my_extension_string
363  expected_strings = []
364  message = unittest_pb2.TestFieldOrderings()
365  message.my_int = 1  # Field 1.
366  expected_strings.append(message.SerializeToString())
367  message.Clear()
368  message.Extensions[my_extension_int] = 23  # Field 5.
369  expected_strings.append(message.SerializeToString())
370  message.Clear()
371  message.my_string = 'foo'  # Field 11.
372  expected_strings.append(message.SerializeToString())
373  message.Clear()
374  message.Extensions[my_extension_string] = 'bar'  # Field 50.
375  expected_strings.append(message.SerializeToString())
376  message.Clear()
377  message.my_float = 1.0
378  expected_strings.append(message.SerializeToString())
379  message.Clear()
380  expected = b''.join(expected_strings)
381
382  if expected != serialized:
383    raise ValueError('Expected %r, found %r' % (expected, serialized))
384
385
386def ExpectAllFieldsSet(test_case, message):
387  """Check all fields for correct values have after Set*Fields() is called."""
388  test_case.assertTrue(message.HasField('optional_int32'))
389  test_case.assertTrue(message.HasField('optional_int64'))
390  test_case.assertTrue(message.HasField('optional_uint32'))
391  test_case.assertTrue(message.HasField('optional_uint64'))
392  test_case.assertTrue(message.HasField('optional_sint32'))
393  test_case.assertTrue(message.HasField('optional_sint64'))
394  test_case.assertTrue(message.HasField('optional_fixed32'))
395  test_case.assertTrue(message.HasField('optional_fixed64'))
396  test_case.assertTrue(message.HasField('optional_sfixed32'))
397  test_case.assertTrue(message.HasField('optional_sfixed64'))
398  test_case.assertTrue(message.HasField('optional_float'))
399  test_case.assertTrue(message.HasField('optional_double'))
400  test_case.assertTrue(message.HasField('optional_bool'))
401  test_case.assertTrue(message.HasField('optional_string'))
402  test_case.assertTrue(message.HasField('optional_bytes'))
403
404  if IsProto2(message):
405    test_case.assertTrue(message.HasField('optionalgroup'))
406  test_case.assertTrue(message.HasField('optional_nested_message'))
407  test_case.assertTrue(message.HasField('optional_foreign_message'))
408  test_case.assertTrue(message.HasField('optional_import_message'))
409
410  test_case.assertTrue(message.optionalgroup.HasField('a'))
411  test_case.assertTrue(message.optional_nested_message.HasField('bb'))
412  test_case.assertTrue(message.optional_foreign_message.HasField('c'))
413  test_case.assertTrue(message.optional_import_message.HasField('d'))
414
415  test_case.assertTrue(message.HasField('optional_nested_enum'))
416  test_case.assertTrue(message.HasField('optional_foreign_enum'))
417  if IsProto2(message):
418    test_case.assertTrue(message.HasField('optional_import_enum'))
419
420  test_case.assertTrue(message.HasField('optional_string_piece'))
421  test_case.assertTrue(message.HasField('optional_cord'))
422
423  test_case.assertEqual(101, message.optional_int32)
424  test_case.assertEqual(102, message.optional_int64)
425  test_case.assertEqual(103, message.optional_uint32)
426  test_case.assertEqual(104, message.optional_uint64)
427  test_case.assertEqual(105, message.optional_sint32)
428  test_case.assertEqual(106, message.optional_sint64)
429  test_case.assertEqual(107, message.optional_fixed32)
430  test_case.assertEqual(108, message.optional_fixed64)
431  test_case.assertEqual(109, message.optional_sfixed32)
432  test_case.assertEqual(110, message.optional_sfixed64)
433  test_case.assertEqual(111, message.optional_float)
434  test_case.assertEqual(112, message.optional_double)
435  test_case.assertEqual(True, message.optional_bool)
436  test_case.assertEqual('115', message.optional_string)
437  test_case.assertEqual(b'116', message.optional_bytes)
438
439  if IsProto2(message):
440    test_case.assertEqual(117, message.optionalgroup.a)
441  test_case.assertEqual(118, message.optional_nested_message.bb)
442  test_case.assertEqual(119, message.optional_foreign_message.c)
443  test_case.assertEqual(120, message.optional_import_message.d)
444  test_case.assertEqual(126, message.optional_public_import_message.e)
445  test_case.assertEqual(127, message.optional_lazy_message.bb)
446
447  test_case.assertEqual(unittest_pb2.TestAllTypes.BAZ,
448                        message.optional_nested_enum)
449  test_case.assertEqual(unittest_pb2.FOREIGN_BAZ,
450                        message.optional_foreign_enum)
451  if IsProto2(message):
452    test_case.assertEqual(unittest_import_pb2.IMPORT_BAZ,
453                          message.optional_import_enum)
454
455  # -----------------------------------------------------------------
456
457  test_case.assertEqual(2, len(message.repeated_int32))
458  test_case.assertEqual(2, len(message.repeated_int64))
459  test_case.assertEqual(2, len(message.repeated_uint32))
460  test_case.assertEqual(2, len(message.repeated_uint64))
461  test_case.assertEqual(2, len(message.repeated_sint32))
462  test_case.assertEqual(2, len(message.repeated_sint64))
463  test_case.assertEqual(2, len(message.repeated_fixed32))
464  test_case.assertEqual(2, len(message.repeated_fixed64))
465  test_case.assertEqual(2, len(message.repeated_sfixed32))
466  test_case.assertEqual(2, len(message.repeated_sfixed64))
467  test_case.assertEqual(2, len(message.repeated_float))
468  test_case.assertEqual(2, len(message.repeated_double))
469  test_case.assertEqual(2, len(message.repeated_bool))
470  test_case.assertEqual(2, len(message.repeated_string))
471  test_case.assertEqual(2, len(message.repeated_bytes))
472
473  if IsProto2(message):
474    test_case.assertEqual(2, len(message.repeatedgroup))
475  test_case.assertEqual(2, len(message.repeated_nested_message))
476  test_case.assertEqual(2, len(message.repeated_foreign_message))
477  test_case.assertEqual(2, len(message.repeated_import_message))
478  test_case.assertEqual(2, len(message.repeated_nested_enum))
479  test_case.assertEqual(2, len(message.repeated_foreign_enum))
480  if IsProto2(message):
481    test_case.assertEqual(2, len(message.repeated_import_enum))
482
483  test_case.assertEqual(2, len(message.repeated_string_piece))
484  test_case.assertEqual(2, len(message.repeated_cord))
485
486  test_case.assertEqual(201, message.repeated_int32[0])
487  test_case.assertEqual(202, message.repeated_int64[0])
488  test_case.assertEqual(203, message.repeated_uint32[0])
489  test_case.assertEqual(204, message.repeated_uint64[0])
490  test_case.assertEqual(205, message.repeated_sint32[0])
491  test_case.assertEqual(206, message.repeated_sint64[0])
492  test_case.assertEqual(207, message.repeated_fixed32[0])
493  test_case.assertEqual(208, message.repeated_fixed64[0])
494  test_case.assertEqual(209, message.repeated_sfixed32[0])
495  test_case.assertEqual(210, message.repeated_sfixed64[0])
496  test_case.assertEqual(211, message.repeated_float[0])
497  test_case.assertEqual(212, message.repeated_double[0])
498  test_case.assertEqual(True, message.repeated_bool[0])
499  test_case.assertEqual('215', message.repeated_string[0])
500  test_case.assertEqual(b'216', message.repeated_bytes[0])
501
502  if IsProto2(message):
503    test_case.assertEqual(217, message.repeatedgroup[0].a)
504  test_case.assertEqual(218, message.repeated_nested_message[0].bb)
505  test_case.assertEqual(219, message.repeated_foreign_message[0].c)
506  test_case.assertEqual(220, message.repeated_import_message[0].d)
507  test_case.assertEqual(227, message.repeated_lazy_message[0].bb)
508
509  test_case.assertEqual(unittest_pb2.TestAllTypes.BAR,
510                        message.repeated_nested_enum[0])
511  test_case.assertEqual(unittest_pb2.FOREIGN_BAR,
512                        message.repeated_foreign_enum[0])
513  if IsProto2(message):
514    test_case.assertEqual(unittest_import_pb2.IMPORT_BAR,
515                          message.repeated_import_enum[0])
516
517  test_case.assertEqual(301, message.repeated_int32[1])
518  test_case.assertEqual(302, message.repeated_int64[1])
519  test_case.assertEqual(303, message.repeated_uint32[1])
520  test_case.assertEqual(304, message.repeated_uint64[1])
521  test_case.assertEqual(305, message.repeated_sint32[1])
522  test_case.assertEqual(306, message.repeated_sint64[1])
523  test_case.assertEqual(307, message.repeated_fixed32[1])
524  test_case.assertEqual(308, message.repeated_fixed64[1])
525  test_case.assertEqual(309, message.repeated_sfixed32[1])
526  test_case.assertEqual(310, message.repeated_sfixed64[1])
527  test_case.assertEqual(311, message.repeated_float[1])
528  test_case.assertEqual(312, message.repeated_double[1])
529  test_case.assertEqual(False, message.repeated_bool[1])
530  test_case.assertEqual('315', message.repeated_string[1])
531  test_case.assertEqual(b'316', message.repeated_bytes[1])
532
533  if IsProto2(message):
534    test_case.assertEqual(317, message.repeatedgroup[1].a)
535  test_case.assertEqual(318, message.repeated_nested_message[1].bb)
536  test_case.assertEqual(319, message.repeated_foreign_message[1].c)
537  test_case.assertEqual(320, message.repeated_import_message[1].d)
538  test_case.assertEqual(327, message.repeated_lazy_message[1].bb)
539
540  test_case.assertEqual(unittest_pb2.TestAllTypes.BAZ,
541                        message.repeated_nested_enum[1])
542  test_case.assertEqual(unittest_pb2.FOREIGN_BAZ,
543                        message.repeated_foreign_enum[1])
544  if IsProto2(message):
545    test_case.assertEqual(unittest_import_pb2.IMPORT_BAZ,
546                          message.repeated_import_enum[1])
547
548  # -----------------------------------------------------------------
549
550  if IsProto2(message):
551    test_case.assertTrue(message.HasField('default_int32'))
552    test_case.assertTrue(message.HasField('default_int64'))
553    test_case.assertTrue(message.HasField('default_uint32'))
554    test_case.assertTrue(message.HasField('default_uint64'))
555    test_case.assertTrue(message.HasField('default_sint32'))
556    test_case.assertTrue(message.HasField('default_sint64'))
557    test_case.assertTrue(message.HasField('default_fixed32'))
558    test_case.assertTrue(message.HasField('default_fixed64'))
559    test_case.assertTrue(message.HasField('default_sfixed32'))
560    test_case.assertTrue(message.HasField('default_sfixed64'))
561    test_case.assertTrue(message.HasField('default_float'))
562    test_case.assertTrue(message.HasField('default_double'))
563    test_case.assertTrue(message.HasField('default_bool'))
564    test_case.assertTrue(message.HasField('default_string'))
565    test_case.assertTrue(message.HasField('default_bytes'))
566
567    test_case.assertTrue(message.HasField('default_nested_enum'))
568    test_case.assertTrue(message.HasField('default_foreign_enum'))
569    test_case.assertTrue(message.HasField('default_import_enum'))
570
571    test_case.assertEqual(401, message.default_int32)
572    test_case.assertEqual(402, message.default_int64)
573    test_case.assertEqual(403, message.default_uint32)
574    test_case.assertEqual(404, message.default_uint64)
575    test_case.assertEqual(405, message.default_sint32)
576    test_case.assertEqual(406, message.default_sint64)
577    test_case.assertEqual(407, message.default_fixed32)
578    test_case.assertEqual(408, message.default_fixed64)
579    test_case.assertEqual(409, message.default_sfixed32)
580    test_case.assertEqual(410, message.default_sfixed64)
581    test_case.assertEqual(411, message.default_float)
582    test_case.assertEqual(412, message.default_double)
583    test_case.assertEqual(False, message.default_bool)
584    test_case.assertEqual('415', message.default_string)
585    test_case.assertEqual(b'416', message.default_bytes)
586
587    test_case.assertEqual(unittest_pb2.TestAllTypes.FOO,
588                          message.default_nested_enum)
589    test_case.assertEqual(unittest_pb2.FOREIGN_FOO,
590                          message.default_foreign_enum)
591    test_case.assertEqual(unittest_import_pb2.IMPORT_FOO,
592                          message.default_import_enum)
593
594
595def GoldenFile(filename):
596  """Finds the given golden file and returns a file object representing it."""
597
598  # Search up the directory tree looking for the C++ protobuf source code.
599  path = '.'
600  while os.path.exists(path):
601    if os.path.exists(os.path.join(path, 'src/google/protobuf')):
602      # Found it.  Load the golden file from the testdata directory.
603      full_path = os.path.join(path, 'src/google/protobuf/testdata', filename)
604      return open(full_path, 'rb')
605    path = os.path.join(path, '..')
606
607  # Search internally.
608  path = '.'
609  full_path = os.path.join(path, 'third_party/py/google/protobuf/testdata',
610                           filename)
611  if os.path.exists(full_path):
612    # Found it.  Load the golden file from the testdata directory.
613    return open(full_path, 'rb')
614
615  raise RuntimeError(
616      'Could not find golden files.  This test must be run from within the '
617      'protobuf source package so that it can read test data files from the '
618      'C++ source tree.')
619
620
621def GoldenFileData(filename):
622  """Finds the given golden file and returns its contents."""
623  with GoldenFile(filename) as f:
624    return f.read()
625
626
627def SetAllPackedFields(message):
628  """Sets every field in the message to a unique value.
629
630  Args:
631    message: A TestPackedTypes instance.
632  """
633  message.packed_int32.extend([601, 701])
634  message.packed_int64.extend([602, 702])
635  message.packed_uint32.extend([603, 703])
636  message.packed_uint64.extend([604, 704])
637  message.packed_sint32.extend([605, 705])
638  message.packed_sint64.extend([606, 706])
639  message.packed_fixed32.extend([607, 707])
640  message.packed_fixed64.extend([608, 708])
641  message.packed_sfixed32.extend([609, 709])
642  message.packed_sfixed64.extend([610, 710])
643  message.packed_float.extend([611.0, 711.0])
644  message.packed_double.extend([612.0, 712.0])
645  message.packed_bool.extend([True, False])
646  message.packed_enum.extend([unittest_pb2.FOREIGN_BAR,
647                              unittest_pb2.FOREIGN_BAZ])
648
649
650def SetAllPackedExtensions(message):
651  """Sets every extension in the message to a unique value.
652
653  Args:
654    message: A unittest_pb2.TestPackedExtensions instance.
655  """
656  extensions = message.Extensions
657  pb2 = unittest_pb2
658
659  extensions[pb2.packed_int32_extension].extend([601, 701])
660  extensions[pb2.packed_int64_extension].extend([602, 702])
661  extensions[pb2.packed_uint32_extension].extend([603, 703])
662  extensions[pb2.packed_uint64_extension].extend([604, 704])
663  extensions[pb2.packed_sint32_extension].extend([605, 705])
664  extensions[pb2.packed_sint64_extension].extend([606, 706])
665  extensions[pb2.packed_fixed32_extension].extend([607, 707])
666  extensions[pb2.packed_fixed64_extension].extend([608, 708])
667  extensions[pb2.packed_sfixed32_extension].extend([609, 709])
668  extensions[pb2.packed_sfixed64_extension].extend([610, 710])
669  extensions[pb2.packed_float_extension].extend([611.0, 711.0])
670  extensions[pb2.packed_double_extension].extend([612.0, 712.0])
671  extensions[pb2.packed_bool_extension].extend([True, False])
672  extensions[pb2.packed_enum_extension].extend([unittest_pb2.FOREIGN_BAR,
673                                                unittest_pb2.FOREIGN_BAZ])
674
675
676def SetAllUnpackedFields(message):
677  """Sets every field in the message to a unique value.
678
679  Args:
680    message: A unittest_pb2.TestUnpackedTypes instance.
681  """
682  message.unpacked_int32.extend([601, 701])
683  message.unpacked_int64.extend([602, 702])
684  message.unpacked_uint32.extend([603, 703])
685  message.unpacked_uint64.extend([604, 704])
686  message.unpacked_sint32.extend([605, 705])
687  message.unpacked_sint64.extend([606, 706])
688  message.unpacked_fixed32.extend([607, 707])
689  message.unpacked_fixed64.extend([608, 708])
690  message.unpacked_sfixed32.extend([609, 709])
691  message.unpacked_sfixed64.extend([610, 710])
692  message.unpacked_float.extend([611.0, 711.0])
693  message.unpacked_double.extend([612.0, 712.0])
694  message.unpacked_bool.extend([True, False])
695  message.unpacked_enum.extend([unittest_pb2.FOREIGN_BAR,
696                                unittest_pb2.FOREIGN_BAZ])
697