• 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/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 #include <google/protobuf/test_util.h>
53 
54 #define MESSAGE_TEST_NAME MessageTest
55 #define GENERATED_DESCRIPTOR_TEST_NAME GeneratedDescriptorTest
56 #define GENERATED_MESSAGE_TEST_NAME GeneratedMessageTest
57 #define GENERATED_ENUM_TEST_NAME GeneratedEnumTest
58 #define GENERATED_SERVICE_TEST_NAME GeneratedServiceTest
59 #define HELPERS_TEST_NAME HelpersTest
60 #define DESCRIPTOR_INIT_TEST_NAME DescriptorInitializationTest
61 
62 #define UNITTEST_PROTO_PATH "net/proto2/internal/unittest.proto"
63 #define UNITTEST ::protobuf_unittest
64 #define UNITTEST_IMPORT ::protobuf_unittest_import
65 
66 // Must include after the above macros.
67 #include <google/protobuf/compiler/cpp/unittest.inc>
68 
69 namespace google {
70 namespace protobuf {
71 namespace compiler {
72 namespace cpp {
73 
74 // Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
75 namespace cpp_unittest {
76 
77 namespace protobuf_unittest = ::protobuf_unittest;
78 
TEST(GENERATED_MESSAGE_TEST_NAME,TestConflictingSymbolNames)79 TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingSymbolNames) {
80   // test_bad_identifiers.proto successfully compiled, then it works.  The
81   // following is just a token usage to insure that the code is, in fact,
82   // being compiled and linked.
83 
84   protobuf_unittest::TestConflictingSymbolNames message;
85   message.set_uint32(1);
86   EXPECT_EQ(3, message.ByteSizeLong());
87 
88   message.set_friend_(5);
89   EXPECT_EQ(5, message.friend_());
90 
91   message.set_class_(6);
92   EXPECT_EQ(6, message.class_());
93 
94   // Instantiate extension template functions to test conflicting template
95   // parameter names.
96   typedef protobuf_unittest::TestConflictingSymbolNamesExtension ExtensionMessage;
97   message.AddExtension(ExtensionMessage::repeated_int32_ext, 123);
98   EXPECT_EQ(123, message.GetExtension(ExtensionMessage::repeated_int32_ext, 0));
99 }
100 
TEST(GENERATED_MESSAGE_TEST_NAME,TestConflictingEnumNames)101 TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingEnumNames) {
102   protobuf_unittest::TestConflictingEnumNames message;
103   message.set_conflicting_enum(
104       protobuf_unittest::TestConflictingEnumNames_while_and_);
105   EXPECT_EQ(1, message.conflicting_enum());
106   message.set_conflicting_enum(
107       protobuf_unittest::TestConflictingEnumNames_while_XOR);
108   EXPECT_EQ(5, message.conflicting_enum());
109 
110   protobuf_unittest::bool_ conflicting_enum;
111   conflicting_enum = protobuf_unittest::NOT_EQ;
112   EXPECT_EQ(1, conflicting_enum);
113   conflicting_enum = protobuf_unittest::return_;
114   EXPECT_EQ(3, conflicting_enum);
115 }
116 
TEST(GENERATED_MESSAGE_TEST_NAME,TestConflictingMessageNames)117 TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingMessageNames) {
118   protobuf_unittest::NULL_ message;
119   message.set_int_(123);
120   EXPECT_EQ(message.int_(), 123);
121 }
122 
TEST(GENERATED_MESSAGE_TEST_NAME,TestConflictingExtension)123 TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingExtension) {
124   protobuf_unittest::TestConflictingSymbolNames message;
125   message.SetExtension(protobuf_unittest::void_, 123);
126   EXPECT_EQ(123, message.GetExtension(protobuf_unittest::void_));
127 }
128 
129 }  // namespace cpp_unittest
130 }  // namespace cpp
131 }  // namespace compiler
132 }  // namespace protobuf
133 }  // namespace google
134