1 // Copyright 2014 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chromeos-dbus-bindings/name_parser.h"
6
7 #include <map>
8 #include <string>
9
10 #include <gmock/gmock.h>
11 #include <gtest/gtest.h>
12
13 #include "chromeos-dbus-bindings/indented_text.h"
14
15 namespace chromeos_dbus_bindings {
16
TEST(NameParser,Parsing_Empty)17 TEST(NameParser, Parsing_Empty) {
18 EXPECT_DEATH(NameParser{""}, "Empty name specified");
19 }
20
TEST(NameParser,Parsing_NoNamespaces)21 TEST(NameParser, Parsing_NoNamespaces) {
22 NameParser parser{"foo"};
23 EXPECT_EQ("foo", parser.type_name);
24 EXPECT_TRUE(parser.namespaces.empty());
25 }
26
TEST(NameParser,Parsing_FullyQualified)27 TEST(NameParser, Parsing_FullyQualified) {
28 NameParser parser{"foo.bar.FooBar"};
29 EXPECT_EQ("FooBar", parser.type_name);
30 EXPECT_THAT(parser.namespaces, testing::ElementsAre("foo", "bar"));
31 }
32
TEST(NameParser,MakeFullCppName)33 TEST(NameParser, MakeFullCppName) {
34 NameParser parser{"foo.bar.FooBar"};
35 EXPECT_EQ("foo::bar::FooBar", parser.MakeFullCppName());
36 }
37
TEST(NameParser,MakeVariableName)38 TEST(NameParser, MakeVariableName) {
39 NameParser parser{"foo.bar.FooBar"};
40 EXPECT_EQ("foo_bar", parser.MakeVariableName());
41 }
42
TEST(NameParser,MakeVariableName_NoCapitals)43 TEST(NameParser, MakeVariableName_NoCapitals) {
44 NameParser parser{"foo"};
45 EXPECT_EQ("foo", parser.MakeVariableName());
46 }
47
TEST(NameParser,MakeVariableName_NoInitialCapital)48 TEST(NameParser, MakeVariableName_NoInitialCapital) {
49 NameParser parser{"fooBarBaz"};
50 EXPECT_EQ("foo_bar_baz", parser.MakeVariableName());
51 }
52
TEST(NameParser,MakeVariableName_AllCapitals)53 TEST(NameParser, MakeVariableName_AllCapitals) {
54 NameParser parser{"UUID"};
55 EXPECT_EQ("uuid", parser.MakeVariableName());
56 }
57
TEST(NameParser,MakeVariableName_MixedCapital)58 TEST(NameParser, MakeVariableName_MixedCapital) {
59 NameParser parser{"FOObarBaz"};
60 EXPECT_EQ("foobar_baz", parser.MakeVariableName());
61 }
62
TEST(NameParser,MakeInterfaceName)63 TEST(NameParser, MakeInterfaceName) {
64 NameParser parser{"foo.bar.FooBar"};
65 EXPECT_EQ("FooBarInterface", parser.MakeInterfaceName(false));
66 EXPECT_EQ("foo::bar::FooBarInterface", parser.MakeInterfaceName(true));
67 }
68
TEST(NameParser,MakeProxyName)69 TEST(NameParser, MakeProxyName) {
70 NameParser parser{"foo.bar.FooBar"};
71 EXPECT_EQ("FooBarProxy", parser.MakeProxyName(false));
72 EXPECT_EQ("foo::bar::FooBarProxy", parser.MakeProxyName(true));
73 }
74
TEST(NameParser,MakeAdaptorName)75 TEST(NameParser, MakeAdaptorName) {
76 NameParser parser{"foo.bar.FooBar"};
77 EXPECT_EQ("FooBarAdaptor", parser.MakeAdaptorName(false));
78 EXPECT_EQ("foo::bar::FooBarAdaptor", parser.MakeAdaptorName(true));
79 }
80
TEST(NameParser,AddOpenNamespaces)81 TEST(NameParser, AddOpenNamespaces) {
82 std::string expected =
83 "namespace foo {\n"
84 "namespace bar {\n";
85 NameParser parser{"foo.bar.FooBar"};
86 IndentedText text;
87 parser.AddOpenNamespaces(&text, false);
88 EXPECT_EQ(expected, text.GetContents());
89 }
90
TEST(NameParser,AddOpenNamespaces_WithMainType)91 TEST(NameParser, AddOpenNamespaces_WithMainType) {
92 std::string expected =
93 "namespace foo {\n"
94 "namespace bar {\n"
95 "namespace FooBar {\n";
96 NameParser parser{"foo.bar.FooBar"};
97 IndentedText text;
98 parser.AddOpenNamespaces(&text, true);
99 EXPECT_EQ(expected, text.GetContents());
100 }
101
TEST(NameParser,AddCloseNamespaces)102 TEST(NameParser, AddCloseNamespaces) {
103 std::string expected =
104 "} // namespace bar\n"
105 "} // namespace foo\n";
106 NameParser parser{"foo.bar.FooBar"};
107 IndentedText text;
108 parser.AddCloseNamespaces(&text, false);
109 EXPECT_EQ(expected, text.GetContents());
110 }
111
TEST(NameParser,AddCloseNamespaces_WithMainType)112 TEST(NameParser, AddCloseNamespaces_WithMainType) {
113 std::string expected =
114 "} // namespace FooBar\n"
115 "} // namespace bar\n"
116 "} // namespace foo\n";
117 NameParser parser{"foo.bar.FooBar"};
118 IndentedText text;
119 parser.AddCloseNamespaces(&text, true);
120 EXPECT_EQ(expected, text.GetContents());
121 }
122
123 } // namespace chromeos_dbus_bindings
124