Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 24 | using std::string; |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 25 | using std::vector; |
| 26 | using std::unique_ptr; |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace aidl { |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 30 | namespace cpp { |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | const char kExpectedHeaderOutput[] = |
| 34 | R"(#ifndef HEADER_INCLUDE_GUARD_H_ |
| 35 | #define HEADER_INCLUDE_GUARD_H_ |
| 36 | |
| 37 | #include <string> |
| 38 | #include <memory> |
| 39 | |
| 40 | namespace android { |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 41 | |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 42 | namespace test { |
| 43 | |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 44 | class TestClass { |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 45 | public: |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 46 | void NormalMethod(int normalarg, float normal2); |
| 47 | virtual void SubMethod(int subarg) const; |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 48 | }; // class TestClass |
Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 49 | |
| 50 | class TestSubClass : public TestClass { |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 51 | public: |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 52 | virtual void SubMethod(int subarg) const; |
Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 53 | }; // class TestSubClass |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 54 | |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 55 | } // namespace test |
Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 56 | |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 57 | } // namespace android |
| 58 | |
Christopher Wiley | 11a9d79 | 2016-02-24 17:20:33 -0800 | [diff] [blame] | 59 | #endif // HEADER_INCLUDE_GUARD_H_ |
| 60 | )"; |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 61 | |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 62 | const char kExpectedEnumOutput[] = |
| 63 | R"(enum Foo { |
| 64 | BAR = 42, |
| 65 | BAZ, |
Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 66 | }; |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 67 | )"; |
| 68 | |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 69 | const char kExpectedSwitchOutput[] = |
| 70 | R"(switch (var) { |
| 71 | case 2: |
| 72 | { |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 73 | baz; |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 74 | } |
| 75 | break; |
| 76 | case 1: |
| 77 | { |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 78 | foo; |
| 79 | bar; |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 80 | } |
| 81 | break; |
| 82 | } |
| 83 | )"; |
| 84 | |
| 85 | const char kExpectedMethodImplOutput[] = |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 86 | R"(return_type ClassName::MethodName(arg 1, arg 2, arg 3) const { |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 87 | foo; |
| 88 | bar; |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 89 | } |
| 90 | )"; |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 91 | } // namespace |
| 92 | |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 93 | class AstCppTests : public ::testing::Test { |
| 94 | protected: |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 95 | void CompareGeneratedCode(const AstNode& node, |
| 96 | const string& expected_output) { |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 97 | string actual_output; |
Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame] | 98 | node.Write(CodeWriter::ForString(&actual_output).get()); |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 99 | EXPECT_EQ(expected_output, actual_output); |
| 100 | } |
| 101 | }; // class AstCppTests |
| 102 | |
| 103 | |
| 104 | TEST_F(AstCppTests, GeneratesHeader) { |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 105 | unique_ptr<MethodDecl> norm{new MethodDecl( |
| 106 | "void", "NormalMethod", |
| 107 | ArgList{vector<string>{"int normalarg", "float normal2"}})}; |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 108 | unique_ptr<MethodDecl> sub{ |
| 109 | new MethodDecl("void", "SubMethod", |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 110 | ArgList{ "int subarg" }, |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 111 | MethodDecl::IS_CONST | MethodDecl::IS_VIRTUAL)}; |
| 112 | unique_ptr<MethodDecl> sub2{ |
| 113 | new MethodDecl("void", "SubMethod", |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 114 | ArgList{ "int subarg" }, |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 115 | MethodDecl::IS_CONST | MethodDecl::IS_VIRTUAL)}; |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 116 | vector<unique_ptr<Declaration>> test_methods; |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 117 | test_methods.push_back(std::move(norm)); |
| 118 | test_methods.push_back(std::move(sub)); |
| 119 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 120 | vector<unique_ptr<Declaration>> test_sub_methods; |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 121 | test_sub_methods.push_back(std::move(sub2)); |
| 122 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 123 | unique_ptr<Declaration> test{new ClassDecl { "TestClass", "", |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 124 | std::move(test_methods), {} }}; |
| 125 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 126 | unique_ptr<Declaration> test_sub{new ClassDecl { "TestSubClass", |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 127 | "TestClass", std::move(test_sub_methods), {} }}; |
| 128 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 129 | vector<unique_ptr<Declaration>> classes; |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 130 | 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 Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 136 | vector<unique_ptr<Declaration>> test_ns_vec; |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 137 | test_ns_vec.push_back(std::move(test_ns)); |
| 138 | |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 139 | unique_ptr<CppNamespace> android_ns{new CppNamespace {"android", |
| 140 | std::move(test_ns_vec) }}; |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 141 | |
Steven Moreland | f3da089 | 2018-10-05 14:52:01 -0700 | [diff] [blame] | 142 | vector<unique_ptr<Declaration>> test_ns_globals; |
| 143 | test_ns_globals.push_back(std::move(android_ns)); |
| 144 | |
| 145 | CppHeader cpp_header{"HEADER_INCLUDE_GUARD_H_", {"string", "memory"}, std::move(test_ns_globals)}; |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 146 | CompareGeneratedCode(cpp_header, kExpectedHeaderOutput); |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 149 | TEST_F(AstCppTests, GeneratesEnum) { |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 150 | Enum e("Foo"); |
| 151 | e.AddValue("BAR", "42"); |
| 152 | e.AddValue("BAZ", ""); |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 153 | CompareGeneratedCode(e, kExpectedEnumOutput); |
| 154 | } |
| 155 | |
| 156 | TEST_F(AstCppTests, GeneratesArgList) { |
| 157 | ArgList simple("foo"); |
| 158 | CompareGeneratedCode(simple, "(foo)"); |
| 159 | ArgList compound({"foo", "bar", "baz"}); |
| 160 | CompareGeneratedCode(compound, "(foo, bar, baz)"); |
Christopher Wiley | f02facf | 2015-11-12 08:54:08 -0800 | [diff] [blame] | 161 | std::vector<unique_ptr<AstNode>> args; |
| 162 | args.emplace_back(new LiteralExpression("foo()")); |
| 163 | ArgList nested(std::move(args)); |
| 164 | CompareGeneratedCode(nested, "(foo())"); |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Christopher Wiley | 3c5d28d | 2015-10-21 09:53:46 -0700 | [diff] [blame] | 167 | TEST_F(AstCppTests, GeneratesStatement) { |
| 168 | Statement s(new LiteralExpression("foo")); |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 169 | CompareGeneratedCode(s, "foo;\n"); |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Christopher Wiley | d55db28 | 2015-10-20 18:16:47 -0700 | [diff] [blame] | 172 | TEST_F(AstCppTests, GeneratesComparison) { |
| 173 | Comparison c( |
| 174 | new LiteralExpression("lhs"), "&&", new LiteralExpression("rhs")); |
| 175 | CompareGeneratedCode(c, "((lhs) && (rhs))"); |
| 176 | } |
| 177 | |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 178 | TEST_F(AstCppTests, GeneratesStatementBlock) { |
| 179 | StatementBlock block; |
Christopher Wiley | 3c5d28d | 2015-10-21 09:53:46 -0700 | [diff] [blame] | 180 | block.AddStatement(unique_ptr<AstNode>(new Statement("foo"))); |
| 181 | block.AddStatement(unique_ptr<AstNode>(new Statement("bar"))); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 182 | CompareGeneratedCode(block, "{\n foo;\n bar;\n}\n"); |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 185 | TEST_F(AstCppTests, GeneratesConstructorImpl) { |
| 186 | ConstructorImpl c("ClassName", ArgList({"a", "b", "c"}), |
| 187 | {"baz_(foo)", "bar_(blah)"}); |
| 188 | string expected = R"(ClassName::ClassName(a, b, c) |
| 189 | : baz_(foo), |
| 190 | bar_(blah){ |
| 191 | } |
| 192 | )"; |
| 193 | CompareGeneratedCode(c, expected); |
| 194 | } |
| 195 | |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 196 | TEST_F(AstCppTests, GeneratesAssignment) { |
| 197 | Assignment simple("foo", "8"); |
| 198 | CompareGeneratedCode(simple, "foo = 8;\n"); |
| 199 | Assignment less_simple("foo", new MethodCall("f", "8")); |
| 200 | CompareGeneratedCode(less_simple, "foo = f(8);\n"); |
| 201 | } |
| 202 | |
| 203 | TEST_F(AstCppTests, GeneratesMethodCall) { |
| 204 | MethodCall single("single", "arg"); |
| 205 | CompareGeneratedCode(single, "single(arg)"); |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 206 | MethodCall multi( |
| 207 | "multi", |
| 208 | ArgList({"has", "some", "args"})); |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 209 | CompareGeneratedCode(multi, "multi(has, some, args)"); |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 212 | TEST_F(AstCppTests, GeneratesIfStatement) { |
| 213 | IfStatement s(new LiteralExpression("foo")); |
| 214 | s.OnTrue()->AddLiteral("on true1"); |
| 215 | s.OnFalse()->AddLiteral("on false"); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 216 | CompareGeneratedCode(s, "if (foo) {\n on true1;\n}\nelse {\n on false;\n}\n"); |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 217 | |
| 218 | IfStatement s2(new LiteralExpression("bar")); |
| 219 | s2.OnTrue()->AddLiteral("on true1"); |
Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 220 | CompareGeneratedCode(s2, "if (bar) {\n on true1;\n}\n"); |
Christopher Wiley | 0eb903e | 2015-10-20 17:07:08 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 223 | TEST_F(AstCppTests, GeneratesSwitchStatement) { |
| 224 | SwitchStatement s("var"); |
| 225 | // These are intentionally out of alphanumeric order. We're testing |
| 226 | // that switch respects case addition order. |
| 227 | auto case2 = s.AddCase("2"); |
Christopher Wiley | 3c5d28d | 2015-10-21 09:53:46 -0700 | [diff] [blame] | 228 | case2->AddStatement(unique_ptr<AstNode>{new Statement{"baz"}}); |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 229 | auto case1 = s.AddCase("1"); |
Christopher Wiley | 3c5d28d | 2015-10-21 09:53:46 -0700 | [diff] [blame] | 230 | case1->AddStatement(unique_ptr<AstNode>{new Statement{"foo"}}); |
| 231 | case1->AddStatement(unique_ptr<AstNode>{new Statement{"bar"}}); |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 232 | CompareGeneratedCode(s, kExpectedSwitchOutput); |
Christopher Wiley | da69599 | 2015-10-05 11:31:41 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | TEST_F(AstCppTests, GeneratesMethodImpl) { |
| 236 | MethodImpl m{"return_type", "ClassName", "MethodName", |
Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 237 | ArgList{{"arg 1", "arg 2", "arg 3"}}, |
| 238 | true}; |
Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 239 | auto b = m.GetStatementBlock(); |
| 240 | b->AddLiteral("foo"); |
| 241 | b->AddLiteral("bar"); |
Christopher Wiley | 2328526 | 2015-10-09 15:06:14 -0700 | [diff] [blame] | 242 | CompareGeneratedCode(m, kExpectedMethodImplOutput); |
Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Jiyong Park | 176905e | 2018-07-04 22:29:41 +0900 | [diff] [blame] | 245 | TEST_F(AstCppTests, ToString) { |
| 246 | std::string literal = "void foo() {}"; |
| 247 | LiteralDecl decl(literal); |
| 248 | std::string actual = decl.ToString(); |
| 249 | EXPECT_EQ(literal, actual); |
| 250 | std::string written; |
| 251 | decl.Write(CodeWriter::ForString(&written).get()); |
| 252 | EXPECT_EQ(literal, written); |
| 253 | } |
| 254 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 255 | } // namespace cpp |
Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 256 | } // namespace aidl |
| 257 | } // namespace android |