238 lines
8.7 KiB
Rust
238 lines
8.7 KiB
Rust
//! Code-table state, raw token case shifting and real paragraph consumers.
|
|
#[path = "support/font_world.rs"]
|
|
mod font_world;
|
|
use std::{
|
|
fs,
|
|
process::Command,
|
|
sync::atomic::{AtomicU64, Ordering},
|
|
};
|
|
use texrra_tex_core::execution::*;
|
|
const HEADER: &str =
|
|
r"\catcode123=1\catcode125=2\catcode35=6\catcode94=7\newlinechar=-1\errorcontextlines=100 ";
|
|
struct Host;
|
|
impl SourceProvider for Host {
|
|
font_world::font_resources!();
|
|
fn read(&mut self, _: &str, _: &str) -> Result<SourceFile, String> {
|
|
Err("no file".into())
|
|
}
|
|
fn diagnostic(&mut self, _: WriteDestination, _: &str) -> Result<(), String> {
|
|
Ok(())
|
|
}
|
|
}
|
|
fn execute(source: &str) -> Result<Execution, Error> {
|
|
Executor::new(
|
|
State::initex()
|
|
.with_etex()
|
|
.with_input_profile(InputProfile::TeX8Bit),
|
|
Limits::default(),
|
|
)
|
|
.run(
|
|
SourceFile {
|
|
name: "probe.tex".into(),
|
|
data: source.as_bytes().to_vec(),
|
|
},
|
|
&mut Host,
|
|
)
|
|
}
|
|
fn reference(source: &str, shows: bool) -> String {
|
|
static ID: AtomicU64 = AtomicU64::new(0);
|
|
let p = std::env::temp_dir().join(format!(
|
|
"texrra-case-{}-{}",
|
|
std::process::id(),
|
|
ID.fetch_add(1, Ordering::Relaxed)
|
|
));
|
|
fs::create_dir_all(&p).unwrap();
|
|
fs::write(p.join("probe.tex"), source).unwrap();
|
|
let mut c = Command::new("pdftex");
|
|
c.args(["-ini", "-etex", "-interaction=nonstopmode"]);
|
|
if !shows {
|
|
c.arg("-halt-on-error");
|
|
}
|
|
let r = c
|
|
.arg("probe.tex")
|
|
.current_dir(&p)
|
|
.output()
|
|
.expect("actual INITEX required");
|
|
let text = fs::read_to_string(p.join("probe.log")).unwrap();
|
|
assert_eq!(r.status.code(), Some(i32::from(shows)), "{text}");
|
|
fs::remove_dir_all(p).unwrap();
|
|
text
|
|
}
|
|
fn compare(body: &str, shows: bool) -> Execution {
|
|
let source = format!("{HEADER}\n{body}\n\\end\n");
|
|
let e = execute(&source).unwrap();
|
|
let r = reference(&source, shows);
|
|
let expected: Vec<_> = r
|
|
.lines()
|
|
.filter(|s| s.starts_with("0="))
|
|
.map(|s| format!("{s}\n"))
|
|
.collect();
|
|
let actual: Vec<_> = e
|
|
.events
|
|
.iter()
|
|
.filter_map(|e| {
|
|
if let Event::Write { text, .. } = e {
|
|
Some(text.clone())
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.collect();
|
|
assert_eq!(actual, expected, "{body}\n{r}");
|
|
for event in &e.events {
|
|
match event {
|
|
Event::ShowBox(report) => assert!(
|
|
r.contains(&report.text),
|
|
"{body}\nnative:\n{}\nreference:\n{r}",
|
|
report.text
|
|
),
|
|
Event::Show(report) => {
|
|
assert!(r.contains(&report.output), "{body}\n{}\n{r}", report.output)
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
e
|
|
}
|
|
#[test]
|
|
fn all_byte_defaults_aliases_global_restore_and_internal_quantities_match_initex() {
|
|
let mut body = String::new();
|
|
for c in 0..256 {
|
|
for name in ["lccode", "uccode"] {
|
|
body.push_str(&format!(
|
|
"\\immediate\\write16{{0={name}{c}=\\the\\{name}{c}}}\n"
|
|
));
|
|
}
|
|
}
|
|
body.push_str(r"\lccode65=0{\lccode65=98\uccode97=89\immediate\write16{0=LOCAL:\the\lccode65/\the\uccode97}}\immediate\write16{0=RESTORE:\the\lccode65/\the\uccode97}
|
|
{\lccode65=1{\global\lccode65=99}\immediate\write16{0=INNER:\the\lccode65}}\immediate\write16{0=GLOBAL:\the\lccode65}
|
|
{\globaldefs=-1\global\uccode97=88}\immediate\write16{0=NEG:\the\uccode97}
|
|
{\globaldefs=1\uccode97=90}\let\low=\lccode\low0=255
|
|
\count0=\lccode0\count1=\numexpr\uccode97+1\relax\dimen0=2\lccode65 sp
|
|
\immediate\write16{0=READ:\number\low0/\the\count0/\the\count1/\the\dimen0}");
|
|
let e = compare(&body, false);
|
|
assert_eq!(e.state.case_code(CaseCode::Lower, 'A'), 99);
|
|
assert_eq!(e.state.case_code(CaseCode::Upper, 'a'), 90);
|
|
assert!(
|
|
e.reads
|
|
.contains(&Dependency::CaseCode(CaseCode::Lower, '\0'))
|
|
);
|
|
let copy: Execution = serde_json::from_slice(&serde_json::to_vec(&e).unwrap()).unwrap();
|
|
assert_eq!(e, copy);
|
|
let next = Executor::from_execution(copy, Limits::default())
|
|
.run(
|
|
SourceFile {
|
|
name: "next.tex".into(),
|
|
data: br"\lowercase{\def\A{A}}\message{\A}\end".to_vec(),
|
|
},
|
|
&mut Host,
|
|
)
|
|
.unwrap();
|
|
assert!(
|
|
next.events
|
|
.iter()
|
|
.any(|e| matches!(e,Event::Message{text,..} if text=="c"))
|
|
);
|
|
}
|
|
#[test]
|
|
fn case_shift_preserves_categories_active_namespace_control_names_and_execution_order() {
|
|
for body in [
|
|
r"\def\A{original}\uppercase{\def\a{one}}\immediate\write16{0=\A/\a}",
|
|
r"\lccode65=0\lowercase{\immediate\write16{0=AbC}}",
|
|
r"\catcode126=13\catcode33=13\def~{LOW}\def!{HIGH}\uccode126=33\uppercase{\immediate\write16{0=~}}",
|
|
r"\uccode35=33\uppercase{\def\A#1{#1}}\immediate\write16{0=\meaning\A/\A{ok}}",
|
|
r"\uccode123=91\uccode125=93\uppercase{\def\A{ok}}\immediate\write16{0=\meaning\A}",
|
|
r"\uccode32=88\uppercase{\immediate\write16{0=a b}}",
|
|
r"\def\A{lower}\uppercase{\immediate\write16{0=\A}}",
|
|
r"\edef\A{\uppercase{abc}}\immediate\write16{0=\meaning\A}",
|
|
r"\def\A{\immediate\write16{0=AFTER}}\afterassignment\A\uppercase{\count0=7}\immediate\write16{0=\the\count0}",
|
|
r"\lowercase{\lccode65=98}\lowercase{\immediate\write16{0=A}}",
|
|
r"\uccode97=0\uppercase{\immediate\write16{0=a}}",
|
|
r"\catcode200=13\catcode255=13\def^^c8{LOW}\def^^ff{HIGH}\uccode200=255\uppercase{\immediate\write16{0=^^c8}}",
|
|
r"\let\L{\uppercase\L\immediate\write16{0=abc}}",
|
|
] {
|
|
compare(body, false);
|
|
}
|
|
let e = compare(
|
|
r"\font\rm=cmr10\rm\setbox0=\hbox{$\lccode65=98\lowercase{A}$}\immediate\write16{0=\the\lccode65}",
|
|
false,
|
|
);
|
|
assert_eq!(e.state.case_code(CaseCode::Lower, 'A'), 97);
|
|
}
|
|
#[test]
|
|
fn real_lccodes_normalize_hyphenation_exceptions_and_control_actual_selected_lines() {
|
|
let setup = r"\defaulthyphenchar=45\font\p=cmr10\p\hsize=42pt\parindent=0pt\baselineskip=12pt\parfillskip=0pt plus1fil\pretolerance=-1\tolerance=10000\hbadness=10000\vbadness=10000\boxmaxdepth=100pt\hyphenpenalty=50\exhyphenpenalty=50\linepenalty=10\adjdemerits=10000\doublehyphendemerits=10000\finalhyphendemerits=5000\lefthyphenmin=2\righthyphenmin=3\showboxbreadth=1000\showboxdepth=1000\tracingonline=1 ";
|
|
for definition in [
|
|
r"\lccode64=97\hyphenation{p@r@-graph}",
|
|
r"\chardef\A=97\hyphenation{p\A r\A-graph}",
|
|
r"\hyphenation{p\char97 r\char97-graph}",
|
|
r"\outer\def\A{para-graph}\hyphenation{\A}",
|
|
r"{\hyphenation{PARA-GRAPH}}",
|
|
r"\lccode112=120\hyphenation{para-graph}\uchyph=1",
|
|
] {
|
|
let e = compare(
|
|
&format!(
|
|
"{setup}{definition}\\setbox0=\\vbox{{X paragraph paragraph paragraph paragraph\\par}}\\showbox0"
|
|
),
|
|
true,
|
|
);
|
|
assert!(
|
|
e.reads
|
|
.contains(&Dependency::CaseCode(CaseCode::Lower, 'p'))
|
|
);
|
|
}
|
|
for body in [
|
|
r"\lccode103=0",
|
|
r"\uchyph=0",
|
|
r"\uchyph=1",
|
|
r"\lccode80=112\uchyph=1",
|
|
] {
|
|
compare(
|
|
&format!(
|
|
"{setup}\\hyphenation{{para-graph}}{body}\\setbox0=\\vbox{{X Paragraph paragraph Paragraph paragraph\\par}}\\showbox0"
|
|
),
|
|
true,
|
|
);
|
|
}
|
|
}
|
|
#[test]
|
|
fn invalid_code_ranges_prefixes_arithmetic_and_outer_scans_remain_real_errors() {
|
|
for body in [
|
|
r"\lccode-1=1",
|
|
r"\lccode256=1",
|
|
r"\lccode65=-1",
|
|
r"\uccode65=256",
|
|
r"\message{\the\uccode256}",
|
|
r"\advance\lccode65 by1",
|
|
r"\multiply\uccode65 by2",
|
|
r"\global\uppercase{a}",
|
|
r"\hyphenation{a!b}",
|
|
r"\hyphenation{a{b}",
|
|
] {
|
|
let source = format!("{HEADER}{body}\\end\n");
|
|
assert!(execute(&source).is_err(), "{body}");
|
|
// Halt-on-error reference status is checked independently of native messages.
|
|
let p = std::env::temp_dir().join(format!("texrra-case-invalid-{}", std::process::id()));
|
|
fs::create_dir_all(&p).unwrap();
|
|
fs::write(p.join("probe.tex"), &source).unwrap();
|
|
let r = Command::new("pdftex")
|
|
.args([
|
|
"-ini",
|
|
"-etex",
|
|
"-interaction=nonstopmode",
|
|
"-halt-on-error",
|
|
"probe.tex",
|
|
])
|
|
.current_dir(&p)
|
|
.output()
|
|
.unwrap();
|
|
assert!(!r.status.success(), "{body}");
|
|
fs::remove_dir_all(p).unwrap();
|
|
}
|
|
let e = execute(&format!(
|
|
"{HEADER}\\outer\\def\\A{{X}}\\uppercase{{\\A}}\\end"
|
|
))
|
|
.unwrap_err();
|
|
assert_eq!(e.scanner_status, ScannerStatus::Absorbing);
|
|
}
|