From 909a9e98999e72262bd353027ce70c6c0377cf9c Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Mon, 12 Jan 2015 00:50:48 +0100 Subject: Added (not yet done) unit test for the Argument class --- CMakeLists.txt | 1 + src/core/common/Argument.hpp | 31 +++++- test/core/common/ArgumentTest.cpp | 194 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 221 insertions(+), 5 deletions(-) create mode 100644 test/core/common/ArgumentTest.cpp 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 . +*/ + +#include + +#include + +#include +#include + +#include + +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()); + } +} + +} + -- cgit v1.2.3