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