| 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; |
| 25 | |
| 26 | namespace android { |
| 27 | namespace aidl { |
| 28 | namespace { |
| 29 | |
| 30 | const char kExpectedHeaderOutput[] = |
| 31 | R"(#ifndef HEADER_INCLUDE_GUARD_H_ |
| 32 | #define HEADER_INCLUDE_GUARD_H_ |
| 33 | |
| 34 | #include <string> |
| 35 | #include <memory> |
| 36 | |
| 37 | namespace android { |
| Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 38 | |
| Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 39 | namespace test { |
| 40 | |
| Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 41 | class TestClass { |
| Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 42 | public: |
| 43 | void NormalMethod(int normalarg, float normal2); |
| 44 | virtual void SubMethod(int subarg) const; |
| 45 | }; // class TestClass |
| Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 46 | |
| 47 | class TestSubClass : public TestClass { |
| Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 48 | public: |
| 49 | virtual void SubMethod(int subarg) const; |
| 50 | }; // class TestSubClass |
| Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 51 | |
| Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 52 | } // namespace test |
| Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 53 | |
| Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 54 | } // namespace android |
| 55 | |
| 56 | #endif // HEADER_INCLUDE_GUARD_H_)"; |
| 57 | |
| 58 | } // namespace |
| 59 | |
| 60 | TEST(AstCppTests, GeneratesHeader) { |
| Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 61 | CppHeader cpp_header{"HEADER_INCLUDE_GUARD_H_", |
| 62 | {"string", "memory"}, |
| 63 | new CppNamespace {"android", { |
| 64 | new CppNamespace {"test", { |
| Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 65 | new CppClassDeclaration { "TestClass", "", |
| Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 66 | { |
| 67 | new CppMethodDeclaration("void", "NormalMethod", { "int normalarg", "float normal2" }), |
| 68 | new CppMethodDeclaration("void", "SubMethod", { "int subarg" }, true, true) |
| 69 | }, |
| Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 70 | {} |
| 71 | }, |
| 72 | new CppClassDeclaration { "TestSubClass", "TestClass", |
| Casey Dahlin | 88924d6 | 2015-09-17 16:28:24 -0700 | [diff] [blame] | 73 | { |
| 74 | new CppMethodDeclaration("void", "SubMethod", { "int subarg" }, true, true) |
| 75 | }, |
| Casey Dahlin | 60a4916 | 2015-09-17 14:23:10 -0700 | [diff] [blame] | 76 | {} |
| 77 | } |
| Casey Dahlin | 34b8610 | 2015-09-16 16:03:06 -0700 | [diff] [blame] | 78 | }} |
| 79 | }} |
| 80 | }; |
| Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 81 | string actual_output; |
| Christopher Wiley | 413e042 | 2015-09-18 10:54:51 -0700 | [diff] [blame^] | 82 | CodeWriterPtr writer = GetStringWriter(&actual_output); |
| Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 83 | cpp_header.Write(writer.get()); |
| 84 | EXPECT_EQ(string(kExpectedHeaderOutput), actual_output); |
| 85 | } |
| 86 | |
| 87 | } // namespace aidl |
| 88 | } // namespace android |