use super::util::parse_json;
use crate::StyleRegistry;
use crate::conversion::{Converter, Convertible, Error};
use crate::expression::{EvaluationContext, Functions};
use crate::layer::convert_layer;
use crate::property::convert::convert_property_value;
use crate::property::expression::PropertyExpression;
use crate::property::value::PropertyValue;
use crate::types::{AnchorOffsetPair, SymbolAnchorType, VariableAnchorOffsetCollection};
fn parse(src: &str, error: &mut Error) -> Option<VariableAnchorOffsetCollection> {
let value = parse_json(src);
<VariableAnchorOffsetCollection as Converter>::convert(&Convertible::from(&value), error)
}
#[test]
fn calculations() {
let mut error = Error::default();
{
let a = parse(r#"["top", [0, 0], "bottom", [2, 2]]"#, &mut error).expect("a");
let b = parse(r#"["top", [1, 1], "bottom", [4, 4]]"#, &mut error).expect("b");
let c = VariableAnchorOffsetCollection::interpolate(&a, &b, 0.5).unwrap();
assert_eq!(
VariableAnchorOffsetCollection::new(vec![
AnchorOffsetPair::new(SymbolAnchorType::Top, [0.5, 0.5]),
AnchorOffsetPair::new(SymbolAnchorType::Bottom, [3.0, 3.0]),
]),
c
);
}
{
let a = parse(r#"["top", [0, 0]]"#, &mut error).expect("a");
let b = parse(r#"["bottom", [1, 1]]"#, &mut error).expect("b");
assert!(VariableAnchorOffsetCollection::interpolate(&a, &b, 0.5).is_err());
let a = parse(r#"["top", [0, 0]]"#, &mut error).expect("a");
let b = parse(r#"["top", [1, 1], "bottom", [2, 2]]"#, &mut error).expect("b");
assert!(VariableAnchorOffsetCollection::interpolate(&a, &b, 0.5).is_err());
}
}
#[test]
fn style_conversion() {
let mut error = Error::default();
{
let json_value = r#"["left",[1,2],"top",[0,0]]"#;
let variable_anchor_offset = parse(json_value, &mut error).expect("valid");
assert_eq!(variable_anchor_offset.len(), 2);
assert_eq!(variable_anchor_offset[0].anchor_type, SymbolAnchorType::Left);
assert_eq!(variable_anchor_offset[0].offset[0], 1.0);
assert_eq!(variable_anchor_offset[0].offset[1], 2.0);
assert_eq!(variable_anchor_offset.to_string(), json_value);
}
let cases = [
("{}", "value must be an array"),
("[]", "array must contain an even number of elements"),
(r#"["test", [1, 2]]"#, "anchor must be a valid anchor value"),
(r#"["top", "left"]"#, "anchor offset must be an array"),
(r#"["top", [1, 2, 3]]"#, "anchor offset must have two elements"),
(r#"["top", ["test", "test"]]"#, "anchor offset must have two numbers"),
];
for (json, expected_error) in cases {
let variable_anchor_offset = parse(json, &mut error);
assert!(variable_anchor_offset.is_none(), "{json}");
assert_eq!(error.message, expected_error, "{json}");
}
{
let json = parse_json(
r#"{
"type": "symbol",
"id": "symbol",
"source": "composite",
"source-layer": "landmarks",
"minzoom": 12,
"maxzoom": 18,
"layout": {
"text-field": "Test Test Test",
"icon-image": ["image", ["get", "landmark_image"]],
"text-size": 24,
"text-justify": "auto",
"text-variable-anchor-offset": ["center", [0, 0],"top", [0, 0]]
}
}"#,
);
let layer = convert_layer(&StyleRegistry::with_builtins(), &Convertible::from(&json), &mut error);
assert!(layer.is_some(), "{}", error.message);
let value = layer.unwrap().serialize();
match value {
crate::feature::Value::Object(object) => assert_eq!(7, object.len()),
other => panic!("not an object: {other:?}"),
}
}
}
#[test]
fn expression() {
let mut error = Error::default();
let json_to_property_value = |json: &str, error: &mut Error| {
let value = parse_json(json);
convert_property_value::<VariableAnchorOffsetCollection>(
&Convertible::from(&value),
error,
true,
false,
&Functions::with_builtins(),
)
};
let json_to_expression = |json: &str, error: &mut Error| -> PropertyExpression<VariableAnchorOffsetCollection> {
match json_to_property_value(json, error) {
Some(PropertyValue::Expression(e)) => e,
other => panic!("not an expression: {other:?} ({})", error.message),
}
};
let left = |x: f32, y: f32| {
VariableAnchorOffsetCollection::new(vec![AnchorOffsetPair::new(SymbolAnchorType::Left, [x, y])])
};
{
let expr = json_to_expression(
r#"{"type": "interval", "stops": [[0, ["left", [0, 0]]], [5, ["left", [10, 20]]]]}"#,
&mut error,
);
assert_eq!(
left(0.0, 0.0),
expr.evaluate(&EvaluationContext::new(0.0), VariableAnchorOffsetCollection::default())
);
assert_eq!(
left(0.0, 0.0),
expr.evaluate(&EvaluationContext::new(4.0), VariableAnchorOffsetCollection::default())
);
assert_eq!(
left(10.0, 20.0),
expr.evaluate(&EvaluationContext::new(5.0), VariableAnchorOffsetCollection::default())
);
}
{
let expr = json_to_expression(
r#"["interpolate", ["linear"], ["zoom"], 0, ["literal", ["left", [0, 0]]], 1, ["literal",["left",
[10, 20]]]]"#,
&mut error,
);
let result = expr.evaluate(&EvaluationContext::new(0.5), VariableAnchorOffsetCollection::default());
assert_eq!(left(5.0, 10.0), result);
}
{
let expected_error = "[4]: Invalid variableAnchorOffset value [\"left\"]: expected an array containing an even number of values.";
let parsed_result = json_to_property_value(
r#"["interpolate", ["linear"], ["zoom"], 0, ["literal", ["left"]], 1, ["literal",["left",
[10, 20]]]]"#,
&mut error,
);
assert!(parsed_result.is_none());
assert_eq!(error.message, expected_error);
let expected_error = "[4]: Invalid variableAnchorOffset value [\"left\",[10]]: expected a string value as the first and an array with two elements as the second element of each pair.";
let parsed_result = json_to_property_value(
r#"["interpolate", ["linear"], ["zoom"], 0, ["literal", ["left", [10]]], 1, ["literal",["left",
[10, 20]]]]"#,
&mut error,
);
assert!(parsed_result.is_none());
assert_eq!(error.message, expected_error);
let expected_error =
"[4]: Invalid variableAnchorOffsetCollection value [\"test\",[0,0]]: unknown anchor type 'test'";
let parsed_result = json_to_property_value(
r#"["interpolate", ["linear"], ["zoom"], 0, ["literal", ["test", [0, 0]]], 1, ["literal",["left",
[10, 20]]]]"#,
&mut error,
);
assert!(parsed_result.is_none());
assert_eq!(error.message, expected_error);
}
}