/
testqgsdoublevalidator.cpp
232 lines (196 loc) · 10.2 KB
/
testqgsdoublevalidator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/***************************************************************************
testqgsdoublespinbox.cpp
--------------------------------------
Date : June 2020
Copyright : (C) 2020 Sebastien Peillet
Email : sebastien dot peillet at oslandia dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgstest.h"
#include "qgsdoublevalidator.h"
#include <QLineEdit>
class TestQgsDoubleValidator: public QObject
{
Q_OBJECT
private slots:
void initTestCase(); // will be called before the first testfunction is executed.
void cleanupTestCase(); // will be called after the last testfunction was executed.
void init(); // will be called before each testfunction is executed.
void cleanup(); // will be called after every testfunction.
void validate();
void validate_data();
void toDouble_data();
void toDouble();
private:
};
void TestQgsDoubleValidator::initTestCase()
{
}
void TestQgsDoubleValidator::cleanupTestCase()
{
}
void TestQgsDoubleValidator::init()
{
}
void TestQgsDoubleValidator::cleanup()
{
}
void TestQgsDoubleValidator::validate_data()
{
QTest::addColumn<QString>( "actualState" );
QTest::addColumn<int>( "expState" );
QTest::addColumn<bool>( "negative" );
QTest::newRow( "C decimal" ) << QString( "4cd6" ) << int( QValidator::Acceptable ) << false;
QTest::newRow( "locale decimal" ) << QString( "4ld6" ) << int( QValidator::Acceptable ) << false;
QTest::newRow( "locale decimal" ) << QString( "4444ld6" ) << int( QValidator::Acceptable ) << false;
QTest::newRow( "C negative C decimal" ) << QString( "cn4cd6" ) << int( QValidator::Acceptable ) << true;
QTest::newRow( "locale negative locale decimal" ) << QString( "ln4ld6" ) << int( QValidator::Acceptable ) << true;
QTest::newRow( "locale negative locale decimal" ) << QString( "ln4444ld6" ) << int( QValidator::Acceptable ) << true;
QTest::newRow( "positive sign C decimal" ) << QString( "+4cd6" ) << int( QValidator::Acceptable ) << false;
// QgsDoubleValidator doesn't expect group separator but it tolerates it,
// so the result will be QValidator::Intermediate and not QValidator::Acceptable
QTest::newRow( "locale group separator + locale decimal" ) << QString( "4lg444ld6" ) << int( QValidator::Intermediate ) << false;
QTest::newRow( "locale group separator misplaced + locale decimal" ) << QString( "44lg44ld6" ) << int( QValidator::Intermediate ) << false;
QTest::newRow( "locale group separator + c decimal" ) << QString( "4lg444cd6" ) << int( QValidator::Invalid ) << false;
QTest::newRow( "c group separator + locale decimal" ) << QString( "4cg444ld6" ) << int( QValidator::Invalid ) << false;
QTest::newRow( "c group separator + c decimal" ) << QString( "4cg444cd6" ) << int( QValidator::Intermediate ) << false;
QTest::newRow( "outside the range + local decimal" ) << QString( "3ld6" ) << int( QValidator::Intermediate ) << false;
QTest::newRow( "outside the range + c decimal" ) << QString( "3cd6" ) << int( QValidator::Intermediate ) << false;
QTest::newRow( "string" ) << QString( "string" ) << int( QValidator::Invalid ) << false;
}
void TestQgsDoubleValidator::toDouble_data()
{
QTest::addColumn<QString>( "actualValue" );
QTest::addColumn<double>( "expValue" );
QTest::newRow( "C decimal" ) << QString( "4cd6" ) << 4.6;
QTest::newRow( "locale decimal" ) << QString( "4ld6" ) << 4.6;
QTest::newRow( "locale decimal" ) << QString( "4444ld6" ) << 4444.6;
QTest::newRow( "C negative C decimal" ) << QString( "cn4cd6" ) << -4.6;
QTest::newRow( "locale negative locale decimal" ) << QString( "ln4ld6" ) << -4.6;
QTest::newRow( "locale negative locale decimal" ) << QString( "ln4444ld6" ) << -4444.6;
QTest::newRow( "positive sign C decimal" ) << QString( "+4cd6" ) << 4.6;
// QgsDoubleValidator doesn't expect group separator but it tolerates it,
// so the result will be QValidator::Intermediate and not QValidator::Acceptable
QTest::newRow( "locale group separator + locale decimal" ) << QString( "4lg444ld6" ) << 4444.6;
QTest::newRow( "locale group separator misplaced + locale decimal" ) << QString( "4lg444ld6" ) << 4444.6;
QTest::newRow( "locale group separator + c decimal" ) << QString( "4lg444cd6" ) << 0.0;
QTest::newRow( "c group separator + locale decimal" ) << QString( "4cg444ld6" ) << 0.0;
QTest::newRow( "c group separator + c decimal" ) << QString( "4cg444cd6" ) << 4444.6;
QTest::newRow( "outside the range + local decimal" ) << QString( "3ld6" ) << 3.6;
QTest::newRow( "outside the range + c decimal" ) << QString( "3cd6" ) << 3.6;
QTest::newRow( "string" ) << QString( "string" ) << 0.0;
}
void TestQgsDoubleValidator::validate()
{
QLineEdit *lineEdit = new QLineEdit();
QgsDoubleValidator *validator = new QgsDoubleValidator( lineEdit );
QFETCH( QString, actualState );
QFETCH( int, expState );
QFETCH( bool, negative );
QString value;
int expectedValue;
if ( negative )
validator->setRange( -10000, -4 );
else
validator->setRange( 4, 10000 );
lineEdit->setValidator( validator );
const QVector<QLocale>listLocale( {QLocale::English, QLocale::French, QLocale::German, QLocale::Italian, QLocale::NorwegianBokmal} );
QLocale loc;
for ( int i = 0; i < listLocale.count(); ++i )
{
loc = listLocale.at( i );
QLocale::setDefault( loc );
validator->setLocale( loc );
value = actualState;
value = value.replace( "ld", QLocale().decimalPoint() )
.replace( "cd", QLocale( QLocale::C ).decimalPoint() )
.replace( "lg", QLocale().groupSeparator() )
.replace( "cg", QLocale( QLocale::C ).groupSeparator() )
.replace( "ln", QLocale().negativeSign() )
.replace( "cn", QLocale( QLocale::C ).negativeSign() );
expectedValue = expState;
// if the local group separator / decimal point is equal to the C one,
// expected result will be different for double with test with mixed
// local/C characters.
// Example with lg as local group separator
// cg as C group separator
// ld as local decimal point
// cd as C decimal point
// for 4cg444ld6 double, if cg == lg then 4cg444ld6 == 4lg444ld6
// and validator->validate(4lg444ld6) == 1 and not 0
// for 4cg444ld6 double, if cd == ld then 4cg444ld6 == 4cg444cd6
// and validator->validate(4cg444cd6) == 1 and not 0
if ( ( QLocale( QLocale::C ).groupSeparator() == QLocale().groupSeparator() ||
QLocale( QLocale::C ).decimalPoint() == QLocale().decimalPoint() )
&& value != "string" && expectedValue == 0 )
expectedValue = 1;
// There is another corner case in the test where the group separator is equal
// to the C decimal point and there is no decimal point,
// in that case the value is valid, because the fall
// back check is to test after removing all group separators
if ( QLocale( ).groupSeparator() == QLocale( QLocale::C ).decimalPoint()
&& ! value.contains( QLocale( ).decimalPoint() )
&& value != "string" && expectedValue == 0 )
{
expectedValue = 1;
}
// qDebug() << value << loc << int( validator->validate( value ) ) << expectedValue;
QCOMPARE( int( validator->validate( value ) ), expectedValue );
}
}
void TestQgsDoubleValidator::toDouble()
{
QFETCH( QString, actualValue );
QFETCH( double, expValue );
QString value;
double expectedValue;
const QVector<QLocale>listLocale( {QLocale::English, QLocale::French, QLocale::German, QLocale::Italian, QLocale::NorwegianBokmal} );
QLocale loc;
for ( int i = 0; i < listLocale.count(); ++i )
{
loc = listLocale.at( i );
QLocale::setDefault( loc );
value = actualValue;
value = value.replace( "ld", QLocale().decimalPoint() )
.replace( "cd", QLocale( QLocale::C ).decimalPoint() )
.replace( "lg", QLocale().groupSeparator() )
.replace( "cg", QLocale( QLocale::C ).groupSeparator() )
.replace( "ln", QLocale().negativeSign() )
.replace( "cn", QLocale( QLocale::C ).negativeSign() );
expectedValue = expValue;
// if the local group separator / decimal point is equal to the C one,
// expected result will be different for double with test with mixed
// local/C characters.
// Example with lg as local group separator
// cg as C group separator
// ld as local decimal point
// cd as C group decimal point
// for 4cg444ld6 double, if cg == lg then 4cg444ld6 == 4lg444ld6
// and QgsDoubleValidator::toDouble(4lg444ld6) == 4444.6 and not 0.0
// for 4cg444ld6 double, if cd == ld then 4cg444ld6 == 4cg444cd6
// and QgsDoubleValidator::toDouble(4cg444cd6) == 4444.6 and not 0.0
if ( ( QLocale( QLocale::C ).groupSeparator() == QLocale().groupSeparator() ||
QLocale( QLocale::C ).decimalPoint() == QLocale().decimalPoint() )
&& value != "string" && expectedValue == 0.0 )
expectedValue = 4444.6;
// There is another corner case in the test where the group separator is equal
// to the C decimal point and there is no decimal point,
// in that case the value is valid, because the fall
// back check is to test after removing all group separators
if ( QLocale( ).groupSeparator() == QLocale( QLocale::C ).decimalPoint()
&& ! value.contains( QLocale( ).decimalPoint() )
&& value != "string" && expectedValue == 0 )
{
expectedValue = 44446;
}
QCOMPARE( QgsDoubleValidator::toDouble( value ), expectedValue );
}
}
QGSTEST_MAIN( TestQgsDoubleValidator )
#include "testqgsdoublevalidator.moc"