Home | History | Annotate | Download | only in aidl
      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 <iostream>
     18 #include <string>
     19 #include <vector>
     20 
     21 #include <gtest/gtest.h>
     22 
     23 #include "options.h"
     24 
     25 using std::cerr;
     26 using std::endl;
     27 using std::string;
     28 using std::unique_ptr;
     29 using std::vector;
     30 
     31 namespace android {
     32 namespace aidl {
     33 namespace {
     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,
     45     nullptr,
     46 };
     47 
     48 const char kCompileCommandInput[] = "directory/ITool.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 kCompileCommandJavaOutput[] = "directory/ITool.java";
     58 
     59 const char kCompileDepFileNinja[] = "-ninja";
     60 const char* kCompileJavaCommandNinja[] = {
     61     "aidl",
     62     "-b",
     63     kCompileDepFileNinja,
     64     kCompileCommandIncludePath,
     65     kCompileCommandInput,
     66     nullptr,
     67 };
     68 
     69 const char kCompileDepFile[] = "-doutput.deps";
     70 const char kCompileCommandHeaderDir[] = "output/dir";
     71 const char kCompileCommandCppOutput[] = "some/file.cpp";
     72 const char* kCompileCppCommand[] = {
     73     "aidl-cpp",
     74     kCompileCommandIncludePath,
     75     kCompileDepFile,
     76     kCompileCommandInput,
     77     kCompileCommandHeaderDir,
     78     kCompileCommandCppOutput,
     79     nullptr,
     80 };
     81 const char* kCompileCppCommandNinja[] = {
     82     "aidl-cpp",
     83     kCompileCommandIncludePath,
     84     kCompileDepFile,
     85     kCompileDepFileNinja,
     86     kCompileCommandInput,
     87     kCompileCommandHeaderDir,
     88     kCompileCommandCppOutput,
     89     nullptr,
     90 };
     91 
     92 template <typename T>
     93 unique_ptr<T> GetOptions(const char* command[]) {
     94   int argc = 0;
     95   const char** command_part = command;
     96   for (; *command_part; ++argc, ++command_part) {}
     97   unique_ptr<T> options(T::Parse(argc, command));
     98   if (!options) {
     99     cerr << "Failed to parse command line:";
    100     for (int i = 0; i < argc; ++i) {
    101       cerr << " " << command[i];
    102       cerr << endl;
    103     }
    104   }
    105   EXPECT_NE(options, nullptr) << "Failed to parse options!";
    106   return options;
    107 }
    108 
    109 }  // namespace
    110 
    111 TEST(JavaOptionsTests, ParsesPreprocess) {
    112   unique_ptr<JavaOptions> options = GetOptions<JavaOptions>(kPreprocessCommand);
    113   EXPECT_EQ(JavaOptions::PREPROCESS_AIDL, options->task);
    114   EXPECT_EQ(false, options->fail_on_parcelable_);
    115   EXPECT_EQ(0u, options->import_paths_.size());
    116   EXPECT_EQ(0u, options->preprocessed_files_.size());
    117   EXPECT_EQ(string{}, options->input_file_name_);
    118   EXPECT_EQ(string{kPreprocessCommandOutputFile}, options->output_file_name_);
    119   EXPECT_EQ(false, options->auto_dep_file_);
    120   const vector<string> expected_input{kPreprocessCommandInput1,
    121                                       kPreprocessCommandInput2,
    122                                       kPreprocessCommandInput3};
    123   EXPECT_EQ(expected_input, options->files_to_preprocess_);
    124 }
    125 
    126 TEST(JavaOptionsTests, ParsesCompileJava) {
    127   unique_ptr<JavaOptions> options =
    128       GetOptions<JavaOptions>(kCompileJavaCommand);
    129   EXPECT_EQ(JavaOptions::COMPILE_AIDL_TO_JAVA, options->task);
    130   EXPECT_EQ(true, options->fail_on_parcelable_);
    131   EXPECT_EQ(1u, options->import_paths_.size());
    132   EXPECT_EQ(0u, options->preprocessed_files_.size());
    133   EXPECT_EQ(string{kCompileCommandInput}, options->input_file_name_);
    134   EXPECT_EQ(string{kCompileCommandJavaOutput}, options->output_file_name_);
    135   EXPECT_EQ(false, options->auto_dep_file_);
    136   EXPECT_EQ(false, options->DependencyFileNinja());
    137 }
    138 
    139 TEST(JavaOptionsTests, ParsesCompileJavaNinja) {
    140   unique_ptr<JavaOptions> options =
    141       GetOptions<JavaOptions>(kCompileJavaCommandNinja);
    142   EXPECT_EQ(JavaOptions::COMPILE_AIDL_TO_JAVA, options->task);
    143   EXPECT_EQ(true, options->fail_on_parcelable_);
    144   EXPECT_EQ(1u, options->import_paths_.size());
    145   EXPECT_EQ(0u, options->preprocessed_files_.size());
    146   EXPECT_EQ(string{kCompileCommandInput}, options->input_file_name_);
    147   EXPECT_EQ(string{kCompileCommandJavaOutput}, options->output_file_name_);
    148   EXPECT_EQ(false, options->auto_dep_file_);
    149   EXPECT_EQ(true, options->DependencyFileNinja());
    150 }
    151 
    152 TEST(CppOptionsTests, ParsesCompileCpp) {
    153   unique_ptr<CppOptions> options = GetOptions<CppOptions>(kCompileCppCommand);
    154   ASSERT_EQ(1u, options->import_paths_.size());
    155   EXPECT_EQ(string{kCompileCommandIncludePath}.substr(2),
    156             options->import_paths_[0]);
    157   EXPECT_EQ(string{kCompileDepFile}.substr(2), options->dep_file_name_);
    158   EXPECT_EQ(false, options->DependencyFileNinja());
    159   EXPECT_EQ(kCompileCommandInput, options->InputFileName());
    160   EXPECT_EQ(kCompileCommandHeaderDir, options->OutputHeaderDir());
    161   EXPECT_EQ(kCompileCommandCppOutput, options->OutputCppFilePath());
    162 }
    163 
    164 TEST(CppOptionsTests, ParsesCompileCppNinja) {
    165   unique_ptr<CppOptions> options = GetOptions<CppOptions>(kCompileCppCommandNinja);
    166   ASSERT_EQ(1u, options->import_paths_.size());
    167   EXPECT_EQ(string{kCompileCommandIncludePath}.substr(2),
    168             options->import_paths_[0]);
    169   EXPECT_EQ(string{kCompileDepFile}.substr(2), options->dep_file_name_);
    170   EXPECT_EQ(true, options->DependencyFileNinja());
    171   EXPECT_EQ(kCompileCommandInput, options->InputFileName());
    172   EXPECT_EQ(kCompileCommandHeaderDir, options->OutputHeaderDir());
    173   EXPECT_EQ(kCompileCommandCppOutput, options->OutputCppFilePath());
    174 }
    175 
    176 TEST(OptionsTests, EndsWith) {
    177   EXPECT_TRUE(EndsWith("foo", ""));
    178   EXPECT_TRUE(EndsWith("foo", "o"));
    179   EXPECT_TRUE(EndsWith("foo", "foo"));
    180   EXPECT_FALSE(EndsWith("foo", "fooo"));
    181   EXPECT_FALSE(EndsWith("", "o"));
    182   EXPECT_TRUE(EndsWith("", ""));
    183 }
    184 
    185 TEST(OptionsTests, ReplaceSuffix) {
    186   struct test_case_t {
    187     const char* input;
    188     const char* old_suffix;
    189     const char* new_suffix;
    190     const char* result;
    191   };
    192   const size_t kNumCases = 3;
    193   test_case_t kTestInput[kNumCases] = {
    194     {"foo.bar", "bar", "foo", "foo.foo"},
    195     {"whole", "whole", "new", "new"},
    196     {"", "", "", ""},
    197   };
    198   for (const auto& test_case : kTestInput) {
    199     string mutated = test_case.input;
    200     EXPECT_TRUE(ReplaceSuffix(test_case.old_suffix,
    201                               test_case.new_suffix,
    202                               &mutated));
    203     EXPECT_EQ(mutated, test_case.result);
    204   }
    205 }
    206 
    207 }  // namespace android
    208 }  // namespace aidl
    209