blob: a2570014cbcd9b2f819b1a6184876da4b007f917 [file] [log] [blame]
Christopher Wileyf600a552015-09-12 14:07:44 -07001/*
2 * Copyright (C) 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <string>
18
19#include <gtest/gtest.h>
20
21#include "ast_cpp.h"
22#include "code_writer.h"
23
24using std::string;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -070025using std::vector;
26using std::unique_ptr;
Christopher Wileyf600a552015-09-12 14:07:44 -070027
28namespace android {
29namespace aidl {
Christopher Wileyf944e792015-09-29 10:00:46 -070030namespace cpp {
Christopher Wileyf600a552015-09-12 14:07:44 -070031namespace {
32
33const char kExpectedHeaderOutput[] =
34R"(#ifndef HEADER_INCLUDE_GUARD_H_
35#define HEADER_INCLUDE_GUARD_H_
36
37#include <string>
38#include <memory>
39
40namespace android {
Casey Dahlin34b86102015-09-16 16:03:06 -070041
Christopher Wileyf600a552015-09-12 14:07:44 -070042namespace test {
43
Casey Dahlin60a49162015-09-17 14:23:10 -070044class TestClass {
Casey Dahlin88924d62015-09-17 16:28:24 -070045public:
46void NormalMethod(int normalarg, float normal2);
47virtual void SubMethod(int subarg) const;
48}; // class TestClass
Casey Dahlin60a49162015-09-17 14:23:10 -070049
50class TestSubClass : public TestClass {
Casey Dahlin88924d62015-09-17 16:28:24 -070051public:
52virtual void SubMethod(int subarg) const;
53}; // class TestSubClass
Casey Dahlin34b86102015-09-16 16:03:06 -070054
Christopher Wileyf600a552015-09-12 14:07:44 -070055} // namespace test
Casey Dahlin34b86102015-09-16 16:03:06 -070056
Christopher Wileyf600a552015-09-12 14:07:44 -070057} // namespace android
58
59#endif // HEADER_INCLUDE_GUARD_H_)";
60
Christopher Wileya7a5c102015-09-29 16:26:52 -070061const char kExpectedEnumOutput[] =
62R"(enum Foo {
63 BAR = 42,
64 BAZ,
Christopher Wiley3bb6bc12015-10-14 10:58:27 -070065};
Christopher Wileya7a5c102015-09-29 16:26:52 -070066)";
67
Christopher Wileyda695992015-10-05 11:31:41 -070068const char kExpectedSwitchOutput[] =
69R"(switch (var) {
70case 2:
71{
72baz;
73}
74break;
75case 1:
76{
77foo;
78bar;
79}
80break;
81}
82)";
83
84const char kExpectedMethodImplOutput[] =
Christopher Wileyade4b452015-10-10 11:06:03 -070085R"(return_type ClassName::MethodName(arg 1, arg 2, arg 3) const {
Christopher Wileyda695992015-10-05 11:31:41 -070086foo;
87bar;
88}
89)";
Christopher Wileyf600a552015-09-12 14:07:44 -070090} // namespace
91
Christopher Wileyda695992015-10-05 11:31:41 -070092class AstCppTests : public ::testing::Test {
93 protected:
Christopher Wiley23285262015-10-09 15:06:14 -070094 void CompareGeneratedCode(const AstNode& node,
95 const string& expected_output) {
Christopher Wileyda695992015-10-05 11:31:41 -070096 string actual_output;
97 CodeWriterPtr writer = GetStringWriter(&actual_output);
Christopher Wiley23285262015-10-09 15:06:14 -070098 node.Write(writer.get());
Christopher Wileyda695992015-10-05 11:31:41 -070099 EXPECT_EQ(expected_output, actual_output);
100 }
101}; // class AstCppTests
102
103
104TEST_F(AstCppTests, GeneratesHeader) {
Christopher Wileyade4b452015-10-10 11:06:03 -0700105 unique_ptr<MethodDecl> norm{new MethodDecl(
106 "void", "NormalMethod",
107 ArgList{vector<string>{"int normalarg", "float normal2"}})};
Christopher Wiley0c732db2015-09-29 14:36:44 -0700108 unique_ptr<MethodDecl> sub{
109 new MethodDecl("void", "SubMethod",
Christopher Wileyade4b452015-10-10 11:06:03 -0700110 ArgList{ "int subarg" },
Christopher Wiley0c732db2015-09-29 14:36:44 -0700111 MethodDecl::IS_CONST | MethodDecl::IS_VIRTUAL)};
112 unique_ptr<MethodDecl> sub2{
113 new MethodDecl("void", "SubMethod",
Christopher Wileyade4b452015-10-10 11:06:03 -0700114 ArgList{ "int subarg" },
Christopher Wiley0c732db2015-09-29 14:36:44 -0700115 MethodDecl::IS_CONST | MethodDecl::IS_VIRTUAL)};
Christopher Wileyf944e792015-09-29 10:00:46 -0700116 vector<unique_ptr<Declaration>> test_methods;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700117 test_methods.push_back(std::move(norm));
118 test_methods.push_back(std::move(sub));
119
Christopher Wileyf944e792015-09-29 10:00:46 -0700120 vector<unique_ptr<Declaration>> test_sub_methods;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700121 test_sub_methods.push_back(std::move(sub2));
122
Christopher Wileyf944e792015-09-29 10:00:46 -0700123 unique_ptr<Declaration> test{new ClassDecl { "TestClass", "",
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700124 std::move(test_methods), {} }};
125
Christopher Wileyf944e792015-09-29 10:00:46 -0700126 unique_ptr<Declaration> test_sub{new ClassDecl { "TestSubClass",
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700127 "TestClass", std::move(test_sub_methods), {} }};
128
Christopher Wileyf944e792015-09-29 10:00:46 -0700129 vector<unique_ptr<Declaration>> classes;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700130 classes.push_back(std::move(test));
131 classes.push_back(std::move(test_sub));
132
133 unique_ptr<CppNamespace> test_ns{new CppNamespace {"test",
134 std::move(classes)}};
135
Christopher Wileyf944e792015-09-29 10:00:46 -0700136 vector<unique_ptr<Declaration>> test_ns_vec;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700137 test_ns_vec.push_back(std::move(test_ns));
138
Christopher Wiley23285262015-10-09 15:06:14 -0700139 unique_ptr<CppNamespace> android_ns{new CppNamespace {"android",
140 std::move(test_ns_vec) }};
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700141
142 CppHeader cpp_header{"HEADER_INCLUDE_GUARD_H_", {"string", "memory"},
143 std::move(android_ns) };
Christopher Wiley23285262015-10-09 15:06:14 -0700144 CompareGeneratedCode(cpp_header, kExpectedHeaderOutput);
Christopher Wileyf600a552015-09-12 14:07:44 -0700145}
146
Christopher Wileyda695992015-10-05 11:31:41 -0700147TEST_F(AstCppTests, GeneratesEnum) {
Christopher Wileya7a5c102015-09-29 16:26:52 -0700148 Enum e("Foo");
149 e.AddValue("BAR", "42");
150 e.AddValue("BAZ", "");
Christopher Wiley23285262015-10-09 15:06:14 -0700151 CompareGeneratedCode(e, kExpectedEnumOutput);
152}
153
154TEST_F(AstCppTests, GeneratesArgList) {
155 ArgList simple("foo");
156 CompareGeneratedCode(simple, "(foo)");
157 ArgList compound({"foo", "bar", "baz"});
158 CompareGeneratedCode(compound, "(foo, bar, baz)");
Christopher Wileyda695992015-10-05 11:31:41 -0700159}
160
161TEST_F(AstCppTests, GeneratesLiteralStatement) {
162 LiteralStatement s("foo");
Christopher Wiley23285262015-10-09 15:06:14 -0700163 CompareGeneratedCode(s, "foo;\n");
Christopher Wileyda695992015-10-05 11:31:41 -0700164}
165
166TEST_F(AstCppTests, GeneratesStatementBlock) {
167 StatementBlock block;
168 block.AddStatement(unique_ptr<AstNode>(new LiteralStatement("foo")));
169 block.AddStatement(unique_ptr<AstNode>(new LiteralStatement("bar")));
Christopher Wiley23285262015-10-09 15:06:14 -0700170 CompareGeneratedCode(block, "{\nfoo;\nbar;\n}\n");
171}
172
Christopher Wileyf9688b02015-10-08 17:17:50 -0700173TEST_F(AstCppTests, GeneratesConstructorImpl) {
174 ConstructorImpl c("ClassName", ArgList({"a", "b", "c"}),
175 {"baz_(foo)", "bar_(blah)"});
176 string expected = R"(ClassName::ClassName(a, b, c)
177 : baz_(foo),
178 bar_(blah){
179}
180)";
181 CompareGeneratedCode(c, expected);
182}
183
Christopher Wiley23285262015-10-09 15:06:14 -0700184TEST_F(AstCppTests, GeneratesAssignment) {
185 Assignment simple("foo", "8");
186 CompareGeneratedCode(simple, "foo = 8;\n");
187 Assignment less_simple("foo", new MethodCall("f", "8"));
188 CompareGeneratedCode(less_simple, "foo = f(8);\n");
189}
190
191TEST_F(AstCppTests, GeneratesMethodCall) {
192 MethodCall single("single", "arg");
193 CompareGeneratedCode(single, "single(arg)");
Christopher Wileyade4b452015-10-10 11:06:03 -0700194 MethodCall multi(
195 "multi",
196 ArgList({"has", "some", "args"}));
Christopher Wiley23285262015-10-09 15:06:14 -0700197 CompareGeneratedCode(multi, "multi(has, some, args)");
Christopher Wileyda695992015-10-05 11:31:41 -0700198}
199
200TEST_F(AstCppTests, GeneratesSwitchStatement) {
201 SwitchStatement s("var");
202 // These are intentionally out of alphanumeric order. We're testing
203 // that switch respects case addition order.
204 auto case2 = s.AddCase("2");
205 case2->AddStatement(unique_ptr<AstNode>{new LiteralStatement{"baz"}});
206 auto case1 = s.AddCase("1");
207 case1->AddStatement(unique_ptr<AstNode>{new LiteralStatement{"foo"}});
208 case1->AddStatement(unique_ptr<AstNode>{new LiteralStatement{"bar"}});
Christopher Wiley23285262015-10-09 15:06:14 -0700209 CompareGeneratedCode(s, kExpectedSwitchOutput);
Christopher Wileyda695992015-10-05 11:31:41 -0700210}
211
212TEST_F(AstCppTests, GeneratesMethodImpl) {
213 MethodImpl m{"return_type", "ClassName", "MethodName",
Christopher Wileyade4b452015-10-10 11:06:03 -0700214 ArgList{{"arg 1", "arg 2", "arg 3"}},
215 true};
Christopher Wileyf9688b02015-10-08 17:17:50 -0700216 auto b = m.GetStatementBlock();
217 b->AddLiteral("foo");
218 b->AddLiteral("bar");
Christopher Wiley23285262015-10-09 15:06:14 -0700219 CompareGeneratedCode(m, kExpectedMethodImplOutput);
Christopher Wileya7a5c102015-09-29 16:26:52 -0700220}
221
Christopher Wileyf944e792015-09-29 10:00:46 -0700222} // namespace cpp
Christopher Wileyf600a552015-09-12 14:07:44 -0700223} // namespace aidl
224} // namespace android