File indexing completed on 2024-04-21 05:44:56

0001 /*
0002  *   Copyright 2018  Jos van den Oever <jos@vandenoever.info>
0003  *
0004  *   This program is free software; you can redistribute it and/or
0005  *   modify it under the terms of the GNU General Public License as
0006  *   published by the Free Software Foundation; either version 2 of
0007  *   the License or (at your option) version 3 or any later version
0008  *   accepted by the membership of KDE e.V. (or its successor approved
0009  *   by the membership of KDE e.V.), which shall act as a proxy
0010  *   defined in Section 14 of version 3 of the license.
0011  *
0012  *   This program is distributed in the hope that it will be useful,
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015  *   GNU General Public License for more details.
0016  *
0017  *   You should have received a copy of the GNU General Public License
0018  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #include "test_list_types_rust.h"
0022 #include <QTest>
0023 #include <QSignalSpy>
0024 
0025 class TestRustListTypes : public QObject
0026 {
0027     Q_OBJECT
0028 private slots:
0029     void testConstructor();
0030     void testStringGetter();
0031     void testStringSetter();
0032     void testBool();
0033     void testOptionalBool();
0034     void testInt8();
0035     void testUint8();
0036     void testInt16();
0037     void testUint16();
0038     void testInt32();
0039     void testUint32();
0040     void testInt64();
0041     void testUint64();
0042     void testFloat();
0043     void testDouble();
0044     void testString();
0045     void testOptionalString();
0046     void testByteArray();
0047     void testOptionalByteArray();
0048 };
0049 
0050 template <typename V, typename Set, typename Get>
0051 void testSetter(const V v, Set set, Get get)
0052 {
0053     // GIVEN
0054     List list;
0055     QSignalSpy spy(&list, &List::dataChanged);
0056 
0057     // WHEN
0058     bool ok = (list.*set)(0, v);
0059     QVERIFY(ok);
0060 
0061     // THEN
0062     QVERIFY(spy.isValid());
0063     QCOMPARE(spy.count(), 1);
0064     QCOMPARE((V)(list.*get)(0), v);
0065 }
0066 
0067 int getRoleFromName(const QAbstractItemModel& model, const char* name)
0068 {
0069     auto names = model.roleNames();
0070     auto i = names.constBegin();
0071     while (i != names.constEnd()) {
0072         if (i.value() == name) {
0073             return i.key();
0074         }
0075         ++i;
0076     }
0077     return -1;
0078 }
0079 
0080 template <typename V>
0081 void testDataSetter(const char* roleName, const V v)
0082 {
0083     // GIVEN
0084     List list;
0085     QSignalSpy spy(&list, &List::dataChanged);
0086 
0087     // WHEN
0088     int role = getRoleFromName(list, roleName);
0089     auto index = list.index(1, 0);
0090     const QVariant vv = QVariant::fromValue(v);
0091     QVERIFY(!vv.isNull());
0092     bool ok = list.setData(index, vv, role);
0093     QVERIFY(ok);
0094 
0095     // THEN
0096     QVERIFY(spy.isValid());
0097     QCOMPARE(spy.count(), 1);
0098     QCOMPARE(list.data(index, role), vv);
0099 }
0100 
0101 template <typename V>
0102 void testOptionalDataSetter(const char* roleName, const V v)
0103 {
0104     // GIVEN
0105     List list;
0106     QSignalSpy spy(&list, &List::dataChanged);
0107     int role = getRoleFromName(list, roleName);
0108     auto index = list.index(1, 0);
0109     QVERIFY(list.data(index, role).isNull());
0110 
0111     // WHEN
0112     QVariant vv = QVariant::fromValue(v);
0113     if (vv.isNull()) {
0114         vv = QVariant();
0115     }
0116     bool ok = list.setData(index, vv, role);
0117     QVERIFY(ok);
0118 
0119     // THEN
0120     QVERIFY(spy.isValid());
0121     QCOMPARE(spy.count(), 1);
0122     QCOMPARE(list.data(index, role), vv);
0123 }
0124 
0125 template <typename V, typename Set, typename Get>
0126 void test(const V v, Set set, Get get, const char* roleName)
0127 {
0128     testSetter(v, set, get);
0129     testDataSetter(roleName, v);
0130 }
0131 
0132 template <typename V, typename Set, typename Get>
0133 void testOptional(const V v, Set set, Get get, const char* roleName)
0134 {
0135     testSetter(v, set, get);
0136     testOptionalDataSetter(roleName, v);
0137 }
0138 
0139 void TestRustListTypes::testConstructor()
0140 {
0141     List list;
0142 }
0143 
0144 void TestRustListTypes::testBool()
0145 {
0146     test(true, &List::setBoolean, &List::boolean, "boolean");
0147     test(false, &List::setBoolean, &List::boolean, "boolean");
0148 }
0149 
0150 void TestRustListTypes::testOptionalBool()
0151 {
0152     testOptional(QVariant(), &List::setOptionalBoolean, &List::optionalBoolean,
0153             "optionalBoolean");
0154     testOptional(QVariant(true), &List::setOptionalBoolean, &List::optionalBoolean,
0155             "optionalBoolean");
0156     testOptional(QVariant(false), &List::setOptionalBoolean, &List::optionalBoolean,
0157             "optionalBoolean");
0158 }
0159 
0160 void TestRustListTypes::testInt8()
0161 {
0162     test(0, &List::setI8, &List::i8, "i8");
0163     test(1, &List::setI8, &List::i8, "i8");
0164     test(std::numeric_limits<int8_t>::min(), &List::setI8, &List::i8, "i8");
0165     test(std::numeric_limits<int8_t>::max(), &List::setI8, &List::i8, "i8");
0166 }
0167 
0168 void TestRustListTypes::testUint8()
0169 {
0170     test(0, &List::setU8, &List::u8, "u8");
0171     test(1, &List::setU8, &List::u8, "u8");
0172     test(std::numeric_limits<uint8_t>::min(), &List::setU8, &List::u8, "u8");
0173     test(std::numeric_limits<uint8_t>::max(), &List::setU8, &List::u8, "u8");
0174 }
0175 
0176 void TestRustListTypes::testInt16()
0177 {
0178     test(0, &List::setI16, &List::i16, "i16");
0179     test(1, &List::setI16, &List::i16, "i16");
0180     test(std::numeric_limits<int16_t>::min(), &List::setI16, &List::i16, "i16");
0181     test(std::numeric_limits<int16_t>::max(), &List::setI16, &List::i16, "i16");
0182 }
0183 
0184 void TestRustListTypes::testUint16()
0185 {
0186     test(0, &List::setU16, &List::u16, "u16");
0187     test(1, &List::setU16, &List::u16, "u16");
0188     test(std::numeric_limits<uint16_t>::min(), &List::setU16, &List::u16, "u16");
0189     test(std::numeric_limits<uint16_t>::max(), &List::setU16, &List::u16, "u16");
0190 }
0191 
0192 void TestRustListTypes::testInt32()
0193 {
0194     test(0, &List::setI32, &List::i32, "i32");
0195     test(1, &List::setI32, &List::i32, "i32");
0196     test(std::numeric_limits<int32_t>::min(), &List::setI32, &List::i32, "i32");
0197     test(std::numeric_limits<int32_t>::max(), &List::setI32, &List::i32, "i32");
0198 }
0199 
0200 void TestRustListTypes::testUint32()
0201 {
0202     test(0, &List::setU32, &List::u32, "u32");
0203     test(1, &List::setU32, &List::u32, "u32");
0204     test(std::numeric_limits<uint32_t>::min(), &List::setU32, &List::u32, "u32");
0205     test(std::numeric_limits<uint32_t>::max(), &List::setU32, &List::u32, "u32");
0206 }
0207 
0208 void TestRustListTypes::testInt64()
0209 {
0210     test(0, &List::setI64, &List::i64, "i64");
0211     test(1, &List::setI64, &List::i64, "i64");
0212     test(std::numeric_limits<int64_t>::min(), &List::setI64, &List::i64, "i64");
0213     test(std::numeric_limits<int64_t>::max(), &List::setI64, &List::i64, "i64");
0214 }
0215 
0216 void TestRustListTypes::testUint64()
0217 {
0218     test(0, &List::setU64, &List::u64, "u64");
0219     test(1, &List::setU64, &List::u64, "u64");
0220     test(std::numeric_limits<uint64_t>::min(), &List::setU64, &List::u64, "u64");
0221     test(std::numeric_limits<uint64_t>::max(), &List::setU64, &List::u64, "u64");
0222 }
0223 
0224 void TestRustListTypes::testFloat()
0225 {
0226     test(0, &List::setF32, &List::f32, "f32");
0227     test(1, &List::setF32, &List::f32, "f32");
0228     test(std::numeric_limits<float>::min(), &List::setF32, &List::f32, "f32");
0229     test(std::numeric_limits<float>::max(), &List::setF32, &List::f32, "f32");
0230 }
0231 
0232 void TestRustListTypes::testDouble()
0233 {
0234     test(0, &List::setF64, &List::f64, "f64");
0235     test(1, &List::setF64, &List::f64, "f64");
0236     test(std::numeric_limits<double>::min(), &List::setF64, &List::f64, "f64");
0237     test(std::numeric_limits<double>::max(), &List::setF64, &List::f64, "f64");
0238 }
0239 
0240 void TestRustListTypes::testString()
0241 {
0242     test(QString(""), &List::setString, &List::string, "string");
0243     test(QString("Konqi"), &List::setString, &List::string, "string");
0244     test(QString("$𐐷𤭢"), &List::setString, &List::string,  "string");
0245 }
0246 
0247 void TestRustListTypes::testOptionalString()
0248 {
0249     testOptional(QString(), &List::setOptionalString, &List::optionalString,
0250             "optionalString");
0251     testOptional(QString(""), &List::setOptionalString, &List::optionalString,
0252             "optionalString");
0253     testOptional(QString("Konqi"), &List::setOptionalString,
0254             &List::optionalString, "optionalString");
0255     testOptional(QString("$𐐷𤭢"), &List::setOptionalString,
0256             &List::optionalString, "optionalString");
0257 }
0258 
0259 void TestRustListTypes::testByteArray()
0260 {
0261     const char data[10] = {0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9};
0262     test(QByteArray(data, 0), &List::setBytearray,
0263         &List::bytearray, "bytearray");
0264     test(QByteArray(data, 10), &List::setBytearray,
0265         &List::bytearray, "bytearray");
0266 }
0267 
0268 void TestRustListTypes::testOptionalByteArray()
0269 {
0270     testOptional(QByteArray(), &List::setOptionalBytearray,
0271             &List::optionalBytearray, "optionalBytearray");
0272     const char data[10] = {0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9};
0273     testOptional(QByteArray(data, 0), &List::setOptionalBytearray,
0274         &List::optionalBytearray, "optionalBytearray");
0275     testOptional(QByteArray(data, 10), &List::setOptionalBytearray,
0276         &List::optionalBytearray, "optionalBytearray");
0277 }
0278 
0279 void TestRustListTypes::testStringGetter()
0280 {
0281     List list;
0282     QCOMPARE(list.rowCount(), 10);
0283     QVariant value = list.data(list.index(0,0));
0284     // value should be empty string in default implementation
0285     QVERIFY(value.isValid());
0286     QCOMPARE(value.type(), QVariant::String);
0287     QCOMPARE(value.toString(), QString());
0288 }
0289 
0290 void TestRustListTypes::testStringSetter()
0291 {
0292     // GIVEN
0293     List list;
0294     QSignalSpy spy(&list, &List::dataChanged);
0295 
0296     // WHEN
0297     const QModelIndex index(list.index(0,0));
0298     const bool set = list.setData(index, "Konqi");
0299 
0300     // THEN
0301     QVERIFY(set);
0302     QVERIFY(spy.isValid());
0303     QCOMPARE(spy.count(), 1);
0304     QVariant value = list.data(list.index(0,0));
0305     QCOMPARE(value.toString(), QString("Konqi"));
0306 }
0307 
0308 QTEST_MAIN(TestRustListTypes)
0309 #include "test_list_types.moc"