156 lines
5.4 KiB
Rust
156 lines
5.4 KiB
Rust
#![cfg(all(target_os = "linux", target_env = "gnu"))]
|
|
|
|
use std::{fs, process::Command};
|
|
use texrra_tex_core::execution::*;
|
|
|
|
const SOURCE: &str = r#"
|
|
\catcode123=1 \catcode125=2 \catcode35=6 \catcode94=7
|
|
\def\probe#1{\message{!pdfmatch:#1:end!}}
|
|
\probe{before=[\pdflastmatch0]}
|
|
\def\dimpat{^([0-9]+([.,][0-9]+)?|[.,][0-9]+)(em|ex|cm|mm|in|pc|pt|dd|cc|bp|sp|nd|nc|px)? *$}
|
|
\def\value{12.5pt}
|
|
\probe{dim=[\pdfmatch{\dimpat}{\value}]/[\pdflastmatch0]/[\pdflastmatch1]/[\pdflastmatch2]/[\pdflastmatch3]}
|
|
\probe{dim-fail=[\pdfmatch{\dimpat}{wd0}]/[\pdflastmatch0]}
|
|
\probe{longest=[\pdfmatch{(a|ab)}{cab}]/[\pdflastmatch0]/[\pdflastmatch1]}
|
|
\probe{leftmost=[\pdfmatch{(a|bcde)}{abcde}]/[\pdflastmatch0]}
|
|
\probe{case=[\pdfmatch icase subcount 2{(a)(B)}{xAb}]/[\pdflastmatch0]/[\pdflastmatch1]/[\pdflastmatch2]}
|
|
\probe{invalid=[\pdfmatch{[}{x}]/[\pdflastmatch0]/[\pdflastmatch1]}
|
|
\probe{zero=[\pdfmatch subcount 0{(a)}{xax}]/[\pdflastmatch0]}
|
|
\probe{negative=[\pdfmatch subcount -2{(a)}{xax}]/[\pdflastmatch0]/[\pdflastmatch1]}
|
|
\probe{optional=[\pdfmatch{(a)?(b*)}{x}]/[\pdflastmatch0]/[\pdflastmatch1]/[\pdflastmatch2]}
|
|
\begingroup \edef\inside{\pdfmatch{(c)}{abc}}\endgroup
|
|
\probe{group=[\pdflastmatch0]/[\pdflastmatch1]}
|
|
\begingroup\escapechar=-1 \xdef\bs{\string\\}\endgroup
|
|
\probe{gnu-backref=[\pdfmatch{^(a)\bs1$}{aa}]/[\pdflastmatch1]}
|
|
\probe{gnu-class=[\pdfmatch{^\bs w+$}{a01}]/[\pdflastmatch0]}
|
|
\probe{byte-offset=[\pdfmatch{(q)}{^^c3^^a9q}]/[\pdflastmatch1]}
|
|
\def\capturedbyte#1->#2{\number`#2}
|
|
\edef\status{\pdfmatch{(.)}{^^ffq}}
|
|
\edef\capture{\pdflastmatch0}
|
|
\probe{nonutf8=[\status]/[\expandafter\capturedbyte\capture]}
|
|
\catcode0=12 \catcode10=12
|
|
\probe{nul-text=[\pdfmatch{c}{ab^^00cd}]/[\pdflastmatch0]}
|
|
\probe{nul-pattern=[\pdfmatch{a^^00b}{xa}]/[\pdflastmatch0]}
|
|
\probe{newline=[\pdfmatch{a.b}{a^^0ab}]}
|
|
\end
|
|
"#;
|
|
|
|
fn file(source: &str) -> SourceFile {
|
|
SourceFile {
|
|
name: "probe.tex".into(),
|
|
data: source.as_bytes().to_vec(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn pdf_regex_matches_reference_bytes_and_retains_job_local_captures() {
|
|
let dir = std::env::temp_dir().join(format!("texrra-pdfmatch-{}", std::process::id()));
|
|
fs::create_dir(&dir).unwrap();
|
|
fs::write(dir.join("probe.tex"), SOURCE).unwrap();
|
|
let reference = Command::new("pdftex")
|
|
.args([
|
|
"-ini",
|
|
"-etex",
|
|
"-interaction=nonstopmode",
|
|
"-halt-on-error",
|
|
"probe.tex",
|
|
])
|
|
.env("LC_ALL", "C")
|
|
.env("max_print_line", "1000")
|
|
.current_dir(&dir)
|
|
.output()
|
|
.unwrap();
|
|
assert!(
|
|
reference.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&reference.stdout)
|
|
);
|
|
let log = fs::read(dir.join("probe.log")).unwrap();
|
|
let reference: String = log.into_iter().map(char::from).collect();
|
|
let expected: Vec<_> = reference
|
|
.split("!pdfmatch:")
|
|
.skip(1)
|
|
.map(|part| part.split_once(":end!").unwrap().0)
|
|
.collect();
|
|
assert_eq!(expected.len(), 18);
|
|
|
|
let native = Executor::new(
|
|
State::initex()
|
|
.with_etex()
|
|
.with_input_profile(InputProfile::TeX8Bit),
|
|
Limits::default(),
|
|
)
|
|
.run(file(SOURCE), &mut NoFiles)
|
|
.unwrap();
|
|
let actual: Vec<_> = native
|
|
.events
|
|
.iter()
|
|
.filter_map(|event| match event {
|
|
Event::Message { text, .. } => text.strip_prefix("!pdfmatch:")?.strip_suffix(":end!"),
|
|
_ => None,
|
|
})
|
|
.collect();
|
|
assert_eq!(actual, expected);
|
|
let fresh = Executor::new(native.state, Limits::default())
|
|
.run(file(r"\message{\pdflastmatch0}\end"), &mut NoFiles)
|
|
.unwrap();
|
|
assert!(
|
|
fresh
|
|
.events
|
|
.iter()
|
|
.any(|event| { matches!(event, Event::Message { text, .. } if text == "-1->") })
|
|
);
|
|
fs::remove_dir_all(dir).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn a_negative_capture_index_is_a_scan_error() {
|
|
let error = Executor::new(
|
|
State::initex()
|
|
.with_etex()
|
|
.with_input_profile(InputProfile::TeX8Bit),
|
|
Limits::default(),
|
|
)
|
|
.run(
|
|
file(r"\catcode123=1 \catcode125=2 \message{\pdflastmatch-1}\end"),
|
|
&mut NoFiles,
|
|
)
|
|
.unwrap_err();
|
|
assert!(error.message.contains("Bad match number"));
|
|
}
|
|
|
|
#[test]
|
|
fn capture_is_restored_after_editing_the_unread_suffix() {
|
|
let initial = r"\catcode123=1 \catcode125=2
|
|
\edef\status{\pdfmatch{(ab)}{zab}}
|
|
\message{original=[\pdflastmatch1]}
|
|
\end";
|
|
let edited = initial.replace("original", "edited");
|
|
let frontier = initial.find("\n\\message").unwrap();
|
|
let state = State::initex()
|
|
.with_etex()
|
|
.with_input_profile(InputProfile::TeX8Bit);
|
|
let (original, captured) = Executor::new(state.clone(), Limits::default()).run_capturing_entry(
|
|
file(initial),
|
|
&mut NoFiles,
|
|
frontier,
|
|
|_| (),
|
|
);
|
|
original.unwrap();
|
|
let (checkpoint, ()) = captured.expect("capture after scanning the match");
|
|
let resumed = checkpoint
|
|
.resume_after_host_replay(file(&edited), &mut NoFiles, Cancellation::default())
|
|
.expect("edited suffix is beyond the checkpoint")
|
|
.unwrap();
|
|
let full = Executor::new(state, Limits::default())
|
|
.run(file(&edited), &mut NoFiles)
|
|
.unwrap();
|
|
assert_eq!(resumed.events, full.events);
|
|
assert!(
|
|
resumed
|
|
.events
|
|
.iter()
|
|
.any(|event| matches!(event, Event::Message { text, .. } if text == "edited=[1->ab]"))
|
|
);
|
|
}
|