Expand native Unicode character primitives and definitions

This commit is contained in:
gloamxun committed 2026-11-21 11:58:23 +08:00
1 parent d17680fcd8
commit 651fab42f1
7 files changed
+227 -31

No files matched your search

@@ -0,0 +1,109 @@
use texrra_tex_core::execution::*;
fn source(text: &str) -> SourceFile {
SourceFile {
name: "characters.tex".into(),
data: text.as_bytes().into(),
}
}
fn state(profile: InputProfile) -> State {
State::initex().with_etex().with_input_profile(profile)
}
const SEED: &str = r#"
\catcode123=1 \catcode125=2
\chardef\allocation=256 \chardef\greek="03B1 \chardef\high="1D6FC
\chardef\surrogate="D800
\edef\letterToken{\Ucharcat"03B1 11 }
\edef\otherToken{\Uchar"03B1 }
\edef\highToken{\Uchar"1D6FC }
\expandafter\def\Ucharcat"03B1 13 {active}
\edef\activeToken{\Ucharcat"03B1 13 }
\edef\spaced{a\Uchar32 b}
\edef\noncanonical{a\Ucharcat65 10 b}
\dump
"#;
#[test]
fn wide_character_definitions_and_generated_tokens_survive_a_format_roundtrip() {
let state = state(InputProfile::UnicodeScalar);
let dump = Executor::new(state.clone(), Limits::default())
.run(source(SEED), &mut NoFiles)
.unwrap();
let data = FormatImage::from_dump(dump, Vec::new(), Vec::new())
.unwrap()
.encode()
.unwrap();
let result = FormatImage::decode(&data)
.unwrap()
.new_job(
state.profile(),
state.input_profile(),
JobTime::new(2026, 1, 1, 0).unwrap(),
Limits::default(),
|_| Ok(SEED.as_bytes().to_vec()),
)
.unwrap()
.run(
source(
r"\message{\number\allocation/\number\greek/\number\high}
\message{\meaning\high}
\message{\meaning\letterToken/\meaning\otherToken/\meaning\highToken}
\message{\ifx\letterToken\otherToken same\else distinct\fi}
\message{\activeToken/\spaced/\noncanonical/\number\surrogate}
\end",
),
&mut NoFiles,
)
.unwrap();
let messages: Vec<_> = result
.events
.iter()
.filter_map(|event| match event {
Event::Message { text, .. } => Some(text.as_str()),
_ => None,
})
.collect();
assert_eq!(
messages,
[
"256/945/120572",
r#"\char"1D6FC"#,
"macro:->α/macro:->α/macro:->𝛼",
"distinct",
"active/a b/aAb/55296",
]
);
}
#[test]
fn character_ranges_and_primitive_availability_follow_the_input_profile() {
for (profile, input, expected) in [
(InputProfile::TeX8Bit, r"\chardef\x=256", "0..255"),
(
InputProfile::TeX8Bit,
r"\Uchar65 ",
"undefined native control sequence",
),
(
InputProfile::UnicodeScalar,
r"\chardef\x=1114112",
"0..1114111",
),
(
InputProfile::UnicodeScalar,
r"\Ucharcat65 14 ",
"invalid category code",
),
] {
let error = Executor::new(state(profile), Limits::default())
.run(source(input), &mut NoFiles)
.unwrap_err();
assert!(
error.message.contains(expected),
"{input}: {}",
error.message
);
}
}