blob: 727bff32c8aad89239f32baae017556d73ae4313 [file] [log] [blame]
Christopher Wiley3616d132015-09-01 11:07:48 -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
Christopher Wileyd93c5b72015-09-14 13:21:37 -070017#include <iostream>
Christopher Wiley3616d132015-09-01 11:07:48 -070018#include <string>
19#include <vector>
20
21#include <gtest/gtest.h>
22
23#include "options.h"
24
Christopher Wileyd93c5b72015-09-14 13:21:37 -070025using std::cerr;
26using std::endl;
Christopher Wiley3616d132015-09-01 11:07:48 -070027using std::string;
Christopher Wiley4427d862015-09-14 11:07:39 -070028using std::unique_ptr;
29using std::vector;
30
31namespace android {
32namespace aidl {
33namespace {
Christopher Wiley3616d132015-09-01 11:07:48 -070034
35const char kPreprocessCommandOutputFile[] = "output_file_name";
36const char kPreprocessCommandInput1[] = "input1";
37const char kPreprocessCommandInput2[] = "input2";
38const char kPreprocessCommandInput3[] = "input3";
39const char* kPreprocessCommand[] = {
40 "aidl", "--preprocess",
41 kPreprocessCommandOutputFile,
42 kPreprocessCommandInput1,
43 kPreprocessCommandInput2,
44 kPreprocessCommandInput3,
Christopher Wileyd93c5b72015-09-14 13:21:37 -070045 nullptr,
Christopher Wiley3616d132015-09-01 11:07:48 -070046};
47
Christopher Wiley4432ccf2015-09-18 18:32:08 -070048const char kCompileCommandInput[] = "directory/ITool.aidl";
Christopher Wileyd93c5b72015-09-14 13:21:37 -070049const char kCompileCommandIncludePath[] = "-Iinclude_path";
50const char* kCompileJavaCommand[] = {
51 "aidl",
52 "-b",
53 kCompileCommandIncludePath,
54 kCompileCommandInput,
55 nullptr,
56};
Christopher Wiley4432ccf2015-09-18 18:32:08 -070057const char kCompileCommandJavaOutput[] = "directory/ITool.java";
Christopher Wileyd93c5b72015-09-14 13:21:37 -070058
Christopher Wileya590de82015-09-15 15:46:28 -070059const char kCompileDepFile[] = "-doutput.deps";
60const char kCompileCommandOutputDir[] = "output/dir";
61const char* kCompileCppCommand[] = {
62 "aidl-cpp",
63 kCompileCommandIncludePath,
64 kCompileDepFile,
65 kCompileCommandInput,
66 kCompileCommandOutputDir,
67 nullptr,
68};
69
Christopher Wiley4432ccf2015-09-18 18:32:08 -070070const char kClientCppPath[] = "output/dir/BpTool.cpp";
71const char kClientHeaderPath[] = "output/dir/BpTool.h";
72const char kServerCppPath[] = "output/dir/BnTool.cpp";
73const char kServerHeaderPath[] = "output/dir/BnTool.h";
74const char kInterfaceCppPath[] = "output/dir/ITool.cpp";
75const char kInterfaceHeaderPath[] = "output/dir/ITool.h";
76
77
Christopher Wiley89eaab52015-09-15 14:46:46 -070078template <typename T>
79unique_ptr<T> GetOptions(const char* command[]) {
Christopher Wileyd93c5b72015-09-14 13:21:37 -070080 int argc = 0;
81 const char** command_part = command;
82 for (; *command_part; ++argc, ++command_part) {}
Christopher Wiley89eaab52015-09-15 14:46:46 -070083 unique_ptr<T> options(T::Parse(argc, command));
Christopher Wileyd93c5b72015-09-14 13:21:37 -070084 if (!options) {
85 cerr << "Failed to parse command line:";
86 for (int i = 0; i < argc; ++i) {
87 cerr << " " << command[i];
88 cerr << endl;
89 }
90 }
91 EXPECT_NE(options, nullptr) << "Failed to parse options!";
92 return options;
93}
94
Christopher Wiley4427d862015-09-14 11:07:39 -070095} // namespace
96
Christopher Wiley89eaab52015-09-15 14:46:46 -070097TEST(JavaOptionsTests, ParsesPreprocess) {
98 unique_ptr<JavaOptions> options = GetOptions<JavaOptions>(kPreprocessCommand);
99 EXPECT_EQ(JavaOptions::PREPROCESS_AIDL, options->task);
Christopher Wileyd93c5b72015-09-14 13:21:37 -0700100 EXPECT_EQ(false, options->fail_on_parcelable_);
101 EXPECT_EQ(0u, options->import_paths_.size());
102 EXPECT_EQ(0u, options->preprocessed_files_.size());
103 EXPECT_EQ(string{}, options->input_file_name_);
104 EXPECT_EQ(string{kPreprocessCommandOutputFile}, options->output_file_name_);
105 EXPECT_EQ(false, options->auto_dep_file_);
Christopher Wiley3616d132015-09-01 11:07:48 -0700106 const vector<string> expected_input{kPreprocessCommandInput1,
107 kPreprocessCommandInput2,
108 kPreprocessCommandInput3};
Christopher Wileyd93c5b72015-09-14 13:21:37 -0700109 EXPECT_EQ(expected_input, options->files_to_preprocess_);
110}
111
Christopher Wiley89eaab52015-09-15 14:46:46 -0700112TEST(JavaOptionsTests, ParsesCompileJava) {
113 unique_ptr<JavaOptions> options =
114 GetOptions<JavaOptions>(kCompileJavaCommand);
115 EXPECT_EQ(JavaOptions::COMPILE_AIDL_TO_JAVA, options->task);
Christopher Wileyd93c5b72015-09-14 13:21:37 -0700116 EXPECT_EQ(true, options->fail_on_parcelable_);
117 EXPECT_EQ(1u, options->import_paths_.size());
118 EXPECT_EQ(0u, options->preprocessed_files_.size());
119 EXPECT_EQ(string{kCompileCommandInput}, options->input_file_name_);
Christopher Wiley4432ccf2015-09-18 18:32:08 -0700120 EXPECT_EQ(string{kCompileCommandJavaOutput}, options->output_file_name_);
Christopher Wileyd93c5b72015-09-14 13:21:37 -0700121 EXPECT_EQ(false, options->auto_dep_file_);
Christopher Wiley3616d132015-09-01 11:07:48 -0700122}
Christopher Wiley4427d862015-09-14 11:07:39 -0700123
Christopher Wileya590de82015-09-15 15:46:28 -0700124TEST(CppOptionsTests, ParsesCompileCpp) {
125 unique_ptr<CppOptions> options = GetOptions<CppOptions>(kCompileCppCommand);
126 ASSERT_EQ(1u, options->import_paths_.size());
127 EXPECT_EQ(string{kCompileCommandIncludePath}.substr(2),
128 options->import_paths_[0]);
129 EXPECT_EQ(string{kCompileDepFile}.substr(2), options->dep_file_name_);
130 EXPECT_EQ(kCompileCommandInput, options->InputFileName());
Christopher Wiley4432ccf2015-09-18 18:32:08 -0700131
132 EXPECT_EQ(kClientCppPath, options->ClientCppFileName());
133 EXPECT_EQ(kClientHeaderPath, options->ClientHeaderFileName());
134 EXPECT_EQ(kServerCppPath, options->ServerCppFileName());
135 EXPECT_EQ(kServerHeaderPath, options->ServerHeaderFileName());
136 EXPECT_EQ(kInterfaceCppPath, options->InterfaceCppFileName());
137 EXPECT_EQ(kInterfaceHeaderPath, options->InterfaceHeaderFileName());
138}
139
140TEST(OptionsTests, EndsWith) {
141 EXPECT_TRUE(EndsWith("foo", ""));
142 EXPECT_TRUE(EndsWith("foo", "o"));
143 EXPECT_TRUE(EndsWith("foo", "foo"));
144 EXPECT_FALSE(EndsWith("foo", "fooo"));
145 EXPECT_FALSE(EndsWith("", "o"));
146 EXPECT_TRUE(EndsWith("", ""));
147}
148
149TEST(OptionsTests, ReplaceSuffix) {
150 struct test_case_t {
151 const char* input;
152 const char* old_suffix;
153 const char* new_suffix;
154 const char* result;
155 };
156 const size_t kNumCases = 3;
157 test_case_t kTestInput[kNumCases] = {
158 {"foo.bar", "bar", "foo", "foo.foo"},
159 {"whole", "whole", "new", "new"},
160 {"", "", "", ""},
161 };
162 for (const auto& test_case : kTestInput) {
163 string mutated = test_case.input;
164 EXPECT_TRUE(ReplaceSuffix(test_case.old_suffix,
165 test_case.new_suffix,
166 &mutated));
167 EXPECT_EQ(mutated, test_case.result);
168 }
Christopher Wileya590de82015-09-15 15:46:28 -0700169}
170
Christopher Wiley4427d862015-09-14 11:07:39 -0700171} // namespace android
172} // namespace aidl