generate_cpp_unittest.cpp revision f944e79bdaf92fffe9ea78314d3636e7e4de8c51
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 "aidl_language.h"
22#include "ast_cpp.h"
23#include "code_writer.h"
24
25using std::string;
26using std::unique_ptr;
27
28namespace android {
29namespace aidl {
30namespace cpp {
31
32namespace internals {
33unique_ptr<Document> BuildClientHeader(interface_type* parsed_doc);
34}
35
36namespace {
37
38const char kTrivialInterfaceAIDL[] =
39R"(interface IPingResponder {
40  int Ping(int token);
41})";
42
43const char kExpectedTrivialClientHeaderOutput[] =
44R"(#ifndef BpPingResponder_H
45#define BpPingResponder_H
46
47#include <binder/IBinder.h>
48#include <binder/IInterface.h>
49#include <utils/Errors.h>
50#include <IPingResponder.h>
51
52namespace android {
53
54class BpPingResponder : public public android::BpInterface<IPingResponder> {
55public:
56BpPingResponder();
57~BpPingResponder();
58virtual android::status_t Ping(int32_t token, int32_t* _aidl_return);
59};  // class BpPingResponder
60
61}  // namespace android
62
63#endif  // BpPingResponder_H)";
64
65}  // namespace
66
67TEST(GenerateCPPTests, GeneratesClientHeader) {
68  Parser p{"BpExampleInterface.h"};
69  p.SetFileContents(kTrivialInterfaceAIDL);
70
71  ASSERT_TRUE(p.RunParser());
72
73  document_item_type *parsed_doc = p.GetDocument();
74
75  ASSERT_NE(nullptr, parsed_doc);
76  EXPECT_EQ(nullptr, parsed_doc->next);
77  ASSERT_EQ(INTERFACE_TYPE_BINDER, parsed_doc->item_type);
78
79  interface_type *interface = (interface_type*)parsed_doc;
80
81  unique_ptr<Document> doc = internals::BuildClientHeader(interface);
82
83  string output;
84  unique_ptr<CodeWriter> cw = GetStringWriter(&output);
85
86  doc->Write(cw.get());
87
88  EXPECT_EQ(kExpectedTrivialClientHeaderOutput, output);
89}
90
91}  // namespace cpp
92}  // namespace aidl
93}  // namespace android
94