File indexing completed on 2024-05-19 05:41:34

0001 /***************************************************************************
0002  *   Copyright (C) 2018 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 <QAbstractItemModelTester>
0022 #include <QModelIndex>
0023 #include <QModelIndexList>
0024 #include <QtCore/QString>
0025 #include <QtTest/QtTest>
0026 #include <memory>
0027 
0028 #include "rwidgets/gmtoolbox/UnitConvertor/unit.h"
0029 #include "rwidgets/gmtoolbox/UnitConvertor/unitmodel.h"
0030 
0031 using namespace GMTOOL;
0032 class TestUnitModel : public QObject
0033 {
0034     Q_OBJECT
0035 public:
0036     TestUnitModel();
0037 
0038 private slots:
0039     void init();
0040 
0041     void addTest();
0042     void addTest_data();
0043 
0044     void insertTest();
0045     void removeTest();
0046 
0047     void saveModelTest();
0048 
0049 private:
0050     std::unique_ptr<UnitModel> m_model;
0051 };
0052 
0053 TestUnitModel::TestUnitModel() {}
0054 
0055 void TestUnitModel::init()
0056 {
0057     m_model.reset(new UnitModel());
0058     new QAbstractItemModelTester(m_model.get());
0059 }
0060 
0061 void TestUnitModel::addTest()
0062 {
0063     QFETCH(QStringList, nameList);
0064     QFETCH(QStringList, symbolList);
0065     QFETCH(QList<int>, category);
0066     QFETCH(int, expected);
0067 
0068     QVERIFY(m_model->rowCount() == 0);
0069 
0070     int i= 0;
0071     for(auto name : nameList)
0072     {
0073         auto symbol= symbolList[i];
0074         auto cat= category[i];
0075         m_model->insertData(new Unit(name, symbol, static_cast<Unit::Category>(cat)));
0076     }
0077 
0078     QCOMPARE(m_model->rowCount(), expected);
0079 }
0080 
0081 void TestUnitModel::addTest_data()
0082 {
0083     QTest::addColumn<QStringList>("nameList");
0084     QTest::addColumn<QStringList>("symbolList");
0085     QTest::addColumn<QList<int>>("category");
0086     QTest::addColumn<int>("expected");
0087 
0088     QTest::addRow("list1") << QStringList() << QStringList() << QList<int>() << 0;
0089     QTest::addRow("list2") << QStringList({"Kilometer"}) << QStringList({"km"}) << QList<int>({Unit::DISTANCE}) << 1;
0090     QTest::addRow("list3") << QStringList({"Kilometer", "meter"}) << QStringList({"km", "m"})
0091                            << QList<int>({Unit::DISTANCE, Unit::DISTANCE}) << 2;
0092     QTest::addRow("list4") << QStringList({"Kilometer", "meter", "gram"}) << QStringList({"km", "m", "g"})
0093                            << QList<int>({Unit::DISTANCE, Unit::DISTANCE, Unit::MASS}) << 3;
0094 }
0095 
0096 void TestUnitModel::removeTest()
0097 {
0098     /* QStringList list;
0099      list << "song1.mp3"
0100           << "song2.ogg";
0101      m_model->addSong(list);
0102 
0103      QCOMPARE(m_model->rowCount(), 2);
0104 
0105      m_model->removeAll();
0106 
0107      QCOMPARE(m_model->rowCount(), 0);
0108 
0109      m_model->addSong(list);
0110 
0111      QCOMPARE(m_model->rowCount(), 2);
0112 
0113      QModelIndexList indexList;
0114      auto index= m_model->index(0, 0);
0115      indexList << index;
0116      m_model->removeSong(indexList);
0117 
0118      QCOMPARE(m_model->rowCount(), 1);
0119 
0120      m_model->removeSong(indexList);
0121 
0122      QCOMPARE(m_model->rowCount(), 0);
0123 
0124      m_model->addSong(list);
0125 
0126      QCOMPARE(m_model->rowCount(), 2);
0127 
0128      index= m_model->index(0, 0);
0129      auto index1= m_model->index(1, 0);
0130      indexList << index << index1;
0131      m_model->removeSong(indexList);
0132      QCOMPARE(m_model->rowCount(), 0);*/
0133 }
0134 
0135 void TestUnitModel::insertTest()
0136 {
0137     /*    QStringList list;
0138         list << "/home/file/song1.mp3"
0139              << "/home/file/song2.ogg";
0140         m_model->addSong(list);
0141 
0142         QCOMPARE(m_model->rowCount(), 2);
0143         QString song("/home/file/songInserted.mp3");
0144         m_model->insertSong(1, song);
0145 
0146         QCOMPARE(m_model->rowCount(), 3);
0147 
0148         auto index= m_model->index(1, 0);
0149         QCOMPARE(index.data().toString(), "songInserted.mp3");*/
0150 }
0151 
0152 void TestUnitModel::saveModelTest()
0153 {
0154     /*   QStringList list;
0155        list << "http://song1.mp3"
0156             << "http://song2.ogg";
0157        m_model->addSong(list);
0158 
0159        QString data;
0160        QTextStream out(&data, QIODevice::WriteOnly);
0161        m_model->saveIn(out);
0162        auto list2= data.split('\n', QString::SkipEmptyParts);
0163 
0164        QCOMPARE(list, list2);*/
0165 }
0166 
0167 QTEST_MAIN(TestUnitModel);
0168 
0169 #include "tst_unitmodel.moc"