Skip to content

Commit af8c166

Browse files
committed
Fix QgsFieldExpressionWidget reporting field names with spaces as
invalid, add tests
1 parent 4e60daf commit af8c166

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/gui/qgsfieldexpressionwidget.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,15 @@ bool QgsFieldExpressionWidget::isExpression() const
122122
QString QgsFieldExpressionWidget::currentField( bool *isExpression, bool *isValid ) const
123123
{
124124
QString text = currentText();
125+
bool valueIsExpression = this->isExpression();
125126
if ( isValid )
126127
{
127-
*isValid = isValidExpression();
128+
// valid if not an expression (ie, set to a field), or set to an expression and expression is valid
129+
*isValid = !valueIsExpression || isValidExpression();
128130
}
129131
if ( isExpression )
130132
{
131-
*isExpression = this->isExpression();
133+
*isExpression = valueIsExpression;
132134
}
133135
return text;
134136
}

tests/src/gui/testqgsfieldexpressionwidget.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class TestQgsFieldExpressionWidget : public QObject
4949

5050
void testRemoveJoin();
5151
void asExpression();
52+
void testIsValid();
5253

5354
private:
5455
QgsFieldExpressionWidget* mWidget;
@@ -161,6 +162,62 @@ void TestQgsFieldExpressionWidget::asExpression()
161162
QgsMapLayerRegistry::instance()->removeMapLayer( layer );
162163
}
163164

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+
}
164221

165222
QTEST_MAIN( TestQgsFieldExpressionWidget )
166223
#include "testqgsfieldexpressionwidget.moc"

0 commit comments

Comments
 (0)