Refactoring class TST_MeasurementRegExp. Speed up testing time.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2016-10-25 21:06:58 +03:00
parent 6ca4abd8fd
commit 81657eb429
8 changed files with 639 additions and 280 deletions

View File

@ -39,7 +39,9 @@ SOURCES += \
qttestmainlambda.cpp \ qttestmainlambda.cpp \
tst_measurementregexp.cpp \ tst_measurementregexp.cpp \
tst_qmuparsererrormsg.cpp \ tst_qmuparsererrormsg.cpp \
tst_tstranslation.cpp tst_tstranslation.cpp \
tst_buitinregexp.cpp \
tst_abstractregexp.cpp
win32-msvc*:SOURCES += stable.cpp win32-msvc*:SOURCES += stable.cpp
@ -47,7 +49,9 @@ HEADERS += \
stable.h \ stable.h \
tst_measurementregexp.h \ tst_measurementregexp.h \
tst_qmuparsererrormsg.h \ tst_qmuparsererrormsg.h \
tst_tstranslation.h tst_tstranslation.h \
tst_buitinregexp.h \
tst_abstractregexp.h
# Set using ccache. Function enable_ccache() defined in common.pri. # Set using ccache. Function enable_ccache() defined in common.pri.
$$enable_ccache() $$enable_ccache()

View File

@ -29,6 +29,7 @@
#include <QtTest> #include <QtTest>
#include "tst_measurementregexp.h" #include "tst_measurementregexp.h"
#include "tst_buitinregexp.h"
#include "tst_qmuparsererrormsg.h" #include "tst_qmuparsererrormsg.h"
#include "tst_tstranslation.h" #include "tst_tstranslation.h"
@ -47,23 +48,24 @@ int main(int argc, char** argv)
ASSERT_TEST(new TST_TSTranslation()); ASSERT_TEST(new TST_TSTranslation());
const QStringList locales = SupportedLocales();
for(quint32 s = 0; s < TST_MeasurementRegExp::systemCounts; ++s)
{ {
const QStringList locales = SupportedLocales();
for(quint32 s = 0; s < TST_MeasurementRegExp::systemCounts; ++s)
{
for(int l = 0, sz = locales.size(); l < sz; ++l)
{
ASSERT_TEST(new TST_MeasurementRegExp(s, locales.at(l)));
}
}
for(int l = 0, sz = locales.size(); l < sz; ++l) for(int l = 0, sz = locales.size(); l < sz; ++l)
{ {
ASSERT_TEST(new TST_QmuParserErrorMsg(locales.at(l))); ASSERT_TEST(new TST_MeasurementRegExp(s, locales.at(l)));
} }
} }
for(int l = 0, sz = locales.size(); l < sz; ++l)
{
ASSERT_TEST(new TST_BuitInRegExp(locales.at(l)));
}
for(int l = 0, sz = locales.size(); l < sz; ++l)
{
ASSERT_TEST(new TST_QmuParserErrorMsg(locales.at(l)));
}
return status; return status;
} }

View File

@ -0,0 +1,198 @@
/************************************************************************
**
** @file
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 25 10, 2016
**
** @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) 2016 Valentina project
** <https://bitbucket.org/dismine/valentina> 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 <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include "tst_abstractregexp.h"
#include "../qmuparser/qmudef.h"
#include "../vmisc/logging.h"
#include "../vpatterndb/vtranslatevars.h"
#include "../ifc/ifcdef.h"
#include <QtTest>
#include <QTranslator>
//---------------------------------------------------------------------------------------------------------------------
TST_AbstractRegExp::TST_AbstractRegExp(const QString &locale, QObject *parent)
: AbstractTest(parent),
m_locale(locale),
m_vTranslator(nullptr),
m_trMs(nullptr)
{
}
//---------------------------------------------------------------------------------------------------------------------
TST_AbstractRegExp::~TST_AbstractRegExp()
{
delete m_vTranslator;
delete m_trMs;
}
//---------------------------------------------------------------------------------------------------------------------
int TST_AbstractRegExp::LoadVariables(const QString &checkedLocale)
{
const QString path = TranslationsPath();
const QString file = QString("valentina_%1.qm").arg(checkedLocale);
if (QFileInfo(path+QLatin1String("/")+file).size() <= 34)
{
const QString message = QString("Translation variables for locale = %1 is empty. \nFull path: %2/%3")
.arg(checkedLocale)
.arg(path)
.arg(file);
QWARN(qUtf8Printable(message));
return ErrorSize;
}
m_vTranslator = new QTranslator(this);
if (not m_vTranslator->load(file, path))
{
const QString message = QString("Can't load translation variables for locale = %1. \nFull path: %2/%3")
.arg(checkedLocale)
.arg(path)
.arg(file);
QWARN(qUtf8Printable(message));
delete m_vTranslator;
return ErrorLoad;
}
if (not QCoreApplication::installTranslator(m_vTranslator))
{
const QString message = QString("Can't install translation variables for locale = %1. \nFull path: %2/%3")
.arg(checkedLocale)
.arg(path)
.arg(file);
QWARN(qUtf8Printable(message));
delete m_vTranslator;
return ErrorInstall;
}
return NoError;
}
//---------------------------------------------------------------------------------------------------------------------
void TST_AbstractRegExp::RemoveTrVariables(const QString &checkedLocale)
{
if (not m_vTranslator.isNull())
{
const bool result = QCoreApplication::removeTranslator(m_vTranslator);
if (result == false)
{
const QString message = QString("Can't remove translation variables for locale = %1")
.arg(checkedLocale);
QWARN(qUtf8Printable(message));
}
delete m_vTranslator;
}
}
//---------------------------------------------------------------------------------------------------------------------
void TST_AbstractRegExp::InitTrMs()
{
if (m_trMs != nullptr)
{
m_trMs->Retranslate();
}
else
{
m_trMs = new VTranslateVars();
}
}
//---------------------------------------------------------------------------------------------------------------------
void TST_AbstractRegExp::CallTestCheckNoEndLine()
{
QFETCH(QString, originalName);
const QString translated = m_trMs->VarToUser(originalName);
if (translated.endsWith(QLatin1String("\n")))
{
const QString message = QString("Translated string '%1' shouldn't contain new line character.")
.arg(translated);
QFAIL(qUtf8Printable(message));
}
}
//---------------------------------------------------------------------------------------------------------------------
void TST_AbstractRegExp::CallTestCheckRegExpNames()
{
QFETCH(QString, originalName);
static const QRegularExpression re(NameRegExp());
const QString translated = m_trMs->VarToUser(originalName);
if (not re.match(translated).hasMatch())
{
const QString message = QString("Original name:'%1', translated name:'%2'").arg(originalName).arg(translated);
QFAIL(qUtf8Printable(message));
}
}
//---------------------------------------------------------------------------------------------------------------------
void TST_AbstractRegExp::CallTestCheckIsNamesUnique()
{
QFETCH(QString, originalName);
QSet<QString> names;
const QString translated = m_trMs->VarToUser(originalName);
if (names.contains(translated))
{
const QString message = QString("Name is not unique. Original name:'%1', translated name:'%2'")
.arg(originalName).arg(translated);
QFAIL(qUtf8Printable(message));
}
names.insert(translated);
}
//---------------------------------------------------------------------------------------------------------------------
void TST_AbstractRegExp::CallTestCheckNoOriginalNamesInTranslation()
{
QFETCH(QString, originalName);
static const QStringList originalNames = AllNames();
static const QSet<QString> names = QSet<QString>::fromList(originalNames);
const QString translated = m_trMs->VarToUser(originalName);
if (names.contains(translated))
{
if (originalName != translated)
{
const QString message = QString("Translation repeat original name from other place. "
"Original name:'%1', translated name:'%2'")
.arg(originalName).arg(translated);
QFAIL(qUtf8Printable(message));
}
}
}

View File

@ -0,0 +1,67 @@
/************************************************************************
**
** @file
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 25 10, 2016
**
** @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) 2016 Valentina project
** <https://bitbucket.org/dismine/valentina> 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 <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#ifndef TST_ABSTRACTREGEXP_H
#define TST_ABSTRACTREGEXP_H
#include "../vmisc/abstracttest.h"
#include <QPointer>
class QTranslator;
class VTranslateVars;
class TST_AbstractRegExp : public AbstractTest
{
Q_OBJECT
public:
TST_AbstractRegExp(const QString &locale, QObject *parent = nullptr);
virtual ~TST_AbstractRegExp();
protected:
QString m_locale;
QPointer<QTranslator> m_vTranslator;
VTranslateVars *m_trMs;
virtual void PrepareData()=0;
virtual QStringList AllNames()=0;
int LoadVariables(const QString &checkedLocale);
void RemoveTrVariables(const QString &checkedLocale);
void InitTrMs();
void CallTestCheckNoEndLine();
void CallTestCheckRegExpNames();
void CallTestCheckIsNamesUnique();
void CallTestCheckNoOriginalNamesInTranslation();
private:
Q_DISABLE_COPY(TST_AbstractRegExp)
};
#endif // TST_ABSTRACTREGEXP_H

View File

@ -0,0 +1,248 @@
/************************************************************************
**
** @file
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 25 10, 2016
**
** @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) 2016 Valentina project
** <https://bitbucket.org/dismine/valentina> 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 <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include "tst_buitinregexp.h"
#include "../qmuparser/qmudef.h"
#include "../vmisc/logging.h"
#include "../vpatterndb/vtranslatevars.h"
#include "../ifc/ifcdef.h"
#include <QtTest>
#include <QTranslator>
//---------------------------------------------------------------------------------------------------------------------
TST_BuitInRegExp::TST_BuitInRegExp(const QString &locale, QObject *parent)
: TST_AbstractRegExp(locale, parent)
{
}
//---------------------------------------------------------------------------------------------------------------------
TST_BuitInRegExp::~TST_BuitInRegExp()
{
}
//---------------------------------------------------------------------------------------------------------------------
void TST_BuitInRegExp::initTestCase()
{
if (m_locale.isEmpty())
{
QFAIL("Empty locale code.");
}
const QStringList locales = SupportedLocales();
if (not locales.contains(m_locale))
{
QFAIL("Unsupported locale code.");
}
if (LoadVariables(m_locale) != NoError)
{
const QString message = QString("Couldn't load variables. Locale = %1").arg(m_locale);
QSKIP(qUtf8Printable(message));
}
InitTrMs();//Very important do this after loading QM files.
}
//---------------------------------------------------------------------------------------------------------------------
void TST_BuitInRegExp::TestCheckNoEndLine_data()
{
PrepareData();
}
//---------------------------------------------------------------------------------------------------------------------
void TST_BuitInRegExp::TestCheckNoEndLine()
{
CallTestCheckNoEndLine();
}
//---------------------------------------------------------------------------------------------------------------------
void TST_BuitInRegExp::TestCheckRegExpNames_data()
{
PrepareData();
}
//---------------------------------------------------------------------------------------------------------------------
void TST_BuitInRegExp::TestCheckRegExpNames()
{
CallTestCheckRegExpNames();
}
//---------------------------------------------------------------------------------------------------------------------
void TST_BuitInRegExp::TestCheckIsNamesUnique_data()
{
PrepareData();
}
//---------------------------------------------------------------------------------------------------------------------
void TST_BuitInRegExp::TestCheckIsNamesUnique()
{
CallTestCheckIsNamesUnique();
}
//---------------------------------------------------------------------------------------------------------------------
void TST_BuitInRegExp::TestCheckNoOriginalNamesInTranslation_data()
{
PrepareData();
}
//---------------------------------------------------------------------------------------------------------------------
void TST_BuitInRegExp::TestCheckNoOriginalNamesInTranslation()
{
CallTestCheckNoOriginalNamesInTranslation();
}
//---------------------------------------------------------------------------------------------------------------------
void TST_BuitInRegExp::TestCheckUnderlineExists_data()
{
QMap<QString, bool> data;
data.insert(line_, true);
data.insert(angleLine_, true);
data.insert(arc_, true);
data.insert(spl_, true);
data.insert(splPath, false);
data.insert(radiusArc_, true);
data.insert(angle1Arc_, true);
data.insert(angle2Arc_, true);
data.insert(angle1Spl_, true);
data.insert(angle2Spl_, true);
data.insert(angle1SplPath, false);
data.insert(angle2SplPath, false);
data.insert(seg_, true);
data.insert(currentLength, false);
data.insert(c1LengthSpl_, true);
data.insert(c2LengthSpl_, true);
data.insert(c1LengthSplPath, false);
data.insert(c2LengthSplPath, false);
//Catch case when new internal variable appears.
QCOMPARE(data.size(), builInVariables.size());
QTest::addColumn<QString>("name");
QTest::addColumn<bool>("exists");
auto i = data.constBegin();
while (i != data.constEnd())
{
const QString tag = QString("Locale: '%1'. Name '%2'").arg(m_locale).arg(i.key());
QTest::newRow(qUtf8Printable(tag)) << i.key() << i.value();
++i;
}
}
//---------------------------------------------------------------------------------------------------------------------
void TST_BuitInRegExp::TestCheckUnderlineExists()
{
QFETCH(QString, name);
QFETCH(bool, exists);
const QString translated = m_trMs->InternalVarToUser(name);
if ((translated.right(1) == QLatin1String("_")) != exists)
{
const QString message = QString("String '%1' doesn't contain underline. Original string is '%2'")
.arg(translated).arg(name);
QFAIL(qUtf8Printable(message));
}
}
//---------------------------------------------------------------------------------------------------------------------
void TST_BuitInRegExp::TestCheckInternalVaribleRegExp_data()
{
QTest::addColumn<QString>("var");
QTest::addColumn<QString>("originalName");
foreach(const QString &var, builInVariables)
{
const QString tag = QString("Locale: '%1'. Var '%2'").arg(m_locale).arg(var);
const QStringList originalNames = AllNames();
foreach(const QString &str, originalNames)
{
QTest::newRow(qUtf8Printable(tag)) << var << str;
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void TST_BuitInRegExp::TestCheckInternalVaribleRegExp()
{
QFETCH(QString, var);
QFETCH(QString, originalName);
static const QString regex = QStringLiteral("(.){1,}_(.){1,}$");
const QString sourceRegex = QLatin1String("^") + var + regex;
const QRegularExpression sourceRe(sourceRegex);
const QString translated = m_trMs->InternalVarToUser(var);
const QString translationRegex = QLatin1String("^") + translated + regex;
const QRegularExpression translationRe(translationRegex);
{
if (sourceRe.match(originalName).hasMatch() || translationRe.match(originalName).hasMatch())
{
const QString message = QString("Invalid original string '%1'").arg(originalName);
QFAIL(qUtf8Printable(message));
}
const QString translated = m_trMs->VarToUser(originalName);
if (sourceRe.match(translated).hasMatch() || translationRe.match(translated).hasMatch())
{
const QString message = QString("Invalid translation string '%1'").arg(translated);
QFAIL(qUtf8Printable(message));
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void TST_BuitInRegExp::cleanupTestCase()
{
RemoveTrVariables(m_locale);
}
//---------------------------------------------------------------------------------------------------------------------
void TST_BuitInRegExp::PrepareData()
{
static const QStringList originalNames = AllNames();
QTest::addColumn<QString>("originalName");
foreach(const QString &str, originalNames)
{
const QString tag = QString("Locale: '%1'. Name '%2'").arg(m_locale).arg(str);
QTest::newRow(qUtf8Printable(tag)) << str;
}
}
//---------------------------------------------------------------------------------------------------------------------
QStringList TST_BuitInRegExp::AllNames()
{
return builInFunctions + builInVariables;
}

View File

@ -0,0 +1,70 @@
/************************************************************************
**
** @file
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 25 10, 2016
**
** @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) 2016 Valentina project
** <https://bitbucket.org/dismine/valentina> 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 <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#ifndef TST_BUITINREGEXP_H
#define TST_BUITINREGEXP_H
#include "tst_abstractregexp.h"
#include <QPointer>
class QTranslator;
class VTranslateVars;
class TST_BuitInRegExp : public TST_AbstractRegExp
{
Q_OBJECT
public:
explicit TST_BuitInRegExp(const QString &locale, QObject *parent = nullptr);
virtual ~TST_BuitInRegExp();
protected:
virtual void PrepareData() Q_DECL_OVERRIDE;
virtual QStringList AllNames() Q_DECL_OVERRIDE;
private slots:
void initTestCase();
void TestCheckNoEndLine_data();
void TestCheckNoEndLine();
void TestCheckRegExpNames_data();
void TestCheckRegExpNames();
void TestCheckIsNamesUnique_data();
void TestCheckIsNamesUnique();
void TestCheckNoOriginalNamesInTranslation_data();
void TestCheckNoOriginalNamesInTranslation();
void TestCheckUnderlineExists_data();
void TestCheckUnderlineExists();
void TestCheckInternalVaribleRegExp_data();
void TestCheckInternalVaribleRegExp();
void cleanupTestCase();
private:
Q_DISABLE_COPY(TST_BuitInRegExp)
};
#endif // TST_BUITINREGEXP_H

View File

@ -40,22 +40,17 @@ const quint32 TST_MeasurementRegExp::systemCounts = 56; // count of pattern maki
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
TST_MeasurementRegExp::TST_MeasurementRegExp(quint32 systemCode, const QString &locale, QObject *parent) TST_MeasurementRegExp::TST_MeasurementRegExp(quint32 systemCode, const QString &locale, QObject *parent)
: AbstractTest(parent), : TST_AbstractRegExp(locale, parent),
m_systemCode(systemCode), m_systemCode(systemCode),
m_system(), m_system(),
m_locale(locale), m_pmsTranslator(nullptr)
pmsTranslator(nullptr),
vTranslator(nullptr),
trMs(nullptr)
{ {
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
TST_MeasurementRegExp::~TST_MeasurementRegExp() TST_MeasurementRegExp::~TST_MeasurementRegExp()
{ {
delete pmsTranslator; delete m_pmsTranslator;
delete vTranslator;
delete trMs;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -104,193 +99,49 @@ void TST_MeasurementRegExp::initTestCase()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void TST_MeasurementRegExp::TestCheckNoEndLine_data() void TST_MeasurementRegExp::TestCheckNoEndLine_data()
{ {
PrepareMeasurementData(); PrepareData();
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void TST_MeasurementRegExp::TestCheckNoEndLine() void TST_MeasurementRegExp::TestCheckNoEndLine()
{ {
QFETCH(QString, originalName); CallTestCheckNoEndLine();
const QString translated = trMs->VarToUser(originalName);
if (translated.endsWith(QLatin1String("\n")))
{
const QString message = QString("Translated string '%1' shouldn't contain new line character.")
.arg(translated);
QFAIL(qUtf8Printable(message));
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void TST_MeasurementRegExp::TestCheckRegExpNames_data() void TST_MeasurementRegExp::TestCheckRegExpNames_data()
{ {
PrepareMeasurementData(); PrepareData();
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void TST_MeasurementRegExp::TestCheckRegExpNames() void TST_MeasurementRegExp::TestCheckRegExpNames()
{ {
QFETCH(QString, originalName); CallTestCheckRegExpNames();
static const QRegularExpression re(NameRegExp());
const QString translated = trMs->VarToUser(originalName);
if (not re.match(translated).hasMatch())
{
const QString message = QString("Original name:'%1', translated name:'%2'").arg(originalName).arg(translated);
QFAIL(qUtf8Printable(message));
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void TST_MeasurementRegExp::TestCheckIsNamesUnique_data() void TST_MeasurementRegExp::TestCheckIsNamesUnique_data()
{ {
PrepareMeasurementData(); PrepareData();
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void TST_MeasurementRegExp::TestCheckIsNamesUnique() void TST_MeasurementRegExp::TestCheckIsNamesUnique()
{ {
QFETCH(QString, originalName); CallTestCheckIsNamesUnique();
QSet<QString> names;
const QString translated = trMs->VarToUser(originalName);
if (names.contains(translated))
{
const QString message = QString("Name is not unique. Original name:'%1', translated name:'%2'")
.arg(originalName).arg(translated);
QFAIL(qUtf8Printable(message));
}
names.insert(translated);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void TST_MeasurementRegExp::TestCheckNoOriginalNamesInTranslation_data() void TST_MeasurementRegExp::TestCheckNoOriginalNamesInTranslation_data()
{ {
PrepareMeasurementData(); PrepareData();
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void TST_MeasurementRegExp::TestCheckNoOriginalNamesInTranslation() void TST_MeasurementRegExp::TestCheckNoOriginalNamesInTranslation()
{ {
QFETCH(QString, originalName); CallTestCheckNoOriginalNamesInTranslation();
static const QStringList originalNames = AllGroupNames() + builInFunctions + builInVariables;
static const QSet<QString> names = QSet<QString>::fromList(originalNames);
const QString translated = trMs->VarToUser(originalName);
if (names.contains(translated))
{
if (originalName != translated)
{
const QString message = QString("Translation repeat original name from other place. "
"Original name:'%1', translated name:'%2'")
.arg(originalName).arg(translated);
QFAIL(qUtf8Printable(message));
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void TST_MeasurementRegExp::TestCheckUnderlineExists_data()
{
QMap<QString, bool> data;
data.insert(line_, true);
data.insert(angleLine_, true);
data.insert(arc_, true);
data.insert(spl_, true);
data.insert(splPath, false);
data.insert(radiusArc_, true);
data.insert(angle1Arc_, true);
data.insert(angle2Arc_, true);
data.insert(angle1Spl_, true);
data.insert(angle2Spl_, true);
data.insert(angle1SplPath, false);
data.insert(angle2SplPath, false);
data.insert(seg_, true);
data.insert(currentLength, false);
data.insert(c1LengthSpl_, true);
data.insert(c2LengthSpl_, true);
data.insert(c1LengthSplPath, false);
data.insert(c2LengthSplPath, false);
//Catch case when new internal variable appears.
QCOMPARE(data.size(), builInVariables.size());
QTest::addColumn<QString>("name");
QTest::addColumn<bool>("exists");
auto i = data.constBegin();
while (i != data.constEnd())
{
const QString tag = QString("System: '%1', locale: '%2'. Name '%3'").arg(m_system).arg(m_locale).arg(i.key());
QTest::newRow(qUtf8Printable(tag)) << i.key() << i.value();
++i;
}
}
//---------------------------------------------------------------------------------------------------------------------
void TST_MeasurementRegExp::TestCheckUnderlineExists()
{
QFETCH(QString, name);
QFETCH(bool, exists);
const QString translated = trMs->InternalVarToUser(name);
if ((translated.right(1) == QLatin1String("_")) != exists)
{
const QString message = QString("String '%1' doesn't contain underline. Original string is '%2'")
.arg(translated).arg(name);
QFAIL(qUtf8Printable(message));
}
}
//---------------------------------------------------------------------------------------------------------------------
void TST_MeasurementRegExp::TestCheckInternalVaribleRegExp_data()
{
QTest::addColumn<QString>("var");
QTest::addColumn<QString>("originalName");
foreach(const QString &var, builInVariables)
{
const QString tag = QString("System: '%1', locale: '%2'. Var '%3'").arg(m_system).arg(m_locale).arg(var);
const QStringList originalNames = AllGroupNames() + builInFunctions + builInVariables;
foreach(const QString &str, originalNames)
{
QTest::newRow(qUtf8Printable(tag)) << var << str;
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void TST_MeasurementRegExp::TestCheckInternalVaribleRegExp()
{
QFETCH(QString, var);
QFETCH(QString, originalName);
static const QString regex = QStringLiteral("(.){1,}_(.){1,}$");
const QString sourceRegex = QLatin1String("^") + var + regex;
const QRegularExpression sourceRe(sourceRegex);
const QString translated = trMs->InternalVarToUser(var);
const QString translationRegex = QLatin1String("^") + translated + regex;
const QRegularExpression translationRe(translationRegex);
{
if (sourceRe.match(originalName).hasMatch() || translationRe.match(originalName).hasMatch())
{
const QString message = QString("Invalid original string '%1'").arg(originalName);
QFAIL(qUtf8Printable(message));
}
const QString translated = trMs->VarToUser(originalName);
if (sourceRe.match(translated).hasMatch() || translationRe.match(translated).hasMatch())
{
const QString message = QString("Invalid translation string '%1'").arg(translated);
QFAIL(qUtf8Printable(message));
}
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -312,9 +163,9 @@ void TST_MeasurementRegExp::TestCombinations(int systemCounts, const QStringList
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void TST_MeasurementRegExp::PrepareMeasurementData() void TST_MeasurementRegExp::PrepareData()
{ {
static const QStringList originalNames = AllGroupNames() + builInFunctions + builInVariables; static const QStringList originalNames = AllNames();
QTest::addColumn<QString>("originalName"); QTest::addColumn<QString>("originalName");
@ -325,6 +176,12 @@ void TST_MeasurementRegExp::PrepareMeasurementData()
} }
} }
//---------------------------------------------------------------------------------------------------------------------
QStringList TST_MeasurementRegExp::AllNames()
{
return AllGroupNames();
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
int TST_MeasurementRegExp::LoadMeasurements(const QString &checkedSystem, const QString &checkedLocale) int TST_MeasurementRegExp::LoadMeasurements(const QString &checkedSystem, const QString &checkedLocale)
{ {
@ -343,10 +200,10 @@ int TST_MeasurementRegExp::LoadMeasurements(const QString &checkedSystem, const
return ErrorSize; return ErrorSize;
} }
delete pmsTranslator; delete m_pmsTranslator;
pmsTranslator = new QTranslator(this); m_pmsTranslator = new QTranslator(this);
if (not pmsTranslator->load(file, path)) if (not m_pmsTranslator->load(file, path))
{ {
const QString message = QString("Can't load translation for system = %1 and locale = %2. \nFull path: %3/%4") const QString message = QString("Can't load translation for system = %1 and locale = %2. \nFull path: %3/%4")
.arg(checkedSystem) .arg(checkedSystem)
@ -355,12 +212,12 @@ int TST_MeasurementRegExp::LoadMeasurements(const QString &checkedSystem, const
.arg(file); .arg(file);
QWARN(qUtf8Printable(message)); QWARN(qUtf8Printable(message));
delete pmsTranslator; delete m_pmsTranslator;
return ErrorLoad; return ErrorLoad;
} }
if (not QCoreApplication::installTranslator(pmsTranslator)) if (not QCoreApplication::installTranslator(m_pmsTranslator))
{ {
const QString message = QString("Can't install translation for system = %1 and locale = %2. \nFull path: %3/%4") const QString message = QString("Can't install translation for system = %1 and locale = %2. \nFull path: %3/%4")
.arg(checkedSystem) .arg(checkedSystem)
@ -369,55 +226,7 @@ int TST_MeasurementRegExp::LoadMeasurements(const QString &checkedSystem, const
.arg(file); .arg(file);
QWARN(qUtf8Printable(message)); QWARN(qUtf8Printable(message));
delete pmsTranslator; delete m_pmsTranslator;
return ErrorInstall;
}
return NoError;
}
//---------------------------------------------------------------------------------------------------------------------
int TST_MeasurementRegExp::LoadVariables(const QString &checkedLocale)
{
const QString path = TranslationsPath();
const QString file = QString("valentina_%1.qm").arg(checkedLocale);
if (QFileInfo(path+QLatin1String("/")+file).size() <= 34)
{
const QString message = QString("Translation variables for locale = %1 is empty. \nFull path: %2/%3")
.arg(checkedLocale)
.arg(path)
.arg(file);
QWARN(qUtf8Printable(message));
return ErrorSize;
}
vTranslator = new QTranslator(this);
if (not vTranslator->load(file, path))
{
const QString message = QString("Can't load translation variables for locale = %1. \nFull path: %2/%3")
.arg(checkedLocale)
.arg(path)
.arg(file);
QWARN(qUtf8Printable(message));
delete vTranslator;
return ErrorLoad;
}
if (not QCoreApplication::installTranslator(vTranslator))
{
const QString message = QString("Can't install translation variables for locale = %1. \nFull path: %2/%3")
.arg(checkedLocale)
.arg(path)
.arg(file);
QWARN(qUtf8Printable(message));
delete vTranslator;
return ErrorInstall; return ErrorInstall;
} }
@ -428,9 +237,9 @@ int TST_MeasurementRegExp::LoadVariables(const QString &checkedLocale)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void TST_MeasurementRegExp::RemoveTrMeasurements(const QString &checkedSystem, const QString &checkedLocale) void TST_MeasurementRegExp::RemoveTrMeasurements(const QString &checkedSystem, const QString &checkedLocale)
{ {
if (not pmsTranslator.isNull()) if (not m_pmsTranslator.isNull())
{ {
const bool result = QCoreApplication::removeTranslator(pmsTranslator); const bool result = QCoreApplication::removeTranslator(m_pmsTranslator);
if (result == false) if (result == false)
{ {
@ -439,36 +248,6 @@ void TST_MeasurementRegExp::RemoveTrMeasurements(const QString &checkedSystem, c
.arg(checkedLocale); .arg(checkedLocale);
QWARN(qUtf8Printable(message)); QWARN(qUtf8Printable(message));
} }
delete pmsTranslator; delete m_pmsTranslator;
}
}
//---------------------------------------------------------------------------------------------------------------------
void TST_MeasurementRegExp::RemoveTrVariables(const QString &checkedLocale)
{
if (not vTranslator.isNull())
{
const bool result = QCoreApplication::removeTranslator(vTranslator);
if (result == false)
{
const QString message = QString("Can't remove translation variables for locale = %1")
.arg(checkedLocale);
QWARN(qUtf8Printable(message));
}
delete vTranslator;
}
}
//---------------------------------------------------------------------------------------------------------------------
void TST_MeasurementRegExp::InitTrMs()
{
if (trMs != nullptr)
{
trMs->Retranslate();
}
else
{
trMs = new VTranslateVars();
} }
} }

View File

@ -29,22 +29,26 @@
#ifndef TST_MEASUREMENTREGEXP_H #ifndef TST_MEASUREMENTREGEXP_H
#define TST_MEASUREMENTREGEXP_H #define TST_MEASUREMENTREGEXP_H
#include "../vmisc/abstracttest.h" #include "tst_abstractregexp.h"
#include <QPointer> #include <QPointer>
class QTranslator; class QTranslator;
class VTranslateVars; class VTranslateVars;
class TST_MeasurementRegExp : public AbstractTest class TST_MeasurementRegExp : public TST_AbstractRegExp
{ {
Q_OBJECT Q_OBJECT
public: public:
TST_MeasurementRegExp(quint32 systemCode, const QString &locale, QObject *parent = nullptr); TST_MeasurementRegExp(quint32 systemCode, const QString &locale, QObject *parent = nullptr);
virtual ~TST_MeasurementRegExp() Q_DECL_OVERRIDE; virtual ~TST_MeasurementRegExp();
static const quint32 systemCounts; static const quint32 systemCounts;
protected:
virtual void PrepareData() Q_DECL_OVERRIDE;
virtual QStringList AllNames() Q_DECL_OVERRIDE;
private slots: private slots:
void initTestCase(); void initTestCase();
void TestCheckNoEndLine_data(); void TestCheckNoEndLine_data();
@ -55,32 +59,19 @@ private slots:
void TestCheckIsNamesUnique(); void TestCheckIsNamesUnique();
void TestCheckNoOriginalNamesInTranslation_data(); void TestCheckNoOriginalNamesInTranslation_data();
void TestCheckNoOriginalNamesInTranslation(); void TestCheckNoOriginalNamesInTranslation();
void TestCheckUnderlineExists_data();
void TestCheckUnderlineExists();
void TestCheckInternalVaribleRegExp_data();
void TestCheckInternalVaribleRegExp();
void cleanupTestCase(); void cleanupTestCase();
private: private:
Q_DISABLE_COPY(TST_MeasurementRegExp) Q_DISABLE_COPY(TST_MeasurementRegExp)
quint32 m_systemCode; quint32 m_systemCode;
QString m_system; QString m_system;
QString m_locale; QPointer<QTranslator> m_pmsTranslator;
QPointer<QTranslator> pmsTranslator;
QPointer<QTranslator> vTranslator;
VTranslateVars *trMs;
void TestCombinations(int systemCounts, const QStringList &locales) const; void TestCombinations(int systemCounts, const QStringList &locales) const;
void PrepareMeasurementData();
int LoadMeasurements(const QString &checkedSystem, const QString &checkedLocale); int LoadMeasurements(const QString &checkedSystem, const QString &checkedLocale);
int LoadVariables(const QString &checkedLocale);
void RemoveTrMeasurements(const QString &checkedSystem, const QString &checkedLocale); void RemoveTrMeasurements(const QString &checkedSystem, const QString &checkedLocale);
void RemoveTrVariables(const QString &checkedLocale);
void InitTrMs();
}; };
#endif // TST_MEASUREMENTREGEXP_H #endif // TST_MEASUREMENTREGEXP_H