summaryrefslogtreecommitdiff
path: root/chromeos-dbus-bindings/name_parser_unittest.cc
blob: 25c6c92cb76308a804bf2f3413a0f8a0033116cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos-dbus-bindings/name_parser.h"

#include <map>
#include <string>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "chromeos-dbus-bindings/indented_text.h"

namespace chromeos_dbus_bindings {

TEST(NameParser, Parsing_Empty) {
  EXPECT_DEATH(NameParser{""}, "Empty name specified");
}

TEST(NameParser, Parsing_NoNamespaces) {
  NameParser parser{"foo"};
  EXPECT_EQ("foo", parser.type_name);
  EXPECT_TRUE(parser.namespaces.empty());
}

TEST(NameParser, Parsing_FullyQualified) {
  NameParser parser{"foo.bar.FooBar"};
  EXPECT_EQ("FooBar", parser.type_name);
  EXPECT_THAT(parser.namespaces, testing::ElementsAre("foo", "bar"));
}

TEST(NameParser, MakeFullCppName) {
  NameParser parser{"foo.bar.FooBar"};
  EXPECT_EQ("foo::bar::FooBar", parser.MakeFullCppName());
}

TEST(NameParser, MakeVariableName) {
  NameParser parser{"foo.bar.FooBar"};
  EXPECT_EQ("foo_bar", parser.MakeVariableName());
}

TEST(NameParser, MakeVariableName_NoCapitals) {
  NameParser parser{"foo"};
  EXPECT_EQ("foo", parser.MakeVariableName());
}

TEST(NameParser, MakeVariableName_NoInitialCapital) {
  NameParser parser{"fooBarBaz"};
  EXPECT_EQ("foo_bar_baz", parser.MakeVariableName());
}

TEST(NameParser, MakeVariableName_AllCapitals) {
  NameParser parser{"UUID"};
  EXPECT_EQ("uuid", parser.MakeVariableName());
}

TEST(NameParser, MakeVariableName_MixedCapital) {
  NameParser parser{"FOObarBaz"};
  EXPECT_EQ("foobar_baz", parser.MakeVariableName());
}

TEST(NameParser, MakeInterfaceName) {
  NameParser parser{"foo.bar.FooBar"};
  EXPECT_EQ("FooBarInterface", parser.MakeInterfaceName(false));
  EXPECT_EQ("foo::bar::FooBarInterface", parser.MakeInterfaceName(true));
}

TEST(NameParser, MakeProxyName) {
  NameParser parser{"foo.bar.FooBar"};
  EXPECT_EQ("FooBarProxy", parser.MakeProxyName(false));
  EXPECT_EQ("foo::bar::FooBarProxy", parser.MakeProxyName(true));
}

TEST(NameParser, MakeAdaptorName) {
  NameParser parser{"foo.bar.FooBar"};
  EXPECT_EQ("FooBarAdaptor", parser.MakeAdaptorName(false));
  EXPECT_EQ("foo::bar::FooBarAdaptor", parser.MakeAdaptorName(true));
}

TEST(NameParser, AddOpenNamespaces) {
  std::string expected =
    "namespace foo {\n"
    "namespace bar {\n";
  NameParser parser{"foo.bar.FooBar"};
  IndentedText text;
  parser.AddOpenNamespaces(&text, false);
  EXPECT_EQ(expected, text.GetContents());
}

TEST(NameParser, AddOpenNamespaces_WithMainType) {
  std::string expected =
    "namespace foo {\n"
    "namespace bar {\n"
    "namespace FooBar {\n";
  NameParser parser{"foo.bar.FooBar"};
  IndentedText text;
  parser.AddOpenNamespaces(&text, true);
  EXPECT_EQ(expected, text.GetContents());
}

TEST(NameParser, AddCloseNamespaces) {
  std::string expected =
    "}  // namespace bar\n"
    "}  // namespace foo\n";
  NameParser parser{"foo.bar.FooBar"};
  IndentedText text;
  parser.AddCloseNamespaces(&text, false);
  EXPECT_EQ(expected, text.GetContents());
}

TEST(NameParser, AddCloseNamespaces_WithMainType) {
  std::string expected =
    "}  // namespace FooBar\n"
    "}  // namespace bar\n"
    "}  // namespace foo\n";
  NameParser parser{"foo.bar.FooBar"};
  IndentedText text;
  parser.AddCloseNamespaces(&text, true);
  EXPECT_EQ(expected, text.GetContents());
}

}  // namespace chromeos_dbus_bindings