summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/core/common/Argument.cpp11
-rw-r--r--src/core/common/Argument.hpp20
-rw-r--r--test/core/common/ArgumentTest.cpp57
3 files changed, 88 insertions, 0 deletions
diff --git a/src/core/common/Argument.cpp b/src/core/common/Argument.cpp
index 3461868..4e80f23 100644
--- a/src/core/common/Argument.cpp
+++ b/src/core/common/Argument.cpp
@@ -48,6 +48,17 @@ Argument::Argument(std::string name, const RttiType &type)
{
}
+
+Argument Argument::Any(std::string name)
+{
+ return Argument{name, RttiTypes::None, RttiTypes::None, nullptr, false};
+}
+
+Argument Argument::Any(std::string name, Variant defaultValue)
+{
+ return Argument{name, RttiTypes::None, RttiTypes::None, defaultValue, true};
+}
+
Argument Argument::Bool(std::string name)
{
return Argument{name, RttiTypes::Bool};
diff --git a/src/core/common/Argument.hpp b/src/core/common/Argument.hpp
index cb08fc3..7227ddb 100644
--- a/src/core/common/Argument.hpp
+++ b/src/core/common/Argument.hpp
@@ -119,6 +119,26 @@ public:
const bool hasDefault;
/**
+ * Named constructor for an argument with any type.
+ *
+ * @param name is the name of the argument as used for error messages and in
+ * case the arguments are given as a map.
+ * @return a new Argument instance.
+ */
+ static Argument Any(std::string name);
+
+ /**
+ * Named constructor for an argument with any type.
+ *
+ * @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.
+ * @return a new Argument instance.
+ */
+ static Argument Any(std::string name, Variant defaultValue);
+
+ /**
* Named constructor for a boolean argument with no default value.
*
* @param name is the name of the argument as used for error messages and in
diff --git a/test/core/common/ArgumentTest.cpp b/test/core/common/ArgumentTest.cpp
index f3f8f2b..bd88035 100644
--- a/test/core/common/ArgumentTest.cpp
+++ b/test/core/common/ArgumentTest.cpp
@@ -52,6 +52,63 @@ static const Rtti<ousia::TestManaged2> TestManaged2 =
RttiBuilder("TestManaged2").parent(&TestManaged1);
}
+TEST(Argument, validateAny)
+{
+ Argument a = Argument::Any("a");
+
+ ASSERT_FALSE(a.hasDefault);
+
+ {
+ Variant v{true};
+ ASSERT_TRUE(a.validate(v, logger));
+ ASSERT_TRUE(v.isBool());
+ ASSERT_TRUE(v.asBool());
+ }
+
+ {
+ Variant v{"test"};
+ ASSERT_TRUE(a.validate(v, logger));
+ ASSERT_TRUE(v.isString());
+ ASSERT_EQ("test", v.asString());
+ }
+
+ {
+ Variant v{{1, 2, 3, 4}};
+ ASSERT_TRUE(a.validate(v, logger));
+ ASSERT_TRUE(v.isArray());
+ ASSERT_EQ(Variant::arrayType({1, 2, 3, 4}), v.asArray());
+ }
+}
+
+TEST(Argument, validateAnyDefault)
+{
+ Argument a = Argument::Any("a", true);
+
+ ASSERT_TRUE(a.hasDefault);
+ ASSERT_TRUE(a.defaultValue.asBool());
+
+ {
+ Variant v{true};
+ ASSERT_TRUE(a.validate(v, logger));
+ ASSERT_TRUE(v.isBool());
+ ASSERT_TRUE(v.asBool());
+ }
+
+ {
+ Variant v{"test"};
+ ASSERT_TRUE(a.validate(v, logger));
+ ASSERT_TRUE(v.isString());
+ ASSERT_EQ("test", v.asString());
+ }
+
+ {
+ Variant v{{1, 2, 3, 4}};
+ ASSERT_TRUE(a.validate(v, logger));
+ ASSERT_TRUE(v.isArray());
+ ASSERT_EQ(Variant::arrayType({1, 2, 3, 4}), v.asArray());
+ }
+}
+
TEST(Argument, validateBool)
{
Argument a = Argument::Bool("a");