13616d138056b61bd151820b9799820b70d797061Christopher Wiley/*
23616d138056b61bd151820b9799820b70d797061Christopher Wiley * Copyright (C) 2015, The Android Open Source Project
33616d138056b61bd151820b9799820b70d797061Christopher Wiley *
43616d138056b61bd151820b9799820b70d797061Christopher Wiley * Licensed under the Apache License, Version 2.0 (the "License");
53616d138056b61bd151820b9799820b70d797061Christopher Wiley * you may not use this file except in compliance with the License.
63616d138056b61bd151820b9799820b70d797061Christopher Wiley * You may obtain a copy of the License at
73616d138056b61bd151820b9799820b70d797061Christopher Wiley *
83616d138056b61bd151820b9799820b70d797061Christopher Wiley *     http://www.apache.org/licenses/LICENSE-2.0
93616d138056b61bd151820b9799820b70d797061Christopher Wiley *
103616d138056b61bd151820b9799820b70d797061Christopher Wiley * Unless required by applicable law or agreed to in writing, software
113616d138056b61bd151820b9799820b70d797061Christopher Wiley * distributed under the License is distributed on an "AS IS" BASIS,
123616d138056b61bd151820b9799820b70d797061Christopher Wiley * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133616d138056b61bd151820b9799820b70d797061Christopher Wiley * See the License for the specific language governing permissions and
143616d138056b61bd151820b9799820b70d797061Christopher Wiley * limitations under the License.
153616d138056b61bd151820b9799820b70d797061Christopher Wiley */
163616d138056b61bd151820b9799820b70d797061Christopher Wiley
17d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley#include <iostream>
183616d138056b61bd151820b9799820b70d797061Christopher Wiley#include <string>
193616d138056b61bd151820b9799820b70d797061Christopher Wiley#include <vector>
203616d138056b61bd151820b9799820b70d797061Christopher Wiley
213616d138056b61bd151820b9799820b70d797061Christopher Wiley#include <gtest/gtest.h>
223616d138056b61bd151820b9799820b70d797061Christopher Wiley
233616d138056b61bd151820b9799820b70d797061Christopher Wiley#include "options.h"
243616d138056b61bd151820b9799820b70d797061Christopher Wiley
25d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wileyusing std::cerr;
26d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wileyusing std::endl;
273616d138056b61bd151820b9799820b70d797061Christopher Wileyusing std::string;
284427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wileyusing std::unique_ptr;
294427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wileyusing std::vector;
304427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wiley
314427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wileynamespace android {
324427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wileynamespace aidl {
334427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wileynamespace {
343616d138056b61bd151820b9799820b70d797061Christopher Wiley
353616d138056b61bd151820b9799820b70d797061Christopher Wileyconst char kPreprocessCommandOutputFile[] = "output_file_name";
363616d138056b61bd151820b9799820b70d797061Christopher Wileyconst char kPreprocessCommandInput1[] = "input1";
373616d138056b61bd151820b9799820b70d797061Christopher Wileyconst char kPreprocessCommandInput2[] = "input2";
383616d138056b61bd151820b9799820b70d797061Christopher Wileyconst char kPreprocessCommandInput3[] = "input3";
393616d138056b61bd151820b9799820b70d797061Christopher Wileyconst char* kPreprocessCommand[] = {
403616d138056b61bd151820b9799820b70d797061Christopher Wiley    "aidl", "--preprocess",
413616d138056b61bd151820b9799820b70d797061Christopher Wiley    kPreprocessCommandOutputFile,
423616d138056b61bd151820b9799820b70d797061Christopher Wiley    kPreprocessCommandInput1,
433616d138056b61bd151820b9799820b70d797061Christopher Wiley    kPreprocessCommandInput2,
443616d138056b61bd151820b9799820b70d797061Christopher Wiley    kPreprocessCommandInput3,
45d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley    nullptr,
463616d138056b61bd151820b9799820b70d797061Christopher Wiley};
473616d138056b61bd151820b9799820b70d797061Christopher Wiley
484432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wileyconst char kCompileCommandInput[] = "directory/ITool.aidl";
49d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wileyconst char kCompileCommandIncludePath[] = "-Iinclude_path";
50d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wileyconst char* kCompileJavaCommand[] = {
51d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley    "aidl",
52d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley    "-b",
53d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley    kCompileCommandIncludePath,
54d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley    kCompileCommandInput,
55d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley    nullptr,
56d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley};
574432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wileyconst char kCompileCommandJavaOutput[] = "directory/ITool.java";
58d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley
59a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wileyconst char kCompileDepFile[] = "-doutput.deps";
60054afbd3037addb4f2e245ecbc36f31d3040f877Christopher Wileyconst char kCompileCommandHeaderDir[] = "output/dir";
61054afbd3037addb4f2e245ecbc36f31d3040f877Christopher Wileyconst char kCompileCommandCppOutput[] = "some/file.cpp";
62a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wileyconst char* kCompileCppCommand[] = {
63a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wiley    "aidl-cpp",
64a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wiley    kCompileCommandIncludePath,
65a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wiley    kCompileDepFile,
66a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wiley    kCompileCommandInput,
67054afbd3037addb4f2e245ecbc36f31d3040f877Christopher Wiley    kCompileCommandHeaderDir,
68054afbd3037addb4f2e245ecbc36f31d3040f877Christopher Wiley    kCompileCommandCppOutput,
69a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wiley    nullptr,
70a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wiley};
71a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wiley
7289eaab56f3ff913c247397b858fac3c8d3d4a206Christopher Wileytemplate <typename T>
7389eaab56f3ff913c247397b858fac3c8d3d4a206Christopher Wileyunique_ptr<T> GetOptions(const char* command[]) {
74d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  int argc = 0;
75d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  const char** command_part = command;
76d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  for (; *command_part; ++argc, ++command_part) {}
7789eaab56f3ff913c247397b858fac3c8d3d4a206Christopher Wiley  unique_ptr<T> options(T::Parse(argc, command));
78d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  if (!options) {
79d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley    cerr << "Failed to parse command line:";
80d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley    for (int i = 0; i < argc; ++i) {
81d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley      cerr << " " << command[i];
82d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley      cerr << endl;
83d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley    }
84d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  }
85d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  EXPECT_NE(options, nullptr) << "Failed to parse options!";
86d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  return options;
87d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley}
88d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley
894427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wiley}  // namespace
904427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wiley
9189eaab56f3ff913c247397b858fac3c8d3d4a206Christopher WileyTEST(JavaOptionsTests, ParsesPreprocess) {
9289eaab56f3ff913c247397b858fac3c8d3d4a206Christopher Wiley  unique_ptr<JavaOptions> options = GetOptions<JavaOptions>(kPreprocessCommand);
9389eaab56f3ff913c247397b858fac3c8d3d4a206Christopher Wiley  EXPECT_EQ(JavaOptions::PREPROCESS_AIDL, options->task);
94d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  EXPECT_EQ(false, options->fail_on_parcelable_);
95d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  EXPECT_EQ(0u, options->import_paths_.size());
96d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  EXPECT_EQ(0u, options->preprocessed_files_.size());
97d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  EXPECT_EQ(string{}, options->input_file_name_);
98d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  EXPECT_EQ(string{kPreprocessCommandOutputFile}, options->output_file_name_);
99d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  EXPECT_EQ(false, options->auto_dep_file_);
1003616d138056b61bd151820b9799820b70d797061Christopher Wiley  const vector<string> expected_input{kPreprocessCommandInput1,
1013616d138056b61bd151820b9799820b70d797061Christopher Wiley                                      kPreprocessCommandInput2,
1023616d138056b61bd151820b9799820b70d797061Christopher Wiley                                      kPreprocessCommandInput3};
103d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  EXPECT_EQ(expected_input, options->files_to_preprocess_);
104d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley}
105d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley
10689eaab56f3ff913c247397b858fac3c8d3d4a206Christopher WileyTEST(JavaOptionsTests, ParsesCompileJava) {
10789eaab56f3ff913c247397b858fac3c8d3d4a206Christopher Wiley  unique_ptr<JavaOptions> options =
10889eaab56f3ff913c247397b858fac3c8d3d4a206Christopher Wiley      GetOptions<JavaOptions>(kCompileJavaCommand);
10989eaab56f3ff913c247397b858fac3c8d3d4a206Christopher Wiley  EXPECT_EQ(JavaOptions::COMPILE_AIDL_TO_JAVA, options->task);
110d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  EXPECT_EQ(true, options->fail_on_parcelable_);
111d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  EXPECT_EQ(1u, options->import_paths_.size());
112d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  EXPECT_EQ(0u, options->preprocessed_files_.size());
113d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  EXPECT_EQ(string{kCompileCommandInput}, options->input_file_name_);
1144432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley  EXPECT_EQ(string{kCompileCommandJavaOutput}, options->output_file_name_);
115d93c5b761bf5a53b23a1f0ab954fa38109e72043Christopher Wiley  EXPECT_EQ(false, options->auto_dep_file_);
1163616d138056b61bd151820b9799820b70d797061Christopher Wiley}
1174427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wiley
118a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher WileyTEST(CppOptionsTests, ParsesCompileCpp) {
119a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wiley  unique_ptr<CppOptions> options = GetOptions<CppOptions>(kCompileCppCommand);
120a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wiley  ASSERT_EQ(1u, options->import_paths_.size());
121a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wiley  EXPECT_EQ(string{kCompileCommandIncludePath}.substr(2),
122a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wiley            options->import_paths_[0]);
123a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wiley  EXPECT_EQ(string{kCompileDepFile}.substr(2), options->dep_file_name_);
124a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wiley  EXPECT_EQ(kCompileCommandInput, options->InputFileName());
125054afbd3037addb4f2e245ecbc36f31d3040f877Christopher Wiley  EXPECT_EQ(kCompileCommandHeaderDir, options->OutputHeaderDir());
126054afbd3037addb4f2e245ecbc36f31d3040f877Christopher Wiley  EXPECT_EQ(kCompileCommandCppOutput, options->OutputCppFilePath());
1274432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley}
1284432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley
1294432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher WileyTEST(OptionsTests, EndsWith) {
1304432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley  EXPECT_TRUE(EndsWith("foo", ""));
1314432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley  EXPECT_TRUE(EndsWith("foo", "o"));
1324432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley  EXPECT_TRUE(EndsWith("foo", "foo"));
1334432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley  EXPECT_FALSE(EndsWith("foo", "fooo"));
1344432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley  EXPECT_FALSE(EndsWith("", "o"));
1354432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley  EXPECT_TRUE(EndsWith("", ""));
1364432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley}
1374432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley
1384432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher WileyTEST(OptionsTests, ReplaceSuffix) {
1394432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley  struct test_case_t {
1404432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley    const char* input;
1414432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley    const char* old_suffix;
1424432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley    const char* new_suffix;
1434432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley    const char* result;
1444432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley  };
1454432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley  const size_t kNumCases = 3;
1464432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley  test_case_t kTestInput[kNumCases] = {
1474432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley    {"foo.bar", "bar", "foo", "foo.foo"},
1484432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley    {"whole", "whole", "new", "new"},
1494432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley    {"", "", "", ""},
1504432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley  };
1514432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley  for (const auto& test_case : kTestInput) {
1524432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley    string mutated = test_case.input;
1534432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley    EXPECT_TRUE(ReplaceSuffix(test_case.old_suffix,
1544432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley                              test_case.new_suffix,
1554432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley                              &mutated));
1564432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley    EXPECT_EQ(mutated, test_case.result);
1574432ccfb09ae2c28e42b9c4c58f0e2c2c5a3ba7aChristopher Wiley  }
158a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wiley}
159a590de8d148bb42aa5f1f32aec6faae18a28e720Christopher Wiley
1604427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wiley}  // namespace android
1614427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wiley}  // namespace aidl
162