File indexing completed on 2024-04-14 15:48:43

0001 /*
0002  *   Copyright 2017  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_rust.h"
0022 #include <QTest>
0023 #include <QSignalSpy>
0024 
0025 class TestRustList : public QObject
0026 {
0027     Q_OBJECT
0028 private slots:
0029     void testConstructor();
0030     void testStringGetter();
0031     void testStringSetter();
0032     void testAccessByDefaultRole();
0033 };
0034 
0035 void TestRustList::testConstructor()
0036 {
0037     Persons persons;
0038 }
0039 
0040 void TestRustList::testStringGetter()
0041 {
0042     Persons persons;
0043     QCOMPARE(persons.rowCount(), 10);
0044     QVariant value = persons.data(persons.index(0,0));
0045     // value should be empty string in default implementation
0046     QVERIFY(value.isValid());
0047     QCOMPARE(value.type(), QVariant::String);
0048     QCOMPARE(value.toString(), QString());
0049 }
0050 
0051 void TestRustList::testStringSetter()
0052 {
0053     // GIVEN
0054     Persons persons;
0055     QSignalSpy spy(&persons, &Persons::dataChanged);
0056 
0057     // WHEN
0058     const QModelIndex index(persons.index(0,0));
0059     const bool set = persons.setData(index, "Konqi");
0060 
0061     // THEN
0062     QCOMPARE(persons.columnCount(), 1);
0063     QVERIFY(set);
0064     QVERIFY(spy.isValid());
0065     QCOMPARE(spy.count(), 1);
0066     QVariant value = persons.data(persons.index(0,0));
0067     QCOMPARE(value.toString(), QString("Konqi"));
0068 }
0069 
0070 void TestRustList::testAccessByDefaultRole()
0071 {
0072     // GIVEN
0073     NoRole norole;
0074     QSignalSpy spy(&norole, &NoRole::dataChanged);
0075     auto ageRole = norole.role("userAge");
0076     auto nameRole = norole.role("userName");
0077 
0078     // WHEN
0079     const QModelIndex index(norole.index(0,0));
0080     const bool setName = norole.setData(index, "Konqi", nameRole);
0081     const bool setAge = norole.setData(index, 21, ageRole);
0082 
0083     // THEN
0084     QCOMPARE(norole.columnCount(), 1);
0085     QVERIFY(setName);
0086     QVERIFY(setAge);
0087     QVERIFY(spy.isValid());
0088     QCOMPARE(spy.count(), 2);
0089     QVariant name = norole.data(norole.index(0,0), nameRole);
0090     QCOMPARE(name.toString(), QString("Konqi"));
0091     QVariant age = norole.data(norole.index(0,0), ageRole);
0092     QCOMPARE(age.value<quint8>(), (quint8)21);
0093 }
0094 
0095 QTEST_MAIN(TestRustList)
0096 #include "test_list.moc"