diff --git a/src/commonfrontend/spreadsheet/SpreadsheetView.h b/src/commonfrontend/spreadsheet/SpreadsheetView.h --- a/src/commonfrontend/spreadsheet/SpreadsheetView.h +++ b/src/commonfrontend/spreadsheet/SpreadsheetView.h @@ -216,6 +216,7 @@ void fillTouchBar(KDMacTouchBar*); #endif void print(QPrinter*) const; + void pasteIntoSelection(); private slots: void createColumnContextMenu(QMenu*); @@ -229,7 +230,6 @@ void cutSelection(); void copySelection(); - void pasteIntoSelection(); void clearSelectedCells(); void maskSelection(); void unmaskSelection(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,3 +4,4 @@ add_subdirectory(analysis) add_subdirectory(import_export) add_subdirectory(nsl) +add_subdirectory(spreadsheet) diff --git a/tests/spreadsheet/CMakeLists.txt b/tests/spreadsheet/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/tests/spreadsheet/CMakeLists.txt @@ -0,0 +1,9 @@ +INCLUDE_DIRECTORIES(${GSL_INCLUDE_DIR}) + +add_executable (spreadsheettest SpreadsheetTest.cpp) + +target_link_libraries(spreadsheettest Qt5::Test) +target_link_libraries(spreadsheettest ${GSL_LIBRARIES} ${GSL_CBLAS_LIBRARIES}) +target_link_libraries(spreadsheettest labplot2lib) + +add_test(NAME spreadsheettest COMMAND spreadsheettest) diff --git a/tests/spreadsheet/SpreadsheetTest.h b/tests/spreadsheet/SpreadsheetTest.h new file mode 100644 --- /dev/null +++ b/tests/spreadsheet/SpreadsheetTest.h @@ -0,0 +1,45 @@ +/*************************************************************************** + File : SpreadsheetTest.h + Project : LabPlot + Description : Tests for the Spreadsheet + -------------------------------------------------------------------- + Copyright : (C) 2020 Alexander Semke (alexander.semke@web.de) + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + ***************************************************************************/ + +#ifndef SPREADSHEETTEST_H +#define SPREADSHEETTEST_H + +#include + +class SpreadsheetTest : public QObject { + Q_OBJECT + +private slots: + void initTestCase(); + + //copy and paste + void testCopyPaste00(); + void testCopyPaste01(); + void testCopyPaste02(); +}; + +#endif diff --git a/tests/spreadsheet/SpreadsheetTest.cpp b/tests/spreadsheet/SpreadsheetTest.cpp new file mode 100644 --- /dev/null +++ b/tests/spreadsheet/SpreadsheetTest.cpp @@ -0,0 +1,151 @@ +/*************************************************************************** + File : SpreadsheetTest.cpp + Project : LabPlot + Description : Tests for the Spreadsheet + -------------------------------------------------------------------- + Copyright : (C) 2020 Alexander Semke (alexander.semke@web.de) + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + ***************************************************************************/ + +#include +#include + +#include "SpreadsheetTest.h" +#include "backend/spreadsheet/Spreadsheet.h" +#include "../src/commonfrontend/spreadsheet/SpreadsheetView.h" + +void SpreadsheetTest::initTestCase() { + qRegisterMetaType("const AbstractAspect*"); + qRegisterMetaType("const AbstractColumn*"); +} + +/*! + insert two columns with float values into an empty spreadsheet +*/ +void SpreadsheetTest::testCopyPaste00() { + Spreadsheet* sheet = new Spreadsheet("test", false); + + const QString str = "10.0 100.0\n20.0 200.0"; + + QApplication::clipboard()->setText(str); + + SpreadsheetView* view = new SpreadsheetView(sheet, false); + view->pasteIntoSelection(); + + //column modes + QCOMPARE(sheet->column(0)->columnMode(), AbstractColumn::Numeric); + QCOMPARE(sheet->column(1)->columnMode(), AbstractColumn::Numeric); + + //values + QCOMPARE(sheet->column(0)->valueAt(0), 10.0); + QCOMPARE(sheet->column(1)->valueAt(0), 100.0); + QCOMPARE(sheet->column(0)->valueAt(1), 20.0); + QCOMPARE(sheet->column(1)->valueAt(1), 200.0); +} + +/*! + insert one column with integer values and one column with float numbers into an empty spreadsheet. + the first column has to be converted to integer column. +*/ +void SpreadsheetTest::testCopyPaste01() { + Spreadsheet* sheet = new Spreadsheet("test", false); + + const QString str = "10 100.0\n20 200.0"; + + QApplication::clipboard()->setText(str); + + SpreadsheetView* view = new SpreadsheetView(sheet, false); + view->pasteIntoSelection(); + + //column modes + QCOMPARE(sheet->column(0)->columnMode(), AbstractColumn::Integer); + QCOMPARE(sheet->column(1)->columnMode(), AbstractColumn::Numeric); + + //values + QCOMPARE(sheet->column(0)->integerAt(0), 10); + QCOMPARE(sheet->column(1)->valueAt(0), 100.0); + QCOMPARE(sheet->column(0)->integerAt(1), 20); + QCOMPARE(sheet->column(1)->valueAt(1), 200.0); +} + +/*! + insert irregular data, new columns should be added appropriately. +*/ +void SpreadsheetTest::testCopyPaste02() { + Spreadsheet* sheet = new Spreadsheet("test", false); + + const QString str = "0\n" + "10 20\n" + "11 21 31\n" + "12 22 32 42\n" + "13 23\n" + "14"; + + QApplication::clipboard()->setText(str); + + int rows = sheet->rowCount(); + + SpreadsheetView* view = new SpreadsheetView(sheet, false); + view->pasteIntoSelection(); + + //spreadsheet size + QCOMPARE(sheet->columnCount(), 4); + QCOMPARE(sheet->rowCount(), rows); + + //column modes + QCOMPARE(sheet->column(0)->columnMode(), AbstractColumn::Integer); + QCOMPARE(sheet->column(1)->columnMode(), AbstractColumn::Integer); + QCOMPARE(sheet->column(2)->columnMode(), AbstractColumn::Integer); + QCOMPARE(sheet->column(3)->columnMode(), AbstractColumn::Integer); + + //values + QCOMPARE(sheet->column(0)->integerAt(0), 0); + QCOMPARE(sheet->column(1)->integerAt(0), 0); + QCOMPARE(sheet->column(2)->integerAt(0), 0); + QCOMPARE(sheet->column(3)->integerAt(0), 0); + + QCOMPARE(sheet->column(0)->integerAt(1), 10); + QCOMPARE(sheet->column(1)->integerAt(1), 20); + QCOMPARE(sheet->column(2)->integerAt(1), 0); + QCOMPARE(sheet->column(3)->integerAt(1), 0); + + QCOMPARE(sheet->column(0)->integerAt(2), 11); + QCOMPARE(sheet->column(1)->integerAt(2), 21); + QCOMPARE(sheet->column(2)->integerAt(2), 31); + QCOMPARE(sheet->column(3)->integerAt(2), 0); + + QCOMPARE(sheet->column(0)->integerAt(3), 12); + QCOMPARE(sheet->column(1)->integerAt(3), 22); + QCOMPARE(sheet->column(2)->integerAt(3), 32); + QCOMPARE(sheet->column(3)->integerAt(3), 42); + + QCOMPARE(sheet->column(0)->integerAt(4), 13); + QCOMPARE(sheet->column(1)->integerAt(4), 23); + QCOMPARE(sheet->column(2)->integerAt(4), 0); + QCOMPARE(sheet->column(3)->integerAt(4), 0); + + QCOMPARE(sheet->column(0)->integerAt(5), 14); + QCOMPARE(sheet->column(1)->integerAt(5), 0); + QCOMPARE(sheet->column(2)->integerAt(5), 0); + QCOMPARE(sheet->column(3)->integerAt(5), 0); +} + +QTEST_MAIN(SpreadsheetTest)