| Christopher Wiley | 3616d13 | 2015-09-01 11:07:48 -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 | |
| Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 17 | #include <iostream> |
| Christopher Wiley | 3616d13 | 2015-09-01 11:07:48 -0700 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include <gtest/gtest.h> |
| 22 | |
| 23 | #include "options.h" |
| 24 | |
| Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 25 | using std::cerr; |
| 26 | using std::endl; |
| Christopher Wiley | 3616d13 | 2015-09-01 11:07:48 -0700 | [diff] [blame] | 27 | using std::string; |
| Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 28 | using std::unique_ptr; |
| 29 | using std::vector; |
| 30 | |
| 31 | namespace android { |
| 32 | namespace aidl { |
| 33 | namespace { |
| Christopher Wiley | 3616d13 | 2015-09-01 11:07:48 -0700 | [diff] [blame] | 34 | |
| 35 | const char kPreprocessCommandOutputFile[] = "output_file_name"; |
| 36 | const char kPreprocessCommandInput1[] = "input1"; |
| 37 | const char kPreprocessCommandInput2[] = "input2"; |
| 38 | const char kPreprocessCommandInput3[] = "input3"; |
| 39 | const char* kPreprocessCommand[] = { |
| 40 | "aidl", "--preprocess", |
| 41 | kPreprocessCommandOutputFile, |
| 42 | kPreprocessCommandInput1, |
| 43 | kPreprocessCommandInput2, |
| 44 | kPreprocessCommandInput3, |
| Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 45 | nullptr, |
| Christopher Wiley | 3616d13 | 2015-09-01 11:07:48 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| Christopher Wiley | 4432ccf | 2015-09-18 18:32:08 -0700 | [diff] [blame] | 48 | const char kCompileCommandInput[] = "directory/ITool.aidl"; |
| Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 49 | const char kCompileCommandIncludePath[] = "-Iinclude_path"; |
| 50 | const char* kCompileJavaCommand[] = { |
| 51 | "aidl", |
| 52 | "-b", |
| 53 | kCompileCommandIncludePath, |
| 54 | kCompileCommandInput, |
| 55 | nullptr, |
| 56 | }; |
| Christopher Wiley | 4432ccf | 2015-09-18 18:32:08 -0700 | [diff] [blame] | 57 | const char kCompileCommandJavaOutput[] = "directory/ITool.java"; |
| Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 58 | |
| Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 59 | const char kCompileDepFile[] = "-doutput.deps"; |
| 60 | const char kCompileCommandOutputDir[] = "output/dir"; |
| 61 | const char* kCompileCppCommand[] = { |
| 62 | "aidl-cpp", |
| 63 | kCompileCommandIncludePath, |
| 64 | kCompileDepFile, |
| 65 | kCompileCommandInput, |
| 66 | kCompileCommandOutputDir, |
| 67 | nullptr, |
| 68 | }; |
| 69 | |
| Christopher Wiley | 4432ccf | 2015-09-18 18:32:08 -0700 | [diff] [blame] | 70 | const char kClientCppPath[] = "output/dir/BpTool.cpp"; |
| 71 | const char kClientHeaderPath[] = "output/dir/BpTool.h"; |
| 72 | const char kServerCppPath[] = "output/dir/BnTool.cpp"; |
| 73 | const char kServerHeaderPath[] = "output/dir/BnTool.h"; |
| 74 | const char kInterfaceCppPath[] = "output/dir/ITool.cpp"; |
| 75 | const char kInterfaceHeaderPath[] = "output/dir/ITool.h"; |
| 76 | |
| 77 | |
| Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame] | 78 | template <typename T> |
| 79 | unique_ptr<T> GetOptions(const char* command[]) { |
| Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 80 | int argc = 0; |
| 81 | const char** command_part = command; |
| 82 | for (; *command_part; ++argc, ++command_part) {} |
| Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame] | 83 | unique_ptr<T> options(T::Parse(argc, command)); |
| Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 84 | 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 Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 95 | } // namespace |
| 96 | |
| Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame] | 97 | TEST(JavaOptionsTests, ParsesPreprocess) { |
| 98 | unique_ptr<JavaOptions> options = GetOptions<JavaOptions>(kPreprocessCommand); |
| 99 | EXPECT_EQ(JavaOptions::PREPROCESS_AIDL, options->task); |
| Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 100 | 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 Wiley | 3616d13 | 2015-09-01 11:07:48 -0700 | [diff] [blame] | 106 | const vector<string> expected_input{kPreprocessCommandInput1, |
| 107 | kPreprocessCommandInput2, |
| 108 | kPreprocessCommandInput3}; |
| Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 109 | EXPECT_EQ(expected_input, options->files_to_preprocess_); |
| 110 | } |
| 111 | |
| Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame] | 112 | TEST(JavaOptionsTests, ParsesCompileJava) { |
| 113 | unique_ptr<JavaOptions> options = |
| 114 | GetOptions<JavaOptions>(kCompileJavaCommand); |
| 115 | EXPECT_EQ(JavaOptions::COMPILE_AIDL_TO_JAVA, options->task); |
| Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 116 | 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 Wiley | 4432ccf | 2015-09-18 18:32:08 -0700 | [diff] [blame] | 120 | EXPECT_EQ(string{kCompileCommandJavaOutput}, options->output_file_name_); |
| Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 121 | EXPECT_EQ(false, options->auto_dep_file_); |
| Christopher Wiley | 3616d13 | 2015-09-01 11:07:48 -0700 | [diff] [blame] | 122 | } |
| Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 123 | |
| Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 124 | TEST(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 Wiley | 4432ccf | 2015-09-18 18:32:08 -0700 | [diff] [blame] | 131 | |
| 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 | |
| 140 | TEST(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 | |
| 149 | TEST(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 Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 171 | } // namespace android |
| 172 | } // namespace aidl |