Added unit tests
This commit is contained in:
parent
4d5547c02f
commit
1cd9abd11d
161
Test_Hazelnupp/Abbreviations.cpp
Normal file
161
Test_Hazelnupp/Abbreviations.cpp
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
#include "CppUnitTest.h"
|
||||||
|
#include "helper.h"
|
||||||
|
#include "../Hazelnupp/Hazelnupp.h"
|
||||||
|
|
||||||
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||||
|
|
||||||
|
namespace TestHazelnupp
|
||||||
|
{
|
||||||
|
TEST_CLASS(_Abbreviations)
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
// Tests keys exist after parsing
|
||||||
|
TEST_METHOD(KeysExist)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"-ms",
|
||||||
|
"billybob",
|
||||||
|
"-mv",
|
||||||
|
"-mf",
|
||||||
|
"-23.199",
|
||||||
|
"-mi",
|
||||||
|
"199",
|
||||||
|
"-mnl",
|
||||||
|
"1",
|
||||||
|
"2",
|
||||||
|
"3",
|
||||||
|
"4",
|
||||||
|
"-msl",
|
||||||
|
"apple",
|
||||||
|
"banana",
|
||||||
|
"pumpkin",
|
||||||
|
});
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
Hazelnupp nupp;
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
nupp.RegisterAbbreviation("-ms", "--my_string");
|
||||||
|
nupp.RegisterAbbreviation("-mv", "--my_void");
|
||||||
|
nupp.RegisterAbbreviation("-mi", "--my_int");
|
||||||
|
nupp.RegisterAbbreviation("-mf", "--my_float");
|
||||||
|
nupp.RegisterAbbreviation("-mnl", "--my_num_list");
|
||||||
|
nupp.RegisterAbbreviation("-msl", "--my_str_list");
|
||||||
|
|
||||||
|
nupp.Parse(C_Ify(args));
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
Assert::IsTrue(nupp.HasParam("--my_string"));
|
||||||
|
Assert::IsTrue(nupp.HasParam("--my_void"));
|
||||||
|
Assert::IsTrue(nupp.HasParam("--my_float"));
|
||||||
|
Assert::IsTrue(nupp.HasParam("--my_int"));
|
||||||
|
Assert::IsTrue(nupp.HasParam("--my_num_list"));
|
||||||
|
Assert::IsTrue(nupp.HasParam("--my_str_list"));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests keys are of the correct type after parsing
|
||||||
|
TEST_METHOD(CorrectType)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"-ms",
|
||||||
|
"billybob",
|
||||||
|
"-mv",
|
||||||
|
"-mf",
|
||||||
|
"-23.199",
|
||||||
|
"-mi",
|
||||||
|
"199",
|
||||||
|
"-mnl",
|
||||||
|
"1",
|
||||||
|
"2",
|
||||||
|
"3",
|
||||||
|
"4",
|
||||||
|
"-msl",
|
||||||
|
"apple",
|
||||||
|
"banana",
|
||||||
|
"pumpkin",
|
||||||
|
});
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
Hazelnupp nupp;
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
nupp.RegisterAbbreviation("-ms", "--my_string");
|
||||||
|
nupp.RegisterAbbreviation("-mv", "--my_void");
|
||||||
|
nupp.RegisterAbbreviation("-mi", "--my_int");
|
||||||
|
nupp.RegisterAbbreviation("-mf", "--my_float");
|
||||||
|
nupp.RegisterAbbreviation("-mnl", "--my_num_list");
|
||||||
|
nupp.RegisterAbbreviation("-msl", "--my_str_list");
|
||||||
|
|
||||||
|
nupp.Parse(C_Ify(args));
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
Assert::IsTrue(nupp["--my_string"].GetDataType() == DATA_TYPE::STRING);
|
||||||
|
Assert::IsTrue(nupp["--my_void"].GetDataType() == DATA_TYPE::VOID);
|
||||||
|
Assert::IsTrue(nupp["--my_float"].GetDataType() == DATA_TYPE::FLOAT);
|
||||||
|
Assert::IsTrue(nupp["--my_int"].GetDataType() == DATA_TYPE::INT);
|
||||||
|
Assert::IsTrue(nupp["--my_num_list"].GetDataType() == DATA_TYPE::LIST);
|
||||||
|
Assert::IsTrue(nupp["--my_str_list"].GetDataType() == DATA_TYPE::LIST);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests keys have the correct value after parsing
|
||||||
|
TEST_METHOD(CorrectValues)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"-ms",
|
||||||
|
"billybob",
|
||||||
|
"-mv",
|
||||||
|
"-mf",
|
||||||
|
"-23.199",
|
||||||
|
"-mi",
|
||||||
|
"199",
|
||||||
|
"-mnl",
|
||||||
|
"1",
|
||||||
|
"2",
|
||||||
|
"3",
|
||||||
|
"4",
|
||||||
|
"-msl",
|
||||||
|
"apple",
|
||||||
|
"banana",
|
||||||
|
"pumpkin",
|
||||||
|
});
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
Hazelnupp nupp;
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
nupp.RegisterAbbreviation("-ms", "--my_string");
|
||||||
|
nupp.RegisterAbbreviation("-mv", "--my_void");
|
||||||
|
nupp.RegisterAbbreviation("-mi", "--my_int");
|
||||||
|
nupp.RegisterAbbreviation("-mf", "--my_float");
|
||||||
|
nupp.RegisterAbbreviation("-mnl", "--my_num_list");
|
||||||
|
nupp.RegisterAbbreviation("-msl", "--my_str_list");
|
||||||
|
|
||||||
|
nupp.Parse(C_Ify(args));
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
Assert::AreEqual(nupp["--my_string"].GetString(), std::string("billybob"));
|
||||||
|
Assert::AreEqual(nupp["--my_float"].GetFloat32(), -23.199);
|
||||||
|
Assert::AreEqual(nupp["--my_int"].GetInt32(), 199);
|
||||||
|
Assert::AreEqual(nupp["--my_num_list"].GetList()[0]->GetInt32(), 1);
|
||||||
|
Assert::AreEqual(nupp["--my_num_list"].GetList()[1]->GetInt32(), 2);
|
||||||
|
Assert::AreEqual(nupp["--my_num_list"].GetList()[2]->GetInt32(), 3);
|
||||||
|
Assert::AreEqual(nupp["--my_num_list"].GetList()[3]->GetInt32(), 4);
|
||||||
|
Assert::AreEqual(nupp["--my_str_list"].GetList()[0]->GetString(), std::string("apple"));
|
||||||
|
Assert::AreEqual(nupp["--my_str_list"].GetList()[1]->GetString(), std::string("banana"));
|
||||||
|
Assert::AreEqual(nupp["--my_str_list"].GetList()[2]->GetString(), std::string("pumpkin"));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
#include "CppUnitTest.h"
|
#include "CppUnitTest.h"
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
#include "../Hazelnupp/Hazelnupp.h"
|
#include "../Hazelnupp/Hazelnupp.h"
|
||||||
|
#include "../Hazelnupp/HazelnuppException.h"
|
||||||
|
|
||||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||||
|
|
||||||
@ -20,6 +21,7 @@ namespace TestHazelnupp
|
|||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
Hazelnupp nupp(C_Ify(args));
|
Hazelnupp nupp(C_Ify(args));
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
Assert::AreEqual(std::string("/my/fake/path/wahoo.out"), nupp.GetExecutableName());
|
Assert::AreEqual(std::string("/my/fake/path/wahoo.out"), nupp.GetExecutableName());
|
||||||
@ -27,8 +29,27 @@ namespace TestHazelnupp
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Edgecase test: We only have one param.
|
||||||
|
TEST_METHOD(Only_One_Param)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"--dummy"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
Hazelnupp nupp(C_Ify(args));
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
Assert::IsTrue(nupp.HasParam("--dummy"));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Tests keys exist after parsing
|
// Tests keys exist after parsing
|
||||||
TEST_METHOD(BasicParsing_KeysExist)
|
TEST_METHOD(KeysExist)
|
||||||
{
|
{
|
||||||
// Setup
|
// Setup
|
||||||
ArgList args({
|
ArgList args({
|
||||||
@ -53,6 +74,7 @@ namespace TestHazelnupp
|
|||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
Hazelnupp nupp(C_Ify(args));
|
Hazelnupp nupp(C_Ify(args));
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
Assert::IsTrue(nupp.HasParam("--my_string"));
|
Assert::IsTrue(nupp.HasParam("--my_string"));
|
||||||
@ -66,7 +88,7 @@ namespace TestHazelnupp
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Tests keys are of the correct type after parsing
|
// Tests keys are of the correct type after parsing
|
||||||
TEST_METHOD(BasicParsing_CorrectType)
|
TEST_METHOD(CorrectType)
|
||||||
{
|
{
|
||||||
// Setup
|
// Setup
|
||||||
ArgList args({
|
ArgList args({
|
||||||
@ -91,6 +113,7 @@ namespace TestHazelnupp
|
|||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
Hazelnupp nupp(C_Ify(args));
|
Hazelnupp nupp(C_Ify(args));
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
Assert::IsTrue(nupp["--my_string"].GetDataType() == DATA_TYPE::STRING);
|
Assert::IsTrue(nupp["--my_string"].GetDataType() == DATA_TYPE::STRING);
|
||||||
@ -104,7 +127,7 @@ namespace TestHazelnupp
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Tests keys have the correct value after parsing
|
// Tests keys have the correct value after parsing
|
||||||
TEST_METHOD(BasicParsing_CorrectValues)
|
TEST_METHOD(CorrectValues)
|
||||||
{
|
{
|
||||||
// Setup
|
// Setup
|
||||||
ArgList args({
|
ArgList args({
|
||||||
@ -129,6 +152,7 @@ namespace TestHazelnupp
|
|||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
Hazelnupp nupp(C_Ify(args));
|
Hazelnupp nupp(C_Ify(args));
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
Assert::AreEqual(nupp["--my_string"].GetString(), std::string("billybob"));
|
Assert::AreEqual(nupp["--my_string"].GetString(), std::string("billybob"));
|
||||||
@ -144,5 +168,44 @@ namespace TestHazelnupp
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tests that an HazelnuppInvalidKeyException gets raised, if an invalid gey is tried to access
|
||||||
|
TEST_METHOD(Exception_On_Invalid_Key)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"--my_string",
|
||||||
|
"billybob",
|
||||||
|
"--my_void",
|
||||||
|
"--my_float",
|
||||||
|
"-23.199",
|
||||||
|
"--my_int",
|
||||||
|
"199",
|
||||||
|
"--my_num_list",
|
||||||
|
"1",
|
||||||
|
"2",
|
||||||
|
"3",
|
||||||
|
"4",
|
||||||
|
"--my_str_list",
|
||||||
|
"apple",
|
||||||
|
"banana",
|
||||||
|
"pumpkin",
|
||||||
|
});
|
||||||
|
|
||||||
|
Hazelnupp nupp(C_Ify(args));
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
// Exercise, Verify
|
||||||
|
Assert::ExpectException<HazelnuppInvalidKeyException>(
|
||||||
|
[args]
|
||||||
|
{
|
||||||
|
Hazelnupp nupp(C_Ify(args));
|
||||||
|
nupp["--borrnana"];
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
1
Test_Hazelnupp/Constraints.cpp
Normal file
1
Test_Hazelnupp/Constraints.cpp
Normal file
@ -0,0 +1 @@
|
|||||||
|
#include "Constraints.h"
|
248
Test_Hazelnupp/Constraints.h
Normal file
248
Test_Hazelnupp/Constraints.h
Normal file
@ -0,0 +1,248 @@
|
|||||||
|
#include "CppUnitTest.h"
|
||||||
|
#include "helper.h"
|
||||||
|
#include "../Hazelnupp/Hazelnupp.h"
|
||||||
|
#include "../Hazelnupp/HazelnuppException.h"
|
||||||
|
|
||||||
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||||
|
|
||||||
|
namespace TestHazelnupp
|
||||||
|
{
|
||||||
|
TEST_CLASS(_Constraints)
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
// Tests that default values get added
|
||||||
|
TEST_METHOD(DefaultValues_GetAdded)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"--dummy",
|
||||||
|
"123"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
Hazelnupp nupp;
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
nupp.RegisterConstraints({
|
||||||
|
ParamConstraint("--elenor-int", {"5994"}),
|
||||||
|
ParamConstraint("--federich-float", {"420.69"}),
|
||||||
|
ParamConstraint("--siegbert-string", {"banana"}),
|
||||||
|
ParamConstraint("--lieber-liste", {"banana", "apple", "59"})
|
||||||
|
});
|
||||||
|
|
||||||
|
nupp.Parse(C_Ify(args));
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
Assert::IsTrue(nupp.HasParam("--elenor-int"));
|
||||||
|
Assert::IsTrue(nupp["--elenor-int"].GetDataType() == DATA_TYPE::INT);
|
||||||
|
Assert::AreEqual(nupp["--elenor-int"].GetInt32(), 5994);
|
||||||
|
|
||||||
|
Assert::IsTrue(nupp.HasParam("--federich-float"));
|
||||||
|
Assert::IsTrue(nupp["--federich-float"].GetDataType() == DATA_TYPE::FLOAT);
|
||||||
|
Assert::AreEqual(nupp["--federich-float"].GetFloat32(), 420.69);
|
||||||
|
|
||||||
|
Assert::IsTrue(nupp.HasParam("--siegbert-string"));
|
||||||
|
Assert::IsTrue(nupp["--siegbert-string"].GetDataType() == DATA_TYPE::STRING);
|
||||||
|
Assert::AreEqual(nupp["--siegbert-string"].GetString(), std::string("banana"));
|
||||||
|
|
||||||
|
Assert::IsTrue(nupp.HasParam("--lieber-liste"));
|
||||||
|
Assert::IsTrue(nupp["--lieber-liste"].GetDataType() == DATA_TYPE::LIST);
|
||||||
|
Assert::AreEqual(nupp["--lieber-liste"].GetList()[0]->GetString(), std::string("banana"));
|
||||||
|
Assert::AreEqual(nupp["--lieber-liste"].GetList()[1]->GetString(), std::string("apple"));
|
||||||
|
Assert::AreEqual(nupp["--lieber-liste"].GetList()[2]->GetInt32(), 59);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that the default values do not override actually set values
|
||||||
|
TEST_METHOD(DefaultValues_DefaultDoesntOverride)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"--dummy",
|
||||||
|
"--elenor-int",
|
||||||
|
"5994",
|
||||||
|
"--federich-float",
|
||||||
|
"420.69",
|
||||||
|
"--siegbert-string",
|
||||||
|
"banana",
|
||||||
|
"--lieber-liste",
|
||||||
|
"banana",
|
||||||
|
"apple",
|
||||||
|
"59"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
Hazelnupp nupp;
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
nupp.RegisterConstraints({
|
||||||
|
ParamConstraint("--elenor-int", {"6871"}),
|
||||||
|
ParamConstraint("--federich-float", {"-199.44"}),
|
||||||
|
ParamConstraint("--siegbert-string", {"bornana"}),
|
||||||
|
ParamConstraint("--lieber-liste", {"bornana", "ollpe", "5"})
|
||||||
|
});
|
||||||
|
|
||||||
|
nupp.Parse(C_Ify(args));
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
Assert::IsTrue(nupp.HasParam("--elenor-int"));
|
||||||
|
Assert::IsTrue(nupp["--elenor-int"].GetDataType() == DATA_TYPE::INT);
|
||||||
|
Assert::AreEqual(nupp["--elenor-int"].GetInt32(), 5994);
|
||||||
|
|
||||||
|
Assert::IsTrue(nupp.HasParam("--federich-float"));
|
||||||
|
Assert::IsTrue(nupp["--federich-float"].GetDataType() == DATA_TYPE::FLOAT);
|
||||||
|
Assert::AreEqual(nupp["--federich-float"].GetFloat32(), 420.69);
|
||||||
|
|
||||||
|
Assert::IsTrue(nupp.HasParam("--siegbert-string"));
|
||||||
|
Assert::IsTrue(nupp["--siegbert-string"].GetDataType() == DATA_TYPE::STRING);
|
||||||
|
Assert::AreEqual(nupp["--siegbert-string"].GetString(), std::string("banana"));
|
||||||
|
|
||||||
|
Assert::IsTrue(nupp.HasParam("--lieber-liste"));
|
||||||
|
Assert::IsTrue(nupp["--lieber-liste"].GetDataType() == DATA_TYPE::LIST);
|
||||||
|
Assert::AreEqual(nupp["--lieber-liste"].GetList()[0]->GetString(), std::string("banana"));
|
||||||
|
Assert::AreEqual(nupp["--lieber-liste"].GetList()[1]->GetString(), std::string("apple"));
|
||||||
|
Assert::AreEqual(nupp["--lieber-liste"].GetList()[2]->GetInt32(), 59);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that data types get forced according to the constraints
|
||||||
|
TEST_METHOD(ForceTypes)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"--dummy",
|
||||||
|
"--num-apples",
|
||||||
|
"39.75",
|
||||||
|
"--table-height",
|
||||||
|
"400",
|
||||||
|
"--license-plate",
|
||||||
|
"193273",
|
||||||
|
"--fav-fruits",
|
||||||
|
"apple",
|
||||||
|
"--indices",
|
||||||
|
"9",
|
||||||
|
"--force",
|
||||||
|
"plsdontuseme"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
Hazelnupp nupp;
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
nupp.RegisterConstraints({
|
||||||
|
ParamConstraint("--num-apples", true, DATA_TYPE::INT),
|
||||||
|
ParamConstraint("--table-height", true, DATA_TYPE::FLOAT),
|
||||||
|
ParamConstraint("--license-plate", true, DATA_TYPE::STRING),
|
||||||
|
ParamConstraint("--fav-fruits", true, DATA_TYPE::LIST),
|
||||||
|
ParamConstraint("--indices", true, DATA_TYPE::LIST),
|
||||||
|
ParamConstraint("--force", true, DATA_TYPE::VOID),
|
||||||
|
});
|
||||||
|
|
||||||
|
nupp.Parse(C_Ify(args));
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
Assert::IsTrue(nupp.HasParam("--num-apples"));
|
||||||
|
Assert::IsTrue(nupp["--num-apples"].GetDataType() == DATA_TYPE::INT);
|
||||||
|
Assert::AreEqual(nupp["--num-apples"].GetInt32(), 39);
|
||||||
|
|
||||||
|
Assert::IsTrue(nupp.HasParam("--table-height"));
|
||||||
|
Assert::IsTrue(nupp["--table-height"].GetDataType() == DATA_TYPE::FLOAT);
|
||||||
|
Assert::AreEqual(nupp["--table-height"].GetFloat32(), 400.0);
|
||||||
|
|
||||||
|
Assert::IsTrue(nupp.HasParam("--license-plate"));
|
||||||
|
Assert::IsTrue(nupp["--license-plate"].GetDataType() == DATA_TYPE::STRING);
|
||||||
|
Assert::AreEqual(nupp["--license-plate"].GetString(), std::string("193273"));
|
||||||
|
|
||||||
|
Assert::IsTrue(nupp.HasParam("--fav-fruits"));
|
||||||
|
Assert::IsTrue(nupp["--fav-fruits"].GetDataType() == DATA_TYPE::LIST);
|
||||||
|
Assert::AreEqual(nupp["--fav-fruits"].GetList()[0]->GetString(), std::string("apple"));
|
||||||
|
|
||||||
|
Assert::IsTrue(nupp.HasParam("--indices"));
|
||||||
|
Assert::IsTrue(nupp["--indices"].GetDataType() == DATA_TYPE::LIST);
|
||||||
|
Assert::AreEqual(nupp["--indices"].GetList()[0]->GetInt32(), 9);
|
||||||
|
|
||||||
|
Assert::IsTrue(nupp.HasParam("--force"));
|
||||||
|
Assert::IsTrue(nupp["--force"].GetDataType() == DATA_TYPE::VOID);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that an HazelnuppConstraintMissingValue gets raised if a required parameter
|
||||||
|
// is missing and does not have a default parameter
|
||||||
|
TEST_METHOD(Exception_MissingImportant_Parameter_WithoutDefault)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"--dummy",
|
||||||
|
"--federich-float",
|
||||||
|
"420.69",
|
||||||
|
"--siegbert-string",
|
||||||
|
"banana",
|
||||||
|
"--lieber-liste",
|
||||||
|
"banana",
|
||||||
|
"apple",
|
||||||
|
"59"
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppConstraintMissingValue>(
|
||||||
|
[args]
|
||||||
|
{
|
||||||
|
Hazelnupp nupp;
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
nupp.RegisterConstraints({
|
||||||
|
ParamConstraint("--elenor-int", std::vector<std::string>(), true),
|
||||||
|
});
|
||||||
|
|
||||||
|
nupp.Parse(C_Ify(args));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that an HazelnuppConstraintTypeMissmatch gets raised if a required parameter
|
||||||
|
// is missing of the wrong type and cannot be converted
|
||||||
|
TEST_METHOD(Exception_TypeMismatch_Parameter_NotConvertable)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"--dummy",
|
||||||
|
"--elenor-int",
|
||||||
|
"hello"
|
||||||
|
"--federich-float",
|
||||||
|
"420.69",
|
||||||
|
"--siegbert-string",
|
||||||
|
"banana",
|
||||||
|
"--lieber-liste",
|
||||||
|
"banana",
|
||||||
|
"apple",
|
||||||
|
"59"
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppConstraintTypeMissmatch>(
|
||||||
|
[args]
|
||||||
|
{
|
||||||
|
Hazelnupp nupp;
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
nupp.RegisterConstraints({
|
||||||
|
ParamConstraint("--elenor-int", true, DATA_TYPE::INT),
|
||||||
|
});
|
||||||
|
|
||||||
|
nupp.Parse(C_Ify(args));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
258
Test_Hazelnupp/Conversion.cpp
Normal file
258
Test_Hazelnupp/Conversion.cpp
Normal file
@ -0,0 +1,258 @@
|
|||||||
|
#include "CppUnitTest.h"
|
||||||
|
#include "helper.h"
|
||||||
|
#include "../Hazelnupp/Hazelnupp.h"
|
||||||
|
#include "../Hazelnupp/HazelnuppException.h"
|
||||||
|
|
||||||
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||||
|
|
||||||
|
namespace TestHazelnupp
|
||||||
|
{
|
||||||
|
TEST_CLASS(_Conversion)
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
// Tests that string conversion methods work
|
||||||
|
TEST_METHOD(Convert_String)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"--pud",
|
||||||
|
"hello"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
Hazelnupp nupp(C_Ify(args));
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
const Hazelnupp* ptnupp = &nupp;
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetInt64();
|
||||||
|
}
|
||||||
|
, L"Int64");
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetInt32();
|
||||||
|
}
|
||||||
|
, L"Int32");
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetFloat64();
|
||||||
|
}
|
||||||
|
, L"Float64");
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetFloat32();
|
||||||
|
}
|
||||||
|
, L"Float32");
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetList();
|
||||||
|
}
|
||||||
|
, L"List");
|
||||||
|
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that void conversion methods work
|
||||||
|
TEST_METHOD(Convert_Void)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"--pud"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
Hazelnupp nupp(C_Ify(args));
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
const Hazelnupp* ptnupp = &nupp;
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetInt64();
|
||||||
|
}
|
||||||
|
, L"Int64");
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetInt32();
|
||||||
|
}
|
||||||
|
, L"Int32");
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetFloat64();
|
||||||
|
}
|
||||||
|
, L"Float64");
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetFloat32();
|
||||||
|
}
|
||||||
|
, L"Float32");
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetString();
|
||||||
|
}
|
||||||
|
, L"String");
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetList();
|
||||||
|
}
|
||||||
|
, L"List");
|
||||||
|
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that int conversion methods work
|
||||||
|
TEST_METHOD(Convert_Int)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"--pud",
|
||||||
|
"39"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
Hazelnupp nupp(C_Ify(args));
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
const Hazelnupp* ptnupp = &nupp;
|
||||||
|
|
||||||
|
Assert::AreEqual(39ll, nupp["--pud"].GetInt64(), L"Int64");
|
||||||
|
Assert::AreEqual(39, nupp["--pud"].GetInt32(), L"Int32");
|
||||||
|
Assert::IsTrue(39.0l == nupp["--pud"].GetFloat64(), L"Float64");
|
||||||
|
Assert::AreEqual(39.0, nupp["--pud"].GetFloat32(), L"Float32");
|
||||||
|
Assert::AreEqual(std::string("39"), nupp["--pud"].GetString(), L"String");
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetList();
|
||||||
|
}
|
||||||
|
, L"List");
|
||||||
|
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that float conversion methods work
|
||||||
|
TEST_METHOD(Convert_Float)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"--pud",
|
||||||
|
"39.5"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
Hazelnupp nupp(C_Ify(args));
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
const Hazelnupp* ptnupp = &nupp;
|
||||||
|
|
||||||
|
Assert::AreEqual(39ll, nupp["--pud"].GetInt64(), L"Int64");
|
||||||
|
Assert::AreEqual(39, nupp["--pud"].GetInt32(), L"Int32");
|
||||||
|
Assert::IsTrue(39.5l == nupp["--pud"].GetFloat64(), L"Float64");
|
||||||
|
Assert::AreEqual(39.5, nupp["--pud"].GetFloat32(), L"Float32");
|
||||||
|
Assert::AreEqual(std::string("39.5"), nupp["--pud"].GetString(), L"String");
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetList();
|
||||||
|
}
|
||||||
|
, L"List");
|
||||||
|
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that list conversion methods work
|
||||||
|
TEST_METHOD(Convert_List)
|
||||||
|
{
|
||||||
|
// Setup
|
||||||
|
ArgList args({
|
||||||
|
"/my/fake/path/wahoo.out",
|
||||||
|
"--pud",
|
||||||
|
"apple",
|
||||||
|
"1",
|
||||||
|
"2",
|
||||||
|
"3"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Exercise
|
||||||
|
Hazelnupp nupp(C_Ify(args));
|
||||||
|
nupp.SetCrashOnFail(false);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
const Hazelnupp* ptnupp = &nupp;
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetInt64();
|
||||||
|
}
|
||||||
|
, L"Int64");
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetInt32();
|
||||||
|
}
|
||||||
|
, L"Int32");
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetFloat64();
|
||||||
|
}
|
||||||
|
, L"Float64");
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetFloat32();
|
||||||
|
}
|
||||||
|
, L"Float32");
|
||||||
|
|
||||||
|
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
|
||||||
|
[ptnupp]
|
||||||
|
{
|
||||||
|
(*ptnupp)["--pud"].GetString();
|
||||||
|
}
|
||||||
|
, L"String");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -156,7 +156,10 @@
|
|||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="Test_Hazelnupp.cpp" />
|
<ClCompile Include="Abbreviations.cpp" />
|
||||||
|
<ClCompile Include="Basics.cpp" />
|
||||||
|
<ClCompile Include="Constraints.cpp" />
|
||||||
|
<ClCompile Include="Conversion.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Hazelnupp\Hazelnupp.vcxproj">
|
<ProjectReference Include="..\Hazelnupp\Hazelnupp.vcxproj">
|
||||||
@ -164,6 +167,7 @@
|
|||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="Constraints.h" />
|
||||||
<ClInclude Include="helper.h" />
|
<ClInclude Include="helper.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
@ -14,14 +14,26 @@
|
|||||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||||
</Filter>
|
</Filter>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
|
||||||
<ClCompile Include="Test_Hazelnupp.cpp">
|
|
||||||
<Filter>Quelldateien</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="helper.h">
|
<ClInclude Include="helper.h">
|
||||||
<Filter>Headerdateien</Filter>
|
<Filter>Headerdateien</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Constraints.h">
|
||||||
|
<Filter>Headerdateien</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="Basics.cpp">
|
||||||
|
<Filter>Quelldateien</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Abbreviations.cpp">
|
||||||
|
<Filter>Quelldateien</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Constraints.cpp">
|
||||||
|
<Filter>Quelldateien</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Conversion.cpp">
|
||||||
|
<Filter>Quelldateien</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
Loading…
x
Reference in New Issue
Block a user