| 1 |
#include "gtest/gtest.h" |
| 2 |
#include "stk_util/environment/OptionsSpecification.hpp" // for OptionsSpecification, DefaultValue |
| 3 |
#include "stk_util/environment/ParseCommandLineArgs.hpp" // for parse_command_line_args |
| 4 |
#include "stk_util/environment/ParsedOptions.hpp" // for ParsedOptions, VariableType |
| 5 |
#include <string> // for string, basic_string |
| 6 |
#include <vector> // for vector |
| 7 |
|
| 8 |
namespace { |
| 9 |
|
| 10 |
class ParseCommandLineOnlyExe : public ::testing::Test |
| 11 |
{ |
| 12 |
protected: |
| 13 |
static constexpr int argc = 1; |
| 14 |
const char *argv[argc] = {"exeName"}; |
| 15 |
}; |
| 16 |
|
| 17 |
TEST_F(ParseCommandLineOnlyExe, parseCmdLine_varMapEmpty) |
| 18 |
{ |
| 19 |
stk::OptionsSpecification optionsSpec; |
| 20 |
stk::ParsedOptions parsedOptions; |
| 21 |
|
| 22 |
stk::parse_command_line_args(argc, argv, optionsSpec, parsedOptions); |
| 23 |
|
| 24 |
EXPECT_TRUE(parsedOptions.empty()); |
| 25 |
} |
| 26 |
|
| 27 |
class ParseCommandLineOneFlag : public ::testing::Test |
| 28 |
{ |
| 29 |
protected: |
| 30 |
static constexpr int argc = 2; |
| 31 |
const char *argv[argc] = {"exeName", "--flag"}; |
| 32 |
}; |
| 33 |
|
| 34 |
TEST_F(ParseCommandLineOneFlag, parseCmdLine_varMapHasFlag) |
| 35 |
{ |
| 36 |
stk::OptionsSpecification optionsSpec; |
| 37 |
stk::ParsedOptions parsedOptions; |
| 38 |
|
| 39 |
optionsSpec.add_options()("flag,f","this is a non-required flag"); |
| 40 |
|
| 41 |
stk::parse_command_line_args(argc, argv, optionsSpec, parsedOptions); |
| 42 |
|
| 43 |
EXPECT_FALSE(parsedOptions.empty()); |
| 44 |
EXPECT_EQ(1u, parsedOptions.count("flag")); |
| 45 |
EXPECT_TRUE(parsedOptions["flag"].empty()); |
| 46 |
} |
| 47 |
|
| 48 |
class ParseCommandLineFlagAndOption : public ::testing::Test |
| 49 |
{ |
| 50 |
protected: |
| 51 |
static constexpr int argc = 4; |
| 52 |
const char *argv[argc] = {"exeName", "--flag", "--option", "42"}; |
| 53 |
}; |
| 54 |
|
| 55 |
TEST_F(ParseCommandLineFlagAndOption, parseCmdLine_varMapHasFlagAndOption) |
| 56 |
{ |
| 57 |
stk::OptionsSpecification optionsSpec; |
| 58 |
stk::ParsedOptions parsedOptions; |
| 59 |
|
| 60 |
optionsSpec.add_options() |
| 61 |
("flag,f","this is a non-required flag") |
| 62 |
("option,o", "this is an option", stk::DefaultValue<int>(24)); |
| 63 |
|
| 64 |
stk::parse_command_line_args(argc, argv, optionsSpec, parsedOptions); |
| 65 |
|
| 66 |
EXPECT_FALSE(parsedOptions.empty()); |
| 67 |
EXPECT_EQ(1u, parsedOptions.count("flag")); |
| 68 |
EXPECT_EQ(1u, parsedOptions.count("option")); |
| 69 |
int expectedValue = 42; |
| 70 |
EXPECT_EQ(expectedValue, parsedOptions["option"].as<int>()); |
| 71 |
} |
| 72 |
|
| 73 |
TEST_F(ParseCommandLineFlagAndOption, parseCmdLine_varMapHasFlagAndOption_storeOptionInVariable) |
| 74 |
{ |
| 75 |
stk::OptionsSpecification optionsSpec; |
| 76 |
stk::ParsedOptions parsedOptions; |
| 77 |
|
| 78 |
int parsedOptionValue = 0; |
| 79 |
|
| 80 |
optionsSpec.add_options() |
| 81 |
("flag,f","this is a non-required flag") |
| 82 |
("option,o", "this is an option", stk::TargetPointer<int>(&parsedOptionValue), stk::DefaultValue<int>(24)); |
| 83 |
|
| 84 |
stk::parse_command_line_args(argc, argv, optionsSpec, parsedOptions); |
| 85 |
|
| 86 |
EXPECT_FALSE(parsedOptions.empty()); |
| 87 |
EXPECT_EQ(1u, parsedOptions.count("flag")); |
| 88 |
EXPECT_EQ(1u, parsedOptions.count("option")); |
| 89 |
int expectedValue = 42; |
| 90 |
EXPECT_EQ(expectedValue, parsedOptions["option"].as<int>()); |
| 91 |
EXPECT_EQ(expectedValue, parsedOptionValue); |
| 92 |
} |
| 93 |
|
| 94 |
class ParseCommandLineFlagAndAbbrevOption : public ::testing::Test |
| 95 |
{ |
| 96 |
protected: |
| 97 |
static constexpr int argc = 4; |
| 98 |
const char *argv[argc] = {"exeName", "--flag", "-o", "42"}; |
| 99 |
}; |
| 100 |
|
| 101 |
TEST_F(ParseCommandLineFlagAndAbbrevOption, parseCmdLine_varMapHasFlagAndOption) |
| 102 |
{ |
| 103 |
stk::OptionsSpecification optionsSpec; |
| 104 |
stk::ParsedOptions parsedOptions; |
| 105 |
|
| 106 |
optionsSpec.add_options() |
| 107 |
("flag,f","this is a non-required flag") |
| 108 |
("option,o", "this is an option", stk::DefaultValue<int>(24)); |
| 109 |
|
| 110 |
stk::parse_command_line_args(argc, argv, optionsSpec, parsedOptions); |
| 111 |
|
| 112 |
EXPECT_FALSE(parsedOptions.empty()); |
| 113 |
EXPECT_EQ(1u, parsedOptions.count("flag")); |
| 114 |
EXPECT_TRUE(parsedOptions["flag"].empty()); |
| 115 |
EXPECT_EQ(1u, parsedOptions.count("option")); |
| 116 |
int expectedValue = 42; |
| 117 |
EXPECT_EQ(expectedValue, parsedOptions["option"].as<int>()); |
| 118 |
} |
| 119 |
|
| 120 |
TEST_F(ParseCommandLineFlagAndAbbrevOption, parseCmdLine_varMapHasFlagAndOptionAndDefaultValue) |
| 121 |
{ |
| 122 |
stk::OptionsSpecification optionsSpec; |
| 123 |
stk::ParsedOptions parsedOptions; |
| 124 |
|
| 125 |
optionsSpec.add_options() |
| 126 |
("flag,f","this is a non-required flag") |
| 127 |
("option,o", "this is an option", stk::DefaultValue<int>(24)) |
| 128 |
("defaultOption,d", "this is an option with a default value", stk::DefaultValue<double>(99.9)); |
| 129 |
|
| 130 |
stk::parse_command_line_args(argc, argv, optionsSpec, parsedOptions); |
| 131 |
|
| 132 |
EXPECT_FALSE(parsedOptions.empty()); |
| 133 |
EXPECT_EQ(1u, parsedOptions.count("flag")); |
| 134 |
EXPECT_TRUE(parsedOptions["flag"].empty()); |
| 135 |
EXPECT_EQ(1u, parsedOptions.count("option")); |
| 136 |
int expectedValue = 42; |
| 137 |
EXPECT_EQ(expectedValue, parsedOptions["option"].as<int>()); |
| 138 |
double expectedDoubleValue = 99.9; |
| 139 |
EXPECT_EQ(1u, parsedOptions.count("defaultOption")); |
| 140 |
EXPECT_EQ(expectedDoubleValue, parsedOptions["defaultOption"].as<double>()); |
| 141 |
EXPECT_EQ(expectedDoubleValue, parsedOptions["d"].as<double>()); |
| 142 |
} |
| 143 |
|
| 144 |
class ParseCommandLineImplicitOptionAndFlag : public ::testing::Test |
| 145 |
{ |
| 146 |
protected: |
| 147 |
static constexpr int argc = 3; |
| 148 |
const char *argv[argc] = {"exeName", "--aprepro", "--flag"}; |
| 149 |
}; |
| 150 |
|
| 151 |
TEST_F(ParseCommandLineImplicitOptionAndFlag, parseCmdLine_varMapHasFlagAndImplicitOptionOn) |
| 152 |
{ |
| 153 |
stk::OptionsSpecification optionsSpec; |
| 154 |
stk::ParsedOptions parsedOptions; |
| 155 |
|
| 156 |
optionsSpec.add_options() |
| 157 |
("flag,f","this is a non-required flag") |
| 158 |
("aprepro,a", "this is an option", stk::ImplicitValue<std::string>("on")); |
| 159 |
|
| 160 |
stk::parse_command_line_args(argc, argv, optionsSpec, parsedOptions); |
| 161 |
|
| 162 |
EXPECT_FALSE(parsedOptions.empty()); |
| 163 |
EXPECT_EQ(1u, parsedOptions.count("flag")); |
| 164 |
EXPECT_TRUE(parsedOptions["flag"].empty()); |
| 165 |
EXPECT_EQ(1u, parsedOptions.count("aprepro")); |
| 166 |
std::string expectedValue = "on"; |
| 167 |
EXPECT_EQ(expectedValue, parsedOptions["aprepro"].as<std::string>()); |
| 168 |
} |
| 169 |
|
| 170 |
class ParseCommandLineImplicitOptionOffAndFlag : public ::testing::Test |
| 171 |
{ |
| 172 |
protected: |
| 173 |
static constexpr int argc = 4; |
| 174 |
const char *argv[argc] = {"exeName", "--aprepro", "off", "--flag"}; |
| 175 |
}; |
| 176 |
|
| 177 |
TEST_F(ParseCommandLineImplicitOptionOffAndFlag, parseCmdLine_varMapHasFlagAndImplicitOptionOff) |
| 178 |
{ |
| 179 |
stk::OptionsSpecification optionsSpec; |
| 180 |
stk::ParsedOptions parsedOptions; |
| 181 |
|
| 182 |
optionsSpec.add_options() |
| 183 |
("flag,f","this is a non-required flag") |
| 184 |
("aprepro,a", "this is an option", stk::ImplicitValue<std::string>("on")); |
| 185 |
|
| 186 |
stk::parse_command_line_args(argc, argv, optionsSpec, parsedOptions); |
| 187 |
|
| 188 |
EXPECT_FALSE(parsedOptions.empty()); |
| 189 |
EXPECT_EQ(1u, parsedOptions.count("flag")); |
| 190 |
EXPECT_TRUE(parsedOptions["flag"].empty()); |
| 191 |
EXPECT_EQ(1u, parsedOptions.count("aprepro")); |
| 192 |
std::string expectedValue = "off"; |
| 193 |
EXPECT_EQ(expectedValue, parsedOptions["aprepro"].as<std::string>()); |
| 194 |
} |
| 195 |
|
| 196 |
class ParseCommandLineImplicitOptionNotPassedAndFlag : public ::testing::Test |
| 197 |
{ |
| 198 |
protected: |
| 199 |
static constexpr int argc = 2; |
| 200 |
const char *argv[argc] = {"exeName", "--flag"}; |
| 201 |
}; |
| 202 |
|
| 203 |
TEST_F(ParseCommandLineImplicitOptionNotPassedAndFlag, parseCmdLine_varMapHasFlagAndNotImplicitOption) |
| 204 |
{ |
| 205 |
stk::OptionsSpecification optionsSpec; |
| 206 |
stk::ParsedOptions parsedOptions; |
| 207 |
|
| 208 |
optionsSpec.add_options() |
| 209 |
("flag,f","this is a non-required flag") |
| 210 |
("aprepro,a", "this is an option", stk::ImplicitValue<std::string>("on")); |
| 211 |
|
| 212 |
stk::parse_command_line_args(argc, argv, optionsSpec, parsedOptions); |
| 213 |
|
| 214 |
EXPECT_FALSE(parsedOptions.empty()); |
| 215 |
EXPECT_EQ(1u, parsedOptions.count("flag")); |
| 216 |
EXPECT_TRUE(parsedOptions["flag"].empty()); |
| 217 |
EXPECT_EQ(0u, parsedOptions.count("aprepro")); |
| 218 |
} |
| 219 |
|
| 220 |
class ParseCommandLineImplicitOptionEqualsOffAndFlag : public ::testing::Test |
| 221 |
{ |
| 222 |
protected: |
| 223 |
static constexpr int argc = 3; |
| 224 |
const char *argv[argc] = {"exeName", "--aprepro=off", "--flag"}; |
| 225 |
}; |
| 226 |
|
| 227 |
TEST_F(ParseCommandLineImplicitOptionEqualsOffAndFlag, parseCmdLine_varMapHasFlagAndImplicitOptionOff) |
| 228 |
{ |
| 229 |
stk::OptionsSpecification optionsSpec; |
| 230 |
stk::ParsedOptions parsedOptions; |
| 231 |
|
| 232 |
optionsSpec.add_options() |
| 233 |
("flag,f","this is a non-required flag") |
| 234 |
("aprepro,a", "this is an option", stk::ImplicitValue<std::string>("on")); |
| 235 |
|
| 236 |
stk::parse_command_line_args(argc, argv, optionsSpec, parsedOptions); |
| 237 |
|
| 238 |
EXPECT_FALSE(parsedOptions.empty()); |
| 239 |
EXPECT_EQ(1u, parsedOptions.count("flag")); |
| 240 |
EXPECT_TRUE(parsedOptions["flag"].empty()); |
| 241 |
EXPECT_EQ(1u, parsedOptions.count("aprepro")); |
| 242 |
std::string expectedValue = "off"; |
| 243 |
EXPECT_EQ(expectedValue, parsedOptions["aprepro"].as<std::string>()); |
| 244 |
} |
| 245 |
|
| 246 |
} |