1 /************************************************************************
2  **
3  **  @file
4  **  @author Roman Telezhynskyi <dismine(at)gmail.com>
5  **  @date   25 10, 2016
6  **
7  **  @brief
8  **  @copyright
9  **  This source code is part of the Valentina project, a pattern making
10  **  program, whose allow create and modeling patterns of clothing.
11  **  Copyright (C) 2016 Valentina project
12  **  <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
13  **
14  **  Valentina is free software: you can redistribute it and/or modify
15  **  it under the terms of the GNU General Public License as published by
16  **  the Free Software Foundation, either version 3 of the License, or
17  **  (at your option) any later version.
18  **
19  **  Valentina is distributed in the hope that it will be useful,
20  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  **  GNU General Public License for more details.
23  **
24  **  You should have received a copy of the GNU General Public License
25  **  along with Valentina.  If not, see <http://www.gnu.org/licenses/>.
26  **
27  *************************************************************************/
28 
29 #include "tst_buitinregexp.h"
30 #include "../qmuparser/qmudef.h"
31 #include "../vpatterndb/vtranslatevars.h"
32 #include "../ifc/ifcdef.h"
33 
34 #include <QtTest>
35 #include <QTranslator>
36 
37 //---------------------------------------------------------------------------------------------------------------------
TST_BuitInRegExp(const QString & locale,QObject * parent)38 TST_BuitInRegExp::TST_BuitInRegExp(const QString &locale, QObject *parent)
39     : TST_AbstractRegExp(locale, parent)
40 {
41 }
42 
43 //---------------------------------------------------------------------------------------------------------------------
initTestCase()44 void TST_BuitInRegExp::initTestCase()
45 {
46     if (m_locale.isEmpty())
47     {
48         QFAIL("Empty locale code.");
49     }
50 
51     const QStringList locales = SupportedLocales();
52 
53     if (not locales.contains(m_locale))
54     {
55         QFAIL("Unsupported locale code.");
56     }
57 
58     if (LoadVariables(m_locale) != NoError)
59     {
60         const QString message = QString("Couldn't load variables. Locale = %1").arg(m_locale);
61         QSKIP(qUtf8Printable(message));
62     }
63 
64     QLocale::setDefault(QLocale(m_locale));
65 
66     InitTrMs();//Very important do this after loading QM files.
67 }
68 
69 //---------------------------------------------------------------------------------------------------------------------
TestCheckNoEndLine_data()70 void TST_BuitInRegExp::TestCheckNoEndLine_data()
71 {
72     PrepareData();
73 }
74 
75 //---------------------------------------------------------------------------------------------------------------------
TestCheckNoEndLine()76 void TST_BuitInRegExp::TestCheckNoEndLine()
77 {
78     CallTestCheckNoEndLine();
79 }
80 
81 //---------------------------------------------------------------------------------------------------------------------
TestCheckRegExpNames_data()82 void TST_BuitInRegExp::TestCheckRegExpNames_data()
83 {
84     PrepareData();
85 }
86 
87 //---------------------------------------------------------------------------------------------------------------------
TestCheckRegExpNames()88 void TST_BuitInRegExp::TestCheckRegExpNames()
89 {
90     CallTestCheckRegExpNames();
91 }
92 
93 //---------------------------------------------------------------------------------------------------------------------
TestCheckIsNamesUnique_data()94 void TST_BuitInRegExp::TestCheckIsNamesUnique_data()
95 {
96     PrepareData();
97 }
98 
99 //---------------------------------------------------------------------------------------------------------------------
TestCheckIsNamesUnique()100 void TST_BuitInRegExp::TestCheckIsNamesUnique()
101 {
102     CallTestCheckIsNamesUnique();
103 }
104 
105 //---------------------------------------------------------------------------------------------------------------------
TestCheckNoOriginalNamesInTranslation_data()106 void TST_BuitInRegExp::TestCheckNoOriginalNamesInTranslation_data()
107 {
108     PrepareData();
109 }
110 
111 //---------------------------------------------------------------------------------------------------------------------
TestCheckNoOriginalNamesInTranslation()112 void TST_BuitInRegExp::TestCheckNoOriginalNamesInTranslation()
113 {
114     CallTestCheckNoOriginalNamesInTranslation();
115 }
116 
117 //---------------------------------------------------------------------------------------------------------------------
TestCheckUnderlineExists_data()118 void TST_BuitInRegExp::TestCheckUnderlineExists_data()
119 {
120     QMap<QString, bool> data;
121 
122     data.insert(measurement_, true);
123     data.insert(increment_, true);
124     data.insert(line_, true);
125     data.insert(angleLine_, true);
126     data.insert(arc_, true);
127     data.insert(elarc_, true);
128     data.insert(spl_, true);
129     data.insert(splPath, false);
130     data.insert(radiusArc_, true);
131     data.insert(radius1ElArc_, true);
132     data.insert(radius2ElArc_, true);
133     data.insert(angle1Arc_, true);
134     data.insert(angle2Arc_, true);
135     data.insert(angle1ElArc_, true);
136     data.insert(angle2ElArc_, true);
137     data.insert(angle1Spl_, true);
138     data.insert(angle2Spl_, true);
139     data.insert(angle1SplPath, false);
140     data.insert(angle2SplPath, false);
141     data.insert(seg_, true);
142     data.insert(currentLength, false);
143     data.insert(currentSeamAllowance, false);
144     data.insert(c1LengthSpl_, true);
145     data.insert(c2LengthSpl_, true);
146     data.insert(c1LengthSplPath, false);
147     data.insert(c2LengthSplPath, false);
148     data.insert(rotationElArc_, true);
149 
150     //Catch case when new internal variable appears.
151     QCOMPARE(data.size(), builInVariables.size());
152 
153     QTest::addColumn<QString>("name");
154     QTest::addColumn<bool>("exists");
155 
156     auto i = data.constBegin();
157     while (i != data.constEnd())
158     {
159         const QString tag = QString("Locale: '%1'. Name '%2'").arg(m_locale, i.key());
160         QTest::newRow(qUtf8Printable(tag)) << i.key() << i.value();
161         ++i;
162     }
163 }
164 
165 //---------------------------------------------------------------------------------------------------------------------
TestCheckUnderlineExists()166 void TST_BuitInRegExp::TestCheckUnderlineExists()
167 {
168     QFETCH(QString, name);
169     QFETCH(bool, exists);
170 
171     const QString translated = m_trMs->InternalVarToUser(name);
172     if ((translated.right(1) == QLatin1String("_")) != exists)
173     {
174         const QString message = QString("String '%1' doesn't contain underline. Original string is '%2'")
175                 .arg(translated, name);
176         QFAIL(qUtf8Printable(message));
177     }
178 }
179 
180 //---------------------------------------------------------------------------------------------------------------------
TestCheckInternalVaribleRegExp_data()181 void TST_BuitInRegExp::TestCheckInternalVaribleRegExp_data()
182 {
183     QTest::addColumn<QString>("var");
184     QTest::addColumn<QString>("originalName");
185 
186     for (auto &var : qAsConst(builInVariables))
187     {
188         const QString tag = QString("Locale: '%1'. Var '%2'").arg(m_locale, var);
189         const QStringList originalNames = AllNames();
190         for (auto &str : originalNames)
191         {
192             QTest::newRow(qUtf8Printable(tag)) << var << str;
193         }
194     }
195 }
196 
197 //---------------------------------------------------------------------------------------------------------------------
TestCheckInternalVaribleRegExp()198 void TST_BuitInRegExp::TestCheckInternalVaribleRegExp()
199 {
200     QFETCH(QString, var);
201     QFETCH(QString, originalName);
202 
203     static const QString regex = QStringLiteral("(.){1,}_(.){1,}$");
204 
205     const QString sourceRegex = QLatin1String("^") + var + regex;
206     const QRegularExpression sourceRe(sourceRegex);
207 
208     const QString translated = m_trMs->InternalVarToUser(var);
209     const QString translationRegex = QLatin1String("^") + translated + regex;
210     const QRegularExpression translationRe(translationRegex);
211 
212     {
213         if (sourceRe.match(originalName).hasMatch() || translationRe.match(originalName).hasMatch())
214         {
215             const QString message = QString("Invalid original string '%1'").arg(originalName);
216             QFAIL(qUtf8Printable(message));
217         }
218 
219         const QString translated = m_trMs->VarToUser(originalName);
220         if (sourceRe.match(translated).hasMatch() || translationRe.match(translated).hasMatch())
221         {
222             const QString message = QString("Invalid translation string '%1'").arg(translated);
223             QFAIL(qUtf8Printable(message));
224         }
225     }
226 }
227 
228 //---------------------------------------------------------------------------------------------------------------------
TestForValidChars_data()229 void TST_BuitInRegExp::TestForValidChars_data()
230 {
231     PrepareData();
232 }
233 
234 //---------------------------------------------------------------------------------------------------------------------
TestForValidChars()235 void TST_BuitInRegExp::TestForValidChars()
236 {
237     CallTestForValidCharacters();
238 }
239 
240 //---------------------------------------------------------------------------------------------------------------------
cleanupTestCase()241 void TST_BuitInRegExp::cleanupTestCase()
242 {
243     RemoveTrVariables(m_locale);
244 }
245 
246 //---------------------------------------------------------------------------------------------------------------------
PrepareData()247 void TST_BuitInRegExp::PrepareData()
248 {
249     static const QStringList originalNames = AllNames();
250 
251     QTest::addColumn<QString>("originalName");
252 
253     for (auto &str : originalNames)
254     {
255         const QString tag = QString("Locale: '%1'. Name '%2'").arg(m_locale, str);
256         QTest::newRow(qUtf8Printable(tag)) << str;
257     }
258 }
259 
260 //---------------------------------------------------------------------------------------------------------------------
AllNames()261 QStringList TST_BuitInRegExp::AllNames()
262 {
263     return builInFunctions + builInVariables;
264 }
265