use crate::run;
use arcstr::ArcStr;
use anyhow::{bail, Result};
use netidx::publisher::Value;
const TUPLES0: &str = r#"
{
let t: (string, Number, Number) = ("foo", 42, 23.5);
t
}
"#;
run!(tuples0, TUPLES0, |v: Result<&Value>| match v {
Ok(Value::Array(a)) => match &a[..] {
[Value::String(s), Value::I64(42), Value::F64(23.5)] => &*s == "foo",
_ => false,
},
_ => false,
});
const TUPLES1: &str = r#"
{
let t: (string, Number, Number) = ("foo", 42, 23.5);
let (_, y, z) = t;
y + z
}
"#;
run!(tuples1, TUPLES1, |v: Result<&Value>| match v {
Ok(Value::F64(65.5)) => true,
_ => false,
});
const TUPLES2: &str = r#"
{
let t = ("foo", 42, 23.5);
select t {
("foo", x, y) => x + y,
_ => never()
}
}
"#;
run!(tuples2, TUPLES2, |v: Result<&Value>| match v {
Ok(Value::F64(65.5)) => true,
_ => false,
});
const TUPLEACCESSOR: &str = r#"
{
let x = ( "bar", 42, 84.0 );
x.1
}
"#;
run!(tupleaccessor, TUPLEACCESSOR, |v: Result<&Value>| match v {
Ok(Value::I64(42)) => true,
_ => false,
});
const STRUCTS0: &str = r#"
{
let x = { foo: "bar", bar: 42, baz: 84.0 };
x
}
"#;
run!(structs0, STRUCTS0, |v: Result<&Value>| match v {
Ok(Value::Array(a)) if a.len() == 3 => match &a[..] {
[Value::Array(f0), Value::Array(f1), Value::Array(f2)]
if f0.len() == 2 && f1.len() == 2 && f2.len() == 2 =>
{
let f0 = match &f0[..] {
[Value::String(n), Value::I64(42)] if n == "bar" => true,
_ => false,
};
let f1 = match &f1[..] {
[Value::String(n), Value::F64(84.0)] if n == "baz" => true,
_ => false,
};
let f2 = match &f2[..] {
[Value::String(n), Value::String(s)] if n == "foo" && s == "bar" => true,
_ => false,
};
f0 && f1 && f2
}
_ => false,
},
_ => false,
});
const BINDSTRUCT: &str = r#"
{
let x = { foo: "bar", bar: 42, baz: 84.0 };
let { foo: _, bar, baz } = x;
bar + baz
}
"#;
run!(bindstruct, BINDSTRUCT, |v: Result<&Value>| match v {
Ok(Value::F64(126.0)) => true,
_ => false,
});
const STRUCTACCESSOR: &str = r#"
{
let x = { foo: "bar", bar: 42, baz: 84.0 };
x.foo
}
"#;
run!(structaccessor, STRUCTACCESSOR, |v: Result<&Value>| match v {
Ok(Value::String(s)) => s == "bar",
_ => false,
});
const STRUCTWITH0: &str = r#"
{
let x = { foo: "bar", bar: 42, baz: 84.0 };
let x = { x with foo: 1 };
x.foo
}
"#;
run!(structwith0, STRUCTWITH0, |v: Result<&Value>| match v {
Err(_) => true,
_ => false,
});
const STRUCTWITH1: &str = r#"
{
let x = { foo: "bar", bar: 42, baz: 84.0 };
let x = { x with bar: 1 };
x.bar + x.baz
}
"#;
run!(structwith1, STRUCTWITH1, |v: Result<&Value>| match v {
Ok(Value::F64(85.0)) => true,
_ => false,
});
const STRUCTWITH2: &str = r#"
{
let selected = { x: 0, y: 0 };
let y = 1;
{ selected with y }
}
"#;
run!(structwith2, STRUCTWITH2, |v: Result<&Value>| match v {
Ok(v) => match v.clone().cast_to::<[(ArcStr, i64); 2]>() {
Ok([(s0, 0), (s1, 1)]) if &*s0 == "x" && &*s1 == "y" => true,
_ => false,
},
_ => false,
});
const STRUCTWITH3: &str = r#"
{
let selected = { x: 0, y: 0 };
{ selected with y: selected.y + 1 }
}
"#;
run!(structwith3, STRUCTWITH3, |v: Result<&Value>| match v {
Ok(v) => match v.clone().cast_to::<[(ArcStr, i64); 2]>() {
Ok([(s0, 0), (s1, 1)]) if &*s0 == "x" && &*s1 == "y" => true,
_ => false,
},
_ => false,
});
const STRUCTWITH4: &str = r#"
{
let selected = { x: 0, y: 0 };
let handle = |e: [`Up, `Down, `Left, `Right]| -> `Stop select e {
e@ `Left => {
selected <- e ~ { selected with x: selected.x - 1 };
`Stop
},
e@ `Right => {
selected <- e ~ { selected with x: selected.x + 1 };
`Stop
},
e@ `Down => {
selected <- e ~ { selected with y: selected.y + 1 };
`Stop
},
e@ `Up => {
selected <- e ~ { selected with y: selected.y - 1 };
`Stop
}
};
handle(array::iter([`Up, `Down, `Left, `Right]));
(array::group(selected, |n, _| n == 5))[1..]
}
"#;
run!(structwith4, STRUCTWITH4, |v: Result<&Value>| match v {
Ok(v) => match v.clone().cast_to::<[[(ArcStr, i64); 2]; 4]>() {
Ok(
[[(f00, 0), (f01, -1)], [(f10, 0), (f11, 0)], [(f20, -1), (f21, 0)], [(f30, 0), (f31, 0)]],
) if f00 == "x"
&& f01 == "y"
&& f10 == f00
&& f20 == f00
&& f30 == f00
&& f11 == f01
&& f21 == f01
&& f31 == f01 =>
true,
_ => false,
},
_ => false,
});
const STRUCTWITH5: &str = r#"
{
let selected = { x: 0, y: 0 };
let handle = |e: [`Up]| -> `Stop select e {
e@ `Up => {
selected <- e ~ { selected with y: selected.y - 1 };
`Stop
}
};
handle(array::iter([`Up]));
(array::group(selected, |n, _| n == 2))[1..]
}
"#;
run!(structwith5, STRUCTWITH5, |v: Result<&Value>| match v {
Ok(v) => match v.clone().cast_to::<[[(ArcStr, i64); 2]; 1]>() {
Ok([[(f00, 0), (f01, -1)]]) if f00 == "x" && f01 == "y" => true,
_ => false,
},
_ => false,
});