#[cfg(test)]
mod tests {
use super::super::common::vm_query;
use serde_json::json;
#[test]
fn test_lambda_body_two_writes_fuse() {
let doc = json!({
"users": [
{"name": "alice", "score": 0},
{"name": "bob", "score": 0}
]
});
let r = vm_query(
r#"$.users.map(lambda o: o.name.set("x") | o.score.set(1))"#,
&doc,
)
.unwrap();
assert_eq!(
r,
json!([
{"name": "x", "score": 1},
{"name": "x", "score": 1}
])
);
}
#[test]
fn test_lambda_body_three_writes_fuse() {
let doc = json!({"items": [{}, {}]});
let r = vm_query(
r#"$.items.map(lambda o: o.a.set(1) | o.b.set(2) | o.c.set(3))"#,
&doc,
)
.unwrap();
assert_eq!(
r,
json!([
{"a": 1, "b": 2, "c": 3},
{"a": 1, "b": 2, "c": 3}
])
);
}
#[test]
fn test_listcomp_body_two_writes_fuse() {
let doc = json!({"list": [{"id": 1}, {"id": 2}, {"id": 3}]});
let r = vm_query(
r#"[o.id.set(o.id + 10) | o.tag.set("p") for o in $.list]"#,
&doc,
)
.unwrap();
assert_eq!(
r,
json!([
{"id": 11, "tag": "p"},
{"id": 12, "tag": "p"},
{"id": 13, "tag": "p"}
])
);
}
#[test]
fn test_dictcomp_body_writes_fuse() {
let doc = json!({"items": [{"name": "a"}, {"name": "b"}]});
let r = vm_query(
r#"{o.name: o.flag.set(true) | o.count.set(0) for o in $.items}"#,
&doc,
)
.unwrap();
assert_eq!(
r,
json!({
"a": {"name": "a", "flag": true, "count": 0},
"b": {"name": "b", "flag": true, "count": 0}
})
);
}
#[test]
fn test_lambda_returns_fused_value() {
let doc = json!({"xs": [{"v": 1}, {"v": 2}]});
let r = vm_query(
r#"$.xs.map(lambda o: o.a.set(10) | o.b.set(20))"#,
&doc,
)
.unwrap();
assert_eq!(
r,
json!([
{"v": 1, "a": 10, "b": 20},
{"v": 2, "a": 10, "b": 20}
])
);
}
#[test]
fn test_lambda_body_read_between_writes_no_fuse() {
let doc = json!({"xs": [{"v": 1}]});
let r = vm_query(
r#"$.xs.map(lambda o: o.a.set(o.v + 100) | o)"#,
&doc,
)
.unwrap();
let _ = r; }
#[test]
fn test_lambda_writes_against_outer_local_dont_iter_fuse() {
let doc = json!({"list": [1, 2, 3], "tag": null});
let r = vm_query(
r#"$.tag.set("outer") | $.list.map(lambda o: o + 100)"#,
&doc,
)
.unwrap();
assert_eq!(r, json!([101, 102, 103]));
}
#[test]
fn test_nested_lambdas_distinct_scopes() {
let doc = json!({"groups": [[{"v": 1}], [{"v": 2}]]});
let r = vm_query(
r#"$.groups.map(lambda g: g.map(lambda x: x.a.set(10) | x.b.set(20)))"#,
&doc,
)
.unwrap();
assert_eq!(
r,
json!([
[{"v": 1, "a": 10, "b": 20}],
[{"v": 2, "a": 10, "b": 20}]
])
);
}
#[test]
fn test_compreh_source_writes_visible_in_iter_body() {
let doc = json!({});
let r = vm_query(
r#"[x for x in ($.a.set(1) | $.b.set(2)).keys()]"#,
&doc,
)
.unwrap();
assert_eq!(r, json!(["a", "b"]));
}
#[test]
fn test_outer_pipe_around_lambda_with_dollar_reads_splits_batches() {
let doc = json!({"a": null, "list": [1, 2], "b": null});
let r = vm_query(
r#"$.a.set(1) | $.list.map(lambda x: x + 100)"#,
&doc,
)
.unwrap();
assert_eq!(r, json!([101, 102]));
}
#[test]
fn test_existing_lambda_set_pipe_form_v1_semantics() {
let doc = json!({"list": [{"id": 1}, {"id": 2}]});
let r = vm_query(
r#"$.list.map(lambda o: o.id.set(99))"#,
&doc,
)
.unwrap();
assert_eq!(r, json!([99, 99]));
}
#[test]
fn test_listcomp_single_write_keeps_v1_semantics() {
let doc = json!({"xs": [{"v": 1}, {"v": 2}]});
let r = vm_query(
r#"[o.v.set(100) for o in $.xs]"#,
&doc,
)
.unwrap();
assert_eq!(r, json!([100, 100]));
}
}