diff options
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/core/common/Argument.hpp | 31 | ||||
-rw-r--r-- | test/core/common/ArgumentTest.cpp | 194 |
3 files changed, 221 insertions, 5 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index c535464..9778b2b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -197,6 +197,7 @@ IF(TEST) test/core/ResourceLocatorTest test/core/TokenizerTest test/core/XMLTest + test/core/common/ArgumentTest test/core/common/CharReaderTest test/core/common/FunctionTest test/core/common/LoggerTest diff --git a/src/core/common/Argument.hpp b/src/core/common/Argument.hpp index 408dfae..cb08fc3 100644 --- a/src/core/common/Argument.hpp +++ b/src/core/common/Argument.hpp @@ -199,10 +199,33 @@ public: static Argument String(std::string name, const Variant::stringType &defaultValue); + /** + * Named constructor for an object argument with no default value. Object + * arguments always point at an instance of the Managed class. The concrete + * Object type must be specified in the "type" argument. + * + * @param name is the name of the argument as used for error messages and in + * case the arguments are given as a map. + * @param type is the RttiType of acceptable objects. All objects where the + * "isa" function returns true for the given type are be accepted. + * @return a new Argument instance. + */ static Argument Object(std::string name, const RttiType &type); + /** + * Named constructor for an object argument with default value. The default + * value can only be nullptr. Object arguments always point at an instance + * of the Managed class. The concrete Object type must be specified in the + * "type" argument. + * + * @param name is the name of the argument as used for error messages and in + * case the arguments are given as a map. + * @param type is the RttiType of acceptable objects. All objects where the + * "isa" function returns true for the given type are be accepted. + * @return a new Argument instance. + */ static Argument Object(std::string name, const RttiType &type, - std::nullptr_t); + std::nullptr_t defaultValue); /** * Named constructor for a function argument with no default value. @@ -292,8 +315,7 @@ public: * @param name is the name of the argument as used for error messages and in * case the arguments are given as a map. * @param defaultValue is the default value to be used in case this argument - *is - * not supplied. + * is not supplied. * @return a new Argument instance. */ static Argument Map(std::string name, const Variant::mapType &defaultValue); @@ -319,8 +341,7 @@ public: * @param innerType is the inner type of the map. All map entries are forced * to be of this type. * @param defaultValue is the default value to be used in case this argument - *is - * not supplied. + * is not supplied. * @return a new Argument instance. */ static Argument Map(std::string name, const RttiType &innerType, diff --git a/test/core/common/ArgumentTest.cpp b/test/core/common/ArgumentTest.cpp new file mode 100644 index 0000000..8976fba --- /dev/null +++ b/test/core/common/ArgumentTest.cpp @@ -0,0 +1,194 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <iostream> + +#include <gtest/gtest.h> + +#include <core/common/Logger.hpp> +#include <core/common/Argument.hpp> + +#include <core/managed/Managed.hpp> + +namespace ousia { + +//static Logger logger; +static TerminalLogger logger(std::cerr, true); + +TEST(Argument, validateBool) +{ + Argument a = Argument::Bool("a"); + + { + Variant v{true}; + ASSERT_TRUE(a.validate(v, logger)); + ASSERT_TRUE(v.isBool()); + ASSERT_TRUE(v.asBool()); + } + + { + Variant v{false}; + ASSERT_TRUE(a.validate(v, logger)); + ASSERT_TRUE(v.isBool()); + ASSERT_FALSE(v.asBool()); + } + + { + Variant v{1}; + ASSERT_FALSE(a.validate(v, logger)); + ASSERT_TRUE(v.isBool()); + ASSERT_FALSE(v.asBool()); + } +} + +TEST(Argument, validateBoolDefault) +{ + Argument a = Argument::Bool("a", true); + + Variant v{1}; + ASSERT_FALSE(a.validate(v, logger)); + ASSERT_TRUE(v.isBool()); + ASSERT_TRUE(v.asBool()); +} + +TEST(Argument, validateInt) +{ + Argument a = Argument::Int("a"); + + { + Variant v{123}; + ASSERT_TRUE(a.validate(v, logger)); + ASSERT_TRUE(v.isInt()); + ASSERT_EQ(123, v.asInt()); + } + + { + Variant v{1.1}; + ASSERT_FALSE(a.validate(v, logger)); + ASSERT_TRUE(v.isInt()); + ASSERT_EQ(0, v.asInt()); + } +} + +TEST(Argument, validateIntDefault) +{ + Argument a = Argument::Int("a", 42); + + Variant v{1.1}; + ASSERT_FALSE(a.validate(v, logger)); + ASSERT_TRUE(v.isInt()); + ASSERT_EQ(42, v.asInt()); +} + +TEST(Argument, validateDouble) +{ + Argument a = Argument::Double("a"); + + { + Variant v{123}; + ASSERT_TRUE(a.validate(v, logger)); + ASSERT_TRUE(v.isDouble()); + ASSERT_EQ(123.0, v.asDouble()); + } + + { + Variant v{1.1}; + ASSERT_TRUE(a.validate(v, logger)); + ASSERT_TRUE(v.isDouble()); + ASSERT_EQ(1.1, v.asDouble()); + } + + { + Variant v{"1.0"}; + ASSERT_FALSE(a.validate(v, logger)); + ASSERT_TRUE(v.isDouble()); + ASSERT_EQ(0.0, v.asDouble()); + } +} + +TEST(Argument, validateDoubleDefault) +{ + Argument a = Argument::Double("a", 42.0); + + Variant v{"1.0"}; + ASSERT_FALSE(a.validate(v, logger)); + ASSERT_TRUE(v.isDouble()); + ASSERT_EQ(42.0, v.asDouble()); +} + +TEST(Argument, validateString) +{ + Argument a = Argument::String("a"); + + { + Variant v{"test"}; + ASSERT_TRUE(a.validate(v, logger)); + ASSERT_TRUE(v.isString()); + ASSERT_EQ("test", v.asString()); + } + + { + Variant v{true}; + ASSERT_TRUE(a.validate(v, logger)); + ASSERT_TRUE(v.isString()); + ASSERT_EQ("true", v.asString()); + } + + { + Variant v{nullptr}; + ASSERT_TRUE(a.validate(v, logger)); + ASSERT_TRUE(v.isString()); + ASSERT_EQ("null", v.asString()); + } + + { + Variant v{42}; + ASSERT_TRUE(a.validate(v, logger)); + ASSERT_TRUE(v.isString()); + ASSERT_EQ("42", v.asString()); + } + + { + Variant v{42.5}; + ASSERT_TRUE(a.validate(v, logger)); + ASSERT_TRUE(v.isString()); + ASSERT_EQ("42.5", v.asString()); + } + + { + Variant v{{1, 2, 3}}; + ASSERT_FALSE(a.validate(v, logger)); + ASSERT_TRUE(v.isString()); + ASSERT_EQ("", v.asString()); + } +} + +TEST(Argument, validateStringDefault) +{ + Argument a = Argument::String("a", "test2"); + + { + Variant v{{1, 2, 3}}; + ASSERT_FALSE(a.validate(v, logger)); + ASSERT_TRUE(v.isString()); + ASSERT_EQ("test2", v.asString()); + } +} + +} + |