use std::{fs, process::Command}; use texrra_tex_core::execution::*; struct Host; impl SourceProvider for Host { fn read(&mut self, name: &str, _from: &str) -> Result { Err(format!("unexpected input {name}")) } } fn trace_lines(text: &str) -> Vec<&str> { text.lines() .filter(|line| line.contains("->") || line.contains("<-")) .collect() } #[test] fn macro_trace_follows_real_calls_and_parameters_in_each_channel() { let dir = std::env::temp_dir().join(format!("texrra-macro-trace-{}", std::process::id())); fs::create_dir_all(&dir).unwrap(); let source = r"\catcode123=1 \catcode125=2 \catcode35=6 \def\foo P#1/#2!{\message{arg=#1,#2}} \protected\long\def\empty#1{} \def\nested#1{\foo P{#1}/beta!} \catcode33=6 \def\alt!1{\message{arg=!1}} \tracingmacros=1 \relax \nested{alpha} \tracingonline=1 \relax \empty{} \alt{gamma} \tracingmacros=0 \relax \empty{QUIET} \end "; fs::write(dir.join("probe.tex"), source).unwrap(); let reference = Command::new("pdftex") .args([ "-ini", "-etex", "-interaction=nonstopmode", "-halt-on-error", "probe.tex", ]) .current_dir(&dir) .output() .unwrap(); assert!( reference.status.success(), "{}", String::from_utf8_lossy(&reference.stdout) ); let log = fs::read_to_string(dir.join("probe.log")).unwrap(); let terminal = String::from_utf8_lossy(&reference.stdout); let result = Executor::new(State::initex().with_etex(), Limits::default()) .run( SourceFile { name: "probe.tex".into(), data: source.as_bytes().into(), }, &mut Host, ) .unwrap(); assert!( result .reads .contains(&Dependency::Parameter(Parameter::Tracingmacros)) ); let mut native_log = String::new(); let mut native_terminal = String::new(); for event in result.events { if let Event::MacroTrace { destination, text } = event { native_log.push_str(&text); if destination == WriteDestination::TerminalAndLog { native_terminal.push_str(&text); } } } assert_eq!(trace_lines(&native_log), trace_lines(&log)); assert_eq!(trace_lines(&native_terminal), trace_lines(&terminal)); assert_eq!(trace_lines(&native_log).len(), 9); fs::remove_dir_all(dir).unwrap(); } #[test] fn definition_scans_aliased_parameter_commands_and_keeps_literal_tokens() { let source = r"\catcode123=1 \catcode125=2 \catcode35=6 \catcode33=6 \let\marker=#\let\alternate=! \protected\def\digit{1} \begingroup \catcode35=12 \catcode33=12 \def\pair\marker1,\alternate2;{\marker1:\alternate2} \edef\expandedpair\marker1{\noexpand\marker\digit} \def\literal{\marker\alternate} \edef\saved{\unexpanded{\marker}} \def\braced\marker{[prefix]} \message{PAIR=(\pair left,right;)} \message{EDEF=(\expandedpair{expanded})} \message{LITERAL=(\meaning\literal)} \message{FROZEN=(\meaning\saved)} \message{BRACE=(\braced{argument})} \message{TEMPLATE=(\meaning\pair)} \endgroup \end"; for profile in [InputProfile::TeX8Bit, InputProfile::XeTeX] { let state = State::initex().with_etex().with_input_profile(profile); let result = Executor::new(state, Limits::default()) .run( SourceFile { name: "marker.tex".into(), data: source.as_bytes().into(), }, &mut Host, ) .unwrap(); let messages = result .events .iter() .filter_map(|event| match event { Event::Message { text, .. } => Some(text.as_str()), _ => None, }) .collect::>(); assert_eq!( messages, [ "PAIR=(left:right)", "EDEF=(expanded)", r"LITERAL=(macro:->\alternate )", r"FROZEN=(macro:->\marker )", "BRACE=([prefix]{argument})", "TEMPLATE=(macro:#1,!2;->!1:!2)", ] ); } }