• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 // Author: kenton@google.com (Kenton Varda)
9 //  Based on original Protocol Buffers design by
10 //  Sanjay Ghemawat, Jeff Dean, and others.
11 //
12 // To test the code generator, we actually use it to generate code for
13 // google/protobuf/unittest.proto, then test that.  This means that we
14 // are actually testing the parser and other parts of the system at the same
15 // time, and that problems in the generator may show up as compile-time errors
16 // rather than unittest failures, which may be surprising.  However, testing
17 // the output of the C++ generator directly would be very hard.  We can't very
18 // well just check it against golden files since those files would have to be
19 // updated for any small change; such a test would be very brittle and probably
20 // not very helpful.  What we really want to test is that the code compiles
21 // correctly and produces the interfaces we expect, which is why this test
22 // is written this way.
23 
24 #include "google/protobuf/compiler/cpp/unittest.h"
25 
26 #include "google/protobuf/test_util.h"
27 #include "google/protobuf/unittest.pb.h"
28 #include "google/protobuf/unittest_embed_optimize_for.pb.h"
29 #include "google/protobuf/unittest_optimize_for.pb.h"
30 
31 #define MESSAGE_TEST_NAME MessageTest
32 #define GENERATED_DESCRIPTOR_TEST_NAME GeneratedDescriptorTest
33 #define GENERATED_MESSAGE_TEST_NAME GeneratedMessageTest
34 #define GENERATED_ENUM_TEST_NAME GeneratedEnumTest
35 #define GENERATED_SERVICE_TEST_NAME GeneratedServiceTest
36 #define HELPERS_TEST_NAME HelpersTest
37 #define DESCRIPTOR_INIT_TEST_NAME DescriptorInitializationTest
38 
39 #define UNITTEST_PROTO_PATH "google/protobuf/unittest.proto"
40 #define UNITTEST ::protobuf_unittest
41 #define UNITTEST_IMPORT ::protobuf_unittest_import
42 
43 // Must include after the above macros.
44 #include "google/protobuf/compiler/cpp/unittest.inc"
45 
46 namespace google {
47 namespace protobuf {
48 namespace compiler {
49 namespace cpp {
50 
51 // Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
52 namespace cpp_unittest {
53 
54 namespace protobuf_unittest = ::protobuf_unittest;
55 
TEST(GENERATED_MESSAGE_TEST_NAME,TestConflictingSymbolNames)56 TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingSymbolNames) {
57   // test_bad_identifiers.proto successfully compiled, then it works.  The
58   // following is just a token usage to insure that the code is, in fact,
59   // being compiled and linked.
60 
61   protobuf_unittest::TestConflictingSymbolNames message;
62   message.set_uint32(1);
63   EXPECT_EQ(3, message.ByteSizeLong());
64 
65   message.set_friend_(5);
66   EXPECT_EQ(5, message.friend_());
67 
68   message.set_class_(6);
69   EXPECT_EQ(6, message.class_());
70 
71   // Instantiate extension template functions to test conflicting template
72   // parameter names.
73   typedef protobuf_unittest::TestConflictingSymbolNamesExtension ExtensionMessage;
74   message.AddExtension(ExtensionMessage::repeated_int32_ext, 123);
75   EXPECT_EQ(123, message.GetExtension(ExtensionMessage::repeated_int32_ext, 0));
76 }
77 
TEST(GENERATED_MESSAGE_TEST_NAME,TestConflictingEnumNames)78 TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingEnumNames) {
79   protobuf_unittest::TestConflictingEnumNames message;
80   message.set_conflicting_enum(
81       protobuf_unittest::TestConflictingEnumNames_while_and_);
82   EXPECT_EQ(1, message.conflicting_enum());
83   message.set_conflicting_enum(
84       protobuf_unittest::TestConflictingEnumNames_while_XOR);
85   EXPECT_EQ(5, message.conflicting_enum());
86 
87   protobuf_unittest::bool_ conflicting_enum;
88   conflicting_enum = protobuf_unittest::NOT_EQ;
89   EXPECT_EQ(1, conflicting_enum);
90   conflicting_enum = protobuf_unittest::return_;
91   EXPECT_EQ(3, conflicting_enum);
92 }
93 
TEST(GENERATED_MESSAGE_TEST_NAME,TestConflictingMessageNames)94 TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingMessageNames) {
95   protobuf_unittest::NULL_ message;
96   message.set_int_(123);
97   EXPECT_EQ(message.int_(), 123);
98 }
99 
TEST(GENERATED_MESSAGE_TEST_NAME,TestConflictingExtension)100 TEST(GENERATED_MESSAGE_TEST_NAME, TestConflictingExtension) {
101   protobuf_unittest::TestConflictingSymbolNames message;
102   message.SetExtension(protobuf_unittest::void_, 123);
103   EXPECT_EQ(123, message.GetExtension(protobuf_unittest::void_));
104 }
105 
106 }  // namespace cpp_unittest
107 }  // namespace cpp
108 }  // namespace compiler
109 }  // namespace protobuf
110 }  // namespace google
111