use std::path::PathBuf;
fn qua(args: &[&str]) -> String {
let out = std::process::Command::new(env!("CARGO_BIN_EXE_qua"))
.env("QUARB_LOCALE", "C")
.args(args)
.stdin(std::process::Stdio::null())
.output()
.unwrap();
assert!(
out.status.success(),
"qua {args:?}: {}",
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8(out.stdout)
.unwrap()
.trim_end()
.to_string()
}
fn editions(name: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("quarb-declared-{}-{name}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let write = |file: &str, text: &str| std::fs::write(dir.join(file), text).unwrap();
write(
"caesar.md",
"---\ntitle: De Bello Gallico\n---\n\n# Liber primus\n\n\
Gallia est omnis divisa in partes tres.\n",
);
write(
"caesar.html",
"<html><head><title>De Bello Gallico</title></head><body>\
<h1>Liber primus</h1><p>Gallia est omnis divisa in partes tres.</p>\
</body></html>",
);
write(
"caesar.tei.xml",
"<?xml version=\"1.0\"?>\n<TEI xmlns=\"http://www.tei-c.org/ns/1.0\"><teiHeader>\
<fileDesc><titleStmt><title>De Bello Gallico</title></titleStmt>\
<publicationStmt><p>x</p></publicationStmt><sourceDesc><p>x</p></sourceDesc>\
</fileDesc></teiHeader><text><body><div><head>Liber primus</head>\
<p>Gallia est omnis divisa in partes tres.</p></div></body></text></TEI>",
);
write(
"caesar.tex",
"\\documentclass{article}\n\\title{De Bello Gallico}\n\\begin{document}\n\
\\maketitle\n\\section{Liber primus}\nGallia est omnis divisa in partes tres.\n\
\\end{document}\n",
);
write(
"caesar.rtf",
"{\\rtf1{\\info{\\title De Bello Gallico}}\\pard Gallia est omnis divisa \
in partes tres.\\par}",
);
dir
}
#[test]
fn a_declared_title_leaves_no_sentence_and_no_token() {
let dir = editions("counts");
for (file, sentences, words, first) in [
("caesar.md", "2", "9", "Liber primus"),
("caesar.html", "2", "9", "Liber primus"),
("caesar.tei.xml", "2", "9", "Liber primus"),
("caesar.tex", "2", "9", "Liber primus"),
(
"caesar.rtf",
"1",
"7",
"Gallia est omnis divisa in partes tres.",
),
] {
let target = format!("corpus:{}", dir.join(file).display());
assert_eq!(qua(&["//sentence @| count", &target]), sentences, "{file}");
assert_eq!(qua(&["//token<word> @| count", &target]), words, "{file}");
assert_eq!(qua(&["//sentence[1]::", &target]), first, "{file}");
assert_eq!(qua(&["/sentence @| count", &target]), "0", "{file}");
assert_eq!(
qua(&["//token[::lower = \"bello\"] @| count", &target]),
"0",
"{file}"
);
}
}
#[test]
fn the_title_is_still_what_the_document_declares() {
let dir = editions("identity");
for file in [
"caesar.md",
"caesar.html",
"caesar.tei.xml",
"caesar.tex",
"caesar.rtf",
] {
for reading in ["text", "corpus"] {
let target = format!("{reading}:{}", dir.join(file).display());
assert_eq!(
qua(&["^ | %(::title)", &target]),
r#"%(title = "De Bello Gallico")"#,
"{reading}:{file}"
);
}
}
}
#[test]
fn a_title_printed_in_the_body_is_counted_once() {
let dir = editions("printed");
let file = dir.join("printed.md");
std::fs::write(
&file,
"---\ntitle: De Bello Gallico\n---\n\n# De Bello Gallico\n\n\
Gallia est omnis divisa in partes tres.\n",
)
.unwrap();
let target = format!("corpus:{}", file.display());
assert_eq!(qua(&["//sentence @| count", &target]), "2");
assert_eq!(qua(&["//token<word> @| count", &target]), "10");
assert_eq!(
qua(&["//token[::lower = \"bello\"] @| count", &target]),
"1"
);
assert_eq!(qua(&["//sentence[1]::", &target]), "De Bello Gallico");
let bare = dir.join("bare.md");
std::fs::write(
&bare,
"# Liber primus\n\nGallia est omnis divisa in partes tres.\n",
)
.unwrap();
let target = format!("corpus:{}", bare.display());
assert_eq!(qua(&["//sentence @| count", &target]), "2");
assert_eq!(qua(&["//token<word> @| count", &target]), "9");
}