use flow_abi_testing::catalog as common;
use common::*;
use flow_abi::{Limits, Mode, StdForm, decode_bytes, encode_bytes};
use flow_types::{Type, normal_path};
fn path() -> Type {
named(PATH)
}
fn bytes_of(ty: &Type, value: &flow_value::Value) -> Vec<u8> {
encode_bytes(&path_catalog(), ty, value).expect("encodes")
}
fn binary_refusal(ty: &Type, bytes: &[u8]) -> (String, String) {
match decode_bytes(&path_catalog(), ty, bytes, Mode::Reply, &Limits::LANGUAGE) {
Ok(value) => panic!("{bytes:?} read as {value:?}"),
Err(error) => (error.path.to_string(), error.problem),
}
}
#[test]
fn a_path_is_encoded_as_its_normal_text_exactly_as_its_base() {
let catalog = path_catalog();
assert_eq!(enc(&catalog, &path(), &text("a/b")), "\"a/b\"");
assert_eq!(
bytes_of(&path(), &text("/a/../b")),
bytes_of(&Type::Text, &text("/a/../b"))
);
}
#[test]
fn json_decoding_reads_any_string_as_the_one_path_it_names() {
let catalog = path_catalog();
for (spelled, normal) in [
("a//b/./", "a/b"),
("", "."),
("./", "."),
("//", "/"),
("/../x/", "/../x"),
("a/b", "a/b"),
] {
assert_eq!(
reread(&catalog, &path(), &format!("\"{spelled}\"")),
format!("\"{normal}\""),
"{spelled:?}"
);
assert_eq!(normal_path(spelled), normal);
}
}
#[test]
fn a_newtype_over_a_path_decodes_to_a_path_in_its_form() {
let catalog = path_catalog();
assert_eq!(reread(&catalog, &named(SOURCE), "\"a//b/\""), "\"a/b\"");
}
#[test]
fn a_type_is_held_to_the_form_of_the_std_type_it_is_or_erases_through() {
let catalog = path_catalog();
assert_eq!(catalog.std_form(&named(PATH)), Some(StdForm::Path));
assert_eq!(catalog.std_form(&named(SOURCE)), Some(StdForm::Path));
for ty in [named(LABEL), named(OTHER_PATH), named(LISTING), Type::Text] {
assert_eq!(catalog.std_form(&ty), None, "{ty:?}");
}
assert_eq!(StdForm::Path.refusal("a/b"), None);
assert_eq!(
StdForm::Path.refusal("a//b"),
Some(r#"is a path written "a//b", which is not its normal form "a/b""#.to_owned())
);
}
#[test]
fn a_newtype_without_a_form_keeps_the_spelling_it_was_given() {
let catalog = path_catalog();
assert_eq!(reread(&catalog, &named(LABEL), "\"a//b/\""), "\"a//b/\"");
assert_eq!(
reread(&catalog, &named(OTHER_PATH), "\"a//b/\""),
"\"a//b/\""
);
}
#[test]
fn a_path_inside_a_record_list_or_dictionary_key_decodes_to_its_normal_form() {
let catalog = path_catalog();
assert_eq!(
reread(
&catalog,
&named(LISTING),
r#"{"root": "/repo/", "sizes": {"src//a.flow": 3, "./b": 4}}"#
),
r#"{"root":"/repo","sizes":{"b":4,"src/a.flow":3}}"#
);
assert_eq!(
reread(&catalog, &Type::list(path()), r#"["a/", "./b"]"#),
r#"["a","b"]"#
);
}
#[test]
fn two_spellings_of_one_path_are_two_equal_keys_and_two_equal_elements() {
let catalog = path_catalog();
let (at, problem) = refusal(
&catalog,
&Type::dict(path(), Type::Int),
r#"{"a/b": 1, "a//b/": 2}"#,
);
assert_eq!((at.as_str(), problem.as_str()), ("", "has two equal keys"));
assert_eq!(
refusal(&catalog, &Type::set(path()), r#"["a", "a/"]"#),
(String::new(), "has two equal elements".to_owned())
);
}
#[test]
fn a_path_refuses_what_text_refuses() {
let catalog = path_catalog();
assert_eq!(
refusal(&catalog, &path(), "1"),
refusal(&catalog, &Type::Text, "1")
);
}
#[test]
fn the_binary_encoding_of_a_path_is_its_normal_text_and_nothing_else() {
let catalog = path_catalog();
let normal = bytes_of(&Type::Text, &text("a/b"));
let read = decode_bytes(&catalog, &path(), &normal, Mode::Reply, &Limits::LANGUAGE)
.expect("the normal text reads");
assert_eq!(text_of(&read), "a/b");
let spelled = bytes_of(&Type::Text, &text("a//b"));
assert_eq!(
binary_refusal(&path(), &spelled),
(
String::new(),
"is a path written \"a//b\", which is not its normal form \"a/b\"".to_owned()
)
);
for ty in [Type::Text, named(LABEL)] {
let read = decode_bytes(&catalog, &ty, &spelled, Mode::Reply, &Limits::LANGUAGE)
.expect("any text reads");
assert_eq!(text_of(&read), "a//b");
}
}
#[test]
fn a_reply_that_holds_a_path_out_of_its_form_anywhere_does_not_decode() {
let catalog = path_catalog();
let listing = rec(&catalog, LISTING, vec![text("/repo/"), dict(Vec::new())]);
let refused = encode_bytes(&catalog, &named(LISTING), &listing).expect_err("out of its form");
assert_eq!(refused.path, "root");
let bytes = [
bytes_of(&Type::Text, &text("/repo/")),
bytes_of(&Type::dict(Type::Text, Type::Int), &dict(Vec::new())),
]
.concat();
assert_eq!(binary_refusal(&named(LISTING), &bytes).0, "root");
let keyed = dict(vec![(text(""), int("1"))]);
let refused = encode_bytes(&catalog, &Type::dict(path(), Type::Int), &keyed)
.expect_err("out of its form");
assert_eq!(
refused.problem,
"is a path written \"\", which is not its normal form \".\""
);
let bytes = bytes_of(&Type::dict(Type::Text, Type::Int), &keyed);
assert_eq!(
binary_refusal(&Type::dict(path(), Type::Int), &bytes).1,
"is a path written \"\", which is not its normal form \".\""
);
let bytes = bytes_of(&Type::Text, &text("./a"));
assert!(
binary_refusal(&named(SOURCE), &bytes)
.1
.contains("normal form \"a\"")
);
}