1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2014 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 #include <memory>
9
10 #include "google/protobuf/any.pb.h"
11 #include <gtest/gtest.h>
12 #include "google/protobuf/compiler/command_line_interface.h"
13 #include "google/protobuf/compiler/csharp/csharp_helpers.h"
14 #include "google/protobuf/descriptor.pb.h"
15 #include "google/protobuf/io/printer.h"
16 #include "google/protobuf/io/zero_copy_stream.h"
17
18 namespace google {
19 namespace protobuf {
20 namespace compiler {
21 namespace csharp {
22 namespace {
23
TEST(CSharpEnumValue,PascalCasedPrefixStripping)24 TEST(CSharpEnumValue, PascalCasedPrefixStripping) {
25 EXPECT_EQ("Bar", GetEnumValueName("Foo", "BAR"));
26 EXPECT_EQ("BarBaz", GetEnumValueName("Foo", "BAR_BAZ"));
27 EXPECT_EQ("Bar", GetEnumValueName("Foo", "FOO_BAR"));
28 EXPECT_EQ("Bar", GetEnumValueName("Foo", "FOO__BAR"));
29 EXPECT_EQ("BarBaz", GetEnumValueName("Foo", "FOO_BAR_BAZ"));
30 EXPECT_EQ("BarBaz", GetEnumValueName("Foo", "Foo_BarBaz"));
31 EXPECT_EQ("Bar", GetEnumValueName("FO_O", "FOO_BAR"));
32 EXPECT_EQ("Bar", GetEnumValueName("FOO", "F_O_O_BAR"));
33 EXPECT_EQ("Bar", GetEnumValueName("Foo", "BAR"));
34 EXPECT_EQ("BarBaz", GetEnumValueName("Foo", "BAR_BAZ"));
35 EXPECT_EQ("Foo", GetEnumValueName("Foo", "FOO"));
36 EXPECT_EQ("Foo", GetEnumValueName("Foo", "FOO___"));
37 // Identifiers can't start with digits
38 EXPECT_EQ("_2Bar", GetEnumValueName("Foo", "FOO_2_BAR"));
39 EXPECT_EQ("_2", GetEnumValueName("Foo", "FOO___2"));
40 }
41
TEST(DescriptorProtoHelpers,IsDescriptorProto)42 TEST(DescriptorProtoHelpers, IsDescriptorProto) {
43 EXPECT_TRUE(IsDescriptorProto(DescriptorProto::descriptor()->file()));
44 EXPECT_FALSE(IsDescriptorProto(google::protobuf::Any::descriptor()->file()));
45 }
46
TEST(DescriptorProtoHelpers,IsDescriptorOptionMessage)47 TEST(DescriptorProtoHelpers, IsDescriptorOptionMessage) {
48 EXPECT_TRUE(IsDescriptorOptionMessage(FileOptions::descriptor()));
49 EXPECT_FALSE(IsDescriptorOptionMessage(google::protobuf::Any::descriptor()));
50 EXPECT_FALSE(IsDescriptorOptionMessage(DescriptorProto::descriptor()));
51 }
52
TEST(CSharpIdentifiers,UnderscoresToCamelCase)53 TEST(CSharpIdentifiers, UnderscoresToCamelCase) {
54 EXPECT_EQ("FooBar", UnderscoresToCamelCase("Foo_Bar", true));
55 EXPECT_EQ("fooBar", UnderscoresToCamelCase("FooBar", false));
56 EXPECT_EQ("foo123", UnderscoresToCamelCase("foo_123", false));
57 // remove leading underscores
58 EXPECT_EQ("Foo123", UnderscoresToCamelCase("_Foo_123", true));
59 // this one has slight unexpected output as it capitalises the first
60 // letter after consuming the underscores, but this was the existing
61 // behaviour so I have not changed it
62 EXPECT_EQ("FooBar", UnderscoresToCamelCase("___fooBar", false));
63 // leave a leading underscore for identifiers that would otherwise
64 // be invalid because they would start with a digit
65 EXPECT_EQ("_123Foo", UnderscoresToCamelCase("_123_foo", true));
66 EXPECT_EQ("_123Foo", UnderscoresToCamelCase("___123_foo", true));
67 }
68
69 } // namespace
70 } // namespace csharp
71 } // namespace compiler
72 } // namespace protobuf
73 } // namespace google
74