1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "testglobal.h"
23 #include <QVariantList>
25 #include "funchelpers.h"
27 TEST(FuncHelpersTest, invokeLambdaWithArgsList)
32 auto callable = [&intVal, &stringVal](int i, const QString& s)
40 auto ret = invokeWithArgsList(callable, {42, "Hello World"});
42 EXPECT_FALSE(ret->isValid()); // Callable returns void, so the returned QVariant should be invalid
43 EXPECT_EQ(42, intVal);
44 EXPECT_EQ("Hello World", stringVal);
49 ASSERT_FALSE(invokeWithArgsList(callable, {23, "Hi Universe", 2.3}));
50 // Values shouldn't have changed
51 EXPECT_EQ(42, intVal);
52 EXPECT_EQ("Hello World", stringVal);
57 ASSERT_FALSE(invokeWithArgsList(callable, {23}));
58 // Values shouldn't have changed
59 EXPECT_EQ(42, intVal);
60 EXPECT_EQ("Hello World", stringVal);
63 // Cannot convert argument type
65 // Ensure type cannot be converted
66 QVariantList wrong{"Foo", "Bar"};
68 ASSERT_FALSE(v.canConvert<QString>());
70 ASSERT_FALSE(invokeWithArgsList(callable, {23, wrong}));
71 // Values shouldn't have changed
72 EXPECT_EQ(42, intVal);
73 EXPECT_EQ("Hello World", stringVal);
77 TEST(FuncHelpersTest, invokeLambdaWithArgsListAndReturnValue)
82 auto callable = [&intVal, &stringVal](int i, const QString& s)
91 auto ret = invokeWithArgsList(callable, {42, "Hello World"});
93 ASSERT_TRUE(ret->isValid());
95 EXPECT_EQ(42, intVal);
96 EXPECT_EQ("Hello World", stringVal);
101 ASSERT_FALSE(invokeWithArgsList(callable, {23}));
108 void voidFunc(int i, const QString& s)
123 TEST(FuncHelpersTest, invokeMemberFunction)
129 auto ret = invokeWithArgsList(&object, &Object::voidFunc, {42, "Hello World"});
131 EXPECT_FALSE(ret->isValid()); // Callable returns void, so the returned QVariant should be invalid
132 EXPECT_EQ(42, object.intVal);
133 EXPECT_EQ("Hello World", object.stringVal);
136 // Good case with return value
138 auto ret = invokeWithArgsList(&object, &Object::intFunc, {42});
140 EXPECT_EQ(-42, *ret);
145 auto ret = invokeWithArgsList(&object, &Object::voidFunc, {23});
147 EXPECT_EQ(42, object.intVal);