mod common;
use flow_abi::primitives::Reader;
use flow_abi::{Signature, TypeSpec};
use flow_types::{DeclId, Type};
use std::collections::{BTreeMap, BTreeSet};
use common::raw;
use flow_toolkit::{Bytes, Decimal, Duration, Instant, Int, JsonValue, Shaped};
fn int(text: &str) -> Int {
Int::parse(text).expect("an integer")
}
const RESULT: DeclId = DeclId(0);
fn binary_type(ty: Type) -> TypeSpec {
let signature = Signature {
params: vec![("value".into(), ty)],
result: Type::Unit,
generic: Vec::new(),
};
TypeSpec::from_catalog(&common::catalog(vec![common::std_result()]), &signature)
.expect("a served type")
}
fn binary<T: Shaped + PartialEq + std::fmt::Debug>(value: &T, ty: Type) -> Vec<u8> {
let spec = binary_type(ty);
let (catalog, ty) = (spec.catalog(), &spec.signature().params[0].1);
let mut bytes = Vec::new();
flow_abi::write_shaped(&mut bytes, catalog, ty, value).expect("it writes at its type");
let mut reader = Reader::new(&bytes);
flow_abi::read_value(
&mut reader,
catalog,
ty,
flow_abi::Mode::Rebuild,
&flow_abi::Limits::default(),
)
.expect("the runtime reads the bytes");
assert!(reader.is_empty(), "the bytes are one value");
let mut reader = Reader::new(&bytes);
let read: T = flow_abi::read_shaped(
&mut reader,
catalog,
ty,
flow_abi::Mode::Rebuild,
&flow_abi::Limits::default(),
)
.expect("the value read is one of the Rust type's");
assert_eq!(&read, value, "the bytes read back as the value");
bytes
}
#[test]
fn every_built_in_mapping_writes_the_runtimes_binary_encoding() {
assert_eq!(binary(&int("0"), Type::Int), [0]);
assert_eq!(binary(&int("-1"), Type::Int), [1]);
assert_eq!(binary(&int("64"), Type::Int), [0x80, 0x01]);
binary(&int("-123456789012345678901234567890123456789"), Type::Int);
assert_eq!(
binary(&Decimal::parse("1.50").unwrap(), Type::Decimal),
[30, 1]
);
binary(&Decimal::parse("0").unwrap(), Type::Decimal);
binary(&Decimal::parse("-12000").unwrap(), Type::Decimal);
assert_eq!(binary(&true, Type::Bool), [1]);
assert_eq!(binary(&"é".to_owned(), Type::Text), [2, 0xc3, 0xa9]);
assert_eq!(binary(&Bytes(vec![0, 255]), Type::Bytes), [2, 0, 255]);
assert_eq!(binary(&(), Type::Unit), <[u8; 0]>::default());
binary(&Duration::from_millis(&Int::from(-1250)), Type::Duration);
binary(
&Instant::from_unix_seconds(Decimal::parse("-0.5").unwrap()),
Type::Instant,
);
assert_eq!(
binary(&vec![int("1"), int("1")], Type::list(Type::Int)),
[2, 2, 2]
);
assert_eq!(
binary(
&BTreeSet::from(["b".to_owned(), "a".to_owned()]),
Type::set(Type::Text)
),
[2, 1, b'a', 1, b'b']
);
binary(
&BTreeMap::from([
(int("2"), "two".to_owned()),
(int("-3"), "minus".to_owned()),
]),
Type::dict(Type::Int, Type::Text),
);
binary(
&BTreeMap::from([("k".to_owned(), vec![true])]),
Type::dict(Type::Text, Type::list(Type::Bool)),
);
assert_eq!(binary(&None::<Int>, Type::option(Type::Int)), [0]);
assert_eq!(binary(&Some(int("1")), Type::option(Type::Int)), [1, 2]);
binary(&Some(Some(())), Type::option(Type::option(Type::Unit)));
assert_eq!(
binary(
&(int("1"), "x".to_owned()),
Type::Tuple(vec![Type::Int, Type::Text])
),
[2, 1, b'x']
);
binary(
&(true, int("2"), Bytes(vec![]), ()),
Type::Tuple(vec![Type::Bool, Type::Int, Type::Bytes, Type::Unit]),
);
let result = || Type::Named(RESULT, vec![Type::Int, Type::Text]);
assert_eq!(binary(&Ok::<Int, String>(int("1")), result()), [0, 2]);
assert_eq!(
binary(&Err::<Int, String>("no".into()), result()),
[1, 2, b'n', b'o']
);
binary(&Box::new(int("5")), Type::Int);
}
#[test]
fn a_json_value_is_written_as_the_ordinary_enum_std_declares() {
for text in [
"null",
"true",
"-2.50",
r#""text""#,
"[1, [], {}]",
r#"{"b": [null], "a": {"c": false}}"#,
] {
binary(&raw(text), Type::Json);
}
assert_eq!(binary(&raw("null"), Type::Json), [0]);
assert_eq!(
binary(&raw(r#"{"a": null}"#), Type::Json),
[5, 1, 1, b'a', 0]
);
}
#[test]
fn json_values_are_equal_as_flow_says_numbers_by_value_and_objects_by_member() {
assert_eq!(raw("1.0"), raw("1"));
assert_eq!(raw("-0.0"), raw("0"));
assert_eq!(raw(r#"{"b": 1, "a": [2]}"#), raw(r#"{"a": [2.0], "b": 1}"#));
assert_ne!(raw("true"), raw("1"));
assert_ne!(raw("\"e\u{301}\""), raw("\"\u{e9}\""));
}
#[test]
fn json_values_order_by_constructor_then_payload_as_flow_orders_std_s_enum() {
let JsonValue::List(items) = raw(
r#"[{"a": 10}, {"b": 1, "a": 2}, {}, [10], [9, 1], [9], [], "😀", "。", "",
10, 9.5, -1, 1.0, 1, true, false, null]"#,
) else {
panic!("a list")
};
let set: BTreeSet<JsonValue> = items.into_iter().collect();
let JsonValue::List(ordered) = raw(
r#"[null,false,true,-1,1,9.5,10,"","。","😀",[],[9],[9,1],[10],{},{"a":2,"b":1},{"a":10}]"#,
) else {
panic!("a list")
};
assert_eq!(set.into_iter().collect::<Vec<_>>(), ordered);
}
#[test]
fn json_syntax_parsing_is_exact() {
let parsed = flow_toolkit::parse_json("90071992547409931234567890").unwrap();
assert_eq!(parsed.canonical(), "90071992547409931234567890");
assert!(flow_toolkit::parse_json("[1,").is_err());
}