cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / tests / common / funchelperstest.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 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, invokeLambdaWithArgsList)
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         auto ret = invokeWithArgsList(callable, {42, "Hello World"});
41         ASSERT_TRUE(ret);
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);
45     }
46
47     // Too many arguments
48     {
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);
53     }
54
55     // Too few arguments
56     {
57         ASSERT_FALSE(invokeWithArgsList(callable, {23}));
58         // Values shouldn't have changed
59         EXPECT_EQ(42, intVal);
60         EXPECT_EQ("Hello World", stringVal);
61     }
62
63     // Cannot convert argument type
64     {
65         // Ensure type cannot be converted
66         QVariantList wrong{"Foo", "Bar"};
67         QVariant v{wrong};
68         ASSERT_FALSE(v.canConvert<QString>());
69
70         ASSERT_FALSE(invokeWithArgsList(callable, {23, wrong}));
71         // Values shouldn't have changed
72         EXPECT_EQ(42, intVal);
73         EXPECT_EQ("Hello World", stringVal);
74     }
75 }
76
77 TEST(FuncHelpersTest, invokeLambdaWithArgsListAndReturnValue)
78 {
79     int intVal{};
80     QString stringVal{};
81
82     auto callable = [&intVal, &stringVal](int i, const QString& s)
83     {
84         intVal = i;
85         stringVal = s;
86         return -i;
87     };
88
89     // Good case
90     {
91         auto ret = invokeWithArgsList(callable, {42, "Hello World"});
92         ASSERT_TRUE(ret);
93         ASSERT_TRUE(ret->isValid());
94         EXPECT_EQ(-42, *ret);
95         EXPECT_EQ(42, intVal);
96         EXPECT_EQ("Hello World", stringVal);
97     }
98
99     // Failed invocation
100     {
101         ASSERT_FALSE(invokeWithArgsList(callable, {23}));
102     }
103 }
104
105 class Object
106 {
107 public:
108     void voidFunc(int i, const QString& s)
109     {
110         intVal = i;
111         stringVal = s;
112     }
113
114     int intFunc(int i)
115     {
116         return -i;
117     }
118
119     int intVal{};
120     QString stringVal{};
121 };
122
123 TEST(FuncHelpersTest, invokeMemberFunction)
124 {
125     Object object;
126
127     // Good case
128     {
129         auto ret = invokeWithArgsList(&object, &Object::voidFunc, {42, "Hello World"});
130         ASSERT_TRUE(ret);
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);
134     }
135
136     // Good case with return value
137     {
138         auto ret = invokeWithArgsList(&object, &Object::intFunc, {42});
139         ASSERT_TRUE(ret);
140         EXPECT_EQ(-42, *ret);
141     }
142
143     // Too few arguments
144     {
145         auto ret = invokeWithArgsList(&object, &Object::voidFunc, {23});
146         ASSERT_FALSE(ret);
147         EXPECT_EQ(42, object.intVal);
148     }
149 }