use std::path::{Path, PathBuf};
fn fixture(tag: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("quarb-textrec-{}-{tag}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
dir.join("raven.md"),
"---\ntitle: The Raven\nauthor: Edgar Allan Poe\nlang: en\n---\n\n\
# Midnight {#sec:midnight}\n\n\
Once upon a midnight dreary. While I pondered, weak and weary.\n\n\
## Chamber\n\n\
See [the door](#sec:midnight).\n",
)
.unwrap();
dir
}
fn ok(dir: &Path, query: &str, target: &str) -> String {
let out = std::process::Command::new(env!("CARGO_BIN_EXE_qua"))
.env("QUARB_LOCALE", "C")
.current_dir(dir)
.args([query, target])
.stdin(std::process::Stdio::null())
.output()
.unwrap();
let err = String::from_utf8_lossy(&out.stderr).trim().to_string();
assert!(out.status.success(), "{query}: {err}");
assert_eq!(err, "", "{query}");
String::from_utf8_lossy(&out.stdout)
.trim()
.replace('\n', " ")
}
#[test]
fn a_text_nodes_record_lists_what_it_answers() {
let d = fixture("record");
let text = |q: &str| ok(&d, q, "text:raven.md");
assert_eq!(
text("^ | %"),
r#"%(lemma = "The Raven"; lang = "en"; author = "Edgar Allan Poe")"#
);
assert_eq!(
text("//section | %"),
r#"%(lemma = "Midnight"; onym = "sec:midnight") %(lemma = "Chamber"; onym = "chamber")"#
);
assert_eq!(text("//paragraph[1] | %"), "%()");
assert_eq!(text("//ref[1] | %"), r##"%(target = "#sec:midnight")"##);
assert_eq!(
text("//section[1] | .me | %($$.me)"),
r#"%(lemma = "Midnight"; onym = "sec:midnight")"#
);
assert_eq!(text("//section[1] | :props"), "@(::lemma; ::onym)");
assert_eq!(
text("//section[1] | %(::lemma; ::title)"),
r#"%(lemma = "Midnight"; title = "Midnight")"#
);
}
#[test]
fn an_entry_lists_its_campi_under_their_keys() {
let d = fixture("entry");
std::fs::write(
d.join("refs.md"),
"# Sources\n\nThe frame is Austen [1]; the war is Homer [2].\n\n## References\n\n\
1. Austen, Jane. Emma. London: John Murray, 1815.\n\
2. Homer. The Iliad. Translated by Samuel Butler. London: Longmans, 1898.\n",
)
.unwrap();
let entry = |q: &str| ok(&d, q, "text:refs.md?references=cmos");
assert_eq!(
entry("//bib[2] | %"),
r#"%(onym = "2"; auctor = "Homer"; titulus = "The Iliad"; locus = "London"; officina = "Longmans"; annus = "1898")"#
);
assert_eq!(
entry("//bib[2] | :props"),
"@(::onym; ::auctor; ::titulus; ::locus; ::officina; ::annus)"
);
assert_eq!(
entry("//bib[1] | %(::author; ::auctor)"),
r#"%(author = "Austen, Jane"; auctor = "Austen, Jane")"#
);
}
#[test]
fn the_corpus_readings_nodes_list_their_own() {
let d = fixture("corpus");
let corpus = |q: &str| ok(&d, q, "corpus:raven.md");
assert_eq!(corpus("//sentence[2] | %"), "%(taxis = 2)");
assert_eq!(
corpus("//token[1] | %"),
r#"%(lower = "midnight"; modern = "Midnight"; class = "word")"#
);
}