| 1 |
#include "gtest/gtest.h" |
| 2 |
#include "stk_util/environment/OptionsSpecification.hpp" // for OptionsSpecification, operator<< |
| 3 |
#include <ostream> // for operator<<, endl, ostringstream |
| 4 |
#include <string> // for char_traits, string, basic_string |
| 5 |
#include <vector> // for vector |
| 6 |
|
| 7 |
namespace { |
| 8 |
|
| 9 |
TEST(OptionsSpecification, construct_empty) |
| 10 |
{ |
| 11 |
stk::OptionsSpecification optionsSpec; |
| 12 |
|
| 13 |
EXPECT_TRUE(optionsSpec.empty()); |
| 14 |
} |
| 15 |
|
| 16 |
TEST(OptionsSpecification, insert_flag_and_print) |
| 17 |
{ |
| 18 |
stk::OptionsSpecification optionsSpec; |
| 19 |
|
| 20 |
optionsSpec.add_options()("flag,f", "this is a flag"); |
| 21 |
|
| 22 |
EXPECT_EQ(1u, optionsSpec.size()); |
| 23 |
EXPECT_FALSE(optionsSpec.empty()); |
| 24 |
|
| 25 |
std::ostringstream os; |
| 26 |
os << optionsSpec; |
| 27 |
std::ostringstream oss; |
| 28 |
oss<<"--flag,-f this is a flag"<<std::endl<<std::endl; |
| 29 |
std::string expected = oss.str(); |
| 30 |
EXPECT_EQ(expected, os.str()); |
| 31 |
} |
| 32 |
|
| 33 |
TEST(OptionsSpecification, insert_required_option_and_print) |
| 34 |
{ |
| 35 |
stk::OptionsSpecification optionsSpec; |
| 36 |
|
| 37 |
const bool isFlag = true; |
| 38 |
const bool isRequired = true; |
| 39 |
optionsSpec.add_options()("option,o", isFlag, isRequired, "this is a required option"); |
| 40 |
|
| 41 |
EXPECT_EQ(1u, optionsSpec.size()); |
| 42 |
EXPECT_FALSE(optionsSpec.empty()); |
| 43 |
|
| 44 |
std::ostringstream os; |
| 45 |
os << optionsSpec; |
| 46 |
std::ostringstream oss; |
| 47 |
oss<<"--option,-o this is a required option"<<std::endl; |
| 48 |
oss<<" (required)"<<std::endl<<std::endl; |
| 49 |
std::string expected = oss.str(); |
| 50 |
EXPECT_EQ(expected, os.str()); |
| 51 |
} |
| 52 |
|
| 53 |
TEST(OptionsSpecification, insert_option_with_default_value_and_print) |
| 54 |
{ |
| 55 |
stk::OptionsSpecification optionsSpec; |
| 56 |
|
| 57 |
optionsSpec.add_options()("option,o", "this is an option", stk::DefaultValue<double>(99.9)); |
| 58 |
|
| 59 |
EXPECT_EQ(1u, optionsSpec.size()); |
| 60 |
EXPECT_FALSE(optionsSpec.empty()); |
| 61 |
|
| 62 |
std::ostringstream os; |
| 63 |
os << optionsSpec; |
| 64 |
std::ostringstream oss; |
| 65 |
oss<<"--option,-o this is an option"<<std::endl; |
| 66 |
oss<<" (default: 99.9)"<<std::endl<<std::endl; |
| 67 |
std::string expected = oss.str(); |
| 68 |
EXPECT_EQ(expected, os.str()); |
| 69 |
} |
| 70 |
|
| 71 |
TEST(OptionsSpecification, insert_option_with_implicit_value_and_print) |
| 72 |
{ |
| 73 |
stk::OptionsSpecification optionsSpec; |
| 74 |
|
| 75 |
optionsSpec.add_options()("option,o", "this is an option", stk::ImplicitValue<double>(99.9)); |
| 76 |
|
| 77 |
EXPECT_EQ(1u, optionsSpec.size()); |
| 78 |
EXPECT_FALSE(optionsSpec.empty()); |
| 79 |
|
| 80 |
std::ostringstream os; |
| 81 |
os << optionsSpec; |
| 82 |
std::ostringstream oss; |
| 83 |
oss<<"--option,-o this is an option"<<std::endl; |
| 84 |
oss<<" (default: 99.9)"<<std::endl<<std::endl; |
| 85 |
std::string expected = oss.str(); |
| 86 |
EXPECT_EQ(expected, os.str()); |
| 87 |
} |
| 88 |
|
| 89 |
TEST(OptionsSpecification, insert_flag_and_option_with_default_value_and_print) |
| 90 |
{ |
| 91 |
stk::OptionsSpecification optionsSpec; |
| 92 |
|
| 93 |
optionsSpec.add_options()("flag,f", "this is a flag"); |
| 94 |
optionsSpec.add_options()("option,o", "this is an option", stk::DefaultValue<double>(99.9)); |
| 95 |
|
| 96 |
EXPECT_EQ(2u, optionsSpec.size()); |
| 97 |
EXPECT_FALSE(optionsSpec.empty()); |
| 98 |
|
| 99 |
std::ostringstream os; |
| 100 |
os << optionsSpec; |
| 101 |
std::ostringstream oss; |
| 102 |
oss<<"--flag,-f this is a flag"<<std::endl; |
| 103 |
oss<<"--option,-o this is an option"<<std::endl; |
| 104 |
oss<<" (default: 99.9)"<<std::endl<<std::endl; |
| 105 |
std::string expected = oss.str(); |
| 106 |
EXPECT_EQ(expected, os.str()); |
| 107 |
} |
| 108 |
|
| 109 |
TEST(OptionsSpecification, insert_positional_option_and_test) |
| 110 |
{ |
| 111 |
stk::OptionsSpecification optionsSpec; |
| 112 |
|
| 113 |
const bool isFlag = false; |
| 114 |
const bool isRequired = false; |
| 115 |
const int expectedPosition = 3; |
| 116 |
optionsSpec.add_options()("positional,p","positional option", |
| 117 |
stk::DefaultValue<std::string>("default"), |
| 118 |
isFlag, isRequired, expectedPosition); |
| 119 |
const stk::Option& option = optionsSpec.get_positional_option(expectedPosition); |
| 120 |
EXPECT_EQ(expectedPosition, option.position); |
| 121 |
|
| 122 |
const stk::Option& invalidOption = optionsSpec.get_positional_option(0); |
| 123 |
EXPECT_EQ(stk::INVALID_POSITION, invalidOption.position); |
| 124 |
} |
| 125 |
|
| 126 |
TEST(OptionsSpecification, print_sub_options) |
| 127 |
{ |
| 128 |
stk::OptionsSpecification optionsSpec("Main Options"); |
| 129 |
|
| 130 |
optionsSpec.add_options()("flag,f", "this is a flag"); |
| 131 |
optionsSpec.add_options()("option,o", "this is an option", stk::DefaultValue<double>(99.9)); |
| 132 |
|
| 133 |
{ |
| 134 |
stk::OptionsSpecification subOptionsSpec("Sub Options"); |
| 135 |
|
| 136 |
subOptionsSpec.add_options()("input-file,i", "input file"); |
| 137 |
subOptionsSpec.add_options()("log-file,l", "log file"); |
| 138 |
|
| 139 |
optionsSpec.add(subOptionsSpec); |
| 140 |
} |
| 141 |
|
| 142 |
const stk::Option& option = optionsSpec.find_option("input-file"); |
| 143 |
EXPECT_FALSE(option.name.empty()); |
| 144 |
|
| 145 |
std::ostringstream os; |
| 146 |
os << optionsSpec; |
| 147 |
std::ostringstream oss; |
| 148 |
oss<<"Main Options\n"<<std::endl; |
| 149 |
oss<<"--flag,-f this is a flag"<<std::endl; |
| 150 |
oss<<"--option,-o this is an option"<<std::endl; |
| 151 |
oss<<" (default: 99.9)"<<std::endl<<std::endl; |
| 152 |
oss<<"Sub Options\n"<<std::endl; |
| 153 |
oss<<"--input-file,-i input file"<<std::endl; |
| 154 |
oss<<"--log-file,-l log file"<<std::endl<<std::endl; |
| 155 |
std::string expected = oss.str(); |
| 156 |
EXPECT_EQ(expected, os.str()); |
| 157 |
} |
| 158 |
|
| 159 |
} |