File indexing completed on 2024-05-12 05:40:50
0001 /*************************************************************************** 0002 * Copyright (C) 2018 by Renaud Guezennec * 0003 * renaud@rolisteam.org * 0004 * * 0005 * Rolisteam is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 0021 #include <QAbstractItemModelTester> 0022 #include <QJsonObject> 0023 #include <QModelIndex> 0024 #include <QModelIndexList> 0025 #include <QtCore/QString> 0026 #include <QtTest/QtTest> 0027 #include <memory> 0028 0029 #include "diceparser/dicealias.h" 0030 #include "model/dicealiasmodel.h" 0031 0032 class TestDiceAliasModel : public QObject 0033 { 0034 Q_OBJECT 0035 public: 0036 TestDiceAliasModel(); 0037 0038 void addDefaultAlias(); 0039 private slots: 0040 void init(); 0041 0042 void addTest(); 0043 void addTest_data(); 0044 0045 void moveTest(); 0046 void removeTest(); 0047 0048 void saveModelTest(); 0049 0050 private: 0051 std::unique_ptr<DiceAliasModel> m_model; 0052 }; 0053 0054 TestDiceAliasModel::TestDiceAliasModel() {} 0055 0056 void TestDiceAliasModel::init() 0057 { 0058 m_model.reset(new DiceAliasModel()); 0059 new QAbstractItemModelTester(m_model.get()); 0060 } 0061 0062 void TestDiceAliasModel::addDefaultAlias() 0063 { 0064 m_model->appendAlias(DiceAlias("l5r", "D10k")); 0065 m_model->appendAlias(DiceAlias("l5R", "D10K")); 0066 m_model->appendAlias(DiceAlias("DF", "D[-1-1]")); 0067 m_model->appendAlias(DiceAlias("nwod", "D10e10c[>7]")); 0068 m_model->appendAlias(DiceAlias("(.*)wod(.*)", "\\1d10e[=10]c[>=\\2]-@c[=1]", "", false)); 0069 } 0070 0071 void TestDiceAliasModel::addTest() 0072 { 0073 QFETCH(QString, pattern); 0074 QFETCH(QString, replace); 0075 QFETCH(bool, regexp); 0076 0077 QVERIFY(m_model->rowCount() == 0); 0078 0079 m_model->appendAlias(DiceAlias(pattern, replace, "", regexp)); 0080 0081 QCOMPARE(m_model->rowCount(), 1); 0082 } 0083 0084 void TestDiceAliasModel::addTest_data() 0085 { 0086 QTest::addColumn<QString>("pattern"); 0087 QTest::addColumn<QString>("replace"); 0088 QTest::addColumn<bool>("regexp"); 0089 0090 QTest::addRow("alias1") << "k" 0091 << "d10k" << false; 0092 QTest::addRow("alias2") << "K" 0093 << "d10e10k" << false; 0094 QTest::addRow("alias3") << "nwod" 0095 << "d10e10c[>7]" << false; 0096 QTest::addRow("alias4") << "blue" 0097 << "d10e10k" << false; 0098 } 0099 0100 void TestDiceAliasModel::removeTest() 0101 { 0102 addDefaultAlias(); 0103 QCOMPARE(m_model->rowCount(), 5); 0104 0105 m_model->clear(); 0106 QCOMPARE(m_model->rowCount(), 0); 0107 0108 addDefaultAlias(); 0109 QCOMPARE(m_model->rowCount(), 5); 0110 0111 QModelIndexList indexList; 0112 auto index= m_model->index(0, 0); 0113 indexList << index; 0114 m_model->deleteAlias(index); 0115 QCOMPARE(m_model->rowCount(), 4); 0116 0117 m_model->clear(); 0118 QCOMPARE(m_model->rowCount(), 0); 0119 0120 addDefaultAlias(); 0121 QCOMPARE(m_model->rowCount(), 5); 0122 0123 index= m_model->index(0, 0); 0124 auto index1= m_model->index(1, 0); 0125 m_model->deleteAlias(index1); 0126 m_model->deleteAlias(index); 0127 QCOMPARE(m_model->rowCount(), 3); 0128 } 0129 0130 void TestDiceAliasModel::moveTest() 0131 { 0132 auto const& listState= m_model->aliases(); 0133 0134 addDefaultAlias(); 0135 QCOMPARE(m_model->rowCount(), 5); 0136 0137 auto index= m_model->index(1, 0); 0138 m_model->upAlias(index); 0139 QCOMPARE(listState.at(0)->pattern(), "l5R"); 0140 0141 index= m_model->index(1, 0); 0142 m_model->downAlias(index); 0143 QCOMPARE(listState.at(1)->pattern(), "DF"); 0144 QCOMPARE(listState.at(2)->pattern(), "l5r"); 0145 0146 index= m_model->index(4, 0); 0147 m_model->topAlias(index); 0148 QCOMPARE(listState.at(0)->pattern(), "(.*)wod(.*)"); 0149 0150 index= m_model->index(0, 0); 0151 m_model->bottomAlias(index); 0152 QCOMPARE(listState.at(4)->pattern(), "(.*)wod(.*)"); 0153 } 0154 0155 void TestDiceAliasModel::saveModelTest() 0156 { 0157 addDefaultAlias(); 0158 QCOMPARE(m_model->rowCount(), 5); 0159 0160 /*QJsonObject obj; 0161 m_model->save(obj); 0162 0163 DiceAliasModel model; 0164 0165 model.load(obj); 0166 QCOMPARE(model.rowCount(), 5); 0167 auto list= model.getAliases(); 0168 QCOMPARE(list->at(0)->getCommand(), "l5r"); 0169 QCOMPARE(list->at(4)->getCommand(), "(.*)wod(.*)");*/ 0170 } 0171 0172 QTEST_MAIN(TestDiceAliasModel) 0173 0174 #include "tst_dicealiasmodel.moc"