blob: 0c5102d2102b7f1dc3bdce3153001b3ff14f2e14 [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 Wileyd93c5b72015-09-14 13:21:37 -070048const char kCompileCommandInput[] = "input.aidl";
49const char kCompileCommandIncludePath[] = "-Iinclude_path";
50const char* kCompileJavaCommand[] = {
51 "aidl",
52 "-b",
53 kCompileCommandIncludePath,
54 kCompileCommandInput,
55 nullptr,
56};
57const char kCompileCommandOutput[] = "input.java";
58
Christopher Wiley89eaab52015-09-15 14:46:46 -070059template <typename T>
60unique_ptr<T> GetOptions(const char* command[]) {
Christopher Wileyd93c5b72015-09-14 13:21:37 -070061 int argc = 0;
62 const char** command_part = command;
63 for (; *command_part; ++argc, ++command_part) {}
Christopher Wiley89eaab52015-09-15 14:46:46 -070064 unique_ptr<T> options(T::Parse(argc, command));
Christopher Wileyd93c5b72015-09-14 13:21:37 -070065 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 Wiley4427d862015-09-14 11:07:39 -070076} // namespace
77
Christopher Wiley89eaab52015-09-15 14:46:46 -070078TEST(JavaOptionsTests, ParsesPreprocess) {
79 unique_ptr<JavaOptions> options = GetOptions<JavaOptions>(kPreprocessCommand);
80 EXPECT_EQ(JavaOptions::PREPROCESS_AIDL, options->task);
Christopher Wileyd93c5b72015-09-14 13:21:37 -070081 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 Wiley3616d132015-09-01 11:07:48 -070087 const vector<string> expected_input{kPreprocessCommandInput1,
88 kPreprocessCommandInput2,
89 kPreprocessCommandInput3};
Christopher Wileyd93c5b72015-09-14 13:21:37 -070090 EXPECT_EQ(expected_input, options->files_to_preprocess_);
91}
92
Christopher Wiley89eaab52015-09-15 14:46:46 -070093TEST(JavaOptionsTests, ParsesCompileJava) {
94 unique_ptr<JavaOptions> options =
95 GetOptions<JavaOptions>(kCompileJavaCommand);
96 EXPECT_EQ(JavaOptions::COMPILE_AIDL_TO_JAVA, options->task);
Christopher Wileyd93c5b72015-09-14 13:21:37 -070097 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 Wiley3616d132015-09-01 11:07:48 -0700103}
Christopher Wiley4427d862015-09-14 11:07:39 -0700104
105} // namespace android
106} // namespace aidl