diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-12 23:54:20 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-12 23:54:20 +0100 |
commit | 35c176d435a7154a7bc3c646be9798e5a1ed3c28 (patch) | |
tree | 4badbea62624ed6561be5d03da8a2bf160ac1a8e /src | |
parent | 5d5f049e331332a805f117e34bb196ca43785863 (diff) |
Added unittests for validateArray and validateMap functions, injecting standard values on failure
Diffstat (limited to 'src')
-rw-r--r-- | src/core/common/Argument.cpp | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/src/core/common/Argument.cpp b/src/core/common/Argument.cpp index 4e80f23..8face55 100644 --- a/src/core/common/Argument.cpp +++ b/src/core/common/Argument.cpp @@ -192,12 +192,12 @@ static std::unordered_map<std::string, size_t> buildArgumentNames( for (const Argument &arg : arguments) { if (!Utils::isIdentifier(arg.name)) { throw OusiaException{std::string("Argument name ") + arg.name + - std::string(" is not a valid identifier!")}; + std::string(" is not a valid identifier")}; } if (!res.emplace(arg.name, i++).second) { throw OusiaException{ std::string("Argument names must be unique (") + arg.name + - std::string(")!")}; + std::string(")")}; } } return res; @@ -210,6 +210,8 @@ Arguments::Arguments(std::initializer_list<Argument> arguments) bool Arguments::validateArray(Variant::arrayType &arr, Logger &logger) { + Logger nullLogger; + // Fetch the number of arguments N and the initial array size n const size_t n = arr.size(); const size_t N = arguments.size(); @@ -235,9 +237,11 @@ bool Arguments::validateArray(Variant::arrayType &arr, Logger &logger) if (arguments[a].hasDefault) { arr[a] = arguments[a].defaultValue; } else { + // Call "validate" to inject a standard value + arguments[a].validate(arr[a], nullLogger); logger.error(std::string("Missing argument ") + - std::to_string(a + 1) + std::string("(") + - arguments[a].name + std::string(")!")); + std::to_string(a + 1) + std::string(" \"") + + arguments[a].name + std::string("\"")); ok = false; } } @@ -249,6 +253,8 @@ bool Arguments::validateArray(Variant::arrayType &arr, Logger &logger) bool Arguments::validateMap(Variant::mapType &map, Logger &logger, bool ignoreUnknown) { + Logger nullLogger; + // Fetch the number of arguments N const size_t N = arguments.size(); std::vector<bool> set(N); @@ -265,10 +271,10 @@ bool Arguments::validateMap(Variant::mapType &map, Logger &logger, ok = ok && set[idx]; } else { if (ignoreUnknown) { - logger.note(std::string("Ignoring argument ") + e.first); + logger.note(std::string("Ignoring argument \"") + e.first + std::string("\"")); } else { - logger.error(std::string("Unknown argument ") + e.first + - std::string("!")); + logger.error(std::string("Unknown argument \"") + e.first + + std::string("\"")); ok = false; } } @@ -280,8 +286,11 @@ bool Arguments::validateMap(Variant::mapType &map, Logger &logger, if (arguments[a].hasDefault) { map[arguments[a].name] = arguments[a].defaultValue; } else { - logger.error(std::string("Missing argument ") + - arguments[a].name); + // Call "validate" to inject a standard value + map[arguments[a].name] = Variant{}; + arguments[a].validate(map[arguments[a].name], nullLogger); + logger.error(std::string("Missing argument \"") + + arguments[a].name + std::string("\"")); ok = false; } } |