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