File indexing completed on 2024-05-12 16:35:54

0001 /* This file is part of the KDE project
0002    Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; only
0007    version 2 of the License.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "TestDependencies.h"
0021 
0022 #include <QTest>
0023 
0024 #include "CellStorage.h"
0025 #include "DependencyManager.h"
0026 #include "DependencyManager_p.h"
0027 #include "Formula.h"
0028 #include "Map.h"
0029 #include "Region.h"
0030 #include "Sheet.h"
0031 #include "Value.h"
0032 
0033 using namespace Calligra::Sheets;
0034 
0035 void TestDependencies::initTestCase()
0036 {
0037     m_map = new Map(0 /* no Doc */);
0038     m_sheet = m_map->addNewSheet();
0039     m_sheet->setSheetName("Sheet1");
0040     m_storage = m_sheet->cellStorage();
0041 }
0042 
0043 void TestDependencies::testCircleRemoval()
0044 {
0045     Formula formula(m_sheet);
0046     formula.setExpression("=A1");
0047     m_storage->setFormula(1, 1, formula); // A1
0048 
0049     QApplication::processEvents(); // handle Damages
0050 
0051     QCOMPARE(m_storage->value(1, 1), Value::errorCIRCLE());
0052     DependencyManager* manager = m_map->dependencyManager();
0053     QVERIFY(manager->d->consumers.count() == 1);
0054     QVERIFY(manager->d->providers.count() == 1);
0055     QList<Cell> consumers = manager->d->consumers.value(m_sheet)->contains(QRect(1, 1, 1, 1));
0056     QCOMPARE(consumers.count(), 1);
0057     QCOMPARE(consumers.first(), Cell(m_sheet, 1, 1));
0058     QCOMPARE(manager->d->providers.value(Cell(m_sheet, 1, 1)), Region(QRect(1, 1, 1, 1), m_sheet));
0059 
0060     m_storage->setFormula(1, 1, Formula()); // A1
0061 
0062     QApplication::processEvents(); // handle Damages
0063 
0064     QCOMPARE(m_storage->value(1, 1), Value());
0065     QVERIFY(manager->d->consumers.value(m_sheet)->contains(QRect(1, 1, 1, 1)).count() == 0);
0066     QVERIFY(manager->d->providers.count() == 0);
0067 }
0068 
0069 void TestDependencies::testCircles()
0070 {
0071     Formula formula(m_sheet);
0072     formula.setExpression("=A3");
0073     m_storage->setFormula(1, 1, formula); // A1
0074     formula.setExpression("=A1");
0075     m_storage->setFormula(1, 2, formula); // A2
0076     formula.setExpression("=A2");
0077     m_storage->setFormula(1, 3, formula); // A3
0078 
0079     QApplication::processEvents(); // handle Damages
0080 
0081     QCOMPARE(m_storage->value(1, 1), Value::errorCIRCLE());
0082     QCOMPARE(m_storage->value(1, 2), Value::errorCIRCLE());
0083     QCOMPARE(m_storage->value(1, 3), Value::errorCIRCLE());
0084 }
0085 
0086 void TestDependencies::testDepths()
0087 {
0088     Cell a1(m_sheet, 1, 1); a1.setUserInput("5");
0089     Cell a2(m_sheet, 1, 2); a2.setUserInput("=A1");
0090     Cell a3(m_sheet, 1, 3); a3.setUserInput("=A2");
0091     Cell a4(m_sheet, 1, 4); a4.setUserInput("=A1 + A3");
0092 
0093     QApplication::processEvents(); // handle Damages
0094     
0095     QMap<Cell, int> depths = m_map->dependencyManager()->depths();
0096     QCOMPARE(depths[a1], 0);
0097     QCOMPARE(depths[a2], 1);
0098     QCOMPARE(depths[a3], 2);
0099     QCOMPARE(depths[a4], 3);
0100 
0101     a2.setUserInput("");
0102     QApplication::processEvents(); // handle Damages
0103     
0104     depths = m_map->dependencyManager()->depths();
0105     QCOMPARE(depths[a1], 0);
0106     QCOMPARE(depths[a2], 0);
0107     QCOMPARE(depths[a3], 1);
0108     QCOMPARE(depths[a4], 2);
0109 }
0110 
0111 void TestDependencies::cleanupTestCase()
0112 {
0113     delete m_map;
0114 }
0115 
0116 QTEST_MAIN(TestDependencies)