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