use super::util::{StubGeometryTileFeature, parse_json, property_map};
use crate::conversion::{Convertible, Error};
use crate::expression::{EvaluationContext, Functions};
use crate::feature::{Identifier, Value};
use crate::filter::{Filter, convert_filter};
use crate::geometry::FeatureType;
fn convert_json(json: &str, error: &mut Error) -> Option<Filter> {
let value = parse_json(json);
convert_filter(&Convertible::from(&value), &Functions::with_builtins(), error)
}
fn filter_full(json: &str, properties: &[(&str, Value)], id: Identifier, type_: FeatureType, zoom: f64) -> bool {
let mut error = Error::default();
let filter = convert_json(json, &mut error);
assert!(filter.is_some(), "{json}: {}", error.message);
assert_eq!(error.message, "", "{json}");
let feature = StubGeometryTileFeature::with_id_type(id, type_, property_map(properties));
let context = EvaluationContext::with_zoom_feature(zoom, Some(&feature));
filter.unwrap().evaluate(&context)
}
fn filter(json: &str, properties: &[(&str, Value)]) -> bool {
filter_full(json, properties, Identifier::Null, FeatureType::Point, 0.0)
}
fn filter_id(json: &str, properties: &[(&str, Value)], id: Identifier) -> bool {
filter_full(json, properties, id, FeatureType::Point, 0.0)
}
fn filter_type(json: &str, type_: FeatureType) -> bool {
filter_full(json, &[], Identifier::Null, type_, 0.0)
}
fn invalid_filter(json: &str) {
let mut error = Error::default();
let filter = convert_json(json, &mut error);
assert!(filter.is_none(), "{json}");
assert_ne!(error.message, "", "{json}");
}
fn stringify_filter(json: &str) -> String {
let mut error = Error::default();
let filter = convert_json(json, &mut error);
assert!(filter.is_some());
assert_eq!(error.message, "");
let value = filter.unwrap().serialize();
crate::expression::value::stringify(&crate::expression::value::from_feature_value(&value))
}
fn s(v: &str) -> Value {
Value::String(v.to_owned())
}
#[test]
fn boolean() {
assert!(filter("true", &[]));
assert!(!filter("false", &[]));
}
#[test]
fn equals_null() {
let f = r#"["==", "foo", null]"#;
assert!(filter(f, &[("foo", Value::Null)]));
assert!(!filter(f, &[("foo", Value::Int(0))]));
assert!(!filter(f, &[("foo", Value::Int(1))]));
assert!(!filter(f, &[("foo", s("0"))]));
assert!(!filter(f, &[("foo", Value::Bool(true))]));
assert!(!filter(f, &[("foo", Value::Bool(false))]));
assert!(!filter(f, &[]));
}
#[test]
fn equals_string() {
let f = r#"["==", "foo", "bar"]"#;
assert!(filter(f, &[("foo", s("bar"))]));
assert!(!filter(f, &[("foo", s("baz"))]));
}
#[test]
fn equals_number() {
let f = r#"["==", "foo", 0]"#;
assert!(filter(f, &[("foo", Value::Int(0))]));
assert!(filter(f, &[("foo", Value::Uint(0))]));
assert!(filter(f, &[("foo", Value::Double(0.0))]));
assert!(!filter(f, &[("foo", Value::Int(1))]));
assert!(!filter(f, &[("foo", Value::Uint(1))]));
assert!(!filter(f, &[("foo", Value::Double(1.0))]));
assert!(!filter(f, &[("foo", s("0"))]));
assert!(!filter(f, &[("foo", Value::Bool(false))]));
assert!(!filter(f, &[("foo", Value::Bool(true))]));
assert!(!filter(f, &[("foo", Value::Null)]));
assert!(!filter(f, &[]));
}
#[test]
fn equals_type() {
let f = r#"["==", "$type", "LineString"]"#;
assert!(!filter_type(f, FeatureType::Point));
assert!(filter_type(f, FeatureType::LineString));
assert!(!filter_type(f, FeatureType::Point));
invalid_filter(r#"["==", "$type"]"#);
invalid_filter(r#"["==", "$type", null]"#);
invalid_filter(r#"["==", "$type", "foo", 1]"#);
invalid_filter(r#"["==", "$type", "foo", "Point"]"#);
}
#[test]
fn in_type() {
let f = r#"["in", "$type", "LineString", "Polygon"]"#;
assert!(!filter_type(f, FeatureType::Point));
assert!(filter_type(f, FeatureType::LineString));
assert!(filter_type(f, FeatureType::Polygon));
}
#[test]
fn in_id() {
let f = r#"["in", "$id", "123", "1234", 1234]"#;
assert!(!filter(f, &[]));
assert!(filter_id(f, &[], Identifier::Uint(1234)));
assert!(filter_id(f, &[], Identifier::String("1234".into())));
assert!(!filter_id(f, &[], Identifier::String("4321".into())));
}
#[test]
fn any() {
assert!(!filter(r#"["any"]"#, &[]));
assert!(filter(r#"["any", ["==", "foo", 1]]"#, &[("foo", Value::Int(1))]));
assert!(!filter(r#"["any", ["==", "foo", 0]]"#, &[("foo", Value::Int(1))]));
assert!(filter(r#"["any", ["==", "foo", 0], ["==", "foo", 1]]"#, &[("foo", Value::Int(1))]));
}
#[test]
fn any_expression() {
assert!(!filter(r#"["any"]"#, &[]));
assert!(filter(r#"["any", ["==", ["get", "foo"], 1]]"#, &[("foo", Value::Int(1))]));
assert!(!filter(r#"["any", ["==", ["get", "foo"], 0]]"#, &[("foo", Value::Int(1))]));
assert!(filter(r#"["any", ["==", ["get", "foo"], 0], ["==", ["get", "foo"], 1]]"#, &[("foo", Value::Int(1))]));
}
#[test]
fn all() {
assert!(filter(r#"["all"]"#, &[]));
assert!(filter(r#"["all", ["==", "foo", 1]]"#, &[("foo", Value::Int(1))]));
assert!(!filter(r#"["all", ["==", "foo", 0]]"#, &[("foo", Value::Int(1))]));
assert!(!filter(r#"["all", ["==", "foo", 0], ["==", "foo", 1]]"#, &[("foo", Value::Int(1))]));
}
#[test]
fn all_expression() {
assert!(filter(r#"["all", ["==", ["get", "foo"], 1]]"#, &[("foo", Value::Int(1))]));
assert!(!filter(r#"["all", ["==", ["get", "foo"], 0]]"#, &[("foo", Value::Int(1))]));
assert!(!filter(r#"["all", ["==", ["get", "foo"], 0], ["==", ["get", "foo"], 1]]"#, &[("foo", Value::Int(1))]));
}
#[test]
fn none() {
assert!(filter(r#"["none"]"#, &[]));
assert!(!filter(r#"["none", ["==", "foo", 1]]"#, &[("foo", Value::Int(1))]));
assert!(filter(r#"["none", ["==", "foo", 0]]"#, &[("foo", Value::Int(1))]));
assert!(!filter(r#"["none", ["==", "foo", 0], ["==", "foo", 1]]"#, &[("foo", Value::Int(1))]));
}
#[test]
fn has() {
assert!(filter(r#"["has", "foo"]"#, &[("foo", Value::Int(1))]));
assert!(filter(r#"["has", "foo"]"#, &[("foo", Value::Int(0))]));
assert!(filter(r#"["has", "foo"]"#, &[("foo", Value::Bool(false))]));
assert!(!filter(r#"["has", "foo"]"#, &[]));
assert!(!filter(r#"["has", "$id"]"#, &[]));
}
#[test]
fn not_has() {
assert!(!filter(r#"["!has", "foo"]"#, &[("foo", Value::Int(1))]));
assert!(!filter(r#"["!has", "foo"]"#, &[("foo", Value::Int(0))]));
assert!(!filter(r#"["!has", "foo"]"#, &[("foo", Value::Bool(false))]));
assert!(filter(r#"["!has", "foo"]"#, &[]));
}
#[test]
fn id() {
let id1 = Identifier::Uint(1234);
assert!(filter_id(r#"["==", "$id", 1234]"#, &[], id1.clone()));
assert!(!filter_id(r#"["==", "$id", "1234"]"#, &[], id1.clone()));
assert!(!filter(r#"["==", "$id", 1234]"#, &[("id", Value::Uint(1234))]));
assert!(!filter(r#"["==", "$id", "1234"]"#, &[("id", Value::Uint(1234))]));
assert!(!filter_id(r#"["<", "$id", 0]"#, &[], id1.clone()));
assert!(!filter_id(r#"["<", "$id", 1234]"#, &[], id1.clone()));
assert!(filter_id(r#"["<=", "$id", 1234]"#, &[], id1.clone()));
assert!(!filter_id(r#"["<", "$id", 123]"#, &[], id1.clone()));
assert!(!filter_id(r#"["<=", "$id", 123]"#, &[], id1.clone()));
assert!(filter_id(r#"[">", "$id", 0]"#, &[], id1.clone()));
assert!(filter_id(r#"[">", "$id", 123]"#, &[], id1.clone()));
assert!(filter_id(r#"[">=", "$id", 123]"#, &[], id1.clone()));
assert!(!filter_id(r#"[">", "$id", 1234]"#, &[], id1.clone()));
assert!(filter_id(r#"[">=", "$id", 1234]"#, &[], id1.clone()));
assert!(!filter_id(r#"[">", "$id", "1"]"#, &[], id1.clone()));
assert!(!filter_id(r#"["<", "$id", "1"]"#, &[], id1.clone()));
assert!(!filter_id(r#"[">=", "$id", "1"]"#, &[], id1.clone()));
assert!(!filter_id(r#"["<=", "$id", "1"]"#, &[], id1));
let id2 = Identifier::String("1".into());
assert!(!filter_id(r#"["<", "$id", "0"]"#, &[], id2.clone()));
assert!(filter_id(r#"["<", "$id", "1234"]"#, &[], id2.clone()));
assert!(filter_id(r#"["<=", "$id", "1234"]"#, &[], id2.clone()));
assert!(!filter_id(r#"["<", "$id", "012"]"#, &[], id2.clone()));
assert!(!filter_id(r#"["<=", "$id", "012"]"#, &[], id2.clone()));
assert!(filter_id(r#"[">", "$id", "0"]"#, &[], id2.clone()));
assert!(!filter_id(r#"[">", "$id", "234"]"#, &[], id2.clone()));
assert!(filter_id(r#"[">=", "$id", "012"]"#, &[], id2.clone()));
assert!(!filter_id(r#"[">", "$id", "1234"]"#, &[], id2.clone()));
assert!(!filter_id(r#"[">=", "$id", "1234"]"#, &[], id2.clone()));
assert!(!filter_id(r#"[">", "$id", 1]"#, &[], id2.clone()));
assert!(!filter_id(r#"["<", "$id", 1]"#, &[], id2.clone()));
assert!(!filter_id(r#"[">=", "$id", 1]"#, &[], id2.clone()));
assert!(!filter_id(r#"["<=", "$id", 1]"#, &[], id2));
}
#[test]
fn expression() {
assert!(filter(r#"["==", ["+", 1, 1], 2]"#, &[]));
assert!(!filter(r#"["==", ["+", 1, 1], 4]"#, &[]));
}
#[test]
fn property_expression() {
assert!(filter(r#"["==", ["get", "two"], 2]"#, &[("two", Value::Int(2))]));
assert!(!filter(r#"["==", ["get", "two"], 4]"#, &[("two", Value::Int(2))]));
}
#[test]
fn legacy_property() {
let i = Value::Int;
assert!(filter(r#"["<", "two", 2]"#, &[("two", i(1))]));
assert!(!filter(r#"["<", "two", "2"]"#, &[("two", i(1))]));
assert!(!filter(r#"["<", "two", 2]"#, &[("two", s("1"))]));
assert!(filter(r#"["<", "two", "2"]"#, &[("two", s("1"))]));
assert!(!filter(r#"["<", "two", 2]"#, &[("two", i(2))]));
assert!(!filter(r#"["<", "two", "2"]"#, &[("two", i(2))]));
assert!(!filter(r#"["<", "two", 2]"#, &[("two", s("2"))]));
assert!(!filter(r#"["<", "two", "2"]"#, &[("two", s("2"))]));
assert!(filter(r#"["<=", "two", 2]"#, &[("two", i(1))]));
assert!(!filter(r#"["<=", "two", "2"]"#, &[("two", i(1))]));
assert!(!filter(r#"["<=", "two", 2]"#, &[("two", s("1"))]));
assert!(filter(r#"["<=", "two", "2"]"#, &[("two", s("1"))]));
assert!(filter(r#"["<=", "two", 2]"#, &[("two", i(2))]));
assert!(!filter(r#"["<=", "two", "2"]"#, &[("two", i(2))]));
assert!(!filter(r#"["<=", "two", 2]"#, &[("two", s("2"))]));
assert!(filter(r#"["<=", "two", "2"]"#, &[("two", s("2"))]));
assert!(!filter(r#"["<=", "two", 2]"#, &[("two", i(3))]));
assert!(!filter(r#"["<=", "two", "2"]"#, &[("two", i(3))]));
assert!(!filter(r#"["<=", "two", 2]"#, &[("two", s("3"))]));
assert!(!filter(r#"["<=", "two", "2"]"#, &[("two", s("3"))]));
assert!(!filter(r#"[">", "two", 2]"#, &[("two", i(2))]));
assert!(!filter(r#"[">", "two", "2"]"#, &[("two", i(2))]));
assert!(!filter(r#"[">", "two", 2]"#, &[("two", s("2"))]));
assert!(!filter(r#"[">", "two", "2"]"#, &[("two", s("2"))]));
assert!(filter(r#"[">", "two", 2]"#, &[("two", i(3))]));
assert!(!filter(r#"[">", "two", "2"]"#, &[("two", i(3))]));
assert!(!filter(r#"[">", "two", 2]"#, &[("two", s("3"))]));
assert!(filter(r#"[">", "two", "2"]"#, &[("two", s("3"))]));
assert!(!filter(r#"[">=", "two", 2]"#, &[("two", i(1))]));
assert!(!filter(r#"[">=", "two", "2"]"#, &[("two", i(1))]));
assert!(!filter(r#"[">=", "two", 2]"#, &[("two", s("1"))]));
assert!(!filter(r#"[">=", "two", "2"]"#, &[("two", s("1"))]));
assert!(filter(r#"[">=", "two", 2]"#, &[("two", i(2))]));
assert!(!filter(r#"[">=", "two", "2"]"#, &[("two", i(2))]));
assert!(!filter(r#"[">=", "two", 2]"#, &[("two", s("2"))]));
assert!(filter(r#"[">=", "two", "2"]"#, &[("two", s("2"))]));
assert!(filter(r#"[">=", "two", 2]"#, &[("two", i(3))]));
assert!(!filter(r#"[">=", "two", "2"]"#, &[("two", i(3))]));
assert!(!filter(r#"[">=", "two", 2]"#, &[("two", s("3"))]));
assert!(filter(r#"[">=", "two", "2"]"#, &[("two", s("3"))]));
assert!(filter(r#"["==", "two", 4]"#, &[("two", i(4))]));
assert!(!filter(r#"["==", "two", 4]"#, &[("two", s("4"))]));
assert!(!filter(r#"["==", "two", "4"]"#, &[("two", i(4))]));
assert!(filter(r#"["==", "two", "4"]"#, &[("two", s("4"))]));
assert!(!filter(r#"["==", "two", 4]"#, &[("two", i(2))]));
assert!(!filter(r#"["==", "two", 4]"#, &[("two", s("2"))]));
assert!(!filter(r#"["==", "two", "4"]"#, &[("two", i(2))]));
assert!(!filter(r#"["==", "two", "4"]"#, &[("two", s("2"))]));
assert!(!filter(r#"["==", "one", true]"#, &[("two", Value::Bool(true))]));
assert!(!filter(r#"["==", "one", false]"#, &[("two", Value::Bool(true))]));
assert!(filter(r#"["==", "two", true]"#, &[("two", Value::Bool(true))]));
assert!(!filter(r#"["==", "two", false]"#, &[("two", Value::Bool(true))]));
}
#[test]
fn serialize() {
let json = r#"["any",["==","foo",0],["==","foo",1]]"#;
assert_eq!(stringify_filter(json), json);
let json = r#"["<=","two",2]"#;
assert_eq!(stringify_filter(json), json);
let json = r#"["==",["+",1,1],2]"#;
assert_eq!(stringify_filter(json), "true");
let json = r#"["all",["==",["get","foo"],0],["==",["get","foo"],1]]"#;
assert_eq!(stringify_filter(json), json);
}
#[test]
fn mixed_legacy_expression() {
let left = [("position", s("left"))];
let right = [("position", s("right"))];
let f = r#"["any", ["==", ["get", "position"], "left"], ["==", "position", "left"]]"#;
assert!(filter(f, &left));
assert!(!filter(f, &right));
let f = r#"["all", ["==", "position", "position"], ["==", ["get", "position"], "left"]]"#;
assert!(filter(f, &left));
assert!(!filter(f, &right));
let f = r#"["any", ["==", "position", "position"], ["all", ["==", ["get", "position"], "left"]]]"#;
assert!(filter(f, &right));
}
#[test]
fn neutral_children() {
let legacy = r#"["all", true, ["has", "foo"], ["!in", "foo", 2]]"#;
assert!(filter(legacy, &[("foo", Value::Int(1))]));
assert!(!filter(legacy, &[("foo", Value::Int(2))]));
assert!(!filter(legacy, &[]));
let expression = r#"["all", true, ["has", "foo"], ["!", ["in", ["get", "foo"], ["literal", [2]]]]]"#;
assert!(filter(expression, &[("foo", Value::Int(1))]));
assert!(!filter(expression, &[("foo", Value::Int(2))]));
assert!(filter(r#"["any", false, ["has", "foo"]]"#, &[("foo", Value::Int(1))]));
assert!(filter(r#"["all", ["has", "foo"], ["in", "foo", "bar"]]"#, &[("foo", s("bar"))]));
assert!(!filter(r#"["all", ["has", "foo"], ["in", "foo", "bar"]]"#, &[("foo", s("barn"))]));
}
#[test]
fn expression_legacy_mix() {
let mut error = Error::default();
let filter =
convert_json(r#"["any", ["all", ["==", ["geometry-type"], "LineString"]], ["==", "x", 1]]"#, &mut error);
assert!(filter.is_none());
assert_ne!(error.message, "");
}
#[test]
fn zoom_expression_nested() {
assert!(filter_full(
r#"["==", ["get", "two"], ["zoom"]]"#,
&[("two", Value::Int(2))],
Identifier::Null,
FeatureType::Point,
2.0
));
assert!(!filter_full(
r#"["==", ["get", "two"], ["+", ["zoom"], 1]]"#,
&[("two", Value::Int(2))],
Identifier::Null,
FeatureType::Point,
2.0
));
}
#[test]
fn geom_type() {
let check = |json: &str, type_: FeatureType| filter_full(json, &[], Identifier::Null, type_, 2.0);
assert!(check(r#"["==", ["geometry-type"], "Point"]"#, FeatureType::Point));
assert!(!check(r#"["==", ["geometry-type"], "LineString"]"#, FeatureType::Point));
assert!(!check(r#"["==", ["geometry-type"], "Polygon"]"#, FeatureType::Point));
assert!(!check(r#"["==", ["geometry-type"], "Unknown"]"#, FeatureType::Point));
assert!(!check(r#"["==", ["geometry-type"], "Point"]"#, FeatureType::LineString));
assert!(check(r#"["==", ["geometry-type"], "LineString"]"#, FeatureType::LineString));
assert!(!check(r#"["==", ["geometry-type"], "Polygon"]"#, FeatureType::LineString));
assert!(!check(r#"["==", ["geometry-type"], "Unknown"]"#, FeatureType::LineString));
assert!(!check(r#"["==", ["geometry-type"], "Point"]"#, FeatureType::Polygon));
assert!(!check(r#"["==", ["geometry-type"], "LineString"]"#, FeatureType::Polygon));
assert!(check(r#"["==", ["geometry-type"], "Polygon"]"#, FeatureType::Polygon));
assert!(!check(r#"["==", ["geometry-type"], "Unknown"]"#, FeatureType::Polygon));
assert!(!check(r#"["==", ["geometry-type"], "Point"]"#, FeatureType::Unknown));
assert!(!check(r#"["==", ["geometry-type"], "LineString"]"#, FeatureType::Unknown));
assert!(!check(r#"["==", ["geometry-type"], "Polygon"]"#, FeatureType::Unknown));
assert!(check(r#"["==", ["geometry-type"], "Unknown"]"#, FeatureType::Unknown));
}
#[test]
fn short() {
filter(r#"["==", ["id"], "foo"]"#, &[]);
}
#[test]
fn legacy_expression_invalid_type() {
let value = serde_json::json!("string");
let mut error = Error::default();
let result = convert_filter(&Convertible::from(&value), &Functions::with_builtins(), &mut error);
assert!(result.is_none());
}
#[test]
fn in_without_operands() {
assert!(filter(r#"["in"]"#, &[]));
assert!(filter(r#"["any", ["in"]]"#, &[]));
}