@@ -49,6 +49,7 @@ class TestQgsFieldExpressionWidget : public QObject
49
49
50
50
void testRemoveJoin ();
51
51
void asExpression ();
52
+ void testIsValid ();
52
53
53
54
private:
54
55
QgsFieldExpressionWidget* mWidget ;
@@ -161,6 +162,62 @@ void TestQgsFieldExpressionWidget::asExpression()
161
162
QgsMapLayerRegistry::instance ()->removeMapLayer ( layer );
162
163
}
163
164
165
+ void TestQgsFieldExpressionWidget::testIsValid ()
166
+ {
167
+ QgsVectorLayer* layer = new QgsVectorLayer ( " point?field=fld:int&field=name%20with%20space:string" , " x" , " memory" );
168
+ QgsMapLayerRegistry::instance ()->addMapLayer ( layer );
169
+
170
+ QScopedPointer< QgsFieldExpressionWidget > widget ( new QgsFieldExpressionWidget () );
171
+ widget->setLayer ( layer );
172
+
173
+ // also check the fieldChanged signal to ensure that the emitted bool isValid value is correct
174
+ QSignalSpy spy ( widget.data (), SIGNAL ( fieldChanged ( QString, bool ) ) );
175
+
176
+ // check with simple field name set
177
+ bool isExpression = false ;
178
+ bool isValid = false ;
179
+ widget->setField ( " fld" );
180
+ QCOMPARE ( widget->currentField ( &isExpression, &isValid ), QString ( " fld" ) );
181
+ QVERIFY ( !isExpression );
182
+ QVERIFY ( isValid );
183
+ QVERIFY ( widget->isValidExpression () );
184
+ QCOMPARE ( spy.count (), 1 );
185
+ QCOMPARE ( spy.last ().at ( 0 ).toString (), QString ( " fld" ) );
186
+ QVERIFY ( spy.last ().at ( 1 ).toBool () );
187
+
188
+
189
+ // check with complex field name set
190
+ widget->setField ( " name with space" );
191
+ QCOMPARE ( widget->currentField ( &isExpression, &isValid ), QString ( " name with space" ) );
192
+ QVERIFY ( !isExpression );
193
+ QVERIFY ( isValid );
194
+ QVERIFY ( !widget->isValidExpression () );
195
+ QCOMPARE ( spy.count (), 2 );
196
+ QCOMPARE ( spy.last ().at ( 0 ).toString (), QString ( " name with space" ) );
197
+ QVERIFY ( spy.last ().at ( 1 ).toBool () );
198
+
199
+ // check with valid expression set
200
+ widget->setField ( " 2 * 4" );
201
+ QCOMPARE ( widget->currentField ( &isExpression, &isValid ), QString ( " 2 * 4" ) );
202
+ QVERIFY ( isExpression );
203
+ QVERIFY ( isValid );
204
+ QVERIFY ( widget->isValidExpression () );
205
+ QCOMPARE ( spy.count (), 3 );
206
+ QCOMPARE ( spy.last ().at ( 0 ).toString (), QString ( " 2 * 4" ) );
207
+ QVERIFY ( spy.last ().at ( 1 ).toBool () );
208
+
209
+ // check with invalid expression set
210
+ widget->setField ( " 2 *" );
211
+ QCOMPARE ( widget->currentField ( &isExpression, &isValid ), QString ( " 2 *" ) );
212
+ QVERIFY ( isExpression );
213
+ QVERIFY ( !isValid );
214
+ QVERIFY ( !widget->isValidExpression () );
215
+ QCOMPARE ( spy.count (), 4 );
216
+ QCOMPARE ( spy.last ().at ( 0 ).toString (), QString ( " 2 *" ) );
217
+ QVERIFY ( !spy.last ().at ( 1 ).toBool () );
218
+
219
+ QgsMapLayerRegistry::instance ()->removeMapLayer ( layer );
220
+ }
164
221
165
222
QTEST_MAIN ( TestQgsFieldExpressionWidget )
166
223
#include " testqgsfieldexpressionwidget.moc"
0 commit comments