diff options
Diffstat (limited to 'compiler/arser/tests/arser.test.cpp')
-rw-r--r-- | compiler/arser/tests/arser.test.cpp | 328 |
1 files changed, 232 insertions, 96 deletions
diff --git a/compiler/arser/tests/arser.test.cpp b/compiler/arser/tests/arser.test.cpp index 28bee4238..63121b845 100644 --- a/compiler/arser/tests/arser.test.cpp +++ b/compiler/arser/tests/arser.test.cpp @@ -23,30 +23,9 @@ #include "arser/arser.h" -using namespace arser; +#include "Prompt.h" -class Prompt -{ -public: - Prompt(const std::string &command) - { - std::istringstream iss(command); - std::vector<std::string> token(std::istream_iterator<std::string>{iss}, - std::istream_iterator<std::string>()); - _arg = std::move(token); - _argv.reserve(_arg.size()); - for (const auto &t : _arg) - { - _argv.push_back(const_cast<char *>(t.data())); - } - } - int argc(void) const { return _argv.size(); } - char **argv(void) { return _argv.data(); } - -private: - std::vector<char *> _argv; - std::vector<std::string> _arg; -}; +using namespace arser; TEST(BasicTest, option) { @@ -54,10 +33,10 @@ TEST(BasicTest, option) Arser arser; arser.add_argument("--verbose") - .nargs(0) - .help("It provides additional details as to what the executable is doing"); + .nargs(0) + .help("It provides additional details as to what the executable is doing"); - Prompt prompt("./executable --verbose"); + test::Prompt prompt("./executable --verbose"); /* act */ arser.parse(prompt.argc(), prompt.argv()); /* assert */ @@ -71,15 +50,15 @@ TEST(BasicTest, OptionalArgument) Arser arser; arser.add_argument("--volume") - .nargs(1) - .type(arser::DataType::INT32) - .help("Set a volume as you provided."); + .nargs(1) + .type(arser::DataType::INT32) + .help("Set a volume as you provided."); arser.add_argument("--frequency") - .nargs(1) - .type(arser::DataType::FLOAT) - .help("Set a frequency as you provided."); + .nargs(1) + .type(arser::DataType::FLOAT) + .help("Set a frequency as you provided."); - Prompt prompt("./radio --volume 5 --frequency 128.5"); + test::Prompt prompt("./radio --volume 5 --frequency 128.5"); /* act */ arser.parse(prompt.argc(), prompt.argv()); /* assert */ @@ -93,17 +72,17 @@ TEST(BasicTest, OptionalArgument) EXPECT_THROW(arser.get<bool>("--volume"), std::runtime_error); } -TEST(BasicTest, NonRequiredOptionalArgument) +TEST(BasicTest, NonRequiredOptionalArgument_NEG) { /* arrange */ Arser arser; arser.add_argument("--weight") - .nargs(1) - .type(arser::DataType::INT32) - .help("Set a volume as you provided."); + .nargs(1) + .type(arser::DataType::INT32) + .help("Set a volume as you provided."); - Prompt prompt("./radio"); // empty argument + test::Prompt prompt("./radio"); // empty argument /* act */ arser.parse(prompt.argc(), prompt.argv()); /* assert */ @@ -111,18 +90,18 @@ TEST(BasicTest, NonRequiredOptionalArgument) EXPECT_THROW(arser.get<int>("--weight"), std::runtime_error); } -TEST(BasicTest, RequiredOptionalArgument) +TEST(BasicTest, RequiredOptionalArgument_NEG) { /* arrange */ Arser arser; arser.add_argument("--volume") - .nargs(1) - .type(arser::DataType::INT32) - .required() - .help("Set a volume as you provided."); + .nargs(1) + .type(arser::DataType::INT32) + .required() + .help("Set a volume as you provided."); - Prompt prompt("./radio"); + test::Prompt prompt("./radio"); /* act */ /* assert */ EXPECT_THROW(arser.parse(prompt.argc(), prompt.argv()), std::runtime_error); } @@ -134,7 +113,7 @@ TEST(BasicTest, OptionalMultipleArgument) arser.add_argument("--add").nargs(2).type(arser::DataType::INT32_VEC).help("Add two numbers."); - Prompt prompt("./calculator --add 3 5"); + test::Prompt prompt("./calculator --add 3 5"); /* act */ arser.parse(prompt.argc(), prompt.argv()); /* assert */ @@ -152,23 +131,23 @@ TEST(BasicTest, MultipleOptionalArgument) Arser arser; arser.add_argument("--input_path") - .nargs(1) - .type(arser::DataType::STR) - .help("input path of this program.") - .required(); + .nargs(1) + .type(arser::DataType::STR) + .help("input path of this program.") + .required(); arser.add_argument("--output_path") - .nargs(1) - .type(arser::DataType::STR) - .help("output path of this program.") - .required(true); + .nargs(1) + .type(arser::DataType::STR) + .help("output path of this program.") + .required(true); arser.add_argument("--training_data") - .nargs(5) - .type(arser::DataType::INT32_VEC) - .help("give traning data to this program.") - .required(); + .nargs(5) + .type(arser::DataType::INT32_VEC) + .help("give traning data to this program.") + .required(); - Prompt prompt("./ml --input_path /I/am/in.put --output_path I/am/out.put " - "--training_data 2 43 234 3 334"); + test::Prompt prompt("./ml --input_path /I/am/in.put --output_path I/am/out.put " + "--training_data 2 43 234 3 334"); /* act */ arser.parse(prompt.argc(), prompt.argv()); /* assert */ @@ -191,11 +170,11 @@ TEST(BasicTest, MultipleFloatValue) Arser arser; arser.add_argument("--add_float") - .nargs(2) - .type(arser::DataType::FLOAT_VEC) - .help("Add two float numbers."); + .nargs(2) + .type(arser::DataType::FLOAT_VEC) + .help("Add two float numbers."); - Prompt prompt("./calculator --add_float 3.2 5.4"); + test::Prompt prompt("./calculator --add_float 3.2 5.4"); /* act */ arser.parse(prompt.argc(), prompt.argv()); /* assert */ @@ -213,11 +192,11 @@ TEST(BasicTest, MultipleStringValue) Arser arser; arser.add_argument("--three_color") - .nargs(3) - .type(arser::DataType::STR_VEC) - .help("insert your three favorite color"); + .nargs(3) + .type(arser::DataType::STR_VEC) + .help("insert your three favorite color"); - Prompt prompt("./color_factory --three_color red blue yellow"); + test::Prompt prompt("./color_factory --three_color red blue yellow"); /* act */ arser.parse(prompt.argc(), prompt.argv()); /* assert */ @@ -241,7 +220,7 @@ TEST(BasicTest, ExitWithFunctionCall) arser.add_argument("--name").nargs(1).type(arser::DataType::STR).help("Name your hero"); - Prompt prompt("./hero --history"); + test::Prompt prompt("./hero --history"); /* act */ /* assert */ EXPECT_EXIT(arser.parse(prompt.argc(), prompt.argv()), testing::ExitedWithCode(0), "When I was young.."); @@ -255,10 +234,10 @@ TEST(BasicTest, ExitWithFunctionCallWithBind) Arser arser; arser.add_argument("--version") - .help("Show version and exit") - .exit_with(std::bind(printVersion, "1.2.0")); + .help("Show version and exit") + .exit_with(std::bind(printVersion, "1.2.0")); - Prompt prompt("./arser --version"); + test::Prompt prompt("./arser --version"); /* act */ /* assert */ EXPECT_EXIT(arser.parse(prompt.argc(), prompt.argv()), testing::ExitedWithCode(0), "arser version : 1.2.0"); @@ -275,7 +254,7 @@ TEST(BasicTest, ExitWithFunctionCallWithLamda) arser.add_argument("OS").nargs(1).type(arser::DataType::STR).help("The OS you want to boot"); - Prompt prompt("./computer --shutdown"); + test::Prompt prompt("./computer --shutdown"); /* act */ /* assert */ EXPECT_EXIT(arser.parse(prompt.argc(), prompt.argv()), testing::ExitedWithCode(0), "Good bye.."); } @@ -286,36 +265,36 @@ TEST(BasicTest, DefaultValue) Arser arser; arser.add_argument("--delivery") - .nargs(3) - .type(arser::DataType::STR_VEC) - .default_value("pizza", "chicken", "hamburger") - .help("Enter three foods that you want to deliver"); + .nargs(3) + .type(arser::DataType::STR_VEC) + .default_value("pizza", "chicken", "hamburger") + .help("Enter three foods that you want to deliver"); arser.add_argument("--assistant") - .type(arser::DataType::STR) - .default_value("Bixby") - .help("Enter name of your assistant"); + .type(arser::DataType::STR) + .default_value("Bixby") + .help("Enter name of your assistant"); arser.add_argument("--sound") - .type(arser::DataType::BOOL) - .nargs(1) - .default_value(true) - .help("Sound on/off"); + .type(arser::DataType::BOOL) + .nargs(1) + .default_value(true) + .help("Sound on/off"); arser.add_argument("--number") - .type(arser::DataType::INT32_VEC) - .nargs(4) - .default_value(1, 2, 3, 4) - .help("Enter the number that you want to call"); + .type(arser::DataType::INT32_VEC) + .nargs(4) + .default_value(1, 2, 3, 4) + .help("Enter the number that you want to call"); arser.add_argument("--time") - .type(arser::DataType::INT32_VEC) - .nargs(3) - .default_value(0, 0, 0) - .help("Current time(H/M/S)"); + .type(arser::DataType::INT32_VEC) + .nargs(3) + .default_value(0, 0, 0) + .help("Current time(H/M/S)"); arser.add_argument("--name") - .type(arser::DataType::STR) - .nargs(1) - .default_value("no name") - .help("Enter your name"); + .type(arser::DataType::STR) + .nargs(1) + .default_value("no name") + .help("Enter your name"); - Prompt prompt("/phone --time 1 52 34 --name arser"); + test::Prompt prompt("/phone --time 1 52 34 --name arser"); /* act */ arser.parse(prompt.argc(), prompt.argv()); /* assert */ @@ -342,3 +321,160 @@ TEST(BasicTest, DefaultValue) // 1 string, 1 argument EXPECT_EQ("arser", arser.get<std::string>("--name")); } + +TEST(BasicTest, shortOption) +{ + /* arrange */ + Arser arser; + + arser.add_argument("--input_path", "-i") + .nargs(1) + .type(arser::DataType::STR) + .help("input path of this program.") + .required(); + arser.add_argument("--output_path", "-o") + .nargs(1) + .type(arser::DataType::STR) + .help("output path of this program.") + .required(true); + + test::Prompt prompt("./driver -i /I/am/in.put --output_path I/am/out.put"); + /* act */ + arser.parse(prompt.argc(), prompt.argv()); + /* assert */ + EXPECT_TRUE(arser["--input_path"]); + EXPECT_EQ("/I/am/in.put", arser.get<std::string>("--input_path")); + EXPECT_TRUE(arser["--output_path"]); + EXPECT_EQ("I/am/out.put", arser.get<std::string>("--output_path")); +} + +TEST(BasicTest, shortMultipleOption) +{ + /* arrange */ + Arser arser; + + arser.add_argument("--input_path", "-i", "--input", "--in") + .nargs(1) + .type(arser::DataType::STR) + .help("input path of this program.") + .required(); + arser.add_argument("--output_path", "-o") + .nargs(1) + .type(arser::DataType::STR) + .help("output path of this program.") + .required(true); + + test::Prompt prompt("./driver --in /I/am/in.put -o I/am/out.put"); + /* act */ + arser.parse(prompt.argc(), prompt.argv()); + /* assert */ + EXPECT_TRUE(arser["--input"]); + EXPECT_EQ("/I/am/in.put", arser.get<std::string>("--input")); + EXPECT_TRUE(arser["--output_path"]); + EXPECT_EQ("I/am/out.put", arser.get<std::string>("--output_path")); +} + +TEST(BasicTest, OptWithRequiredDuplicate_NEG) +{ + /* arrange */ + Arser arser; + + arser.add_argument("--input_path", "-i", "--input", "--in") + .nargs(1) + .type(arser::DataType::STR) + .help("input path of this program.") + .required(); + arser.add_argument("--output_path", "-o") + .nargs(1) + .type(arser::DataType::STR) + .help("output path of this program.") + .required(true); + + test::Prompt prompt("./driver --in /I/am/in.put -o I/am/out.put -i /I/am/duplicate"); + /* act */ /* assert */ + EXPECT_THROW(arser.parse(prompt.argc(), prompt.argv()), std::runtime_error); +} + +TEST(BasicTest, OptWithNonRequiredDuplicate) +{ + /* arrange */ + Arser arser; + + arser.add_argument("--input_path", "-i", "--input", "--in") + .nargs(1) + .type(arser::DataType::STR) + .help("input path of this program."); + /* .required() */ + arser.add_argument("--output_path", "-o") + .nargs(1) + .type(arser::DataType::STR) + .help("output path of this program.") + .required(true); + + test::Prompt prompt("./driver --in /I/am/in.put -o I/am/out.put -i /I/am/duplicate"); + /* act */ + arser.parse(prompt.argc(), prompt.argv()); + /* assert */ + EXPECT_TRUE(arser["--input"]); + EXPECT_EQ("/I/am/duplicate", arser.get<std::string>("--input")); + EXPECT_TRUE(arser["--output_path"]); + EXPECT_EQ("I/am/out.put", arser.get<std::string>("--output_path")); +} + +TEST(BasicTest, AccumulateVectorOptions) +{ + /* arrange */ + Arser arser; + + arser.add_argument("--specify").nargs(3).accumulated(true).type(arser::DataType::STR_VEC); + + test::Prompt prompt("./driver --specify a b c --specify 1 2 3"); + /* act */ + arser.parse(prompt.argc(), prompt.argv()); + /* assert */ + EXPECT_TRUE(arser["--specify"]); + + auto specify = arser.get<std::vector<std::vector<std::string>>>("--specify"); + auto first = specify[0]; + EXPECT_EQ("a", first.at(0)); + EXPECT_EQ("b", first.at(1)); + EXPECT_EQ("c", first.at(2)); + auto second = specify[1]; + EXPECT_EQ("1", second.at(0)); + EXPECT_EQ("2", second.at(1)); + EXPECT_EQ("3", second.at(2)); +} + +TEST(BasicTest, AccumulateScalarOptions) +{ + /* arrange */ + Arser arser; + + arser.add_argument("--specify").nargs(1).accumulated(true).type(arser::DataType::FLOAT); + + test::Prompt prompt("./driver --specify 1 --specify 2"); + /* act */ + arser.parse(prompt.argc(), prompt.argv()); + /* assert */ + EXPECT_TRUE(arser["--specify"]); + + auto specify = arser.get<std::vector<float>>("--specify"); + EXPECT_EQ(1, specify.at(0)); + EXPECT_EQ(2, specify.at(1)); +} + +TEST(BasicTest, AccumulateScalarOptions_WrongType_NEG) +{ + /* arrange */ + Arser arser; + + arser.add_argument("--specify").nargs(1).accumulated(true).type(arser::DataType::FLOAT); + + test::Prompt prompt("./driver --specify 1 --specify 2"); + /* act */ + arser.parse(prompt.argc(), prompt.argv()); + /* assert */ + EXPECT_TRUE(arser["--specify"]); + + EXPECT_THROW(arser.get<float>("--specify"), std::runtime_error); +} |