/***************************************************************************
- * Copyright (C) 2005-2018 by the Quassel Project *
+ * Copyright (C) 2005-2022 by the Quassel Project *
* devel@quassel-irc.org *
* *
* This program is free software; you can redistribute it and/or modify *
#include "funchelpers.h"
-TEST(FuncHelpersTest, invokeWithArgsList)
+TEST(FuncHelpersTest, invokeLambdaWithArgsList)
{
int intVal{};
QString stringVal{};
// Good case
{
- QVariantList argsList{42, "Hello World"};
- auto ret = invokeWithArgsList(callable, argsList);
+ auto ret = invokeWithArgsList(callable, {42, "Hello World"});
ASSERT_TRUE(ret);
EXPECT_FALSE(ret->isValid()); // Callable returns void, so the returned QVariant should be invalid
EXPECT_EQ(42, intVal);
// Too many arguments
{
- QVariantList argsList{23, "Hi Universe", 2.3};
- ASSERT_FALSE(invokeWithArgsList(callable, argsList));
+ ASSERT_FALSE(invokeWithArgsList(callable, {23, "Hi Universe", 2.3}));
// Values shouldn't have changed
EXPECT_EQ(42, intVal);
EXPECT_EQ("Hello World", stringVal);
// Too few arguments
{
- QVariantList argsList{23};
- ASSERT_FALSE(invokeWithArgsList(callable, argsList));
+ ASSERT_FALSE(invokeWithArgsList(callable, {23}));
// Values shouldn't have changed
EXPECT_EQ(42, intVal);
EXPECT_EQ("Hello World", stringVal);
QVariant v{wrong};
ASSERT_FALSE(v.canConvert<QString>());
- QVariantList argsList{23, wrong};
- ASSERT_FALSE(invokeWithArgsList(callable, argsList));
+ ASSERT_FALSE(invokeWithArgsList(callable, {23, wrong}));
// Values shouldn't have changed
EXPECT_EQ(42, intVal);
EXPECT_EQ("Hello World", stringVal);
}
}
-TEST(FuncHelpersTest, invokeWithArgsListAndReturnValue)
+TEST(FuncHelpersTest, invokeLambdaWithArgsListAndReturnValue)
{
+ int intVal{};
+ QString stringVal{};
+
+ auto callable = [&intVal, &stringVal](int i, const QString& s)
+ {
+ intVal = i;
+ stringVal = s;
+ return -i;
+ };
+
+ // Good case
+ {
+ auto ret = invokeWithArgsList(callable, {42, "Hello World"});
+ ASSERT_TRUE(ret);
+ ASSERT_TRUE(ret->isValid());
+ EXPECT_EQ(-42, *ret);
+ EXPECT_EQ(42, intVal);
+ EXPECT_EQ("Hello World", stringVal);
+ }
+
+ // Failed invocation
+ {
+ ASSERT_FALSE(invokeWithArgsList(callable, {23}));
+ }
+}
+
+class Object
+{
+public:
+ void voidFunc(int i, const QString& s)
+ {
+ intVal = i;
+ stringVal = s;
+ }
+
+ int intFunc(int i)
+ {
+ return -i;
+ }
+
+ int intVal{};
+ QString stringVal{};
+};
+
+TEST(FuncHelpersTest, invokeMemberFunction)
+{
+ Object object;
+
+ // Good case
+ {
+ auto ret = invokeWithArgsList(&object, &Object::voidFunc, {42, "Hello World"});
+ ASSERT_TRUE(ret);
+ EXPECT_FALSE(ret->isValid()); // Callable returns void, so the returned QVariant should be invalid
+ EXPECT_EQ(42, object.intVal);
+ EXPECT_EQ("Hello World", object.stringVal);
+ }
+
+ // Good case with return value
+ {
+ auto ret = invokeWithArgsList(&object, &Object::intFunc, {42});
+ ASSERT_TRUE(ret);
+ EXPECT_EQ(-42, *ret);
+ }
+
+ // Too few arguments
{
- int intVal{};
- QString stringVal{};
-
- auto callable = [&intVal, &stringVal](int i, const QString& s)
- {
- intVal = i;
- stringVal = s;
- return -i;
- };
-
- // Good case
- {
- QVariantList argsList{42, "Hello World"};
- auto ret = invokeWithArgsList(callable, argsList);
- ASSERT_TRUE(ret);
- ASSERT_TRUE(ret->isValid());
- EXPECT_EQ(-42, *ret);
- EXPECT_EQ(42, intVal);
- EXPECT_EQ("Hello World", stringVal);
- }
-
- // Failed invocation
- {
- QVariantList argsList{23};
- ASSERT_FALSE(invokeWithArgsList(callable, argsList));
- }
+ auto ret = invokeWithArgsList(&object, &Object::voidFunc, {23});
+ ASSERT_FALSE(ret);
+ EXPECT_EQ(42, object.intVal);
}
}