diff --git a/src/test/TranslationsTest/TranslationsTest.pro b/src/test/TranslationsTest/TranslationsTest.pro index 1176a5293..90763a514 100644 --- a/src/test/TranslationsTest/TranslationsTest.pro +++ b/src/test/TranslationsTest/TranslationsTest.pro @@ -39,7 +39,9 @@ SOURCES += \ qttestmainlambda.cpp \ tst_measurementregexp.cpp \ tst_qmuparsererrormsg.cpp \ - tst_tstranslation.cpp + tst_tstranslation.cpp \ + tst_buitinregexp.cpp \ + tst_abstractregexp.cpp win32-msvc*:SOURCES += stable.cpp @@ -47,7 +49,9 @@ HEADERS += \ stable.h \ tst_measurementregexp.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. $$enable_ccache() diff --git a/src/test/TranslationsTest/qttestmainlambda.cpp b/src/test/TranslationsTest/qttestmainlambda.cpp index a8db16c5e..1215ba911 100644 --- a/src/test/TranslationsTest/qttestmainlambda.cpp +++ b/src/test/TranslationsTest/qttestmainlambda.cpp @@ -29,6 +29,7 @@ #include #include "tst_measurementregexp.h" +#include "tst_buitinregexp.h" #include "tst_qmuparsererrormsg.h" #include "tst_tstranslation.h" @@ -47,23 +48,24 @@ int main(int argc, char** argv) 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) { - 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; } diff --git a/src/test/TranslationsTest/tst_abstractregexp.cpp b/src/test/TranslationsTest/tst_abstractregexp.cpp new file mode 100644 index 000000000..f3d807ae8 --- /dev/null +++ b/src/test/TranslationsTest/tst_abstractregexp.cpp @@ -0,0 +1,198 @@ +/************************************************************************ + ** + ** @file + ** @author Roman Telezhynskyi + ** @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 + ** 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_abstractregexp.h" +#include "../qmuparser/qmudef.h" + +#include "../vmisc/logging.h" +#include "../vpatterndb/vtranslatevars.h" +#include "../ifc/ifcdef.h" + +#include +#include + +//--------------------------------------------------------------------------------------------------------------------- +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 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 names = QSet::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)); + } + } +} + diff --git a/src/test/TranslationsTest/tst_abstractregexp.h b/src/test/TranslationsTest/tst_abstractregexp.h new file mode 100644 index 000000000..387f16b66 --- /dev/null +++ b/src/test/TranslationsTest/tst_abstractregexp.h @@ -0,0 +1,67 @@ +/************************************************************************ + ** + ** @file + ** @author Roman Telezhynskyi + ** @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 + ** 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_ABSTRACTREGEXP_H +#define TST_ABSTRACTREGEXP_H + +#include "../vmisc/abstracttest.h" + +#include + +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 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 diff --git a/src/test/TranslationsTest/tst_buitinregexp.cpp b/src/test/TranslationsTest/tst_buitinregexp.cpp new file mode 100644 index 000000000..60a2b30e3 --- /dev/null +++ b/src/test/TranslationsTest/tst_buitinregexp.cpp @@ -0,0 +1,248 @@ +/************************************************************************ + ** + ** @file + ** @author Roman Telezhynskyi + ** @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 + ** 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_buitinregexp.h" +#include "../qmuparser/qmudef.h" + +#include "../vmisc/logging.h" +#include "../vpatterndb/vtranslatevars.h" +#include "../ifc/ifcdef.h" + +#include +#include + +//--------------------------------------------------------------------------------------------------------------------- +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 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("name"); + QTest::addColumn("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("var"); + QTest::addColumn("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("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; +} diff --git a/src/test/TranslationsTest/tst_buitinregexp.h b/src/test/TranslationsTest/tst_buitinregexp.h new file mode 100644 index 000000000..4e3c52a63 --- /dev/null +++ b/src/test/TranslationsTest/tst_buitinregexp.h @@ -0,0 +1,70 @@ +/************************************************************************ + ** + ** @file + ** @author Roman Telezhynskyi + ** @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 + ** 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_BUITINREGEXP_H +#define TST_BUITINREGEXP_H + +#include "tst_abstractregexp.h" + +#include + +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 diff --git a/src/test/TranslationsTest/tst_measurementregexp.cpp b/src/test/TranslationsTest/tst_measurementregexp.cpp index 8cb82c789..dc6bc5eb5 100644 --- a/src/test/TranslationsTest/tst_measurementregexp.cpp +++ b/src/test/TranslationsTest/tst_measurementregexp.cpp @@ -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) - : AbstractTest(parent), + : TST_AbstractRegExp(locale, parent), m_systemCode(systemCode), m_system(), - m_locale(locale), - pmsTranslator(nullptr), - vTranslator(nullptr), - trMs(nullptr) + m_pmsTranslator(nullptr) { } //--------------------------------------------------------------------------------------------------------------------- TST_MeasurementRegExp::~TST_MeasurementRegExp() { - delete pmsTranslator; - delete vTranslator; - delete trMs; + delete m_pmsTranslator; } //--------------------------------------------------------------------------------------------------------------------- @@ -104,193 +99,49 @@ void TST_MeasurementRegExp::initTestCase() //--------------------------------------------------------------------------------------------------------------------- void TST_MeasurementRegExp::TestCheckNoEndLine_data() { - PrepareMeasurementData(); + PrepareData(); } //--------------------------------------------------------------------------------------------------------------------- void TST_MeasurementRegExp::TestCheckNoEndLine() { - QFETCH(QString, originalName); - - 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)); - } + CallTestCheckNoEndLine(); } //--------------------------------------------------------------------------------------------------------------------- void TST_MeasurementRegExp::TestCheckRegExpNames_data() { - PrepareMeasurementData(); + PrepareData(); } //--------------------------------------------------------------------------------------------------------------------- void TST_MeasurementRegExp::TestCheckRegExpNames() { - QFETCH(QString, originalName); - - 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)); - } + CallTestCheckRegExpNames(); } //--------------------------------------------------------------------------------------------------------------------- void TST_MeasurementRegExp::TestCheckIsNamesUnique_data() { - PrepareMeasurementData(); + PrepareData(); } //--------------------------------------------------------------------------------------------------------------------- void TST_MeasurementRegExp::TestCheckIsNamesUnique() { - QFETCH(QString, originalName); - - QSet 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); + CallTestCheckIsNamesUnique(); } //--------------------------------------------------------------------------------------------------------------------- void TST_MeasurementRegExp::TestCheckNoOriginalNamesInTranslation_data() { - PrepareMeasurementData(); + PrepareData(); } //--------------------------------------------------------------------------------------------------------------------- void TST_MeasurementRegExp::TestCheckNoOriginalNamesInTranslation() { - QFETCH(QString, originalName); - - static const QStringList originalNames = AllGroupNames() + builInFunctions + builInVariables; - static const QSet names = QSet::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 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("name"); - QTest::addColumn("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("var"); - QTest::addColumn("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)); - } - } + CallTestCheckNoOriginalNamesInTranslation(); } //--------------------------------------------------------------------------------------------------------------------- @@ -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("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) { @@ -343,10 +200,10 @@ int TST_MeasurementRegExp::LoadMeasurements(const QString &checkedSystem, const return ErrorSize; } - delete pmsTranslator; - pmsTranslator = new QTranslator(this); + delete m_pmsTranslator; + 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") .arg(checkedSystem) @@ -355,12 +212,12 @@ int TST_MeasurementRegExp::LoadMeasurements(const QString &checkedSystem, const .arg(file); QWARN(qUtf8Printable(message)); - delete pmsTranslator; + delete m_pmsTranslator; 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") .arg(checkedSystem) @@ -369,55 +226,7 @@ int TST_MeasurementRegExp::LoadMeasurements(const QString &checkedSystem, const .arg(file); QWARN(qUtf8Printable(message)); - delete 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; + delete m_pmsTranslator; return ErrorInstall; } @@ -428,9 +237,9 @@ int TST_MeasurementRegExp::LoadVariables(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) { @@ -439,36 +248,6 @@ void TST_MeasurementRegExp::RemoveTrMeasurements(const QString &checkedSystem, c .arg(checkedLocale); QWARN(qUtf8Printable(message)); } - delete 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(); + delete m_pmsTranslator; } } diff --git a/src/test/TranslationsTest/tst_measurementregexp.h b/src/test/TranslationsTest/tst_measurementregexp.h index 6a65fa238..f3aafae4e 100644 --- a/src/test/TranslationsTest/tst_measurementregexp.h +++ b/src/test/TranslationsTest/tst_measurementregexp.h @@ -29,22 +29,26 @@ #ifndef TST_MEASUREMENTREGEXP_H #define TST_MEASUREMENTREGEXP_H -#include "../vmisc/abstracttest.h" +#include "tst_abstractregexp.h" #include class QTranslator; class VTranslateVars; -class TST_MeasurementRegExp : public AbstractTest +class TST_MeasurementRegExp : public TST_AbstractRegExp { Q_OBJECT public: TST_MeasurementRegExp(quint32 systemCode, const QString &locale, QObject *parent = nullptr); - virtual ~TST_MeasurementRegExp() Q_DECL_OVERRIDE; + virtual ~TST_MeasurementRegExp(); static const quint32 systemCounts; +protected: + virtual void PrepareData() Q_DECL_OVERRIDE; + virtual QStringList AllNames() Q_DECL_OVERRIDE; + private slots: void initTestCase(); void TestCheckNoEndLine_data(); @@ -55,32 +59,19 @@ private slots: void TestCheckIsNamesUnique(); void TestCheckNoOriginalNamesInTranslation_data(); void TestCheckNoOriginalNamesInTranslation(); - void TestCheckUnderlineExists_data(); - void TestCheckUnderlineExists(); - void TestCheckInternalVaribleRegExp_data(); - void TestCheckInternalVaribleRegExp(); void cleanupTestCase(); private: Q_DISABLE_COPY(TST_MeasurementRegExp) - quint32 m_systemCode; - QString m_system; - QString m_locale; - QPointer pmsTranslator; - QPointer vTranslator; - VTranslateVars *trMs; + quint32 m_systemCode; + QString m_system; + QPointer m_pmsTranslator; void TestCombinations(int systemCounts, const QStringList &locales) const; - void PrepareMeasurementData(); int LoadMeasurements(const QString &checkedSystem, const QString &checkedLocale); - int LoadVariables(const QString &checkedLocale); - void RemoveTrMeasurements(const QString &checkedSystem, const QString &checkedLocale); - void RemoveTrVariables(const QString &checkedLocale); - - void InitTrMs(); }; #endif // TST_MEASUREMENTREGEXP_H