blob: cf40a55554ce4512161dd9859d19250c542cac87 [file] [log] [blame]
Christopher Wileyf600a552015-09-12 14:07:44 -07001/*
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
24using std::string;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -070025using std::vector;
26using std::unique_ptr;
Christopher Wileyf600a552015-09-12 14:07:44 -070027
28namespace android {
29namespace aidl {
Christopher Wileyf944e792015-09-29 10:00:46 -070030namespace cpp {
Christopher Wileyf600a552015-09-12 14:07:44 -070031namespace {
32
33const char kExpectedHeaderOutput[] =
34R"(#ifndef HEADER_INCLUDE_GUARD_H_
35#define HEADER_INCLUDE_GUARD_H_
36
37#include <string>
38#include <memory>
39
40namespace android {
Casey Dahlin34b86102015-09-16 16:03:06 -070041
Christopher Wileyf600a552015-09-12 14:07:44 -070042namespace test {
43
Casey Dahlin60a49162015-09-17 14:23:10 -070044class TestClass {
Casey Dahlin88924d62015-09-17 16:28:24 -070045public:
46void NormalMethod(int normalarg, float normal2);
47virtual void SubMethod(int subarg) const;
48}; // class TestClass
Casey Dahlin60a49162015-09-17 14:23:10 -070049
50class TestSubClass : public TestClass {
Casey Dahlin88924d62015-09-17 16:28:24 -070051public:
52virtual void SubMethod(int subarg) const;
53}; // class TestSubClass
Casey Dahlin34b86102015-09-16 16:03:06 -070054
Christopher Wileyf600a552015-09-12 14:07:44 -070055} // namespace test
Casey Dahlin34b86102015-09-16 16:03:06 -070056
Christopher Wileyf600a552015-09-12 14:07:44 -070057} // namespace android
58
59#endif // HEADER_INCLUDE_GUARD_H_)";
60
Christopher Wileya7a5c102015-09-29 16:26:52 -070061const char kExpectedEnumOutput[] =
62R"(enum Foo {
63 BAR = 42,
64 BAZ,
65}
66)";
67
Christopher Wileyf600a552015-09-12 14:07:44 -070068} // namespace
69
70TEST(AstCppTests, GeneratesHeader) {
Christopher Wiley0c732db2015-09-29 14:36:44 -070071 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 Wileyf944e792015-09-29 10:00:46 -070082 vector<unique_ptr<Declaration>> test_methods;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -070083 test_methods.push_back(std::move(norm));
84 test_methods.push_back(std::move(sub));
85
Christopher Wileyf944e792015-09-29 10:00:46 -070086 vector<unique_ptr<Declaration>> test_sub_methods;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -070087 test_sub_methods.push_back(std::move(sub2));
88
Christopher Wileyf944e792015-09-29 10:00:46 -070089 unique_ptr<Declaration> test{new ClassDecl { "TestClass", "",
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -070090 std::move(test_methods), {} }};
91
Christopher Wileyf944e792015-09-29 10:00:46 -070092 unique_ptr<Declaration> test_sub{new ClassDecl { "TestSubClass",
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -070093 "TestClass", std::move(test_sub_methods), {} }};
94
Christopher Wileyf944e792015-09-29 10:00:46 -070095 vector<unique_ptr<Declaration>> classes;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -070096 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 Wileyf944e792015-09-29 10:00:46 -0700102 vector<unique_ptr<Declaration>> test_ns_vec;
Casey Dahlinb7d0f7f2015-09-22 17:21:08 -0700103 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 Wileyf600a552015-09-12 14:07:44 -0700109 string actual_output;
Christopher Wiley413e0422015-09-18 10:54:51 -0700110 CodeWriterPtr writer = GetStringWriter(&actual_output);
Christopher Wileyf600a552015-09-12 14:07:44 -0700111 cpp_header.Write(writer.get());
112 EXPECT_EQ(string(kExpectedHeaderOutput), actual_output);
113}
114
Christopher Wileya7a5c102015-09-29 16:26:52 -0700115TEST(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 Wileyf944e792015-09-29 10:00:46 -0700125} // namespace cpp
Christopher Wileyf600a552015-09-12 14:07:44 -0700126} // namespace aidl
127} // namespace android