2016-01-18 19:53:26 +01:00
|
|
|
|
#include "catch_ext.hpp"
|
2015-12-08 12:42:55 +01:00
|
|
|
|
|
|
|
|
|
#include <mapnik/expression.hpp>
|
|
|
|
|
#include <mapnik/expression_evaluator.hpp>
|
|
|
|
|
#include <mapnik/expression_string.hpp>
|
|
|
|
|
#include <mapnik/wkt/wkt_factory.hpp>
|
|
|
|
|
#include <mapnik/feature.hpp>
|
|
|
|
|
#include <mapnik/feature_factory.hpp>
|
|
|
|
|
#include <mapnik/unicode.hpp>
|
2021-05-17 17:06:34 +02:00
|
|
|
|
#include <mapnik/util/from_u8string.hpp>
|
2016-01-18 19:53:26 +01:00
|
|
|
|
|
|
|
|
|
#include <functional>
|
2018-06-27 11:56:36 +02:00
|
|
|
|
#include <map>
|
2015-12-08 12:42:55 +01:00
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
template <typename Properties>
|
|
|
|
|
mapnik::feature_ptr make_test_feature(mapnik::value_integer id, std::string const& wkt, Properties const& prop)
|
|
|
|
|
{
|
|
|
|
|
auto ctx = std::make_shared<mapnik::context_type>();
|
|
|
|
|
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, id));
|
|
|
|
|
mapnik::geometry::geometry<double> geom;
|
|
|
|
|
if (mapnik::from_wkt(wkt, geom))
|
|
|
|
|
{
|
|
|
|
|
feature->set_geometry(std::move(geom));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto const& kv : prop)
|
|
|
|
|
{
|
|
|
|
|
feature->put_new(kv.first, kv.second);
|
|
|
|
|
}
|
|
|
|
|
return feature;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Feature, typename Expression>
|
|
|
|
|
mapnik::value_type evaluate(Feature const& feature, Expression const& expr)
|
|
|
|
|
{
|
|
|
|
|
auto value = mapnik::util::apply_visitor(
|
|
|
|
|
mapnik::evaluate<Feature, mapnik::value_type, mapnik::attributes>(
|
|
|
|
|
feature, mapnik::attributes()), expr);
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-18 19:53:26 +01:00
|
|
|
|
mapnik::value evaluate_string(mapnik::feature_ptr const& feature, std::string const& str)
|
|
|
|
|
{
|
|
|
|
|
auto expr = mapnik::parse_expression(str);
|
|
|
|
|
return evaluate(*feature, *expr);
|
2015-12-08 12:42:55 +01:00
|
|
|
|
}
|
2016-01-18 19:53:26 +01:00
|
|
|
|
|
|
|
|
|
std::string parse_and_dump(std::string const& str)
|
|
|
|
|
{
|
|
|
|
|
auto expr = mapnik::parse_expression(str);
|
|
|
|
|
return mapnik::to_expression_string(*expr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2015-12-08 12:42:55 +01:00
|
|
|
|
TEST_CASE("expressions")
|
|
|
|
|
{
|
2016-01-18 19:53:26 +01:00
|
|
|
|
using namespace std::placeholders;
|
2021-05-17 17:06:34 +02:00
|
|
|
|
using namespace mapnik::util;
|
2018-06-27 11:56:36 +02:00
|
|
|
|
using properties_type = std::map<std::string, mapnik::value>;
|
2015-12-08 12:42:55 +01:00
|
|
|
|
mapnik::transcoder tr("utf8");
|
2015-12-08 12:58:10 +01:00
|
|
|
|
|
|
|
|
|
properties_type prop = {{ "foo" , tr.transcode("bar") },
|
2015-12-08 17:25:59 +01:00
|
|
|
|
{ "name" , tr.transcode("Québec")},
|
2016-01-18 16:34:09 +01:00
|
|
|
|
{ "grass" , tr.transcode("grow")},
|
|
|
|
|
{ "wind" , tr.transcode("blow")},
|
|
|
|
|
{ "sky" , tr.transcode("is blue")},
|
2018-06-27 11:58:56 +02:00
|
|
|
|
{ "τ" , mapnik::value_double(6.2831853)},
|
2015-12-08 12:58:10 +01:00
|
|
|
|
{ "double", mapnik::value_double(1.23456)},
|
|
|
|
|
{ "int" , mapnik::value_integer(123)},
|
|
|
|
|
{ "bool" , mapnik::value_bool(true)},
|
|
|
|
|
{ "null" , mapnik::value_null()}};
|
2015-12-08 12:42:55 +01:00
|
|
|
|
|
|
|
|
|
auto feature = make_test_feature(1, "POINT(100 200)", prop);
|
2016-01-18 19:53:26 +01:00
|
|
|
|
auto eval = std::bind(evaluate_string, feature, _1);
|
|
|
|
|
auto approx = Approx::custom().epsilon(1e-6);
|
2015-12-08 12:58:10 +01:00
|
|
|
|
|
2015-12-08 12:42:55 +01:00
|
|
|
|
// primary expressions
|
|
|
|
|
// null
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(parse_and_dump("null") == "null");
|
2015-12-08 12:42:55 +01:00
|
|
|
|
// boolean
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(parse_and_dump("true") == "true");
|
|
|
|
|
TRY_CHECK(parse_and_dump("false") == "false");
|
2015-12-08 12:42:55 +01:00
|
|
|
|
// floating point
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(parse_and_dump("3.14159") == "3.14159");
|
2015-12-08 12:42:55 +01:00
|
|
|
|
// integer
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(parse_and_dump("123") == "123");
|
2015-12-08 12:42:55 +01:00
|
|
|
|
// unicode
|
2016-03-15 00:26:21 +01:00
|
|
|
|
TRY_CHECK(parse_and_dump("''") == "''");
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(parse_and_dump("'single-quoted string'") == "'single-quoted string'");
|
|
|
|
|
TRY_CHECK(parse_and_dump("\"double-quoted string\"") == "'double-quoted string'");
|
2016-02-15 02:23:19 +01:00
|
|
|
|
TRY_CHECK(parse_and_dump("'escaped \\' apostrophe'") == "'escaped \\' apostrophe'");
|
|
|
|
|
TRY_CHECK(parse_and_dump("'escaped \\\\ backslash'") == "'escaped \\\\ backslash'");
|
2015-12-08 12:42:55 +01:00
|
|
|
|
|
|
|
|
|
// floating point constants
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(parse_and_dump("pi") == "3.14159");
|
|
|
|
|
TRY_CHECK(parse_and_dump("deg_to_rad") == "0.0174533");
|
|
|
|
|
TRY_CHECK(parse_and_dump("rad_to_deg") == "57.2958");
|
2015-12-08 12:42:55 +01:00
|
|
|
|
|
2018-06-27 11:58:56 +02:00
|
|
|
|
// ascii attribute name
|
|
|
|
|
TRY_CHECK(eval(" [foo]='bar' ") == true);
|
|
|
|
|
|
|
|
|
|
// unicode attribute name
|
|
|
|
|
TRY_CHECK(eval("[τ]") == prop.at("τ"));
|
2021-05-17 17:06:34 +02:00
|
|
|
|
TRY_CHECK(eval("[τ]") == eval(from_u8string(u8"[\u03C4]")));
|
2018-06-27 11:58:56 +02:00
|
|
|
|
|
|
|
|
|
// change to TRY_CHECK once \u1234 escape sequence in attribute name
|
|
|
|
|
// is implemented in expression grammar
|
2021-05-17 17:06:34 +02:00
|
|
|
|
CHECK_NOFAIL(eval("[τ]") == eval(from_u8string(u8"[\\u03C3]")));
|
2018-06-27 11:58:56 +02:00
|
|
|
|
|
2015-12-08 12:42:55 +01:00
|
|
|
|
// unary functions
|
|
|
|
|
// sin / cos
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(eval(" sin(0.25 * pi) / cos(0.25 * pi) ").to_double() == approx(1.0));
|
2015-12-08 12:42:55 +01:00
|
|
|
|
// tan
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(eval(" tan(0.25 * pi) ").to_double() == approx(1.0));
|
2015-12-08 12:42:55 +01:00
|
|
|
|
// atan
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(eval(" rad_to_deg * atan(1.0) ").to_double() == approx(45.0));
|
2015-12-08 12:42:55 +01:00
|
|
|
|
// exp
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(eval(" exp(0.0) ") == 1.0);
|
2016-08-01 14:42:57 +02:00
|
|
|
|
// log
|
|
|
|
|
TRY_CHECK(eval(" log(1.0) ") == 0.0);
|
|
|
|
|
TRY_CHECK(eval(" log(exp(1.0)) ") == 1.0);
|
2015-12-08 12:42:55 +01:00
|
|
|
|
// abs
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(eval(" abs(cos(-pi)) ") == 1.0);
|
2015-12-08 12:42:55 +01:00
|
|
|
|
// length (string)
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(eval(" length('1234567890') ").to_int() == 10);
|
2015-12-08 12:42:55 +01:00
|
|
|
|
|
|
|
|
|
// binary functions
|
|
|
|
|
// min
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(eval(" min(-0.01, 0.001) ") == -0.01);
|
2015-12-08 12:42:55 +01:00
|
|
|
|
// max
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(eval(" max(0.01, -0.1) ") == 0.01);
|
2015-12-08 12:42:55 +01:00
|
|
|
|
// pow
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(eval(" pow(2, 32) ") == 4294967296.0);
|
2015-12-08 12:42:55 +01:00
|
|
|
|
|
|
|
|
|
// geometry types
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(eval(" [mapnik::geometry_type] = point ") == true);
|
2017-01-10 11:57:54 +01:00
|
|
|
|
TRY_CHECK(eval(" [ mapnik::geometry_type] <> linestring ") == true);
|
|
|
|
|
TRY_CHECK(eval(" [mapnik::geometry_type ] != polygon ") == true);
|
|
|
|
|
TRY_CHECK(eval(" [ mapnik::geometry_type ] neq collection ") == true);
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(eval(" [mapnik::geometry_type] eq collection ") == false);
|
2015-12-08 12:42:55 +01:00
|
|
|
|
|
|
|
|
|
//unary expression
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(eval(" -123.456 ") == -123.456);
|
|
|
|
|
TRY_CHECK(eval(" +123.456 ") == 123.456);
|
2015-12-08 12:42:55 +01:00
|
|
|
|
|
|
|
|
|
// multiplicative/additive
|
2016-01-18 19:53:26 +01:00
|
|
|
|
auto expr = mapnik::parse_expression("(2.0 * 2.0 + 3.0 * 3.0)/(2.0 * 2.0 - 3.0 * 3.0)");
|
|
|
|
|
TRY_CHECK(evaluate(*feature, *expr) == -2.6);
|
|
|
|
|
auto expr2 = mapnik::parse_expression("(2.0 * 2.0 + 3.0 * 3.0)/((2.0 - 3.0) * (2.0 + 3.0))");
|
|
|
|
|
TRY_CHECK(evaluate(*feature, *expr) == evaluate(*feature, *expr2));
|
2015-12-08 12:42:55 +01:00
|
|
|
|
|
2015-12-08 12:58:10 +01:00
|
|
|
|
// logical
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(eval(" [int] = 123 and [double] = 1.23456 && [bool] = true and [null] = null && [foo] = 'bar' ") == true);
|
|
|
|
|
TRY_CHECK(eval(" [int] = 456 or [foo].match('foo') || length([foo]) = 3 ") == true);
|
2020-01-02 17:30:24 +01:00
|
|
|
|
TRY_CHECK(eval(" not true and not true ") == false); // (not true) and (not true)
|
|
|
|
|
TRY_CHECK(eval(" not false and not true ") == false); // (not false) and (not true)
|
|
|
|
|
TRY_CHECK(eval(" not true or not false ") == true); // (not true) or (not false)
|
|
|
|
|
TRY_CHECK(eval(" not false or not false ") == true); // (not false) or (not false)
|
2016-01-18 16:34:09 +01:00
|
|
|
|
|
|
|
|
|
// test not/and/or precedence using combinations of "not EQ1 OP1 not EQ2 OP2 not EQ3"
|
|
|
|
|
TRY_CHECK(eval(" not [grass] = 'grow' and not [wind] = 'blow' and not [sky] = 'is blue' ") == false);
|
|
|
|
|
TRY_CHECK(eval(" not [grass] = 'grow' and not [wind] = 'blow' or not [sky] = 'is blue' ") == false);
|
|
|
|
|
TRY_CHECK(eval(" not [grass] = 'grow' or not [wind] = 'blow' and not [sky] = 'is blue' ") == false);
|
|
|
|
|
TRY_CHECK(eval(" not [grass] = 'grow' or not [wind] = 'blow' or not [sky] = 'is blue' ") == false);
|
|
|
|
|
TRY_CHECK(eval(" not [grass] = 'grew' and not [wind] = 'blew' and not [sky] = 'was blue' ") == true);
|
|
|
|
|
TRY_CHECK(eval(" not [grass] = 'grew' and not [wind] = 'blew' or not [sky] = 'was blue' ") == true);
|
|
|
|
|
TRY_CHECK(eval(" not [grass] = 'grew' or not [wind] = 'blew' and not [sky] = 'was blue' ") == true);
|
|
|
|
|
TRY_CHECK(eval(" not [grass] = 'grew' or not [wind] = 'blew' or not [sky] = 'was blue' ") == true);
|
2015-12-08 17:25:59 +01:00
|
|
|
|
|
|
|
|
|
// relational
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(eval(" [int] > 100 and [int] gt 100.0 and [double] < 2 and [double] lt 2.0 ") == true);
|
|
|
|
|
TRY_CHECK(eval(" [int] >= 123 and [int] ge 123.0 and [double] <= 1.23456 and [double] le 1.23456 ") == true);
|
2015-12-08 17:25:59 +01:00
|
|
|
|
|
2016-02-25 11:21:07 +01:00
|
|
|
|
// empty string/null equality
|
|
|
|
|
TRY_CHECK(eval("[null] = null") == true);
|
|
|
|
|
TRY_CHECK(eval("[null] != null") == false);
|
|
|
|
|
TRY_CHECK(eval("[null] = ''") == false);
|
|
|
|
|
///////////////////// ref: https://github.com/mapnik/mapnik/issues/1859
|
|
|
|
|
TRY_CHECK(eval("[null] != ''") == false); // back compatible - will be changed in 3.1.x
|
|
|
|
|
//////////////////////
|
|
|
|
|
TRY_CHECK(eval("'' = [null]") == false);
|
|
|
|
|
TRY_CHECK(eval("'' != [null]") == true);
|
|
|
|
|
|
2015-12-08 17:25:59 +01:00
|
|
|
|
// regex
|
|
|
|
|
// replace
|
2018-07-02 16:42:35 +02:00
|
|
|
|
TRY_CHECK(eval(" [foo].replace('(\\B)|( )','$1 ') ") == tr.transcode("b a r")); // single quotes
|
|
|
|
|
TRY_CHECK(eval(" [foo].replace(\"(\\B)|( )\",\"$1 \") ") == tr.transcode("b a r")); // double quotes
|
2016-08-09 17:13:12 +02:00
|
|
|
|
|
|
|
|
|
// https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode
|
|
|
|
|
//'\u265C\u265E\u265D\u265B\u265A\u265D\u265E\u265C' - black chess figures
|
|
|
|
|
// replace black knights with white knights
|
2021-05-17 17:06:34 +02:00
|
|
|
|
auto val0 = eval(from_u8string(u8"'\u265C\u265E\u265D\u265B\u265A\u265D\u265E\u265C'.replace('\u265E','\u2658')"));
|
2016-08-12 07:27:55 +02:00
|
|
|
|
auto val1 = eval("'♜♞♝♛♚♝♞♜'.replace('♞','♘')"); // ==> expected ♜♘♝♛♚♝♘♜
|
2016-08-09 17:13:12 +02:00
|
|
|
|
TRY_CHECK(val0 == val1);
|
2016-08-09 17:25:59 +02:00
|
|
|
|
TRY_CHECK(val0.to_string() == val1.to_string()); // UTF-8
|
2017-03-06 15:59:07 +01:00
|
|
|
|
TRY_CHECK(val0.to_unicode() == val1.to_unicode()); // Unicode
|
|
|
|
|
// \u+NNNN \U+NNNNNNNN \xNN\xNN
|
2018-07-02 16:42:35 +02:00
|
|
|
|
// single quotes
|
2018-07-02 15:10:28 +02:00
|
|
|
|
auto val3 = eval("'\\u262f\\xF0\\x9F\\x8D\\xB7'");
|
|
|
|
|
auto val4 = eval("'\\U0000262f\\U0001F377'");
|
2018-07-02 16:42:35 +02:00
|
|
|
|
// double quotes
|
2018-07-02 15:10:28 +02:00
|
|
|
|
auto val5 = eval("\"\\u262f\\xF0\\x9F\\x8D\\xB7\"");
|
|
|
|
|
auto val6 = eval("\"\\U0000262f\\U0001F377\"");
|
2017-03-06 15:59:07 +01:00
|
|
|
|
// UTF16 surrogate pairs work also ;)
|
2018-07-02 21:55:27 +02:00
|
|
|
|
auto val7 = eval("'\\ud83d\\udd7a\\ud83c\\udffc'");
|
|
|
|
|
auto val8 = eval("'\\U0001F57A\\U0001F3FC'");
|
|
|
|
|
|
2017-03-06 15:59:07 +01:00
|
|
|
|
TRY_CHECK(val3 == val4);
|
2018-07-02 15:10:28 +02:00
|
|
|
|
TRY_CHECK(val5 == val6);
|
2017-03-06 15:59:07 +01:00
|
|
|
|
TRY_CHECK(val3.to_string() == val4.to_string()); // UTF-8
|
2018-07-02 21:55:27 +02:00
|
|
|
|
TRY_CHECK(val3.to_unicode() == val4.to_unicode()); // Unicode
|
2018-07-02 15:10:28 +02:00
|
|
|
|
TRY_CHECK(val5.to_string() == val6.to_string()); // UTF-8
|
|
|
|
|
TRY_CHECK(val5.to_unicode() == val6.to_unicode()); // Unicode
|
2018-07-02 21:55:27 +02:00
|
|
|
|
TRY_CHECK(val7 == val8);
|
|
|
|
|
TRY_CHECK(val7.to_string() == val8.to_string()); // UTF-8
|
|
|
|
|
TRY_CHECK(val7.to_unicode() == val8.to_unicode()); // Unicode
|
|
|
|
|
|
2016-08-09 17:13:12 +02:00
|
|
|
|
|
|
|
|
|
// following test will fail if boost_regex is built without ICU support (unpaired surrogates in output)
|
2016-08-12 07:27:55 +02:00
|
|
|
|
TRY_CHECK(eval("[name].replace('(\\B)|( )',' ') ") == tr.transcode("Q u é b e c"));
|
|
|
|
|
TRY_CHECK(eval("'Москва'.replace('(?<!^)(\\B|b)(?!$)',' ')") == tr.transcode("М о с к в а"));
|
2016-02-15 02:23:19 +01:00
|
|
|
|
// 'foo' =~ s:(\w)\1:$1x:r
|
|
|
|
|
TRY_CHECK(eval(" 'foo'.replace('(\\w)\\1', '$1x') ") == tr.transcode("fox"));
|
|
|
|
|
TRY_CHECK(parse_and_dump(" 'foo'.replace('(\\w)\\1', '$1x') ") == "'foo'.replace('(\\w)\\1','$1x')");
|
2015-12-08 17:25:59 +01:00
|
|
|
|
|
|
|
|
|
// match
|
2016-01-18 19:53:26 +01:00
|
|
|
|
TRY_CHECK(eval(" [name].match('Québec') ") == true);
|
2016-02-15 02:23:19 +01:00
|
|
|
|
// 'Québec' =~ m:^Q\S*$:
|
|
|
|
|
TRY_CHECK(eval(" [name].match('^Q\\S*$') ") == true);
|
|
|
|
|
TRY_CHECK(parse_and_dump(" [name].match('^Q\\S*$') ") == "[name].match('^Q\\S*$')");
|
2016-04-01 00:36:47 +02:00
|
|
|
|
|
|
|
|
|
// string & value concatenation
|
2016-08-09 17:13:12 +02:00
|
|
|
|
// this should evaluate as two strings concatenating
|
2016-04-01 00:36:47 +02:00
|
|
|
|
TRY_CHECK(eval("Hello + '!'") == eval("'Hello!'"));
|
2016-08-09 17:13:12 +02:00
|
|
|
|
// this should evaulate as a combination of an int value and string
|
2016-04-01 00:36:47 +02:00
|
|
|
|
TRY_CHECK(eval("[int]+m") == eval("'123m'"));
|
2015-12-08 12:42:55 +01:00
|
|
|
|
}
|