File indexing completed on 2024-05-12 05:40:47
0001 /*************************************************************************** 0002 * Copyright (C) 2011 by Renaud Guezennec * 0003 * http://renaudguezennec.homelinux.org/accueil,3.html * 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 <QtCore/QCoreApplication> 0022 #include <QtCore/QString> 0023 #include <QtTest/QtTest> 0024 0025 #include "charactersheet.h" 0026 #include "field.h" 0027 0028 #include "formula/formulamanager.h" 0029 0030 class TestCharacterSheet : public QObject 0031 { 0032 Q_OBJECT 0033 0034 public: 0035 TestCharacterSheet(); 0036 0037 private slots: 0038 void initTestCase(); 0039 void getAndSetTest(); 0040 void cleanupTestCase(); 0041 void changeValue(); 0042 void changeReadOnly(); 0043 void changeLabel(); 0044 void insertField(); 0045 void commandsTest(); 0046 void commandsTest_data(); 0047 void wrongCommandsTest(); 0048 0049 private: 0050 CharacterSheet* m_sheet= nullptr; 0051 QList<CharacterSheetItem*> m_list; 0052 Formula::FormulaManager* m_formulaMan= nullptr; 0053 QHash<QString, QString>* m_variable= nullptr; 0054 }; 0055 0056 TestCharacterSheet::TestCharacterSheet() {} 0057 0058 void TestCharacterSheet::initTestCase() 0059 { 0060 m_sheet= new CharacterSheet(); 0061 Field* field= new Field(); 0062 field->setLabel("name"); 0063 field->setId("id_1"); 0064 m_list << field; 0065 0066 field= new Field(); 0067 field->setLabel("last name"); 0068 field->setId("id_2"); 0069 m_list << field; 0070 0071 field= new Field(); 0072 field->setLabel("age"); 0073 field->setId("id_3"); 0074 m_list << field; 0075 0076 field= new Field(); 0077 field->setLabel("Species"); 0078 field->setId("id_4"); 0079 m_list << field; 0080 0081 field= new Field(); 0082 field->setLabel("Intelligence"); 0083 field->setId("id_5"); 0084 m_list << field; 0085 0086 // Formula 0087 m_formulaMan= new Formula::FormulaManager(); 0088 m_variable= new QHash<QString, QString>(); 0089 0090 m_variable->insert("Name", "John Doe"); 0091 m_variable->insert("weight", "85kg"); 0092 m_variable->insert("age", "18"); 0093 m_variable->insert("size", "1.69"); 0094 m_variable->insert("strenght", "4"); 0095 m_variable->insert("intelligence", "5"); 0096 m_variable->insert("speed", "59"); 0097 m_variable->insert("ecole", "Akodo"); 0098 m_variable->insert("arme", "Katana"); 0099 m_variable->insert("XP", "0"); 0100 m_variable->insert("max health", "82"); 0101 m_variable->insert("mana", "10000"); 0102 m_variable->insert("level", "-1"); 0103 m_variable->insert("agility", "-2.5"); 0104 m_variable->insert("manipulation", "10"); 0105 m_variable->insert("mental health", "90%"); 0106 0107 m_formulaMan->setConstantHash(m_variable); 0108 } 0109 void TestCharacterSheet::insertField() 0110 { 0111 for(auto field : m_list) 0112 { 0113 m_sheet->insertCharacterItem(field); 0114 } 0115 QVERIFY2(m_sheet->getFieldCount() == m_list.size(), "The number of field is not right."); 0116 } 0117 void TestCharacterSheet::getAndSetTest() {} 0118 void TestCharacterSheet::changeValue() {} 0119 void TestCharacterSheet::changeReadOnly() {} 0120 void TestCharacterSheet::changeLabel() {} 0121 void TestCharacterSheet::commandsTest() 0122 { 0123 QFETCH(QString, cmd); 0124 QFETCH(qreal, result); 0125 0126 auto a= m_formulaMan->getValue(cmd).toDouble(); 0127 QVERIFY(qFuzzyCompare(a, result)); 0128 } 0129 0130 void TestCharacterSheet::commandsTest_data() 0131 { 0132 QTest::addColumn<QString>("cmd"); 0133 QTest::addColumn<qreal>("result"); 0134 QTest::addColumn<bool>("expected"); 0135 0136 QTest::addRow("cmd1") << "=4*4" << 16. << true; 0137 QTest::addRow("cmd2") << "=10+8" << 18. << true; 0138 QTest::addRow("cmd3") << "=10-7" << 3. << true; 0139 QTest::addRow("cmd4") << "=50/5" << 10. << true; 0140 QTest::addRow("cmd5") << "=25-52" << -27. << true; 0141 QTest::addRow("cmd6") << "=${speed}+10" << 69. << true; 0142 QTest::addRow("cmd7") << "=${size}*10" << 16.9 << true; 0143 QTest::addRow("cmd8") << "=${level}-100" << -101. << true; 0144 QTest::addRow("cmd9") << "=2+4/4" << 3. << true; 0145 QTest::addRow("cmd10") << "=${manipulation}/100" << 0.1 << true; 0146 QTest::addRow("cmd11") << "=${size}*${speed}" << 99.71 << true; 0147 QTest::addRow("cmd12") << "=${agility}-100" << -102.5 << true; 0148 QTest::addRow("cmd13") << "=${agility}*100" << -250. << true; 0149 QTest::addRow("cmd14") << "=min(${intelligence},${strenght})" << 4. << true; 0150 QTest::addRow("cmd15") << "=max(${intelligence},${strenght})" << 5. << true; 0151 QTest::addRow("cmd16") << "=abs(${size})" << 1.69 << true; 0152 QTest::addRow("cmd17") << "=18*1.69-10/-1" << 40.42 << true; 0153 QTest::addRow("cmd18") << "=avg(${age},20})" << 19. << true; 0154 QTest::addRow("cmd19") << "=(10+2)*3" << 36. << true; 0155 QTest::addRow("cmd20") << "=(10-4)*3" << 18. << true; 0156 QTest::addRow("cmd21") << "=((10-4)+6)*2" << 24. << true; 0157 QTest::addRow("cmd22") << "=(10+2)*(10-8)" << 24. << true; 0158 } 0159 void TestCharacterSheet::wrongCommandsTest() 0160 { 0161 QStringList commands; 0162 0163 for(QString cmd : commands) 0164 { 0165 QVariant a= m_formulaMan->getValue(cmd); 0166 0167 QCOMPARE(a.toString(), cmd.toStdString().c_str()); 0168 } 0169 } 0170 void TestCharacterSheet::cleanupTestCase() 0171 { 0172 if(m_sheet) 0173 delete m_sheet; 0174 0175 if(m_formulaMan) 0176 delete m_formulaMan; 0177 0178 /* if(m_variable) 0179 delete m_variable;*/ 0180 // qDeleteAll(m_list); 0181 } 0182 0183 QTEST_MAIN(TestCharacterSheet); 0184 0185 #include "tst_charactersheet.moc"