c40cadef2054fa3a9e956c3c05d93e89b09e590f
[quassel.git] / tests / common / funchelperstest.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "testglobal.h"
22
23 #include <QVariantList>
24
25 #include "funchelpers.h"
26
27 TEST(FuncHelpersTest, invokeWithArgsList)
28 {
29     int intVal{};
30     QString stringVal{};
31
32     auto callable = [&intVal, &stringVal](int i, const QString& s)
33     {
34         intVal = i;
35         stringVal = s;
36     };
37
38     // Good case
39     {
40         QVariantList argsList{42, "Hello World"};
41         auto ret = invokeWithArgsList(callable, argsList);
42         ASSERT_TRUE(ret);
43         EXPECT_FALSE(ret->isValid());  // Callable returns void, so the returned QVariant should be invalid
44         EXPECT_EQ(42, intVal);
45         EXPECT_EQ("Hello World", stringVal);
46     }
47
48     // Too many arguments
49     {
50         QVariantList argsList{23, "Hi Universe", 2.3};
51         ASSERT_FALSE(invokeWithArgsList(callable, argsList));
52         // Values shouldn't have changed
53         EXPECT_EQ(42, intVal);
54         EXPECT_EQ("Hello World", stringVal);
55     }
56
57     // Too few arguments
58     {
59         QVariantList argsList{23};
60         ASSERT_FALSE(invokeWithArgsList(callable, argsList));
61         // Values shouldn't have changed
62         EXPECT_EQ(42, intVal);
63         EXPECT_EQ("Hello World", stringVal);
64     }
65
66     // Cannot convert argument type
67     {
68         // Ensure type cannot be converted
69         QVariantList wrong{"Foo", "Bar"};
70         QVariant v{wrong};
71         ASSERT_FALSE(v.canConvert<QString>());
72
73         QVariantList argsList{23, wrong};
74         ASSERT_FALSE(invokeWithArgsList(callable, argsList));
75         // Values shouldn't have changed
76         EXPECT_EQ(42, intVal);
77         EXPECT_EQ("Hello World", stringVal);
78     }
79 }
80
81 TEST(FuncHelpersTest, invokeWithArgsListAndReturnValue)
82 {
83     {
84         int intVal{};
85         QString stringVal{};
86
87         auto callable = [&intVal, &stringVal](int i, const QString& s)
88         {
89             intVal = i;
90             stringVal = s;
91             return -i;
92         };
93
94         // Good case
95         {
96             QVariantList argsList{42, "Hello World"};
97             auto ret = invokeWithArgsList(callable, argsList);
98             ASSERT_TRUE(ret);
99             ASSERT_TRUE(ret->isValid());
100             EXPECT_EQ(-42, *ret);
101             EXPECT_EQ(42, intVal);
102             EXPECT_EQ("Hello World", stringVal);
103         }
104
105         // Failed invocation
106         {
107             QVariantList argsList{23};
108             ASSERT_FALSE(invokeWithArgsList(callable, argsList));
109         }
110     }
111 }