@@ -1983,7 +1983,7 @@ void TestQgsProcessing::parameterBoolean()
1983
1983
QVERIFY ( def->checkValueIsAcceptable ( true ) );
1984
1984
QVERIFY ( def->checkValueIsAcceptable ( " false" ) );
1985
1985
QVERIFY ( def->checkValueIsAcceptable ( " true" ) );
1986
- QVERIFY ( ! def->checkValueIsAcceptable ( QVariant () ) );
1986
+ QVERIFY ( def->checkValueIsAcceptable ( QVariant () ) ); // should be acceptable, because it falls back to default value
1987
1987
1988
1988
params.insert ( " non_optional_default_true" , false );
1989
1989
QCOMPARE ( QgsProcessingParameters::parameterAsBool ( def.get (), params, context ), false );
@@ -2006,6 +2006,14 @@ void TestQgsProcessing::parameterBoolean()
2006
2006
QCOMPARE ( fromCode->description (), QStringLiteral ( " non optional default true" ) );
2007
2007
QCOMPARE ( fromCode->flags (), def->flags () );
2008
2008
QCOMPARE ( fromCode->defaultValue ().toBool (), true );
2009
+
2010
+ def.reset ( new QgsProcessingParameterBoolean ( " non_optional_no_default" , QString (), QVariant (), false ) );
2011
+
2012
+ QVERIFY ( def->checkValueIsAcceptable ( false ) );
2013
+ QVERIFY ( def->checkValueIsAcceptable ( true ) );
2014
+ QVERIFY ( def->checkValueIsAcceptable ( " false" ) );
2015
+ QVERIFY ( def->checkValueIsAcceptable ( " true" ) );
2016
+ QVERIFY ( !def->checkValueIsAcceptable ( QVariant () ) ); // should NOT be acceptable, because it falls back to invalid default value
2009
2017
}
2010
2018
2011
2019
void TestQgsProcessing::parameterCrs ()
@@ -3034,7 +3042,7 @@ void TestQgsProcessing::parameterDistance()
3034
3042
QVERIFY ( !def->checkValueIsAcceptable ( " 1.1,2" ) );
3035
3043
QVERIFY ( !def->checkValueIsAcceptable ( " layer12312312" ) );
3036
3044
QVERIFY ( !def->checkValueIsAcceptable ( " " ) );
3037
- QVERIFY ( ! def->checkValueIsAcceptable ( QVariant () ) );
3045
+ QVERIFY ( def->checkValueIsAcceptable ( QVariant () ) ); // should be acceptable, falls back to default value
3038
3046
3039
3047
// string representing a number
3040
3048
QVariantMap params;
@@ -3102,6 +3110,18 @@ void TestQgsProcessing::parameterDistance()
3102
3110
params.insert ( " optional" , QVariant ( " aaaa" ) );
3103
3111
number = QgsProcessingParameters::parameterAsDouble ( def.get (), params, context );
3104
3112
QGSCOMPARENEAR ( number, 5.4 , 0.001 );
3113
+
3114
+ // non-optional, invalid default
3115
+ def.reset ( new QgsProcessingParameterDistance ( " non_optional" , QString (), QVariant (), QStringLiteral ( " parent" ), false ) );
3116
+ QCOMPARE ( def->parentParameterName (), QStringLiteral ( " parent" ) );
3117
+ def->setParentParameterName ( QStringLiteral ( " parent2" ) );
3118
+ QCOMPARE ( def->parentParameterName (), QStringLiteral ( " parent2" ) );
3119
+ QVERIFY ( def->checkValueIsAcceptable ( 5 ) );
3120
+ QVERIFY ( def->checkValueIsAcceptable ( " 1.1" ) );
3121
+ QVERIFY ( !def->checkValueIsAcceptable ( " 1.1,2" ) );
3122
+ QVERIFY ( !def->checkValueIsAcceptable ( " layer12312312" ) );
3123
+ QVERIFY ( !def->checkValueIsAcceptable ( " " ) );
3124
+ QVERIFY ( !def->checkValueIsAcceptable ( QVariant () ) ); // should NOT be acceptable, falls back to invalid default value
3105
3125
}
3106
3126
3107
3127
void TestQgsProcessing::parameterNumber ()
@@ -3115,7 +3135,7 @@ void TestQgsProcessing::parameterNumber()
3115
3135
QVERIFY ( !def->checkValueIsAcceptable ( " 1.1,2" ) );
3116
3136
QVERIFY ( !def->checkValueIsAcceptable ( " layer12312312" ) );
3117
3137
QVERIFY ( !def->checkValueIsAcceptable ( " " ) );
3118
- QVERIFY ( ! def->checkValueIsAcceptable ( QVariant () ) );
3138
+ QVERIFY ( def->checkValueIsAcceptable ( QVariant () ) ); // should be acceptable, falls back to default value
3119
3139
3120
3140
// string representing a number
3121
3141
QVariantMap params;
@@ -3221,6 +3241,15 @@ void TestQgsProcessing::parameterNumber()
3221
3241
QCOMPARE ( fromCode->description (), QStringLiteral ( " optional" ) );
3222
3242
QCOMPARE ( fromCode->flags (), def->flags () );
3223
3243
QVERIFY ( !fromCode->defaultValue ().isValid () );
3244
+
3245
+ // non-optional, invalid default
3246
+ def.reset ( new QgsProcessingParameterNumber ( " non_optional" , QString (), QgsProcessingParameterNumber::Double, QVariant (), false ) );
3247
+ QVERIFY ( def->checkValueIsAcceptable ( 5 ) );
3248
+ QVERIFY ( def->checkValueIsAcceptable ( " 1.1" ) );
3249
+ QVERIFY ( !def->checkValueIsAcceptable ( " 1.1,2" ) );
3250
+ QVERIFY ( !def->checkValueIsAcceptable ( " layer12312312" ) );
3251
+ QVERIFY ( !def->checkValueIsAcceptable ( " " ) );
3252
+ QVERIFY ( !def->checkValueIsAcceptable ( QVariant () ) ); // should NOT be acceptable, falls back to invalid default value
3224
3253
}
3225
3254
3226
3255
void TestQgsProcessing::parameterRange ()
@@ -3458,7 +3487,7 @@ void TestQgsProcessing::parameterEnum()
3458
3487
QVERIFY ( !def->checkValueIsAcceptable ( -1 ) );
3459
3488
QVERIFY ( !def->checkValueIsAcceptable ( " layer12312312" ) );
3460
3489
QVERIFY ( !def->checkValueIsAcceptable ( " " ) );
3461
- QVERIFY ( ! def->checkValueIsAcceptable ( QVariant () ) );
3490
+ QVERIFY ( def->checkValueIsAcceptable ( QVariant () ) ); // should be acceptable, because falls back to default value
3462
3491
3463
3492
// string representing a number
3464
3493
QVariantMap params;
@@ -3571,7 +3600,7 @@ void TestQgsProcessing::parameterEnum()
3571
3600
QVERIFY ( !def->checkValueIsAcceptable ( -1 ) );
3572
3601
QVERIFY ( !def->checkValueIsAcceptable ( " layer12312312" ) );
3573
3602
QVERIFY ( !def->checkValueIsAcceptable ( " " ) );
3574
- QVERIFY ( def->checkValueIsAcceptable ( QVariant () ) );
3603
+ QVERIFY ( ! def->checkValueIsAcceptable ( QVariant () ) );
3575
3604
3576
3605
code = def->asScriptCode ();
3577
3606
QCOMPARE ( code, QStringLiteral ( " ##optional=optional enum a;b 5" ) );
@@ -3628,6 +3657,23 @@ void TestQgsProcessing::parameterEnum()
3628
3657
QCOMPARE ( fromCode->defaultValue (), def->defaultValue () );
3629
3658
QCOMPARE ( fromCode->options (), def->options () );
3630
3659
QCOMPARE ( fromCode->allowMultiple (), def->allowMultiple () );
3660
+
3661
+ // non optional, no default
3662
+ def.reset ( new QgsProcessingParameterEnum ( " non_optional" , QString (), QStringList () << " A" << " B" << " C" , false , QVariant (), false ) );
3663
+ QVERIFY ( !def->checkValueIsAcceptable ( false ) );
3664
+ QVERIFY ( !def->checkValueIsAcceptable ( true ) );
3665
+ QVERIFY ( def->checkValueIsAcceptable ( 1 ) );
3666
+ QVERIFY ( def->checkValueIsAcceptable ( " 1" ) );
3667
+ QVERIFY ( !def->checkValueIsAcceptable ( " 1,2" ) );
3668
+ QVERIFY ( def->checkValueIsAcceptable ( 0 ) );
3669
+ QVERIFY ( !def->checkValueIsAcceptable ( QVariantList () ) );
3670
+ QVERIFY ( !def->checkValueIsAcceptable ( QVariantList () << 1 ) );
3671
+ QVERIFY ( !def->checkValueIsAcceptable ( QVariantList () << " a" ) );
3672
+ QVERIFY ( !def->checkValueIsAcceptable ( 15 ) );
3673
+ QVERIFY ( !def->checkValueIsAcceptable ( -1 ) );
3674
+ QVERIFY ( !def->checkValueIsAcceptable ( " layer12312312" ) );
3675
+ QVERIFY ( !def->checkValueIsAcceptable ( " " ) );
3676
+ QVERIFY ( !def->checkValueIsAcceptable ( QVariant () ) ); // should NOT be acceptable, because falls back to invalid default value
3631
3677
}
3632
3678
3633
3679
void TestQgsProcessing::parameterString ()
@@ -3760,18 +3806,25 @@ void TestQgsProcessing::parameterString()
3760
3806
QCOMPARE ( fromCode->flags (), def->flags () );
3761
3807
QCOMPARE ( fromCode->defaultValue (), def->defaultValue () );
3762
3808
QCOMPARE ( fromCode->multiLine (), def->multiLine () );
3809
+
3810
+ // not optional, valid default!
3811
+ def.reset ( new QgsProcessingParameterString ( " non_optional" , QString (), QString ( " def" ), false , false ) );
3812
+ QVERIFY ( def->checkValueIsAcceptable ( 1 ) );
3813
+ QVERIFY ( def->checkValueIsAcceptable ( " test" ) );
3814
+ QVERIFY ( !def->checkValueIsAcceptable ( " " ) );
3815
+ QVERIFY ( def->checkValueIsAcceptable ( QVariant () ) ); // should be valid, falls back to valid default
3763
3816
}
3764
3817
3765
3818
void TestQgsProcessing::parameterExpression ()
3766
3819
{
3767
3820
QgsProcessingContext context;
3768
3821
3769
3822
// not optional!
3770
- std::unique_ptr< QgsProcessingParameterExpression > def ( new QgsProcessingParameterExpression ( " non_optional" , QString (), QString (), QString (), false ) );
3823
+ std::unique_ptr< QgsProcessingParameterExpression > def ( new QgsProcessingParameterExpression ( " non_optional" , QString (), QString ( " 1+1 " ), QString (), false ) );
3771
3824
QVERIFY ( def->checkValueIsAcceptable ( 1 ) );
3772
3825
QVERIFY ( def->checkValueIsAcceptable ( " test" ) );
3773
3826
QVERIFY ( !def->checkValueIsAcceptable ( " " ) );
3774
- QVERIFY ( ! def->checkValueIsAcceptable ( QVariant () ) );
3827
+ QVERIFY ( def->checkValueIsAcceptable ( QVariant () ) ); // should be acceptable, because it will fallback to default value
3775
3828
3776
3829
// string
3777
3830
QVariantMap params;
@@ -3785,7 +3838,7 @@ void TestQgsProcessing::parameterExpression()
3785
3838
QCOMPARE ( def->valueAsPythonString ( QVariant::fromValue ( QgsProperty::fromExpression ( " \" a\" =1" ) ), context ), QStringLiteral ( " QgsProperty.fromExpression('\" a\" =1')" ) );
3786
3839
3787
3840
QString code = def->asScriptCode ();
3788
- QCOMPARE ( code, QStringLiteral ( " ##non_optional=expression" ) );
3841
+ QCOMPARE ( code, QStringLiteral ( " ##non_optional=expression 1+1 " ) );
3789
3842
std::unique_ptr< QgsProcessingParameterExpression > fromCode ( dynamic_cast < QgsProcessingParameterExpression * >( QgsProcessingParameters::parameterFromScriptCode ( code ) ) );
3790
3843
QVERIFY ( fromCode.get () );
3791
3844
QCOMPARE ( fromCode->name (), def->name () );
@@ -3832,6 +3885,14 @@ void TestQgsProcessing::parameterExpression()
3832
3885
QCOMPARE ( fromCode->description (), QStringLiteral ( " optional" ) );
3833
3886
QCOMPARE ( fromCode->flags (), def->flags () );
3834
3887
QCOMPARE ( fromCode->defaultValue (), def->defaultValue () );
3888
+
3889
+ // non optional, no default
3890
+ def.reset ( new QgsProcessingParameterExpression ( " non_optional" , QString (), QString (), QString (), false ) );
3891
+ QVERIFY ( def->checkValueIsAcceptable ( 1 ) );
3892
+ QVERIFY ( def->checkValueIsAcceptable ( " test" ) );
3893
+ QVERIFY ( !def->checkValueIsAcceptable ( " " ) );
3894
+ QVERIFY ( !def->checkValueIsAcceptable ( QVariant () ) ); // should NOT be acceptable, because it will fallback to invalid default value
3895
+
3835
3896
}
3836
3897
3837
3898
void TestQgsProcessing::parameterField ()
0 commit comments