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()
}
#[test]
fn a_folder_of_documents_reads_at_the_text_level() {
let dir = std::env::temp_dir().join(format!("quarb-manuscript-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("part2")).unwrap();
std::fs::write(
dir.join("01-one.md"),
"# One\n\nTom *painted* the fence.\n\n## Aside\n\nBen watched.\n",
)
.unwrap();
std::fs::write(
dir.join("02-two.md"),
"# Two\n\nHuck came by. He had a dead cat.\n",
)
.unwrap();
std::fs::write(
dir.join("part2/03-three.md"),
"# Three\n\nAunt Polly sighed.\n",
)
.unwrap();
let text = format!("text:{}", dir.display());
assert_eq!(qua(&["/* | :name", &text]), "01-one.md\n02-two.md\npart2\n");
assert_eq!(
qua(&["//section::lemma", &text]),
"One\nAside\nTwo\nThree\n"
);
assert_eq!(
qua(&[
r#"//*[:name == *".md"] | "${:name} ${(//paragraph | :: | wc @| sum)}""#,
&text
]),
"01-one.md 6\n02-two.md 8\n03-three.md 3\n"
);
assert_eq!(
qua(&[
"/ms//section @| count",
&format!("ms={text}"),
&format!("again={text}")
]),
"4\n"
);
let corpus = format!("corpus:{}", dir.display());
assert_eq!(qua(&["//sentence @| count", &corpus]), "9\n");
assert_eq!(
qua(&[
r#"//*[:name == *".md"] | "${:name} ${(//token<word> @| count)}""#,
&corpus
]),
"01-one.md 8\n02-two.md 9\n03-three.md 4\n"
);
assert_eq!(
qua(&[r#"//token[::lower = "tom"] @| count"#, &corpus]),
"1\n"
);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_folder_of_editions_reads_at_the_literary_level() {
let dir = std::env::temp_dir().join(format!("quarb-editions-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let play =
"@@@!litogramma\n\n@#() A Play\n\n@: Tom\nHello there.\n:@\n\n@: Huck\nHello.\n:@\n\n#@\n";
std::fs::write(dir.join("play.atd"), play).unwrap();
std::fs::write(dir.join("notes.csv"), "a,b\n1,2\n").unwrap();
let lit = format!("lit:{}", dir.display());
assert_eq!(qua(&["//speech @| count", &lit]), "2\n");
assert_eq!(qua(&["/* | :name", &lit]), "notes.csv\nplay.atd\n");
let text = format!("text:{}", dir.display());
assert_eq!(qua(&["//token @| count", &text]), "0\n");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_step_that_asks_for_dialogue_is_told_the_speech() {
let dir = std::env::temp_dir().join(format!("quarb-old-name-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("editions")).unwrap();
let play =
"@@@!litogramma\n\n@#() A Play\n\n@: Tom\nHello there.\n:@\n\n@: Huck\nHello.\n:@\n\n#@\n";
std::fs::write(dir.join("editions/play.atd"), play).unwrap();
std::fs::write(dir.join("books.json"), r#"{"books": [{"title": "Emma"}]}"#).unwrap();
const NOTE: &str = "warning KIND_RENAMED (//dialogue): no node of a text reading is named \
'dialogue': the drama speech is 'speech' since 0.75. Spell it another \
way: qua man KIND_RENAMED";
let file = dir.join("editions/play.atd");
let said = |query: &str, sources: &[&str]| {
let mut args = vec![query];
args.extend(sources);
let (ok, out, err) = qua_full(&args);
assert!(ok, "{query} {sources:?}: {err}");
(out.trim().to_string(), err.trim().to_string())
};
for source in [
format!("lit:{}", file.display()),
format!("text:{}", file.display()),
format!("corpus:{}", file.display()),
format!("lit:{}", dir.join("editions").display()),
] {
assert_eq!(
said("//dialogue @| count", &[&source]),
("0".to_string(), NOTE.to_string()),
"{source}"
);
}
let lit = format!("lit:{}", file.display());
for query in [
"//speech[//dialogue] @| count",
"//speech @| count | (^//dialogue @| count)",
"//dialogue | (^//dialogue @| count)",
] {
let (_, err) = said(query, &[&lit]);
assert_eq!(err, NOTE, "{query}");
}
for (query, answer) in [
("//speech @| count", "2"),
(r#"//speech[:name = "dialogue"] @| count"#, "0"),
("//paragraf @| count", "0"),
] {
assert_eq!(
said(query, &[&lit]),
(answer.to_string(), String::new()),
"{query}"
);
}
let json = dir.join("books.json").display().to_string();
let play = format!("play={lit}");
let books = format!("books={json}");
for (query, sources) in [
("//dialogue @| count", &[play.as_str()][..]),
("/play//dialogue @| count", &[play.as_str()][..]),
("//dialogue @| count", &[play.as_str(), books.as_str()][..]),
(
"/play//dialogue @| count",
&[books.as_str(), play.as_str()][..],
),
] {
assert_eq!(
said(query, sources),
("0".to_string(), NOTE.to_string()),
"{query} {sources:?}"
);
}
std::fs::write(
dir.join("talk.json"),
r#"{"scenes": [{"dialogue": "a line"}, {"dialogue": "another"}]}"#,
)
.unwrap();
let talk = format!("talk={}", dir.join("talk.json").display());
for (query, answer, told) in [
("//dialogue @| count", "2", ""),
("/talk//dialogue @| count", "2", ""),
("/talk/scenes/*/dialogue::", "a line\nanother", ""),
("/play//dialogue @| count", "0", NOTE),
("/talk//dialogue/dialogue @| count", "0", NOTE),
("/talk/scenes/*[/dialogue] @| count", "2", NOTE),
] {
assert_eq!(
said(query, &[&play, &talk]),
(answer.to_string(), told.to_string()),
"{query}"
);
}
assert_eq!(
said("//dialogue @| count", &[&talk]),
("2".to_string(), String::new())
);
assert_eq!(
said("//dialogue @| count", &[&json]),
("0".to_string(), String::new())
);
assert_eq!(
said("//dialogue @| count", &[&books, &format!("more={json}")]),
("0".to_string(), String::new())
);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_step_that_asks_for_verse_strophe_or_stichos_is_told_the_names_they_bear() {
let dir = std::env::temp_dir().join(format!("quarb-verse-names-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let ode = "@@@!koine\n\n@@@=\nHappy the man, whose wish and care\nA few paternal acres \
bound,\n\nContent to breathe his native air\nIn his own ground.\n=@@@\n";
let file = dir.join("ode.atd");
std::fs::write(&file, ode).unwrap();
const POETRY: &str = "warning KIND_RENAMED (//verse): no node of a text reading is named \
'verse': the block of verse is 'poetry' since 0.75. Spell it another \
way: qua man KIND_RENAMED";
const STANZA: &str = "warning KIND_RENAMED (//strophe): no node of a text reading is named \
'strophe': the stanza of a poetry block is 'stanza' since 0.75. Spell \
it another way: qua man KIND_RENAMED";
const LINE: &str = "warning KIND_RENAMED (//stichos): no node of a text reading is named \
'stichos': the line of a poetry block is 'line' since 0.75. Spell it \
another way: qua man KIND_RENAMED";
let said = |query: &str, source: &str| {
let (ok, out, err) = qua_full(&[query, source]);
assert!(ok, "{query} {source}: {err}");
(out.trim().replace('\n', " "), err.trim().to_string())
};
let quiet = |answer: &str| (answer.to_string(), String::new());
for reading in ["lit", "text", "koine", "corpus"] {
let source = format!("{reading}:{}", file.display());
for (query, answer) in [
("//poetry @| count", "1"),
("//stanza @| count", "2"),
("//poetry/stanza/line @| count", "4"),
("//stanza[2]/line[1]::taxis", "3"),
("//line[::taxis = 4]::", "In his own ground."),
] {
assert_eq!(said(query, &source), quiet(answer), "{query} {source}");
}
assert_eq!(
said("//verse @| count", &source),
("0".to_string(), POETRY.to_string()),
"{source}"
);
assert_eq!(
said("//strophe @| count", &source),
("0".to_string(), STANZA.to_string()),
"{source}"
);
assert_eq!(
said("//stichos @| count", &source),
("0".to_string(), LINE.to_string()),
"{source}"
);
let (answer, err) = said("//strophe/stichos @| count", &source);
assert_eq!(answer, "0", "{source}");
let mut told: Vec<&str> = err.lines().collect();
told.sort_unstable();
assert_eq!(told, [LINE, STANZA], "{source}");
}
std::fs::create_dir_all(dir.join("shelf/verse")).unwrap();
std::fs::write(
dir.join("shelf/verse/iliad.txt"),
"Sing, goddess, the wrath of Achilles.\n\nMany souls it sent to Hades.\n",
)
.unwrap();
std::fs::write(
dir.join("mark.json"),
r#"{"book": "Mark", "verse": [{"n": 1}, {"n": 2}]}"#,
)
.unwrap();
let shelf = format!("corpus:{}", dir.join("shelf").display());
let ode = format!("ode=lit:{}", file.display());
let data = format!("data={}", dir.join("mark.json").display());
for (query, sources, answer, told) in [
("/verse//paragraph @| count", &[shelf.as_str()][..], "2", ""),
("//verse @| count", &[shelf.as_str()][..], "1", ""),
("/verse//verse @| count", &[shelf.as_str()][..], "0", POETRY),
(
"/data//verse @| count",
&[ode.as_str(), data.as_str()][..],
"1",
"",
),
(
"//verse @| count",
&[ode.as_str(), data.as_str()][..],
"1",
"",
),
(
"/ode//verse @| count",
&[ode.as_str(), data.as_str()][..],
"0",
POETRY,
),
] {
let mut args = vec!["--fail-on-warning", query];
args.extend(sources);
let (ok, out, err) = qua_full(&args);
assert_eq!(
(ok, out.trim(), err.trim()),
(told.is_empty(), answer, told),
"{query} {sources:?}"
);
}
let bare = file.display().to_string();
for (query, answer) in [
("//stichoi/strophe/stichos @| count", "4"),
("//strophe @| count", "2"),
("//stichos[4]::", "In his own ground."),
] {
assert_eq!(said(query, &bare), quiet(answer), "{query}");
}
}
#[test]
fn a_step_that_filters_by_dangling_is_told_the_undefined_reference() {
let dir = std::env::temp_dir().join(format!("quarb-undef-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("wrath.html");
std::fs::write(
&file,
"<html><body>\n\
<p id=\"one\">Sing, goddess<a role=\"doc-noteref\" href=\"#fn9\">9</a>, the wrath.</p>\n\
<p>See <a href=\"#one\">above</a> and <a href=\"#nowhere\">elsewhere</a>.</p>\n\
</body></html>\n",
)
.unwrap();
const TOLD: &str = "warning KIND_RENAMED (//*<dangling>): no node of a text reading wears \
'dangling': an undefined reference wears 'undef-ref' since 0.75. Spell \
it another way: qua man KIND_RENAMED";
let said = |query: &str, source: &str| {
let (ok, out, err) = qua_full(&[query, source]);
assert!(ok, "{query} {source}: {err}");
(out.trim().replace('\n', " "), err.trim().to_string())
};
let quiet = |answer: &str| (answer.to_string(), String::new());
let text = format!("text:{}", file.display());
for (query, answer) in [
("//*<undef-noteref> | :name", "paragraph"),
("//*<undef-deixis> @| count", "1"),
("//footnote @| count", "0"),
("//ref<undef-ref>::target", "#nowhere"),
("//ref<!undef-ref>::target", "#one"),
("//*<undef-ref> @| count", "2"),
("//*<noteref> @| count", "0"),
] {
assert_eq!(said(query, &text), quiet(answer), "{query}");
}
assert_eq!(
said("//*<dangling> @| count", &text),
("0".to_string(), TOLD.to_string())
);
assert_eq!(
said("//ref<!dangling> @| count", &text),
("2".to_string(), TOLD.to_string())
);
let corpus = format!("corpus:{}", file.display());
assert_eq!(said("//token<undef-noteref>::", &corpus), quiet("goddess"));
let atd = dir.join("wrath.atd");
std::fs::write(&atd, "@@@!koine\n\nSing, goddess@^(n9), the wrath.\n").unwrap();
let (_, err) = said("//*<dangling> @| count", &atd.display().to_string());
assert_eq!(err, "");
}
#[test]
fn notes_mount_in_the_tree_by_the_option() {
let dir = std::env::temp_dir().join(format!("quarb-notes-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let md = dir.join("wrath.md");
std::fs::write(
&md,
"# The Wrath\n\nSing, goddess[^1], the wrath of Achilles.[^2]\n\n\
[^1]: The Muse is meant.\n[^2]: Its cause is told later.\n",
)
.unwrap();
let tei = dir.join("wrath.xml");
std::fs::write(
&tei,
"<TEI xmlns=\"http://www.tei-c.org/ns/1.0\"><text><body><div><head>The Wrath</head>\
<p>Sing, goddess<note place=\"foot\">The Muse is meant.</note>, the wrath of \
Achilles.</p></div></body></text></TEI>\n",
)
.unwrap();
let said = |query: &str, source: &str| {
let (ok, out, err) = qua_full(&[query, source]);
assert!(ok, "{query} {source}: {err}");
assert_eq!(err.trim(), "", "{query} {source}");
out.trim().replace('\n', " ")
};
let md = md.display().to_string();
let tei = tei.display().to_string();
for reading in ["text", "koine"] {
let plain = format!("p={reading}:{md}");
assert_eq!(said("/p/* | :name", &plain), "section", "{reading}");
assert_eq!(said("/p//footnote @| count", &plain), "0", "{reading}");
assert_eq!(said("*//footnote @| count", &plain), "2", "{reading}");
for how in ["mounted", "hidden", "omit"] {
let mounted = format!("p={reading}:{md}?notes={how}");
assert_eq!(
said("/p/* | :name", &mounted),
"section footnotes",
"{reading} {how}"
);
assert_eq!(
said("/p/footnotes/footnote::", &mounted),
"The Muse is meant. Its cause is told later.",
"{reading} {how}"
);
assert_eq!(
said("/p//*<noteref>->footnote | :parent-name", &mounted),
"footnotes footnotes",
"{reading} {how}"
);
}
}
let atd = dir.join("hamlet.atd");
std::fs::write(
&atd,
"@@@!litogramma\n\n@#() Hamlet\n\nTo be, or not to be.@^(n1)\n\n@^\nThe Folio reads \
thus.\n^@(n1)\n\n#@\n",
)
.unwrap();
let atd = atd.display().to_string();
for reading in ["text", "koine", "corpus", "lit"] {
assert_eq!(
said("/p//footnote @| count", &format!("p={reading}:{atd}")),
"0",
"{reading}"
);
assert_eq!(
said(
"/p//footnote::",
&format!("p={reading}:{atd}?notes=mounted")
),
"The Folio reads thus.",
"{reading}"
);
}
let odt = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../quarb-text-odt/tests/fixtures/tom-sawyer.odt"
);
for reading in ["text", "corpus"] {
assert_eq!(
said("/p//footnote @| count", &format!("p={reading}:{odt}")),
"0",
"{reading}"
);
assert_eq!(
said(
"/p/footnotes/footnote @| count",
&format!("p={reading}:{odt}?notes=mounted")
),
"2",
"{reading}"
);
}
let corpus = |options: &str| format!("p=corpus:{md}{options}");
assert_eq!(said("/p//token @| count", &corpus("")), "11");
assert_eq!(said("*//token @| count", &corpus("")), "22");
assert_eq!(said("/p//token @| count", &corpus("?notes=mounted")), "22");
assert_eq!(said("*//token @| count", &corpus("?notes=mounted")), "22");
assert_eq!(
said(
"/p//footnote//token[1] | %(w = ::; n = :::n)",
&corpus("?notes=mounted")
),
r#"%(w = "The"; n = 12) %(w = "Its"; n = 17)"#
);
for how in ["hidden", "omit"] {
let hidden = corpus(&format!("?notes={how}"));
assert_eq!(said("/p//token @| count", &hidden), "11", "{how}");
assert_eq!(said("*//token @| count", &hidden), "11", "{how}");
assert_eq!(said("/p//footnote @| count", &hidden), "2", "{how}");
}
let lit = |options: &str| format!("p=lit:{tei}{options}");
assert_eq!(said("/p/* | :name", &lit("")), "section");
assert_eq!(said("/p//token @| count", &lit("")), "11");
assert_eq!(
said("/p/* | :name", &lit("?notes=mounted")),
"section footnotes"
);
assert_eq!(said("/p//token @| count", &lit("?notes=mounted")), "16");
assert_eq!(said("/p//token @| count", &lit("?notes=hidden")), "11");
assert_eq!(
said(
"/p//*<note> | %(k = :name; up = :parent-name)",
&lit("?notes=hidden")
),
r#"%(k = "footnote"; up = "footnotes")"#
);
for (source, told) in [
(
format!("p=text:{md}?notes=all"),
"notes=all — notes= takes mounted or hidden (a note stands outside the tree by default)",
),
(
format!("p=corpus:{md}?notes=all"),
"corpus: notes=all — notes= takes mounted or hidden",
),
(
format!("p=lit:{tei}?notes=all"),
"lit: notes=all — notes= takes mounted or hidden",
),
(
format!("p=koine:{md}?notes=all"),
"notes=all — notes= takes mounted or hidden",
),
(
format!("p={md}?notes=mounted"),
"unknown option \"notes=mounted\"",
),
] {
let (ok, _, err) = qua_full(&["/p/* | :name", &source]);
assert!(!ok && err.contains(told), "{source}: {err}");
}
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn an_offset_counts_the_bytes_of_the_file() {
let dir = std::env::temp_dir().join(format!("quarb-offset-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("shelf")).unwrap();
let write = |name: &str, bytes: &[u8]| std::fs::write(dir.join(name), bytes).unwrap();
let wrath = "# The Wrath\n\nSing, goddess[^1], the wrath.\n\n[^1]: The Muse is meant.\n";
write("shelf/wrath.md", wrath.as_bytes());
write(
"shelf/wrath.tex",
b"\\section{Wrath}\n\nSing, goddess\\footnote{The Muse.}, the wrath.\n",
);
write(
"shelf/wrath.html",
b"<html><body><h1>Wrath</h1><p>Sing, goddess.</p></body></html>\n",
);
write("shelf/wrath.txt", b"Sing, goddess, the wrath.\n");
write(
"marked.md",
"\u{feff}# The Wrath\n\nSing, goddess.\n".as_bytes(),
);
let gnev = "# Гнев\n\nГнев, богиня, воспой.\n";
write("gnev.md", gnev.as_bytes());
let cp1251: Vec<u8> = gnev
.chars()
.map(|c| match c {
'А'..='я' => (c as u32 - 0x410 + 0xC0) as u8,
c => c as u8,
})
.collect();
write("gnev-1251.md", &cp1251);
let said = |query: &str, source: &str| {
let (ok, out, err) = qua_full(&[query, &format!("{}/{source}", dir.display())]);
assert!(ok, "{query} {source}: {err}");
assert_eq!(err.trim(), "", "{query} {source}");
out.trim().replace('\n', " ")
};
let said_of = |query: &str, reading: &str, source: &str| {
let (ok, out, err) = qua_full(&[query, &format!("{reading}:{}/{source}", dir.display())]);
assert!(ok, "{query} {reading}:{source}: {err}");
out.trim().replace('\n', " ")
};
let _ = said;
const PLACES: &str = "//* | %(:name; o = :::offset)";
for reading in ["text", "corpus"] {
assert_eq!(
said_of("//section | %(o = :::offset)", reading, "shelf/wrath.md"),
"%(o = 0)",
"{reading}"
);
assert_eq!(
said_of("//paragraph | %(o = :::offset)", reading, "shelf/wrath.md"),
"%(o = 13)",
"{reading}"
);
assert_eq!(
said_of(
"//*<noteref> | ->footnote | %(at = $-::at; off = $-::offset)",
reading,
"shelf/wrath.md"
),
"%(at = 13; off = 26)",
"{reading}"
);
assert_eq!(
said_of("//paragraph | %(o = :::offset)", reading, "shelf/wrath.tex"),
"%(o = 17)",
"{reading}"
);
}
assert_eq!(&wrath[26..30], "[^1]");
assert_eq!(
said_of("//paragraph | %(o = :::offset)", "text", "shelf/wrath.html"),
"%(o = null)"
);
assert_eq!(
said_of("//paragraph | %(o = :::offset)", "text", "shelf/wrath.txt"),
"%(o = null)"
);
assert_eq!(
said_of(PLACES, "text", "marked.md"),
r#"%(name = "section"; o = 3) %(name = "paragraph"; o = 16)"#
);
assert_eq!(
said_of(PLACES, "text", "gnev.md"),
r#"%(name = "section"; o = 0) %(name = "paragraph"; o = 12)"#
);
assert_eq!(
said_of(PLACES, "text", "gnev-1251.md?encoding=windows-1251"),
r#"%(name = "section"; o = null) %(name = "paragraph"; o = null)"#
);
assert_eq!(
said_of(
"/* | %(:name; o = (//paragraph[1]:::offset))",
"text",
"shelf"
),
r#"%(name = "wrath.html"; o = null) %(name = "wrath.md"; o = 13) %(name = "wrath.tex"; o = 17) %(name = "wrath.txt"; o = null)"#
);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_folders_leaves_and_an_office_file_take_the_mounts_options() {
let dir = std::env::temp_dir().join(format!("quarb-leaves-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("shelf")).unwrap();
let refs = "# 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";
std::fs::write(dir.join("shelf/refs.md"), refs).unwrap();
std::fs::write(
dir.join("shelf/wrath.md"),
"# The Wrath\n\nSing, goddess[^1], the wrath.\n\n[^1]: The Muse is meant.\n",
)
.unwrap();
std::fs::write(
dir.join("shelf/wrath.html"),
"<html><body><h1>Wrath</h1><p>Sing, goddess<a epub:type=\"noteref\" \
href=\"#n1\">1</a>.</p><aside epub:type=\"footnote\" id=\"n1\"><p>The Muse.</p>\
</aside></body></html>\n",
)
.unwrap();
std::fs::write(
dir.join("shelf/wrath.tex"),
"\\section{Wrath}\n\nSing, goddess\\footnote{The Muse.}, the wrath.\n",
)
.unwrap();
let said = |query: &str, source: &str| {
let (ok, out, err) = qua_full(&[query, source]);
assert!(ok, "{query} {source}: {err}");
assert_eq!(err.trim(), "", "{query} {source}");
out.trim().replace('\n', " ")
};
let shelf = format!("text:{}/shelf", dir.display());
assert_eq!(
said("/* | %(:name; notes = (//footnote @| count))", &shelf),
r#"%(name = "refs.md"; notes = 0) %(name = "wrath.html"; notes = 0) %(name = "wrath.md"; notes = 0) %(name = "wrath.tex"; notes = 0)"#
);
assert_eq!(
said(
"/* | %(:name; notes = (/footnotes/footnote @| count))",
&format!("{shelf}?notes=mounted")
),
r#"%(name = "refs.md"; notes = 0) %(name = "wrath.html"; notes = 1) %(name = "wrath.md"; notes = 1) %(name = "wrath.tex"; notes = 1)"#
);
assert_eq!(
said("/footnotes @| count", &format!("{shelf}?notes=mounted")),
"0"
);
assert_eq!(
said(
"/* | %(:name; bibs = (//bib @| count); cits = (//cit @| count))",
&format!("{shelf}?references=cmos")
),
r#"%(name = "refs.md"; bibs = 2; cits = 2) %(name = "wrath.html"; bibs = 0; cits = 0) %(name = "wrath.md"; bibs = 0; cits = 0) %(name = "wrath.tex"; bibs = 0; cits = 0)"#
);
let odt = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../quarb-text-odt/tests/fixtures/refs.odt"
);
assert_eq!(said("//bib @| count", &format!("text:{odt}")), "0");
assert_eq!(
said("//bib[2] | %", &format!("text:{odt}?references=cmos")),
r#"%(onym = "2"; auctor = "Homer"; titulus = "The Iliad"; locus = "London"; officina = "Longmans"; annus = "1898")"#
);
assert_eq!(
said("//cit @| count", &format!("text:{odt}?references=cmos")),
"2"
);
let _ = std::fs::remove_dir_all(&dir);
}
fn qua_full(args: &[&str]) -> (bool, String, 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();
(
out.status.success(),
String::from_utf8(out.stdout).unwrap(),
String::from_utf8(out.stderr).unwrap(),
)
}
#[test]
fn an_unreadable_leaf_answers_null_with_the_reason_in_explain() {
let dir = std::env::temp_dir().join(format!("quarb-letters-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
dir.join("good.xml"),
r#"<TEI xmlns="http://www.tei-c.org/ns/1.0">
<text><body>
<div type="chapter"><head>The fence</head>
<p>Tom painted the fence.</p>
<p>Ben watched.</p></div>
</body></text></TEI>"#,
)
.unwrap();
std::fs::write(
dir.join("bad.xml"),
r#"<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader><fileDesc><titleStmt><title>A letter</title></titleStmt></fileDesc></teiHeader>
<text><body><p>Dear friend, H<sub>2</sub>O.</p></body></text></TEI>"#,
)
.unwrap();
std::fs::write(
dir.join("plain.xml"),
"<things><p>no identity declared</p></things>\n",
)
.unwrap();
for level in ["text", "corpus", "lit"] {
let mount = format!("{level}:{}", dir.display());
assert_eq!(qua(&["//paragraph @| count", &mount]), "2\n", "{level}");
assert_eq!(
qua(&[r#"//*[:name = "bad.xml"]//* @| count"#, &mount]),
"0\n",
"{level}"
);
assert_eq!(
qua(&[r#"//*[:name = "bad.xml"] | %(::lemma)"#, &mount]),
"%(lemma = null)\n",
"{level}"
);
assert_eq!(
qua(&["/* | :name", &mount]),
"bad.xml\ngood.xml\nplain.xml\n",
"{level}"
);
let (ok, out, err) = qua_full(&["--diagnostics=info", "//paragraph @| count", &mount]);
assert!(ok && out == "2\n", "{level}: {out} {err}");
assert!(
err.contains("bad.xml") && err.contains("answer null") && err.contains("sub"),
"{level}: {err}"
);
let (_, _, err) = qua_full(&["//paragraph @| count", &mount]);
assert!(!err.contains("answer null"), "{level}: {err}");
let (ok, _, err) = qua_full(&[
"//paragraph @| count",
&format!("{level}:{}", dir.join("bad.xml").display()),
]);
assert!(!ok && err.contains("sub"), "{level}: {err}");
}
assert_eq!(
qua(&["//p::", &format!("text:{}", dir.display())]),
"no identity declared\n"
);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn the_plan_prices_the_files_the_folder_exposes() {
let dir = std::env::temp_dir().join(format!("quarb-planned-shelf-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("raw")).unwrap();
std::fs::write(dir.join("a.txt"), "one paragraph\n").unwrap();
std::fs::write(dir.join("raw/b.txt"), "sub paragraph, longer\n").unwrap();
std::fs::write(dir.join(".gitignore"), "raw/\n").unwrap();
let mount = format!("text:{}", dir.display());
assert_eq!(qua(&["//paragraph @| count", &mount]), "1\n");
let plan = qua(&["--plan", "//paragraph @| count", &mount]);
assert!(plan.contains("bytes-min=14\n"), "{plan}");
assert!(plan.contains("bytes-max=14\n"), "{plan}");
assert_eq!(
qua(&["--budget", "bytes=20", "//paragraph @| count", &mount]),
"1\n"
);
let (ok, _, err) = qua_full(&["--budget", "bytes=10", "//paragraph @| count", &mount]);
assert!(!ok && err.contains("14 bytes"), "{err}");
assert_eq!(qua(&["--no-ignore", "//paragraph @| count", &mount]), "2\n");
let plan = qua(&["--no-ignore", "--plan", "//paragraph @| count", &mount]);
assert!(plan.contains("bytes-min=36\n"), "{plan}");
let plan = qua(&[
"--no-ignore",
"--hidden",
"--plan",
"//paragraph @| count",
&mount,
]);
assert!(plan.contains("bytes-min=41\n"), "{plan}");
let _ = std::fs::remove_dir_all(&dir);
}