blob: 9f07400da83a537c316dab66f8ffc2e51c6701cb [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
Jiyong Park74595c12018-07-23 15:22:50 +090017#include "options.h"
18
Christopher Wileyd93c5b72015-09-14 13:21:37 -070019#include <iostream>
Jiyong Park74595c12018-07-23 15:22:50 +090020#include <memory>
Christopher Wiley3616d132015-09-01 11:07:48 -070021#include <string>
22#include <vector>
23
Jooyung Hanc522ac82020-10-23 14:24:12 +090024#include <gmock/gmock.h>
Christopher Wiley3616d132015-09-01 11:07:48 -070025#include <gtest/gtest.h>
26
Jooyung Han888c5bc2020-12-22 17:28:47 +090027#include "diagnostics.h"
28
29using android::aidl::DiagnosticID;
30using android::aidl::DiagnosticSeverity;
Christopher Wileyd93c5b72015-09-14 13:21:37 -070031using std::cerr;
32using std::endl;
Christopher Wiley3616d132015-09-01 11:07:48 -070033using std::string;
Christopher Wiley4427d862015-09-14 11:07:39 -070034using std::unique_ptr;
35using std::vector;
Jooyung Hanc522ac82020-10-23 14:24:12 +090036using testing::internal::CaptureStderr;
37using testing::internal::GetCapturedStderr;
Christopher Wiley4427d862015-09-14 11:07:39 -070038
39namespace android {
40namespace aidl {
41namespace {
Christopher Wiley3616d132015-09-01 11:07:48 -070042
43const char kPreprocessCommandOutputFile[] = "output_file_name";
Jiyong Park74595c12018-07-23 15:22:50 +090044const char kPreprocessCommandInput1[] = "input1.aidl";
45const char kPreprocessCommandInput2[] = "input2.aidl";
46const char kPreprocessCommandInput3[] = "input3.aidl";
Christopher Wiley3616d132015-09-01 11:07:48 -070047const char* kPreprocessCommand[] = {
48 "aidl", "--preprocess",
49 kPreprocessCommandOutputFile,
50 kPreprocessCommandInput1,
51 kPreprocessCommandInput2,
52 kPreprocessCommandInput3,
Christopher Wileyd93c5b72015-09-14 13:21:37 -070053 nullptr,
Christopher Wiley3616d132015-09-01 11:07:48 -070054};
55
Christopher Wiley4432ccf2015-09-18 18:32:08 -070056const char kCompileCommandInput[] = "directory/ITool.aidl";
Christopher Wileyd93c5b72015-09-14 13:21:37 -070057const char kCompileCommandIncludePath[] = "-Iinclude_path";
58const char* kCompileJavaCommand[] = {
59 "aidl",
60 "-b",
61 kCompileCommandIncludePath,
62 kCompileCommandInput,
63 nullptr,
64};
Christopher Wiley4432ccf2015-09-18 18:32:08 -070065const char kCompileCommandJavaOutput[] = "directory/ITool.java";
Christopher Wileyd93c5b72015-09-14 13:21:37 -070066
Steven Morelandb436cb72018-07-06 11:33:47 -070067const char kCompileDepFileNinja[] = "--ninja";
Dan Willemsen93298ee2016-11-10 23:55:55 -080068const char* kCompileJavaCommandNinja[] = {
69 "aidl",
70 "-b",
71 kCompileDepFileNinja,
72 kCompileCommandIncludePath,
73 kCompileCommandInput,
74 nullptr,
75};
76
Christopher Wileya590de82015-09-15 15:46:28 -070077const char kCompileDepFile[] = "-doutput.deps";
Jiyong Park05463732018-08-09 16:03:02 +090078const char kCompileCommandHeaderDir[] = "output/dir/";
Christopher Wiley054afbd2015-10-16 17:08:43 -070079const char kCompileCommandCppOutput[] = "some/file.cpp";
Christopher Wileya590de82015-09-15 15:46:28 -070080const char* kCompileCppCommand[] = {
81 "aidl-cpp",
82 kCompileCommandIncludePath,
83 kCompileDepFile,
84 kCompileCommandInput,
Christopher Wiley054afbd2015-10-16 17:08:43 -070085 kCompileCommandHeaderDir,
86 kCompileCommandCppOutput,
Christopher Wileya590de82015-09-15 15:46:28 -070087 nullptr,
88};
Dan Willemsen93298ee2016-11-10 23:55:55 -080089const char* kCompileCppCommandNinja[] = {
90 "aidl-cpp",
91 kCompileCommandIncludePath,
92 kCompileDepFile,
93 kCompileDepFileNinja,
94 kCompileCommandInput,
95 kCompileCommandHeaderDir,
96 kCompileCommandCppOutput,
97 nullptr,
98};
Christopher Wileya590de82015-09-15 15:46:28 -070099
Jiyong Park74595c12018-07-23 15:22:50 +0900100unique_ptr<Options> GetOptions(const char* command[],
101 Options::Language default_lang = Options::Language::JAVA) {
Christopher Wileyd93c5b72015-09-14 13:21:37 -0700102 int argc = 0;
103 const char** command_part = command;
104 for (; *command_part; ++argc, ++command_part) {}
Jiyong Park74595c12018-07-23 15:22:50 +0900105 unique_ptr<Options> ret(new Options(argc, command, default_lang));
106 if (!ret->Ok()) {
107 cerr << ret->GetErrorMessage();
Christopher Wileyd93c5b72015-09-14 13:21:37 -0700108 cerr << "Failed to parse command line:";
109 for (int i = 0; i < argc; ++i) {
110 cerr << " " << command[i];
111 cerr << endl;
112 }
113 }
Jiyong Park74595c12018-07-23 15:22:50 +0900114 EXPECT_NE(ret, nullptr) << "Failed to parse options!";
115 return ret;
Christopher Wileyd93c5b72015-09-14 13:21:37 -0700116}
117
Christopher Wiley4427d862015-09-14 11:07:39 -0700118} // namespace
119
Jiyong Park74595c12018-07-23 15:22:50 +0900120TEST(OptionsTests, ParsesPreprocess) {
121 unique_ptr<Options> options = GetOptions(kPreprocessCommand);
122 EXPECT_EQ(Options::Task::PREPROCESS, options->GetTask());
123 EXPECT_EQ(false, options->FailOnParcelable());
Jiyong Park3c35e392018-08-30 13:10:30 +0900124 EXPECT_EQ(0u, options->ImportDirs().size());
Jiyong Park74595c12018-07-23 15:22:50 +0900125 EXPECT_EQ(0u, options->PreprocessedFiles().size());
126 EXPECT_EQ(string{kPreprocessCommandOutputFile}, options->OutputFile());
127 EXPECT_EQ(false, options->AutoDepFile());
Christopher Wiley3616d132015-09-01 11:07:48 -0700128 const vector<string> expected_input{kPreprocessCommandInput1,
129 kPreprocessCommandInput2,
130 kPreprocessCommandInput3};
Jiyong Park74595c12018-07-23 15:22:50 +0900131 EXPECT_EQ(expected_input, options->InputFiles());
Christopher Wileyd93c5b72015-09-14 13:21:37 -0700132}
133
Jiyong Park74595c12018-07-23 15:22:50 +0900134TEST(OptionsTests, ParsesCompileJava) {
135 unique_ptr<Options> options = GetOptions(kCompileJavaCommand);
136 EXPECT_EQ(Options::Task::COMPILE, options->GetTask());
137 EXPECT_EQ(Options::Language::JAVA, options->TargetLanguage());
138 EXPECT_EQ(true, options->FailOnParcelable());
Jiyong Park3c35e392018-08-30 13:10:30 +0900139 EXPECT_EQ(1u, options->ImportDirs().size());
Jiyong Park74595c12018-07-23 15:22:50 +0900140 EXPECT_EQ(0u, options->PreprocessedFiles().size());
141 EXPECT_EQ(string{kCompileCommandInput}, options->InputFiles().front());
142 EXPECT_EQ(string{kCompileCommandJavaOutput}, options->OutputFile());
143 EXPECT_EQ(false, options->AutoDepFile());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800144 EXPECT_EQ(false, options->DependencyFileNinja());
145}
146
Jiyong Park74595c12018-07-23 15:22:50 +0900147TEST(OptionsTests, ParsesCompileJavaNinja) {
148 unique_ptr<Options> options = GetOptions(kCompileJavaCommandNinja);
149 EXPECT_EQ(Options::Task::COMPILE, options->GetTask());
150 EXPECT_EQ(Options::Language::JAVA, options->TargetLanguage());
151 EXPECT_EQ(true, options->FailOnParcelable());
Jiyong Park3c35e392018-08-30 13:10:30 +0900152 EXPECT_EQ(1u, options->ImportDirs().size());
Jiyong Park74595c12018-07-23 15:22:50 +0900153 EXPECT_EQ(0u, options->PreprocessedFiles().size());
154 EXPECT_EQ(string{kCompileCommandInput}, options->InputFiles().front());
155 EXPECT_EQ(string{kCompileCommandJavaOutput}, options->OutputFile());
156 EXPECT_EQ(false, options->AutoDepFile());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800157 EXPECT_EQ(true, options->DependencyFileNinja());
Christopher Wiley3616d132015-09-01 11:07:48 -0700158}
Christopher Wiley4427d862015-09-14 11:07:39 -0700159
Jiyong Park74595c12018-07-23 15:22:50 +0900160TEST(OptionsTests, ParsesCompileCpp) {
161 unique_ptr<Options> options = GetOptions(kCompileCppCommand, Options::Language::CPP);
Jiyong Park3c35e392018-08-30 13:10:30 +0900162 ASSERT_EQ(1u, options->ImportDirs().size());
Jiyong Park8c380532018-08-30 14:55:26 +0900163 EXPECT_EQ(string{kCompileCommandIncludePath}.substr(2), *options->ImportDirs().begin());
Jiyong Park74595c12018-07-23 15:22:50 +0900164 EXPECT_EQ(string{kCompileDepFile}.substr(2), options->DependencyFile());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800165 EXPECT_EQ(false, options->DependencyFileNinja());
Jiyong Park74595c12018-07-23 15:22:50 +0900166 EXPECT_EQ(kCompileCommandInput, options->InputFiles().front());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800167 EXPECT_EQ(kCompileCommandHeaderDir, options->OutputHeaderDir());
Jiyong Park74595c12018-07-23 15:22:50 +0900168 EXPECT_EQ(kCompileCommandCppOutput, options->OutputFile());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800169}
170
Jiyong Park74595c12018-07-23 15:22:50 +0900171TEST(OptionsTests, ParsesCompileCppNinja) {
172 unique_ptr<Options> options = GetOptions(kCompileCppCommandNinja, Options::Language::CPP);
Jiyong Park3c35e392018-08-30 13:10:30 +0900173 ASSERT_EQ(1u, options->ImportDirs().size());
Jiyong Park8c380532018-08-30 14:55:26 +0900174 EXPECT_EQ(string{kCompileCommandIncludePath}.substr(2), *options->ImportDirs().begin());
Jiyong Park74595c12018-07-23 15:22:50 +0900175 EXPECT_EQ(string{kCompileDepFile}.substr(2), options->DependencyFile());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800176 EXPECT_EQ(true, options->DependencyFileNinja());
Jiyong Park74595c12018-07-23 15:22:50 +0900177 EXPECT_EQ(kCompileCommandInput, options->InputFiles().front());
Christopher Wiley054afbd2015-10-16 17:08:43 -0700178 EXPECT_EQ(kCompileCommandHeaderDir, options->OutputHeaderDir());
Jiyong Park74595c12018-07-23 15:22:50 +0900179 EXPECT_EQ(kCompileCommandCppOutput, options->OutputFile());
Christopher Wiley4432ccf2015-09-18 18:32:08 -0700180}
181
Jiyong Park74595c12018-07-23 15:22:50 +0900182TEST(OptionsTests, ParsesCompileJavaMultiInput) {
183 const char* argv[] = {
184 "aidl",
185 "--lang=java",
186 kCompileCommandIncludePath,
187 "-o src_out",
188 "directory/input1.aidl",
189 "directory/input2.aidl",
190 "directory/input3.aidl",
191 nullptr,
192 };
193 unique_ptr<Options> options = GetOptions(argv);
194 EXPECT_EQ(Options::Task::COMPILE, options->GetTask());
195 EXPECT_EQ(Options::Language::JAVA, options->TargetLanguage());
196 EXPECT_EQ(false, options->FailOnParcelable());
Jiyong Park3c35e392018-08-30 13:10:30 +0900197 EXPECT_EQ(1u, options->ImportDirs().size());
Jiyong Park74595c12018-07-23 15:22:50 +0900198 EXPECT_EQ(0u, options->PreprocessedFiles().size());
199 const vector<string> expected_input{"directory/input1.aidl", "directory/input2.aidl",
200 "directory/input3.aidl"};
201 EXPECT_EQ(expected_input, options->InputFiles());
202 EXPECT_EQ(string{""}, options->OutputFile());
203 EXPECT_EQ(false, options->AutoDepFile());
204 EXPECT_EQ(false, options->DependencyFileNinja());
205 EXPECT_EQ(string{""}, options->OutputHeaderDir());
Jiyong Park05463732018-08-09 16:03:02 +0900206 EXPECT_EQ(string{"src_out/"}, options->OutputDir());
Christopher Wiley4432ccf2015-09-18 18:32:08 -0700207}
208
Andrei Homescub62afd92020-05-11 19:24:59 -0700209TEST(OptionsTests, ParsesCompileRust) {
210 const char* argv[] = {
211 "aidl", "--lang=rust", kCompileCommandIncludePath,
212 "-o src_out", kCompileCommandInput, nullptr,
213 };
214 unique_ptr<Options> options = GetOptions(argv);
215 EXPECT_EQ(Options::Task::COMPILE, options->GetTask());
216 EXPECT_EQ(Options::Language::RUST, options->TargetLanguage());
217 EXPECT_EQ(false, options->FailOnParcelable());
218 EXPECT_EQ(1u, options->ImportDirs().size());
219 EXPECT_EQ(0u, options->PreprocessedFiles().size());
220 EXPECT_EQ(string{kCompileCommandInput}, options->InputFiles().front());
221 EXPECT_EQ(string{""}, options->OutputFile());
222 EXPECT_EQ(string{""}, options->OutputHeaderDir());
223 EXPECT_EQ(string{"src_out/"}, options->OutputDir());
224 EXPECT_EQ(false, options->AutoDepFile());
225 EXPECT_EQ(false, options->DependencyFileNinja());
Andrei Homescub62afd92020-05-11 19:24:59 -0700226}
227
Jooyung Hanc522ac82020-10-23 14:24:12 +0900228TEST(OptionsTests, ParsesCompileJavaInvalid_OutRequired) {
Jiyong Park74595c12018-07-23 15:22:50 +0900229 // -o option is required
Jooyung Hanc522ac82020-10-23 14:24:12 +0900230 string expected_error = "Output directory is not set. Set with --out.";
231 CaptureStderr();
Jiyong Park74595c12018-07-23 15:22:50 +0900232 const char* arg_with_no_out_dir[] = {
233 "aidl",
234 "--lang=java",
235 kCompileCommandIncludePath,
236 "directory/input1.aidl",
237 "directory/input2.aidl",
238 "directory/input3.aidl",
239 nullptr,
Christopher Wiley4432ccf2015-09-18 18:32:08 -0700240 };
Jiyong Park74595c12018-07-23 15:22:50 +0900241 EXPECT_EQ(false, GetOptions(arg_with_no_out_dir)->Ok());
Jooyung Hanc522ac82020-10-23 14:24:12 +0900242 EXPECT_THAT(GetCapturedStderr(), testing::HasSubstr(expected_error));
243}
Jiyong Park74595c12018-07-23 15:22:50 +0900244
Jooyung Hanc522ac82020-10-23 14:24:12 +0900245TEST(OptionsTests, ParsesCompileJavaInvalid_RejectHeaderOut) {
246 string expected_error = "Header output directory is set, which does not make sense for Java.";
247 CaptureStderr();
Jiyong Park74595c12018-07-23 15:22:50 +0900248 // -h options is not for Java
249 const char* arg_with_header_dir[] = {
250 "aidl", "--lang=java", kCompileCommandIncludePath, "-o src_out",
251 "-h header_out", "directory/input1.aidl", "directory/input2.aidl", "directory/input3.aidl",
252 nullptr,
Christopher Wiley4432ccf2015-09-18 18:32:08 -0700253 };
Jiyong Park74595c12018-07-23 15:22:50 +0900254 EXPECT_EQ(false, GetOptions(arg_with_header_dir)->Ok());
Jooyung Hanc522ac82020-10-23 14:24:12 +0900255 EXPECT_THAT(GetCapturedStderr(), testing::HasSubstr(expected_error));
Jiyong Park74595c12018-07-23 15:22:50 +0900256}
257
258TEST(OptionsTests, ParsesCompileCppMultiInput) {
259 const char* argv[] = {
260 "aidl",
261 "--lang=cpp",
262 kCompileCommandIncludePath,
263 "-h header_out",
264 "-o src_out",
265 "directory/input1.aidl",
266 "directory/input2.aidl",
267 "directory/input3.aidl",
268 nullptr,
269 };
270 unique_ptr<Options> options = GetOptions(argv);
271 EXPECT_EQ(Options::Task::COMPILE, options->GetTask());
272 EXPECT_EQ(Options::Language::CPP, options->TargetLanguage());
273 EXPECT_EQ(false, options->FailOnParcelable());
Jiyong Park3c35e392018-08-30 13:10:30 +0900274 EXPECT_EQ(1u, options->ImportDirs().size());
Jiyong Park74595c12018-07-23 15:22:50 +0900275 EXPECT_EQ(0u, options->PreprocessedFiles().size());
276 const vector<string> expected_input{"directory/input1.aidl", "directory/input2.aidl",
277 "directory/input3.aidl"};
278 EXPECT_EQ(expected_input, options->InputFiles());
279 EXPECT_EQ(string{""}, options->OutputFile());
280 EXPECT_EQ(false, options->AutoDepFile());
281 EXPECT_EQ(false, options->DependencyFileNinja());
Jiyong Park05463732018-08-09 16:03:02 +0900282 EXPECT_EQ(string{"header_out/"}, options->OutputHeaderDir());
283 EXPECT_EQ(string{"src_out/"}, options->OutputDir());
Jiyong Park74595c12018-07-23 15:22:50 +0900284}
285
Jooyung Hanc522ac82020-10-23 14:24:12 +0900286TEST(OptionsTests, ParsesCompileCppInvalid_OutRequired) {
Jiyong Park74595c12018-07-23 15:22:50 +0900287 // -o option is required
Jooyung Hanc522ac82020-10-23 14:24:12 +0900288 string expected_error = "Output directory is not set. Set with --out.";
289 CaptureStderr();
Jiyong Park74595c12018-07-23 15:22:50 +0900290 const char* arg_with_no_out_dir[] = {
291 "aidl",
292 "--lang=cpp",
293 kCompileCommandIncludePath,
294 "directory/input1.aidl",
295 "directory/input2.aidl",
296 "directory/input3.aidl",
297 nullptr,
298 };
299 EXPECT_EQ(false, GetOptions(arg_with_no_out_dir)->Ok());
Jooyung Hanc522ac82020-10-23 14:24:12 +0900300 EXPECT_THAT(GetCapturedStderr(), testing::HasSubstr(expected_error));
301}
Jiyong Park74595c12018-07-23 15:22:50 +0900302
Jooyung Hanc522ac82020-10-23 14:24:12 +0900303TEST(OptionsTests, ParsesCompileCppInvalid_HeaderOutRequired) {
Jiyong Park74595c12018-07-23 15:22:50 +0900304 // -h options is required as well
Jooyung Hanc522ac82020-10-23 14:24:12 +0900305 string expected_error = "Header output directory is not set. Set with --header_out";
306 CaptureStderr();
Jiyong Park74595c12018-07-23 15:22:50 +0900307 const char* arg_with_no_header_dir[] = {
308 "aidl",
309 "--lang=cpp",
310 kCompileCommandIncludePath,
311 "-o src_out",
312 "directory/input1.aidl",
313 "directory/input2.aidl",
314 "directory/input3.aidl",
315 nullptr,
316 };
317 EXPECT_EQ(false, GetOptions(arg_with_no_header_dir)->Ok());
Jooyung Hanc522ac82020-10-23 14:24:12 +0900318 EXPECT_THAT(GetCapturedStderr(), testing::HasSubstr(expected_error));
Christopher Wileya590de82015-09-15 15:46:28 -0700319}
320
Jooyung Hanc522ac82020-10-23 14:24:12 +0900321TEST(OptionsTests, ParsesCompileRustInvalid_OutRequired) {
Andrei Homescub62afd92020-05-11 19:24:59 -0700322 // -o option is required
Jooyung Hanc522ac82020-10-23 14:24:12 +0900323 string expected_error = "Output directory is not set. Set with --out";
324 CaptureStderr();
Andrei Homescub62afd92020-05-11 19:24:59 -0700325 const char* arg_with_no_out_dir[] = {
326 "aidl",
327 "--lang=rust",
328 kCompileCommandIncludePath,
329 "directory/input1.aidl",
330 "directory/input2.aidl",
331 "directory/input3.aidl",
332 nullptr,
333 };
334 EXPECT_EQ(false, GetOptions(arg_with_no_out_dir)->Ok());
Jooyung Hanc522ac82020-10-23 14:24:12 +0900335 EXPECT_THAT(GetCapturedStderr(), testing::HasSubstr(expected_error));
336}
Andrei Homescub62afd92020-05-11 19:24:59 -0700337
Jooyung Hanc522ac82020-10-23 14:24:12 +0900338TEST(OptionsTests, ParsesCompileRustInvalid_RejectHeaderOut) {
339 string expected_error = "Header output directory is set, which does not make sense for Rust.";
340 CaptureStderr();
Andrei Homescub62afd92020-05-11 19:24:59 -0700341 // -h options is not for Rust
342 const char* arg_with_header_dir[] = {
343 "aidl", "--lang=rust", kCompileCommandIncludePath, "-o src_out",
344 "-h header_out", "directory/input1.aidl", "directory/input2.aidl", "directory/input3.aidl",
345 nullptr,
346 };
347 EXPECT_EQ(false, GetOptions(arg_with_header_dir)->Ok());
Jooyung Hanc522ac82020-10-23 14:24:12 +0900348 EXPECT_THAT(GetCapturedStderr(), testing::HasSubstr(expected_error));
Andrei Homescub62afd92020-05-11 19:24:59 -0700349}
350
Jooyung Han888c5bc2020-12-22 17:28:47 +0900351TEST(OptionsTests, ParsesWarningEnableAll) {
352 const char* args[] = {
353 "aidl", "--lang=java", "-Weverything", "--out=out", "input.aidl", nullptr,
354 };
355 auto options = GetOptions(args);
356 EXPECT_TRUE(options->Ok());
Jooyung Han808a2a02020-12-28 16:46:54 +0900357 auto mapping = options->GetDiagnosticMapping();
358 EXPECT_EQ(DiagnosticSeverity::WARNING, mapping.Severity(DiagnosticID::interface_name));
Jooyung Han888c5bc2020-12-22 17:28:47 +0900359}
360
361TEST(OptionsTests, ParsesWarningEnableSpecificWarning) {
362 const char* args[] = {
363 "aidl", "--lang=java", "-Winterface-name", "--out=out", "input.aidl", nullptr,
364 };
365 auto options = GetOptions(args);
366 EXPECT_TRUE(options->Ok());
Jooyung Han808a2a02020-12-28 16:46:54 +0900367 auto mapping = options->GetDiagnosticMapping();
368 EXPECT_EQ(DiagnosticSeverity::WARNING, mapping.Severity(DiagnosticID::interface_name));
Jooyung Han888c5bc2020-12-22 17:28:47 +0900369}
370
371TEST(OptionsTests, ParsesWarningDisableSpecificWarning) {
372 const char* args[] = {
373 "aidl", "--lang=java", "-Weverything", "-Wno-interface-name",
374 "--out=out", "input.aidl", nullptr,
375 };
376 auto options = GetOptions(args);
377 EXPECT_TRUE(options->Ok());
Jooyung Han808a2a02020-12-28 16:46:54 +0900378 auto mapping = options->GetDiagnosticMapping();
379 EXPECT_EQ(DiagnosticSeverity::DISABLED, mapping.Severity(DiagnosticID::interface_name));
Jooyung Han888c5bc2020-12-22 17:28:47 +0900380}
381
382TEST(OptionsTests, ParsesWarningAsErrors) {
383 const char* args[] = {
384 "aidl", "--lang=java", "-Werror", "-Weverything", "--out=out", "input.aidl", nullptr,
385 };
386 auto options = GetOptions(args);
387 EXPECT_TRUE(options->Ok());
Jooyung Han808a2a02020-12-28 16:46:54 +0900388 auto mapping = options->GetDiagnosticMapping();
389 EXPECT_EQ(DiagnosticSeverity::ERROR, mapping.Severity(DiagnosticID::interface_name));
Jooyung Han888c5bc2020-12-22 17:28:47 +0900390}
391
392TEST(OptionsTests, RejectsUnknownWarning) {
393 const char* args[] = {
394 "aidl", "--lang=java", "-Wfoobar", "--out=out", "input.aidl", nullptr,
395 };
396 CaptureStderr();
397 auto options = GetOptions(args);
398 EXPECT_FALSE(options->Ok());
399 EXPECT_THAT(GetCapturedStderr(), testing::HasSubstr("unknown warning: foobar"));
400}
401
Jooyung Hanb8a97772021-01-19 01:27:38 +0900402TEST(OptionsTests, CheckApi) {
403 const char* args[] = {
404 "aidl", "--checkapi", "old", "new", nullptr,
405 };
406 CaptureStderr();
407 auto options = GetOptions(args);
408 EXPECT_TRUE(options->Ok());
409 EXPECT_EQ("", GetCapturedStderr());
410 EXPECT_EQ(Options::Task::CHECK_API, options->GetTask());
411 EXPECT_EQ(Options::CheckApiLevel::COMPATIBLE, options->GetCheckApiLevel());
412}
413
414TEST(OptionsTests, CheckApiWithCompatible) {
415 const char* args[] = {
416 "aidl", "--checkapi=compatible", "old", "new", nullptr,
417 };
418 CaptureStderr();
419 auto options = GetOptions(args);
420 EXPECT_TRUE(options->Ok());
421 EXPECT_EQ("", GetCapturedStderr());
422 EXPECT_EQ(Options::Task::CHECK_API, options->GetTask());
423 EXPECT_EQ(Options::CheckApiLevel::COMPATIBLE, options->GetCheckApiLevel());
424}
425
426TEST(OptionsTests, CheckApiWithEqual) {
427 const char* args[] = {
428 "aidl", "--checkapi=equal", "old", "new", nullptr,
429 };
430 CaptureStderr();
431 auto options = GetOptions(args);
432 EXPECT_TRUE(options->Ok());
433 EXPECT_EQ("", GetCapturedStderr());
434 EXPECT_EQ(Options::Task::CHECK_API, options->GetTask());
435 EXPECT_EQ(Options::CheckApiLevel::EQUAL, options->GetCheckApiLevel());
436}
437
438TEST(OptionsTests, CheckApiWithUnknown) {
439 const char* args[] = {
440 "aidl", "--checkapi=unknown", "old", "new", nullptr,
441 };
442 CaptureStderr();
443 auto options = GetOptions(args);
444 EXPECT_FALSE(options->Ok());
445 EXPECT_THAT(GetCapturedStderr(), testing::HasSubstr("Unsupported --checkapi level: 'unknown'"));
446}
447
Christopher Wiley4427d862015-09-14 11:07:39 -0700448} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -0700449} // namespace android