diff --git a/Hazelnupp.vpp b/Hazelnupp.vpp index f946694..895f174 100644 Binary files a/Hazelnupp.vpp and b/Hazelnupp.vpp differ diff --git a/Hazelnupp/ParamConstraint.h b/Hazelnupp/ParamConstraint.h index deeacf9..dc1bd5b 100644 --- a/Hazelnupp/ParamConstraint.h +++ b/Hazelnupp/ParamConstraint.h @@ -9,6 +9,28 @@ public: //! Empty constructor ParamConstraint() = default; + //! Constructs a require constraint + static ParamConstraint Require(const std::string& key, const std::vector& defaultValue = {}, bool required = true) + { + ParamConstraint pc; + pc.key = key; + pc.defaultValue = defaultValue; + pc.required = required; + + return pc; + } + + //! Constructs a type-safety constraint + static ParamConstraint TypeSafety(const std::string& key, DATA_TYPE wantedType, bool constrainType = true) + { + ParamConstraint pc; + pc.key = key; + pc.constrainType = constrainType; + pc.wantedType = wantedType; + + return pc; + } + //! Whole constructor ParamConstraint(const std::string& key, bool constrainType, DATA_TYPE wantedType, const std::vector& defaultValue, bool required) : @@ -21,26 +43,6 @@ public: return; } - //! Type-Constraint constructor - ParamConstraint(const std::string& key, bool constrainType, DATA_TYPE wantedType) - : - key{ key }, - constrainType{ constrainType }, - wantedType{ wantedType } - { - return; - } - - //! Require-Constraint constructor - ParamConstraint(const std::string& key, const std::vector& defaultValue, bool required = false) - : - key{ key }, - defaultValue{ defaultValue }, - required{ required } - { - return; - } - //! The key of the parameter to constrain std::string key; diff --git a/Hazelnupp/main.cpp b/Hazelnupp/main.cpp index 0b2535f..6a4eb80 100644 --- a/Hazelnupp/main.cpp +++ b/Hazelnupp/main.cpp @@ -48,7 +48,7 @@ int main(int argc, char** argv) // Use if (args.HasParam("--alfredo")) { - std::cout << args["--alfredo"] << std::endl; + std::cout << args["--alfredo"].GetInt32() << std::endl; } else { diff --git a/Test_Hazelnupp/Constraints.cpp b/Test_Hazelnupp/Constraints.cpp index 6711703..5334266 100644 --- a/Test_Hazelnupp/Constraints.cpp +++ b/Test_Hazelnupp/Constraints.cpp @@ -1 +1,248 @@ -#include "Constraints.h" +#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::Require("--elenor-int", {"5994"}), + ParamConstraint::Require("--federich-float", {"420.69"}), + ParamConstraint::Require("--siegbert-string", {"banana"}), + ParamConstraint::Require("--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::Require("--elenor-int", {"6871"}), + ParamConstraint::Require("--federich-float", {"-199.44"}), + ParamConstraint::Require("--siegbert-string", {"bornana"}), + ParamConstraint::Require("--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::TypeSafety("--num-apples", DATA_TYPE::INT), + ParamConstraint::TypeSafety("--table-height", DATA_TYPE::FLOAT), + ParamConstraint::TypeSafety("--license-plate", DATA_TYPE::STRING), + ParamConstraint::TypeSafety("--fav-fruits", DATA_TYPE::LIST), + ParamConstraint::TypeSafety("--indices", DATA_TYPE::LIST), + ParamConstraint::TypeSafety("--force", 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( + [args] + { + Hazelnupp nupp; + nupp.SetCrashOnFail(false); + + nupp.RegisterConstraints({ + ParamConstraint::Require("--elenor-int"), + }); + + 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( + [args] + { + Hazelnupp nupp; + nupp.SetCrashOnFail(false); + + nupp.RegisterConstraints({ + ParamConstraint::TypeSafety("--elenor-int", DATA_TYPE::INT), + }); + + nupp.Parse(C_Ify(args)); + } + ); + + return; + } + }; +} diff --git a/Test_Hazelnupp/Constraints.h b/Test_Hazelnupp/Constraints.h deleted file mode 100644 index f47678a..0000000 --- a/Test_Hazelnupp/Constraints.h +++ /dev/null @@ -1,248 +0,0 @@ -#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( - [args] - { - Hazelnupp nupp; - nupp.SetCrashOnFail(false); - - nupp.RegisterConstraints({ - ParamConstraint("--elenor-int", std::vector(), 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( - [args] - { - Hazelnupp nupp; - nupp.SetCrashOnFail(false); - - nupp.RegisterConstraints({ - ParamConstraint("--elenor-int", true, DATA_TYPE::INT), - }); - - nupp.Parse(C_Ify(args)); - } - ); - - return; - } - }; -} diff --git a/Test_Hazelnupp/Test_Hazelnupp.vcxproj b/Test_Hazelnupp/Test_Hazelnupp.vcxproj index 70c5078..a59b932 100644 --- a/Test_Hazelnupp/Test_Hazelnupp.vcxproj +++ b/Test_Hazelnupp/Test_Hazelnupp.vcxproj @@ -167,7 +167,6 @@ - diff --git a/Test_Hazelnupp/Test_Hazelnupp.vcxproj.filters b/Test_Hazelnupp/Test_Hazelnupp.vcxproj.filters index 6e58c90..87cbe39 100644 --- a/Test_Hazelnupp/Test_Hazelnupp.vcxproj.filters +++ b/Test_Hazelnupp/Test_Hazelnupp.vcxproj.filters @@ -18,9 +18,6 @@ Headerdateien - - Headerdateien -