| 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 | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 48 | const char kCompileCommandInput[] = "input.aidl"; |
| 49 | const char kCompileCommandIncludePath[] = "-Iinclude_path"; |
| 50 | const char* kCompileJavaCommand[] = { |
| 51 | "aidl", |
| 52 | "-b", |
| 53 | kCompileCommandIncludePath, |
| 54 | kCompileCommandInput, |
| 55 | nullptr, |
| 56 | }; |
| 57 | const char kCompileCommandOutput[] = "input.java"; |
| 58 | |
| Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 59 | template <typename T> |
| 60 | unique_ptr<T> GetOptions(const char* command[]) { |
| Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 61 | int argc = 0; |
| 62 | const char** command_part = command; |
| 63 | for (; *command_part; ++argc, ++command_part) {} |
| Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 64 | unique_ptr<T> options(T::Parse(argc, command)); |
| Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 65 | if (!options) { |
| 66 | cerr << "Failed to parse command line:"; |
| 67 | for (int i = 0; i < argc; ++i) { |
| 68 | cerr << " " << command[i]; |
| 69 | cerr << endl; |
| 70 | } |
| 71 | } |
| 72 | EXPECT_NE(options, nullptr) << "Failed to parse options!"; |
| 73 | return options; |
| 74 | } |
| 75 | |
| Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 76 | } // namespace |
| 77 | |
| Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 78 | TEST(JavaOptionsTests, ParsesPreprocess) { |
| 79 | unique_ptr<JavaOptions> options = GetOptions<JavaOptions>(kPreprocessCommand); |
| 80 | EXPECT_EQ(JavaOptions::PREPROCESS_AIDL, options->task); |
| Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 81 | EXPECT_EQ(false, options->fail_on_parcelable_); |
| 82 | EXPECT_EQ(0u, options->import_paths_.size()); |
| 83 | EXPECT_EQ(0u, options->preprocessed_files_.size()); |
| 84 | EXPECT_EQ(string{}, options->input_file_name_); |
| 85 | EXPECT_EQ(string{kPreprocessCommandOutputFile}, options->output_file_name_); |
| 86 | EXPECT_EQ(false, options->auto_dep_file_); |
| Christopher Wiley | 3616d13 | 2015-09-01 11:07:48 -0700 | [diff] [blame] | 87 | const vector<string> expected_input{kPreprocessCommandInput1, |
| 88 | kPreprocessCommandInput2, |
| 89 | kPreprocessCommandInput3}; |
| Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 90 | EXPECT_EQ(expected_input, options->files_to_preprocess_); |
| 91 | } |
| 92 | |
| Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 93 | TEST(JavaOptionsTests, ParsesCompileJava) { |
| 94 | unique_ptr<JavaOptions> options = |
| 95 | GetOptions<JavaOptions>(kCompileJavaCommand); |
| 96 | EXPECT_EQ(JavaOptions::COMPILE_AIDL_TO_JAVA, options->task); |
| Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 97 | EXPECT_EQ(true, options->fail_on_parcelable_); |
| 98 | EXPECT_EQ(1u, options->import_paths_.size()); |
| 99 | EXPECT_EQ(0u, options->preprocessed_files_.size()); |
| 100 | EXPECT_EQ(string{kCompileCommandInput}, options->input_file_name_); |
| 101 | EXPECT_EQ(string{kCompileCommandOutput}, options->output_file_name_); |
| 102 | EXPECT_EQ(false, options->auto_dep_file_); |
| Christopher Wiley | 3616d13 | 2015-09-01 11:07:48 -0700 | [diff] [blame] | 103 | } |
| Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 104 | |
| 105 | } // namespace android |
| 106 | } // namespace aidl |