use anyhow::Result;
use link_cli::{import_lino_text, NamedTypeLinks};
#[cfg(not(target_arch = "wasm32"))]
#[path = "common/storage.rs"]
mod test_store;
#[cfg(target_arch = "wasm32")]
use crate::test_store;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn invalid_import_leaves_links_and_names_unchanged() -> Result<()> {
for populated in [false, true] {
for invalid in [
"(c: c",
"(c: c)",
"(c: c c c)",
"(x y)",
"(c: (nested: a b) (bad: a))",
"(c: a ())",
] {
let (_file, mut storage) = test_store::links()?;
if populated {
import_lino_text(&mut storage, "(a: a a)\n(b: b b)\n(existing: a b)")?;
}
let before = storage.lino_lines();
assert!(
import_lino_text(
&mut storage,
&format!("(a: b a)\n(new: fresh fresh)\n{invalid}")
)
.is_err(),
"accepted {invalid}"
);
assert_eq!(storage.lino_lines(), before, "input: {invalid}");
assert_eq!(storage.get_by_name("new"), None);
assert_eq!(storage.get_by_name("fresh"), None);
}
}
Ok(())
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn import_lino_text_accepts_named_and_anonymous_nested_links() -> Result<()> {
let (_file, mut storage) = test_store::links()?;
import_lino_text(
&mut storage,
"(a: a a)\n(b: b b)\n(child: (nested: a b) (b a))",
)?;
let nested = storage.get_by_name("nested").unwrap();
let child = storage
.get_link(storage.get_by_name("child").unwrap())
.unwrap();
assert_eq!(child.source, nested);
let target = storage.get_link(child.target).unwrap();
assert_eq!(target.source, storage.get_by_name("b").unwrap());
assert_eq!(target.target, storage.get_by_name("a").unwrap());
Ok(())
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn import_lino_text_reproduces_numbered_links_at_explicit_indexes() -> Result<()> {
let (_temp_file, mut storage) = test_store::links()?;
import_lino_text(
&mut storage,
r#"
(1: 1 1)
(2: 1 2)
(3: 2 1)
"#,
)?;
assert_eq!(
storage.lino_lines(),
vec!["(1: 1 1)", "(2: 1 2)", "(3: 2 1)"]
);
Ok(())
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn import_lino_text_creates_named_references_as_point_links() -> Result<()> {
let (_temp_file, mut storage) = test_store::links()?;
import_lino_text(&mut storage, "(child: father mother)")?;
let father = storage.get_by_name("father").expect("father should exist");
let mother = storage.get_by_name("mother").expect("mother should exist");
let child = storage.get_by_name("child").expect("child should exist");
let father_link = storage.get_link(father).expect("father link should exist");
let mother_link = storage.get_link(mother).expect("mother link should exist");
let child_link = storage.get_link(child).expect("child link should exist");
assert_eq!(father_link.source, father);
assert_eq!(father_link.target, father);
assert_eq!(mother_link.source, mother);
assert_eq!(mother_link.target, mother);
assert_eq!(child_link.source, father);
assert_eq!(child_link.target, mother);
let lines = storage.lino_lines();
assert!(lines.contains(&"(father: father father)".to_string()));
assert!(lines.contains(&"(mother: mother mother)".to_string()));
assert!(lines.contains(&"(child: father mother)".to_string()));
Ok(())
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn import_lino_text_treats_out_of_range_numbers_as_names() -> Result<()> {
let (_temp_file, mut storage) = test_store::links()?;
let out_of_range_number = u64::from(u32::MAX) + 1;
import_lino_text(&mut storage, &format!("(child: {out_of_range_number} 2)"))?;
let numeric_name = storage
.get_by_name(&out_of_range_number.to_string())
.expect("out-of-range number should be named");
let child = storage.get_by_name("child").expect("child should exist");
let numeric_name_link = storage
.get_link(numeric_name)
.expect("named number link should exist");
let child_link = storage.get_link(child).expect("child link should exist");
assert_eq!(numeric_name_link.source, numeric_name);
assert_eq!(numeric_name_link.target, numeric_name);
assert_eq!(child_link.source, numeric_name);
Ok(())
}