1 /* 2 * Copyright (c) 2018, Google Inc. 3 * All rights reserved. 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "perf_to_profile_lib.h" 9 #include "base/logging.h" 10 #include "test_compat.h" 11 12 namespace { 13 14 using ::testing::Eq; 15 16 TEST(PerfToProfileTest, ParseArguments) { 17 struct Test { 18 string desc; 19 std::vector<const char*> argv; 20 string expectedInput; 21 string expectedOutput; 22 bool expectedOverwriteOutput; 23 bool wantError; 24 }; 25 26 std::vector<Test> tests; 27 tests.push_back(Test{ 28 .desc = "With input, output and overwrite flags", 29 .argv = {"<exec>", "-i", "input_perf_file", "-o", "output_profile", "-f"}, 30 .expectedInput = "input_perf_file", 31 .expectedOutput = "output_profile", 32 .expectedOverwriteOutput = true, 33 .wantError = false, 34 }); 35 tests.push_back(Test{ 36 .desc = "With input and output flags", 37 .argv = {"<exec>", "-i", "input_perf_file", "-o", "output_profile"}, 38 .expectedInput = "input_perf_file", 39 .expectedOutput = "output_profile", 40 .expectedOverwriteOutput = false, 41 .wantError = false, 42 }); 43 tests.push_back(Test{ 44 .desc = "With only overwrite flag", 45 .argv = {"<exec>", "-f"}, 46 .expectedInput = "", 47 .expectedOutput = "", 48 .expectedOverwriteOutput = false, 49 .wantError = true, 50 }); 51 tests.push_back(Test{ 52 .desc = "With input, output, and invalid flags", 53 .argv = {"<exec>", "-i", "input_perf_file", "-o", "output_profile", "-F"}, 54 .expectedInput = "", 55 .expectedOutput = "", 56 .expectedOverwriteOutput = false, 57 .wantError = true, 58 }); 59 tests.push_back(Test{ 60 .desc = "With an invalid flag", 61 .argv = {"<exec>", "-F"}, 62 .expectedInput = "", 63 .expectedOutput = "", 64 .expectedOverwriteOutput = false, 65 .wantError = true, 66 }); 67 for (auto test : tests) { 68 string input; 69 string output; 70 bool overwriteOutput; 71 LOG(INFO) << "Testing: " << test.desc; 72 EXPECT_THAT(ParseArguments(test.argv.size(), test.argv.data(), &input, 73 &output, &overwriteOutput), 74 Eq(!test.wantError)); 75 if (!test.wantError) { 76 EXPECT_THAT(input, Eq(test.expectedInput)); 77 EXPECT_THAT(output, Eq(test.expectedOutput)); 78 EXPECT_THAT(overwriteOutput, Eq(test.expectedOverwriteOutput)); 79 } 80 optind = 1; 81 } 82 } 83 84 } // namespace 85 86 int main(int argc, char** argv) { 87 testing::InitGoogleTest(&argc, argv); 88 return RUN_ALL_TESTS(); 89 } 90