use links_notation::{LiNo, needs_quoting, parse_lino_to_links, quote_reference};
#[test]
fn quote_reference_writes_text_that_needs_no_delimiters_as_it_is() {
for text in ["plain", "issue#1047", "a-b_c.d", "π", "~1"] {
assert_eq!(quote_reference(text), text);
assert!(!needs_quoting(text), "{text:?}");
}
}
#[test]
fn quote_reference_quotes_text_a_parser_would_read_differently() {
assert_eq!(quote_reference("has space"), "'has space'");
assert_eq!(quote_reference("12:30"), "'12:30'");
assert_eq!(quote_reference("#release"), "'#release'");
assert_eq!(quote_reference("(a)"), "'(a)'");
assert_eq!(quote_reference("\""), "'\"'");
assert_eq!(quote_reference("'"), "\"'\"");
assert_eq!(quote_reference(""), "~1{}");
assert_eq!(quote_reference("a\nb"), "~1{610a62}");
for text in ["has space", "12:30", "#release", "", "\"", "a\nb"] {
assert!(needs_quoting(text), "{text:?}");
}
}
#[test]
fn quote_reference_is_what_the_formatter_writes() {
for text in ["a b", "a", "it's", "", "#x", "x:y"] {
let link = LiNo::Link {
id: None,
values: vec![LiNo::Ref("v".to_owned()), LiNo::Ref(text.to_owned())],
};
assert_eq!(link.to_string(), format!("(v {})", quote_reference(text)));
}
}
const TRICKY: &[&str] = &[
"",
" ",
"\"",
"'",
"`",
"\"\"",
"''",
"\"\"\"\"",
"\"'",
"'\"",
"#",
"#x",
"a #b",
":",
"a:",
":a",
"12:30",
"a: b",
"(",
")",
"~1{}",
"~1{61}",
"a\nb",
"\"\" ",
" \"\"",
"\"\n",
"it's",
"say \"hi\"",
];
const ALPHABET: &[&str] = &[
"a", "b", "1", " ", " ", "\t", "\n", "\r", "\"", "\"", "'", "'", "`", "#", ":", "(", ")", "~",
"{", "}", "é", "😀",
];
fn texts(count: usize, seed: u32) -> Vec<String> {
let mut state = seed;
let mut next = move || {
state = state.wrapping_add(0x6d2b_79f5);
let mut value = state;
value = (value ^ (value >> 15)).wrapping_mul(value | 1);
value ^= value.wrapping_add((value ^ (value >> 7)).wrapping_mul(value | 61));
f64::from(value ^ (value >> 14)) / 4_294_967_296.0
};
(0..count)
.map(|_| {
let length = (next() * 9.0) as usize;
(0..length)
.map(|_| ALPHABET[(next() * ALPHABET.len() as f64) as usize])
.collect()
})
.collect()
}
fn tree(links: &[LiNo<String>]) -> String {
links
.iter()
.map(|link| {
let (id, values) = match link {
LiNo::Ref(id) => (Some(id), &[][..]),
LiNo::Link { id, values } => (id.as_ref(), values.as_slice()),
};
let id = id.map_or_else(|| "~".to_owned(), |id| format!("{id:?}"));
if values.is_empty() {
format!("[{id}]")
} else {
format!("[{id} {}]", tree(values))
}
})
.collect::<Vec<_>>()
.join(" ")
}
fn read_back(document: &str) -> String {
tree(&parse_lino_to_links(document).unwrap_or_else(|e| panic!("{document:?}: {e}")))
}
#[test]
fn every_quoted_reference_reads_back_in_every_position() {
let seeded = texts(2000, 443_444);
for text in TRICKY
.iter()
.copied()
.chain(seeded.iter().map(String::as_str))
{
let w = quote_reference(text);
let message = format!("text {text:?} written {w:?}");
let t = format!("{text:?}");
assert_eq!(read_back(&w), format!("[~ [{t}]]"), "{message}");
let link = format!("[{t} [{t}] [\"x\"]]");
assert_eq!(read_back(&format!("({w}: {w} x)")), link, "{message}");
assert_eq!(read_back(&format!("{w}: {w} x")), link, "{message}");
assert_eq!(read_back(&format!("{w}: {w} x\n")), link, "{message}");
assert_eq!(read_back(&format!("{w}:\n {w}\n x\n")), link, "{message}");
assert_eq!(
read_back(&format!("root:\n a {w}\n b {w}\n")),
format!("[\"root\" [~ [\"a\"] [{t}]] [~ [\"b\"] [{t}]]]"),
"{message}"
);
assert_eq!(
read_back(&format!("(a ({w} {w}) b)")),
format!("[~ [\"a\"] [~ [{t}] [{t}]] [\"b\"]]"),
"{message}"
);
}
}