forked from opengisch/QField
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_expressionevaluator.cpp
More file actions
85 lines (67 loc) · 4.09 KB
/
test_expressionevaluator.cpp
File metadata and controls
85 lines (67 loc) · 4.09 KB
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
/***************************************************************************
test_expressionevaluator.h
--------------------
begin : Aug 2024
copyright : (C) 2024 by Mathieu Pellerin
email : mathieu at opengis dot ch
***************************************************************************/
/***************************************************************************
* *
* 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 "appexpressioncontextscopesgenerator.h"
#include "catch2.h"
#include "expressionevaluator.h"
#include "positioningutils.h"
#include <QTemporaryFile>
#include <qgsvectorlayer.h>
TEST_CASE( "ExpressionEvaluator" )
{
ExpressionEvaluator evaluator;
AppExpressionContextScopesGenerator appExpressionContextScopesGenerator;
evaluator.setAppExpressionContextScopesGenerator( &appExpressionContextScopesGenerator );
QgsProject project;
project.setTitle( QStringLiteral( "QField rocks!" ) );
evaluator.setProject( &project );
QgsVectorLayer layer( QStringLiteral( "Point?crs=EPSG:4326&field=test_field:string(255,0)" ), QStringLiteral( "test" ), QStringLiteral( "memory" ) );
QgsFeature feature( layer.fields() );
feature.setAttribute( QStringLiteral( "test_field" ), QStringLiteral( "success" ) );
evaluator.setLayer( &layer );
evaluator.setFeature( feature );
GnssPositionInformation positionInformation = PositioningUtils::createGnssPositionInformation( 1.234, 1.234, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, QDateTime(), QStringLiteral( "test" ) );
evaluator.appExpressionContextScopesGenerator()->setPositionInformation( positionInformation );
CloudUserInformation cloudUserInformation( QStringLiteral( "nyuki" ), QStringLiteral( "nyuki@opengis.ch" ) );
evaluator.appExpressionContextScopesGenerator()->setCloudUserInformation( cloudUserInformation );
SECTION( "Expression mode" )
{
evaluator.setMode( ExpressionEvaluator::ExpressionMode );
evaluator.setExpressionText( "10 + 10" );
REQUIRE( evaluator.evaluate() == QStringLiteral( "20" ) );
evaluator.setExpressionText( QStringLiteral( "@project_title" ) );
REQUIRE( evaluator.evaluate() == QStringLiteral( "QField rocks!" ) );
evaluator.setExpressionText( QStringLiteral( "\"test_field\"" ) );
REQUIRE( evaluator.evaluate() == QStringLiteral( "success" ) );
evaluator.setExpressionText( QStringLiteral( "x(@gnss_coordinate)" ) );
REQUIRE( evaluator.evaluate() == QStringLiteral( "1.234" ) );
evaluator.setExpressionText( QStringLiteral( "@cloud_username || ' - ' || @cloud_useremail" ) );
REQUIRE( evaluator.evaluate() == QStringLiteral( "nyuki - nyuki@opengis.ch" ) );
}
SECTION( "Expression template mode" )
{
evaluator.setMode( ExpressionEvaluator::ExpressionTemplateMode );
evaluator.setExpressionText( QStringLiteral( "Result: [%10 + 10%]" ) );
REQUIRE( evaluator.evaluate() == QStringLiteral( "Result: 20" ) );
evaluator.setExpressionText( QStringLiteral( "[%@project_title%] What a title!" ) );
REQUIRE( evaluator.evaluate() == QStringLiteral( "QField rocks! What a title!" ) );
evaluator.setExpressionText( QStringLiteral( "This is [% \"test_field\" %]ful" ) );
REQUIRE( evaluator.evaluate() == QStringLiteral( "This is successful" ) );
evaluator.setExpressionText( QStringLiteral( "[%x(@gnss_coordinate)%]" ) );
REQUIRE( evaluator.evaluate() == QStringLiteral( "1.234" ) );
evaluator.setExpressionText( QStringLiteral( "User: [%@cloud_username%]\nEmail: [%@cloud_useremail%]" ) );
REQUIRE( evaluator.evaluate() == QStringLiteral( "User: nyuki\nEmail: nyuki@opengis.ch" ) );
}
}