From f52d609b9c6b8af4548e44dff88294e6a321a025 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 31 Oct 2015 14:41:52 +0200 Subject: [PATCH] Refactoring. Test functions RelativeMPath and AbsoluteMPath. --HG-- branch : develop --- src/app/valentina/mainwindow.cpp | 35 ------ src/app/valentina/mainwindow.h | 3 - src/libs/vmisc/def.cpp | 38 ++++++ src/libs/vmisc/def.h | 2 + src/test/ValentinaTest/ValentinaTest.pro | 6 +- src/test/ValentinaTest/qttestmainlambda.cpp | 2 + src/test/ValentinaTest/tst_misc.cpp | 132 ++++++++++++++++++++ src/test/ValentinaTest/tst_misc.h | 51 ++++++++ 8 files changed, 229 insertions(+), 40 deletions(-) create mode 100644 src/test/ValentinaTest/tst_misc.cpp create mode 100644 src/test/ValentinaTest/tst_misc.h diff --git a/src/app/valentina/mainwindow.cpp b/src/app/valentina/mainwindow.cpp index 1da1d13ca..0a3ac673a 100644 --- a/src/app/valentina/mainwindow.cpp +++ b/src/app/valentina/mainwindow.cpp @@ -420,41 +420,6 @@ void MainWindow::ToggleMSync(bool toggle) } } -//--------------------------------------------------------------------------------------------------------------------- -QString MainWindow::RelativeMPath(const QString &patternPath, const QString &absoluteMPath) const -{ - if (patternPath.isEmpty()) - { - return absoluteMPath; - } - - if (absoluteMPath.isEmpty() || QFileInfo(absoluteMPath).isRelative()) - { - return absoluteMPath; - } - - QDir dir(QFileInfo(patternPath).absoluteDir()); - return dir.relativeFilePath(absoluteMPath); -} - -//--------------------------------------------------------------------------------------------------------------------- -QString MainWindow::AbsoluteMPath(const QString &patternPath, const QString &relativeMPath) const -{ - if (patternPath.isEmpty()) - { - return relativeMPath; - } - else - { - if (relativeMPath.isEmpty() || QFileInfo(relativeMPath).isAbsolute()) - { - return relativeMPath; - } - - return QFileInfo(QFileInfo(patternPath).absoluteDir(), relativeMPath).absoluteFilePath(); - } -} - //--------------------------------------------------------------------------------------------------------------------- /** * @brief OptionDraw help change name of pattern piece. diff --git a/src/app/valentina/mainwindow.h b/src/app/valentina/mainwindow.h index 07bb83c89..0650b337c 100644 --- a/src/app/valentina/mainwindow.h +++ b/src/app/valentina/mainwindow.h @@ -304,9 +304,6 @@ private: void ToggleMSync(bool toggle); - QString RelativeMPath(const QString &patternPath, const QString &absoluteMPath) const; - QString AbsoluteMPath(const QString &patternPath, const QString &relativeMPath) const; - bool LoadPattern(const QString &curFile, const QString &customMeasureFile = QString()); void ReopenFilesAfterCrash(QStringList &args); void DoExport(const VCommandLinePtr& expParams); diff --git a/src/libs/vmisc/def.cpp b/src/libs/vmisc/def.cpp index 29afeb98f..4dcc015af 100644 --- a/src/libs/vmisc/def.cpp +++ b/src/libs/vmisc/def.cpp @@ -31,6 +31,7 @@ #include "../vpatterndb/vtranslatevars.h" #include +#include // Keep synchronize all names with initialization in VTranslateVars class!!!!! //measurements @@ -1681,3 +1682,40 @@ QString StrippedName(const QString &fullFileName) { return QFileInfo(fullFileName).fileName(); } + +//--------------------------------------------------------------------------------------------------------------------- +QString RelativeMPath(const QString &patternPath, const QString &absoluteMPath) +{ + if (patternPath.isEmpty()) + { + return absoluteMPath; + } + + if (absoluteMPath.isEmpty() || QFileInfo(absoluteMPath).isRelative()) + { + return absoluteMPath; + } + + QDir dir(QFileInfo(patternPath).absoluteDir()); + return dir.relativeFilePath(absoluteMPath); +} + +//--------------------------------------------------------------------------------------------------------------------- +QString AbsoluteMPath(const QString &patternPath, const QString &relativeMPath) +{ + if (patternPath.isEmpty()) + { + return relativeMPath; + } + else + { + if (relativeMPath.isEmpty() || QFileInfo(relativeMPath).isAbsolute()) + { + return relativeMPath; + } + + return QFileInfo(QFileInfo(patternPath).absoluteDir(), relativeMPath).absoluteFilePath(); + } + + return QString();// should never reach +} diff --git a/src/libs/vmisc/def.h b/src/libs/vmisc/def.h index 0f5b95245..5bfa30d26 100644 --- a/src/libs/vmisc/def.h +++ b/src/libs/vmisc/def.h @@ -588,5 +588,7 @@ QStringList SupportedLocales(); QStringList AllGroupNames(); QString StrippedName(const QString &fullFileName); +QString RelativeMPath(const QString &patternPath, const QString &absoluteMPath); +QString AbsoluteMPath(const QString &patternPath, const QString &relativeMPath); #endif // DEF_H diff --git a/src/test/ValentinaTest/ValentinaTest.pro b/src/test/ValentinaTest/ValentinaTest.pro index 8c2e09243..13cf5a921 100644 --- a/src/test/ValentinaTest/ValentinaTest.pro +++ b/src/test/ValentinaTest/ValentinaTest.pro @@ -49,7 +49,8 @@ SOURCES += \ tst_qmutokenparser.cpp \ tst_vmeasurements.cpp \ tst_qmuparsererrormsg.cpp \ - tst_vlockguard.cpp + tst_vlockguard.cpp \ + tst_misc.cpp HEADERS += \ tst_vposter.h \ @@ -66,7 +67,8 @@ HEADERS += \ tst_qmutokenparser.h \ tst_vmeasurements.h \ tst_qmuparsererrormsg.h \ - tst_vlockguard.h + tst_vlockguard.h \ + tst_misc.h # Set using ccache. Function enable_ccache() defined in common.pri. $$enable_ccache() diff --git a/src/test/ValentinaTest/qttestmainlambda.cpp b/src/test/ValentinaTest/qttestmainlambda.cpp index 6e197e22f..76fb6a09e 100644 --- a/src/test/ValentinaTest/qttestmainlambda.cpp +++ b/src/test/ValentinaTest/qttestmainlambda.cpp @@ -41,6 +41,7 @@ #include "tst_vmeasurements.h" #include "tst_qmuparsererrormsg.h" #include "tst_vlockguard.h" +#include "tst_misc.h" int main(int argc, char** argv) { @@ -68,6 +69,7 @@ int main(int argc, char** argv) ASSERT_TEST(new TST_VMeasurements()); ASSERT_TEST(new TST_QmuParserErrorMsg()); ASSERT_TEST(new TST_VLockGuard()); + ASSERT_TEST(new TST_Misc()); return status; } diff --git a/src/test/ValentinaTest/tst_misc.cpp b/src/test/ValentinaTest/tst_misc.cpp new file mode 100644 index 000000000..f5b4ff4f0 --- /dev/null +++ b/src/test/ValentinaTest/tst_misc.cpp @@ -0,0 +1,132 @@ +/************************************************************************ + ** + ** @file tst_misc.cpp + ** @author Roman Telezhynskyi + ** @date 31 10, 2015 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentine project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2015 Valentina project + ** All Rights Reserved. + ** + ** Valentina 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 3 of the License, or + ** (at your option) any later version. + ** + ** Valentina 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 Valentina. If not, see . + ** + *************************************************************************/ + +#include "tst_misc.h" +#include "../vmisc/def.h" + +#include + +//--------------------------------------------------------------------------------------------------------------------- +TST_Misc::TST_Misc(QObject *parent) + :QObject(parent) +{ +} + +//--------------------------------------------------------------------------------------------------------------------- +void TST_Misc::TestRelativeFilePath_data() +{ + QTest::addColumn("patternPath"); + QTest::addColumn("absoluteMPath"); + QTest::addColumn("output"); + + QTest::newRow("Measurements one level above") + << "/home/user/patterns/pattern.val" << "/home/user/measurements/m.vit" << "../measurements/m.vit"; + + QTest::newRow("Measurements one level under") + << "/home/user/patterns/pattern.val" << "/home/user/patterns/measurements/m.vit" << "measurements/m.vit"; + + QTest::newRow("Measurements in the same folder") + << "/home/user/patterns/pattern.val" << "/home/user/patterns/m.vit" << "m.vit"; + + QTest::newRow("Path to measurements is empty") + << "/home/user/patterns/pattern.val" << "" << ""; + + QTest::newRow("Path to a pattern file is empty. Ablosute measurements path.") + << "" << "/home/user/patterns/m.vit" << "/home/user/patterns/m.vit"; + + QTest::newRow("Path to a pattern file is empty. Relative measurements path.") + << "" << "measurements/m.vit" << "measurements/m.vit"; + + QTest::newRow("Relative measurements path.") + << "/home/user/patterns/pattern.val" << "../measurements/m.vit" << "../measurements/m.vit"; + + QTest::newRow("Both paths are empty") << "" << "" << ""; + + QTest::newRow("Path to measurements is relative") + << "/home/user/patterns/pattern.val" << "m.vit" << "m.vit"; + + QTest::newRow("Absolute pattern path.") << "/home/user/patterns" << "m.vit" << "m.vit"; +} + +//--------------------------------------------------------------------------------------------------------------------- +void TST_Misc::TestRelativeFilePath() +{ + QFETCH(QString, patternPath); + QFETCH(QString, absoluteMPath); + QFETCH(QString, output); + + const QString result = RelativeMPath(patternPath, absoluteMPath); + QCOMPARE(output, result); +} + +//--------------------------------------------------------------------------------------------------------------------- +void TST_Misc::TestAbsoluteFilePath_data() +{ + QTest::addColumn("patternPath"); + QTest::addColumn("relativeMPath"); + QTest::addColumn("output"); + + QTest::newRow("Measurements one level above") + << "/home/user/patterns/pattern.val" << "../measurements/m.vit" << "/home/user/measurements/m.vit"; + + QTest::newRow("Measurements one level under") + << "/home/user/patterns/pattern.val" << "measurements/m.vit" << "/home/user/patterns/measurements/m.vit"; + + QTest::newRow("Measurements in the same folder") + << "/home/user/patterns/pattern.val" << "m.vit" << "/home/user/patterns/m.vit"; + + QTest::newRow("Path to measurements is empty") + << "/home/user/patterns/pattern.val" << "" << ""; + + QTest::newRow("Path to a pattern file is empty. Ablosute measurements path.") + << "" << "/home/user/patterns/m.vit" << "/home/user/patterns/m.vit"; + + QTest::newRow("Path to a pattern file is empty. Relative measurements path.") + << "" << "measurements/m.vit" << "measurements/m.vit"; + + QTest::newRow("Relative measurements path.") + << "/home/user/patterns/pattern.val" << "../measurements/m.vit" << "/home/user/measurements/m.vit"; + + QTest::newRow("Both paths are empty") << "" << "" << ""; + + QTest::newRow("Path to measurements is relative") + << "/home/user/patterns/pattern.val" << "m.vit" << "/home/user/patterns/m.vit"; + + QTest::newRow("Absolute pattern path.") << "/home/user/patterns" << "m.vit" << "/home/user/m.vit"; +} + +//--------------------------------------------------------------------------------------------------------------------- +void TST_Misc::TestAbsoluteFilePath() +{ + QFETCH(QString, patternPath); + QFETCH(QString, relativeMPath); + QFETCH(QString, output); + + const QString result = AbsoluteMPath(patternPath, relativeMPath); + QCOMPARE(output, result); +} diff --git a/src/test/ValentinaTest/tst_misc.h b/src/test/ValentinaTest/tst_misc.h new file mode 100644 index 000000000..61455a655 --- /dev/null +++ b/src/test/ValentinaTest/tst_misc.h @@ -0,0 +1,51 @@ +/************************************************************************ + ** + ** @file tst_misc.h + ** @author Roman Telezhynskyi + ** @date 31 10, 2015 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentine project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2015 Valentina project + ** All Rights Reserved. + ** + ** Valentina 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 3 of the License, or + ** (at your option) any later version. + ** + ** Valentina 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 Valentina. If not, see . + ** + *************************************************************************/ + +#ifndef TST_MISC_H +#define TST_MISC_H + +#include + +class TST_Misc :public QObject +{ + Q_OBJECT +public: + explicit TST_Misc(QObject *parent = nullptr); + +private slots: + void TestRelativeFilePath_data(); + void TestRelativeFilePath(); + + void TestAbsoluteFilePath_data(); + void TestAbsoluteFilePath(); + +private: + Q_DISABLE_COPY(TST_Misc) +}; + +#endif // TST_MISC_H