Files
texrra-engine/crates/texrra-tex-core/tests/execution_diagnostics.rs
T

304 lines
10 KiB
Rust

//! Actual native box/list displays against independent INITEX diagnostics.
#[path = "support/font_world.rs"]
mod font_world;
use std::{fs, process::Command};
use texrra_tex_core::execution::*;
const SETUP: &str = include_str!("../../../examples/native-bootstrap/math-fonts.tex");
const PRINT: &str =
r"\newlinechar=-1\showboxbreadth=1000\showboxdepth=20\hfuzz=100pt\vfuzz=100pt\tracingonline=1";
#[derive(Default)]
struct Host {
output: Vec<(WriteDestination, String)>,
}
impl SourceProvider for Host {
font_world::font_resources!();
fn read(&mut self, _: &str, _: &str) -> Result<SourceFile, String> {
Err("no source".into())
}
fn observe_diagnostic(&mut self, d: WriteDestination, t: &str) -> Result<(), String> {
self.output.push((d, t.into()));
Ok(())
}
}
fn execute(source: &str, host: &mut impl SourceProvider) -> Result<Execution, Error> {
Executor::new(State::initex().with_etex(), Limits::default()).run(
SourceFile {
name: "diagnostics.tex".into(),
data: source.as_bytes().to_vec(),
},
host,
)
}
fn directory(label: &str) -> std::path::PathBuf {
let p = std::env::temp_dir().join(format!("texrra-showbox-{label}-{}", std::process::id()));
fs::create_dir_all(&p).unwrap();
p
}
fn reference(dir: &std::path::Path, s: &str) -> Option<(String, String)> {
fs::write(dir.join("probe.tex"), s).unwrap();
let o = match Command::new("pdftex")
.args(["-ini", "-etex", "-interaction=nonstopmode", "probe.tex"])
.current_dir(dir)
.output()
{
Ok(o) => o,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
eprintln!("pdftex absent: showbox oracle skipped");
return None;
}
Err(e) => panic!("{e}"),
};
let log = fs::read_to_string(dir.join("probe.log")).unwrap();
assert_eq!(o.status.code(), Some(1), "{log}");
for l in log.lines().filter(|l| l.starts_with("! ")) {
assert_eq!(l, "! OK.", "{log}");
}
Some((log, String::from_utf8_lossy(&o.stdout).into()))
}
fn reports(e: &Execution) -> Vec<&ShowBoxReport> {
e.events
.iter()
.filter_map(|e| {
if let Event::ShowBox(r) = e {
Some(r)
} else {
None
}
})
.collect()
}
fn compare(source: &str, label: &str) -> Execution {
let e = execute(source, &mut Host::default()).unwrap();
let dir = directory(label);
if let Some((log, _)) = reference(&dir, source) {
for r in reports(&e) {
assert!(
log.contains(&r.text),
"native:\n{}\nreference:\n{log}",
r.text
);
}
}
fs::remove_dir_all(dir).unwrap();
e
}
#[test]
fn actual_box_nodes_breadth_depth_and_glue_settings_match_initex() {
let mut s = format!("{SETUP}{PRINT}");
let mut count = 0;
for breadth in [-2, 0, 1, 2, 5, 100] {
for depth in [-1, 0, 1, 2, 5, 20] {
for body in [
r"\hbox to50pt{A ffi\hskip1pt plus2fil minus1pt\kern3pt\penalty5 b\-c}",
r"\vbox{\hbox{A}\vskip2pt plus1fil minus1pt\kern-1pt\penalty42\hbox{bg}\hrule}",
r"\vtop to40pt{\hbox{A}\hbox{bg}\vfill}",
r"\hbox{\hbox{A}\vbox{\hbox{b}\hbox{c}}\hskip0pt plus1filll}",
] {
s.push_str(&format!(
"\\showboxbreadth={breadth}\\showboxdepth={depth}\\setbox0={body}\\showbox0\n"
));
count += 1;
}
}
}
for body in [
r"\hbox to9000pt{\hskip0pt plus1sp}",
r"\hbox spread1sp{\hskip0pt plus1fil}",
r"\hbox to10pt{aaa\hskip2pt minus1pt}",
r"\hbox to11pt{\hskip1pt plus3pt}",
r"\vbox{\hrule width2pt height3pt depth1pt\vskip0pt plus1filll}",
r"\hbox{$a$}",
r"\hbox{$ab$}",
] {
s.push_str(&format!(
"\\showboxbreadth=100\\showboxdepth=20\\setbox0={body}\\showbox0\n"
));
count += 1;
}
s.push_str("\\showbox1\\end");
count += 1;
let e = compare(&s, "boxes");
assert_eq!(reports(&e).len(), count);
eprintln!("{count} actual INITEX recursive showbox/breadth/depth/glue cases passed");
}
#[test]
fn actual_paragraph_diagnostics_retain_raw_glue_and_selected_material() {
let mut s = format!(
"{SETUP}{PRINT}\\hyphenchar\\tenrm=45\\lefthyphenmin=1\\righthyphenmin=1\\hyphenation{{al-pha-bet}}"
);
let mut count = 0;
for width in [32, 60, 100] {
for left in [0, 1] {
for text in [
"A short paragraph for box diagnostics.",
r"A alphabet alphabet alphabet.",
r"A ab\-cdef gh\-ijkl.",
r"A ffi AVA To.\par More lines.",
] {
s.push_str(&format!("{{\\hsize={width}pt\\leftskip={left}pt\\parskip=3pt\\pretolerance=-1\\tolerance=10000\\showboxdepth=20\\setbox0=\\vbox{{\\noindent {text}\\par}}\\showbox0}}\n"));
count += 1;
}
}
}
s.push_str("\\end");
let e = compare(&s, "paragraphs");
assert_eq!(reports(&e).len(), count);
eprintln!("{count} actual INITEX paragraph diagnostic list cases passed");
}
#[test]
fn font_identifiers_channels_scope_and_printer_bytes_match_initex() {
let source = format!(
r"{SETUP}{PRINT}\setbox0=\hbox{{A ffi}}\font\latest=cmr10 \let\alias\latest
{{\font\local=cmr10 }}\showbox0
{{\showboxbreadth=1\showboxdepth=1\showbox0}}\showbox0
\tracingonline=0\showbox0\tracingonline=-1\showbox0\tracingonline=1\showbox0
\setbox300=\copy0\showbox300\setbox1=\hbox{{f\showbox0 i}}\showbox1\end"
);
let e = compare(&source, "channels");
assert!(
reports(&e)
.iter()
.any(|r| r.destination == WriteDestination::Log)
);
assert!(
reports(&e)
.last()
.unwrap()
.text
.contains("\\local f\n.\\local i")
);
let copy: Execution = serde_json::from_str(&serde_json::to_string(&e).unwrap()).unwrap();
assert_eq!(e, copy);
let dir = directory("printer");
let mut count = 0;
for newline in [-1, 0, 33, 46, 92, 65] {
for escape in [-1, 92, 64, 200] {
let s = format!(
r"{SETUP}{PRINT}\setbox0=\hbox{{\char0\char127 A\hskip1pt plus1fil\kern2pt}}\newlinechar={newline}\escapechar={escape}\showbox0\end"
);
let e = execute(&s, &mut Host::default()).unwrap();
fs::write(dir.join("probe.tex"), &s).unwrap();
let o = Command::new("pdftex")
.args(["-ini", "-etex", "-interaction=nonstopmode", "probe.tex"])
.current_dir(&dir)
.output()
.unwrap();
assert_eq!(o.status.code(), Some(1));
let log = fs::read_to_string(dir.join("probe.log")).unwrap();
for r in reports(&e) {
assert!(
log.contains(&r.text),
"newline={newline} escape={escape}\n{}\n{log}",
r.text
);
}
count += 1;
}
}
fs::remove_dir_all(dir).unwrap();
eprintln!("{count} actual INITEX diagnostic escape/newline byte cases passed");
}
#[test]
fn honest_unsupported_lists_observer_failures_and_capacity_are_errors() {
let mut nested = format!(r"{SETUP}{PRINT}\setbox0=\hbox{{A}}");
for _ in 0..12 {
nested.push_str(r"\setbox0=\hbox{\copy0}");
}
nested.push_str(r"\showbox0\end");
let e = compare(&nested, "nested-copy");
assert!(
serde_json::to_vec(&e).unwrap().len() < 1_000_000,
"nested diagnostic copies must not recursively duplicate painted boxes"
);
assert_eq!(State::initex().newlinechar, 0);
let e = execute(
&format!(r"{SETUP}{PRINT}\setbox0=\hbox{{$ab$}}\showbox0\end"),
&mut Host::default(),
)
.unwrap();
assert!(reports(&e)[0].text.contains("mathon"));
let e = compare(
&format!(r"{SETUP}{PRINT}\setbox0=\hbox{{$a+b$}}\showbox0\end"),
"math-glue",
);
assert!(reports(&e)[0].text.contains("medmuskip"));
let e = compare(
&format!(r"{SETUP}{PRINT}\setbox0=\hbox{{$x_i^2$}}\showbox0\end"),
"owned-scripts",
);
assert!(reports(&e)[0].text.contains("vbox"));
let e = compare(
&format!(r"{SETUP}{PRINT}\setbox0=\hbox{{${{x\over y}}$}}\showbox0\end"),
"owned-fraction",
);
assert!(reports(&e)[0].text.contains("vbox"));
let e = execute(
&format!(r"{SETUP}{PRINT}\setbox0=\hbox{{$x_i^2$}}\showboxdepth=0\showbox0\end"),
&mut Host::default(),
)
.unwrap();
assert!(reports(&e)[0].text.contains(" []"));
struct Fail;
impl SourceProvider for Fail {
font_world::font_resources!();
fn read(&mut self, _: &str, _: &str) -> Result<SourceFile, String> {
Err("no source".into())
}
fn observe_diagnostic(&mut self, _: WriteDestination, _: &str) -> Result<(), String> {
Err("actual diagnostic sink failed".into())
}
}
assert!(
execute(&format!(r"{SETUP}{PRINT}\showbox0\end"), &mut Fail)
.unwrap_err()
.message
.contains("sink failed")
);
assert!(execute(&format!(r"{SETUP}\showbox32768\end"), &mut Host::default()).is_err());
let text = format!(
r"\catcode123=1\catcode125=2\font\p=cmr10 \p\showboxbreadth=1000\showboxdepth=20\setbox0=\hbox{{{}}}\showbox0\end",
"a".repeat(200)
);
let e = Executor::new(
State::initex().with_etex(),
Limits {
tokens: 500,
..Limits::default()
},
)
.run(
SourceFile {
name: "capacity.tex".into(),
data: text.as_bytes().to_vec(),
},
&mut Host::default(),
)
.unwrap_err();
assert!(e.message.contains("diagnostic output capacity"), "{e:?}");
}
#[test]
fn errmessage_uses_scoped_unexpanded_error_help() {
let error = execute(
r"\catcode123=1\catcode125=2
\def\hidden{EXPANDED}
\errhelp{CUSTOM \hidden}
{\errhelp{LOCAL}}
\errmessage{BODY}",
&mut Host::default(),
)
.unwrap_err();
assert_eq!(error.message, "TeX errmessage: BODY");
assert_eq!(error.help.as_deref(), Some("CUSTOM \\hidden "));
let error = execute(
r"\catcode123=1\catcode125=2
\errhelp{CUSTOM}
\errhelp{}
\errmessage{BODY}",
&mut Host::default(),
)
.unwrap_err();
assert_eq!(error.help, None);
}