diff options
-rw-r--r-- | src/core/common/Argument.cpp | 15 | ||||
-rw-r--r-- | src/core/common/Argument.hpp | 13 | ||||
-rw-r--r-- | test/core/common/ArgumentTest.cpp | 12 |
3 files changed, 38 insertions, 2 deletions
diff --git a/src/core/common/Argument.cpp b/src/core/common/Argument.cpp index ac1edf4..6bf2917 100644 --- a/src/core/common/Argument.cpp +++ b/src/core/common/Argument.cpp @@ -184,6 +184,9 @@ bool Argument::validate(Variant &var, Logger &logger) const /* Class Arguments */ +// Instantiations of the "None" arguments +const Arguments Arguments::None; + static std::unordered_map<std::string, size_t> buildArgumentNames( std::initializer_list<Argument> arguments) { @@ -205,12 +208,17 @@ static std::unordered_map<std::string, size_t> buildArgumentNames( } Arguments::Arguments(std::initializer_list<Argument> arguments) - : arguments(arguments), names(buildArgumentNames(arguments)) + : arguments(arguments), names(buildArgumentNames(arguments)), valid(true) { } bool Arguments::validateArray(Variant::arrayType &arr, Logger &logger) const { + // Abort if no arguments were explicitly given -- everything is valid + if (!valid) { + return true; + } + Logger nullLogger; // Fetch the number of arguments N and the initial array size n @@ -254,6 +262,11 @@ bool Arguments::validateArray(Variant::arrayType &arr, Logger &logger) const bool Arguments::validateMap(Variant::mapType &map, Logger &logger, bool ignoreUnknown) const { + // Abort if no arguments were explicitly given -- everything is valid + if (!valid) { + return true; + } + Logger nullLogger; // Fetch the number of arguments N diff --git a/src/core/common/Argument.hpp b/src/core/common/Argument.hpp index 78f7ec1..dd652de 100644 --- a/src/core/common/Argument.hpp +++ b/src/core/common/Argument.hpp @@ -402,12 +402,23 @@ private: */ std::unordered_map<std::string, size_t> names; + /** + * Set to true if arguments were explicitly given in the constructor, + * false otherwise. + */ + bool valid; + public: /** + * Static Arguments instance with no explicit arguments set. + */ + static const Arguments None; + + /** * Default constructor. Provides no arguments. */ - Arguments() {}; + Arguments() : valid(false) {}; /** * Constructor of the Arguments class from a list of Argument instances. diff --git a/test/core/common/ArgumentTest.cpp b/test/core/common/ArgumentTest.cpp index 0dec809..c8ecd2f 100644 --- a/test/core/common/ArgumentTest.cpp +++ b/test/core/common/ArgumentTest.cpp @@ -785,6 +785,18 @@ TEST(Arguments, construction) ASSERT_THROW(Arguments({Argument::Int("test test")}), OusiaException); } +TEST(Arguments, invalid) +{ + Arguments argsInvalid{}; + + Arguments argsValid{{}}; + + Variant::arrayType arr{1}; + + ASSERT_TRUE(argsInvalid.validateArray(arr, logger)); // No error message + ASSERT_FALSE(argsValid.validateArray(arr, logger)); // Too many arguments +} + TEST(Arguments, validateArray) { Arguments args{Argument::Int("a"), Argument::String("b", "test"), |