| 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: |
| 46 | void NormalMethod(int normalarg, float normal2); |
| 47 | virtual void SubMethod(int subarg) const; |
| 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: |
| 52 | virtual void SubMethod(int subarg) const; |
| 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 | |
| 59 | #endif // HEADER_INCLUDE_GUARD_H_)"; |
| 60 | |
| Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 61 | const char kExpectedEnumOutput[] = |
| 62 | R"(enum Foo { |
| 63 | BAR = 42, |
| 64 | BAZ, |
| 65 | } |
| 66 | )"; |
| 67 | |
| Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 68 | } // namespace |
| 69 | |
| 70 | TEST(AstCppTests, GeneratesHeader) { |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 71 | unique_ptr<MethodDecl> norm{ |
| 72 | new MethodDecl("void", "NormalMethod", |
| 73 | { "int normalarg", "float normal2" })}; |
| 74 | unique_ptr<MethodDecl> sub{ |
| 75 | new MethodDecl("void", "SubMethod", |
| 76 | { "int subarg" }, |
| 77 | MethodDecl::IS_CONST | MethodDecl::IS_VIRTUAL)}; |
| 78 | unique_ptr<MethodDecl> sub2{ |
| 79 | new MethodDecl("void", "SubMethod", |
| 80 | { "int subarg" }, |
| 81 | MethodDecl::IS_CONST | MethodDecl::IS_VIRTUAL)}; |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 82 | vector<unique_ptr<Declaration>> test_methods; |
| Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 83 | test_methods.push_back(std::move(norm)); |
| 84 | test_methods.push_back(std::move(sub)); |
| 85 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 86 | vector<unique_ptr<Declaration>> test_sub_methods; |
| Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 87 | test_sub_methods.push_back(std::move(sub2)); |
| 88 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 89 | unique_ptr<Declaration> test{new ClassDecl { "TestClass", "", |
| Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 90 | std::move(test_methods), {} }}; |
| 91 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 92 | unique_ptr<Declaration> test_sub{new ClassDecl { "TestSubClass", |
| Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 93 | "TestClass", std::move(test_sub_methods), {} }}; |
| 94 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 95 | vector<unique_ptr<Declaration>> classes; |
| Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 96 | classes.push_back(std::move(test)); |
| 97 | classes.push_back(std::move(test_sub)); |
| 98 | |
| 99 | unique_ptr<CppNamespace> test_ns{new CppNamespace {"test", |
| 100 | std::move(classes)}}; |
| 101 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 102 | vector<unique_ptr<Declaration>> test_ns_vec; |
| Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 103 | test_ns_vec.push_back(std::move(test_ns)); |
| 104 | |
| 105 | unique_ptr<CppNamespace> android_ns{new CppNamespace {"android", std::move(test_ns_vec) }}; |
| 106 | |
| 107 | CppHeader cpp_header{"HEADER_INCLUDE_GUARD_H_", {"string", "memory"}, |
| 108 | std::move(android_ns) }; |
| Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 109 | string actual_output; |
| Christopher Wiley | 413e042 | 2015-09-18 10:54:51 -0700 | [diff] [blame] | 110 | CodeWriterPtr writer = GetStringWriter(&actual_output); |
| Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 111 | cpp_header.Write(writer.get()); |
| 112 | EXPECT_EQ(string(kExpectedHeaderOutput), actual_output); |
| 113 | } |
| 114 | |
| Christopher Wiley | a7a5c10 | 2015-09-29 16:26:52 -0700 | [diff] [blame] | 115 | TEST(AstCppTests, GeneratesEnum) { |
| 116 | Enum e("Foo"); |
| 117 | e.AddValue("BAR", "42"); |
| 118 | e.AddValue("BAZ", ""); |
| 119 | string actual_output; |
| 120 | CodeWriterPtr writer = GetStringWriter(&actual_output); |
| 121 | e.Write(writer.get()); |
| 122 | EXPECT_EQ(string(kExpectedEnumOutput), actual_output); |
| 123 | } |
| 124 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 125 | } // namespace cpp |
| Christopher Wiley | f600a55 | 2015-09-12 14:07:44 -0700 | [diff] [blame] | 126 | } // namespace aidl |
| 127 | } // namespace android |