• 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 // Author: kenton@google.com (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 //
35 // To test the code generator, we actually use it to generate code for
36 // net/proto2/internal/unittest.proto, then test that.  This means that we
37 // are actually testing the parser and other parts of the system at the same
38 // time, and that problems in the generator may show up as compile-time errors
39 // rather than unittest failures, which may be surprising.  However, testing
40 // the output of the C++ generator directly would be very hard.  We can't very
41 // well just check it against golden files since those files would have to be
42 // updated for any small change; such a test would be very brittle and probably
43 // not very helpful.  What we really want to test is that the code compiles
44 // correctly and produces the interfaces we expect, which is why this test
45 // is written this way.
46 
47 #include <google/protobuf/compiler/cpp/cpp_unittest.h>
48 
49 #include <google/protobuf/unittest.pb.h>
50 #include <google/protobuf/unittest_embed_optimize_for.pb.h>
51 #include <google/protobuf/unittest_optimize_for.pb.h>
52 
53 #include <google/protobuf/test_util.h>
54 
55 #define MESSAGE_TEST_NAME MessageTest
56 #define GENERATED_DESCRIPTOR_TEST_NAME GeneratedDescriptorTest
57 #define GENERATED_MESSAGE_TEST_NAME GeneratedMessageTest
58 #define GENERATED_ENUM_TEST_NAME GeneratedEnumTest
59 #define GENERATED_SERVICE_TEST_NAME GeneratedServiceTest
60 #define HELPERS_TEST_NAME HelpersTest
61 #define DESCRIPTOR_INIT_TEST_NAME DescriptorInitializationTest
62 
63 #define UNITTEST_PROTO_PATH "net/proto2/internal/unittest.proto"
64 #define UNITTEST ::protobuf_unittest
65 #define UNITTEST_IMPORT ::protobuf_unittest_import
66 
67 // Must include after the above macros.
68 #include <google/protobuf/compiler/cpp/cpp_unittest.inc>
69 
70 namespace google {
71 namespace protobuf {
72 namespace compiler {
73 namespace cpp {
74 
75 // Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
76 namespace cpp_unittest {
77 
78 namespace protobuf_unittest = ::protobuf_unittest;
79 
TEST(GENERATED_MESSAGE_TEST_NAME,TestConflictingSymbolNames)80 TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingSymbolNames) {
81   // test_bad_identifiers.proto successfully compiled, then it works.  The
82   // following is just a token usage to insure that the code is, in fact,
83   // being compiled and linked.
84 
85   protobuf_unittest::TestConflictingSymbolNames message;
86   message.set_uint32(1);
87   EXPECT_EQ(3, message.ByteSizeLong());
88 
89   message.set_friend_(5);
90   EXPECT_EQ(5, message.friend_());
91 
92   message.set_class_(6);
93   EXPECT_EQ(6, message.class_());
94 
95   // Instantiate extension template functions to test conflicting template
96   // parameter names.
97   typedef protobuf_unittest::TestConflictingSymbolNamesExtension ExtensionMessage;
98   message.AddExtension(ExtensionMessage::repeated_int32_ext, 123);
99   EXPECT_EQ(123, message.GetExtension(ExtensionMessage::repeated_int32_ext, 0));
100 }
101 
TEST(GENERATED_MESSAGE_TEST_NAME,TestConflictingEnumNames)102 TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingEnumNames) {
103   protobuf_unittest::TestConflictingEnumNames message;
104   message.set_conflicting_enum(
105       protobuf_unittest::TestConflictingEnumNames_while_and_);
106   EXPECT_EQ(1, message.conflicting_enum());
107   message.set_conflicting_enum(
108       protobuf_unittest::TestConflictingEnumNames_while_XOR);
109   EXPECT_EQ(5, message.conflicting_enum());
110 
111   protobuf_unittest::bool_ conflicting_enum;
112   conflicting_enum = protobuf_unittest::NOT_EQ;
113   EXPECT_EQ(1, conflicting_enum);
114   conflicting_enum = protobuf_unittest::return_;
115   EXPECT_EQ(3, conflicting_enum);
116 }
117 
TEST(GENERATED_MESSAGE_TEST_NAME,TestConflictingMessageNames)118 TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingMessageNames) {
119   protobuf_unittest::NULL_ message;
120   message.set_int_(123);
121   EXPECT_EQ(message.int_(), 123);
122 }
123 
TEST(GENERATED_MESSAGE_TEST_NAME,TestConflictingExtension)124 TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingExtension) {
125   protobuf_unittest::TestConflictingSymbolNames message;
126   message.SetExtension(protobuf_unittest::void_, 123);
127   EXPECT_EQ(123, message.GetExtension(protobuf_unittest::void_));
128 }
129 
130 }  // namespace cpp_unittest
131 }  // namespace cpp
132 }  // namespace compiler
133 }  // namespace protobuf
134 }  // namespace google
135