use flow_abi::description::Term;
use flow_abi::wire::{Declared, Wire};
use flow_abi::{Catalog, write_shaped};
use flow_value::Int;
const DEPTH: usize = 100_000;
#[test]
fn a_term_nested_a_hundred_thousand_deep_reads_and_drops_on_a_small_stack() {
let mut bytes = vec![10; DEPTH];
bytes.push(0);
flow_testing::on_a_small_stack(|| {
let term = Term::decode(&bytes).expect("a term of any depth reads");
let mut depth = 0;
let mut at = &term;
while let Term::List(item) = at {
depth += 1;
at = item;
}
assert_eq!((depth, matches!(at, Term::Int)), (DEPTH, true));
});
}
mod one {
flow_abi::wire! {
#[derive(Debug, PartialEq)]
pub struct Field {
pub n: flow_value::Int,
}
}
}
mod two {
flow_abi::wire! {
#[derive(Debug, PartialEq)]
pub struct Field {
pub on: bool,
pub name: String,
}
}
}
flow_abi::wire! {
#[derive(Debug, PartialEq)]
pub struct Both {
pub first: one::Field,
pub second: two::Field,
pub third: Shapes,
}
}
flow_abi::wire! {
#[derive(Debug, PartialEq)]
pub enum Shapes {
One(bool),
Two(bool, String),
Three(String, bool, one::Field),
}
}
fn catalog() -> Catalog {
Catalog::new(Vec::new(), flow_abi_testing::catalog::option_shape(), None).expect("std's Option")
}
#[test]
fn two_declared_types_of_one_name_are_two_declarations() {
let (catalog, ty) = Both::shape().project(&catalog()).expect("a Flow type");
let fields: Vec<_> = catalog
.decls()
.filter(|(_, decl)| decl.identity.name == "Field")
.map(|(id, decl)| (id, decl.lexical_ordinal))
.collect();
assert_eq!(fields.len(), 2, "{fields:?}");
assert_ne!(fields[0].0, fields[1].0);
assert_eq!([fields[0].1, fields[1].1], [1, 2]);
let both = Both {
first: one::Field { n: Int::from(7) },
second: two::Field {
on: true,
name: "x".to_owned(),
},
third: Shapes::Three("y".to_owned(), false, one::Field { n: Int::from(-1) }),
};
let mut value = Vec::new();
write_shaped(&mut value, &catalog, &ty, &both).expect("its bytes at its type");
assert_eq!(value, both.bytes());
assert_eq!(Both::decode(&both.bytes()), Ok(both));
}
#[test]
fn a_constructor_takes_any_number_of_parts_by_position() {
for shapes in [
Shapes::One(true),
Shapes::Two(false, "a".to_owned()),
Shapes::Three("b".to_owned(), true, one::Field { n: Int::from(3) }),
] {
let (catalog, ty) = Shapes::shape().project(&catalog()).expect("a Flow type");
let mut value = Vec::new();
write_shaped(&mut value, &catalog, &ty, &shapes).expect("its bytes at its type");
assert_eq!(value, shapes.bytes());
assert_eq!(Shapes::decode(&shapes.bytes()), Ok(shapes));
}
}
flow_abi::wire! {
#[derive(Debug, PartialEq)]
pub struct Empty {}
}
#[test]
fn a_stream_of_a_message_that_reads_no_bytes_ends_where_it_stands() {
let (values, rest) = flow_abi::wire::stream::<Empty>(&[1, 2]);
assert_eq!((values.len(), rest), (0, Some(0)));
}