1 # Protocol Buffers - Google's data interchange format
2 # Copyright 2008 Google Inc.
3 # http://code.google.com/p/protobuf/
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # TODO(robinson): Flesh this out considerably. We focused on reflection_test.py
18 # first, since it's testing the subtler code, and since it provides decent
19 # indirect testing of the protocol compiler output.
21 """Unittest that directly tests the output of the pure-Python protocol
22 compiler. See //net/proto2/internal/reflection_test.py for a test which
23 further ensures that we can use Python protocol message objects as we expect.
26 __author__
= 'robinson@google.com (Will Robinson)'
29 from google
.protobuf
import unittest_mset_pb2
30 from google
.protobuf
import unittest_pb2
33 class GeneratorTest(unittest
.TestCase
):
35 def testNestedMessageDescriptor(self
):
36 field_name
= 'optional_nested_message'
37 proto_type
= unittest_pb2
.TestAllTypes
39 proto_type
.NestedMessage
.DESCRIPTOR
,
40 proto_type
.DESCRIPTOR
.fields_by_name
[field_name
].message_type
)
43 # We test only module-level enums here.
44 # TODO(robinson): Examine descriptors directly to check
45 # enum descriptor output.
46 self
.assertEqual(4, unittest_pb2
.FOREIGN_FOO
)
47 self
.assertEqual(5, unittest_pb2
.FOREIGN_BAR
)
48 self
.assertEqual(6, unittest_pb2
.FOREIGN_BAZ
)
50 proto
= unittest_pb2
.TestAllTypes()
51 self
.assertEqual(1, proto
.FOO
)
52 self
.assertEqual(1, unittest_pb2
.TestAllTypes
.FOO
)
53 self
.assertEqual(2, proto
.BAR
)
54 self
.assertEqual(2, unittest_pb2
.TestAllTypes
.BAR
)
55 self
.assertEqual(3, proto
.BAZ
)
56 self
.assertEqual(3, unittest_pb2
.TestAllTypes
.BAZ
)
58 def testContainingTypeBehaviorForExtensions(self
):
59 self
.assertEqual(unittest_pb2
.optional_int32_extension
.containing_type
,
60 unittest_pb2
.TestAllExtensions
.DESCRIPTOR
)
61 self
.assertEqual(unittest_pb2
.TestRequired
.single
.containing_type
,
62 unittest_pb2
.TestAllExtensions
.DESCRIPTOR
)
64 def testExtensionScope(self
):
65 self
.assertEqual(unittest_pb2
.optional_int32_extension
.extension_scope
,
67 self
.assertEqual(unittest_pb2
.TestRequired
.single
.extension_scope
,
68 unittest_pb2
.TestRequired
.DESCRIPTOR
)
70 def testIsExtension(self
):
71 self
.assertTrue(unittest_pb2
.optional_int32_extension
.is_extension
)
72 self
.assertTrue(unittest_pb2
.TestRequired
.single
.is_extension
)
74 message_descriptor
= unittest_pb2
.TestRequired
.DESCRIPTOR
75 non_extension_descriptor
= message_descriptor
.fields_by_name
['a']
76 self
.assertTrue(not non_extension_descriptor
.is_extension
)
78 def testOptions(self
):
79 proto
= unittest_mset_pb2
.TestMessageSet()
80 self
.assertTrue(proto
.DESCRIPTOR
.GetOptions().message_set_wire_format
)
83 if __name__
== '__main__':