| 1 |
|
| 2 |
|
| 3 |
use super::*; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#[derive(Default)] |
| 8 |
struct Rec(Vec<String>); |
| 9 |
|
| 10 |
impl Perform for Rec { |
| 11 |
fn print(&mut self, c: char) { |
| 12 |
self.0.push(format!("print({c:?})")); |
| 13 |
} |
| 14 |
fn execute(&mut self, byte: u8) { |
| 15 |
self.0.push(format!("exec({byte:#04x})")); |
| 16 |
} |
| 17 |
fn csi_dispatch(&mut self, params: &Params, intermediates: &[u8], ignore: bool, action: char) { |
| 18 |
let p: Vec<Vec<u16>> = params.iter().map(<[u16]>::to_vec).collect(); |
| 19 |
self.0.push(format!( |
| 20 |
"csi(params={p:?}, intermediates={intermediates:?}, ignore={ignore}, action={action:?})" |
| 21 |
)); |
| 22 |
} |
| 23 |
fn esc_dispatch(&mut self, intermediates: &[u8], ignore: bool, byte: u8) { |
| 24 |
self.0.push(format!( |
| 25 |
"esc(intermediates={intermediates:?}, ignore={ignore}, byte={byte:#04x})" |
| 26 |
)); |
| 27 |
} |
| 28 |
fn osc_dispatch(&mut self, params: &[&[u8]], bell_terminated: bool) { |
| 29 |
let p: Vec<Vec<u8>> = params.iter().map(|s| s.to_vec()).collect(); |
| 30 |
self.0 |
| 31 |
.push(format!("osc(params={p:?}, bell={bell_terminated})")); |
| 32 |
} |
| 33 |
fn apc_dispatch(&mut self, data: &[u8]) { |
| 34 |
self.0.push(format!("apc({data:?})")); |
| 35 |
} |
| 36 |
fn hook(&mut self, params: &Params, intermediates: &[u8], ignore: bool, action: char) { |
| 37 |
let p: Vec<Vec<u16>> = params.iter().map(<[u16]>::to_vec).collect(); |
| 38 |
self.0.push(format!( |
| 39 |
"hook(params={p:?}, intermediates={intermediates:?}, ignore={ignore}, action={action:?})" |
| 40 |
)); |
| 41 |
} |
| 42 |
fn put(&mut self, byte: u8) { |
| 43 |
self.0.push(format!("put({byte:#04x})")); |
| 44 |
} |
| 45 |
fn unhook(&mut self) { |
| 46 |
self.0.push("unhook".into()); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
fn run(bytes: &[u8]) -> Vec<String> { |
| 51 |
let mut p = Parser::new(); |
| 52 |
let mut r = Rec::default(); |
| 53 |
p.advance(&mut r, bytes); |
| 54 |
r.0 |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
#[test] |
| 60 |
fn plain_ascii_prints() { |
| 61 |
assert_eq!(run(b"abc"), vec!["print('a')", "print('b')", "print('c')"]); |
| 62 |
} |
| 63 |
|
| 64 |
#[test] |
| 65 |
fn c0_control_executes() { |
| 66 |
assert_eq!(run(b"\r\n"), vec!["exec(0x0d)", "exec(0x0a)"]); |
| 67 |
} |
| 68 |
|
| 69 |
#[test] |
| 70 |
fn utf8_two_byte_prints_one_char() { |
| 71 |
|
| 72 |
assert_eq!(run(&[0xC3, 0xA9]), vec!["print('é')"]); |
| 73 |
} |
| 74 |
|
| 75 |
#[test] |
| 76 |
fn utf8_three_byte_prints_one_char() { |
| 77 |
|
| 78 |
assert_eq!(run(&[0xE2, 0x98, 0x83]), vec!["print('☃')"]); |
| 79 |
} |
| 80 |
|
| 81 |
#[test] |
| 82 |
fn utf8_four_byte_prints_one_char() { |
| 83 |
|
| 84 |
assert_eq!(run(&[0xF0, 0x9F, 0xA6, 0x80]), vec!["print('🦀')"]); |
| 85 |
} |
| 86 |
|
| 87 |
#[test] |
| 88 |
fn utf8_survives_chunk_boundary() { |
| 89 |
let mut p = Parser::new(); |
| 90 |
let mut r = Rec::default(); |
| 91 |
|
| 92 |
p.advance(&mut r, &[0xF0, 0x9F]); |
| 93 |
p.advance(&mut r, &[0xA6, 0x80]); |
| 94 |
assert_eq!(r.0, vec!["print('🦀')"]); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
#[test] |
| 100 |
fn csi_single_param() { |
| 101 |
assert_eq!( |
| 102 |
run(b"\x1b[10A"), |
| 103 |
vec!["csi(params=[[10]], intermediates=[], ignore=false, action='A')"] |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
#[test] |
| 108 |
fn csi_multi_params() { |
| 109 |
assert_eq!( |
| 110 |
run(b"\x1b[1;2;3m"), |
| 111 |
vec!["csi(params=[[1], [2], [3]], intermediates=[], ignore=false, action='m')"] |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
#[test] |
| 116 |
fn csi_subparams_colon() { |
| 117 |
|
| 118 |
assert_eq!( |
| 119 |
run(b"\x1b[38:2::1:2:3m"), |
| 120 |
vec!["csi(params=[[38, 2, 0, 1, 2, 3]], intermediates=[], ignore=false, action='m')"] |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
#[test] |
| 125 |
fn csi_private_marker() { |
| 126 |
assert_eq!( |
| 127 |
run(b"\x1b[?25h"), |
| 128 |
vec!["csi(params=[[25]], intermediates=[63], ignore=false, action='h')"] |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
#[test] |
| 133 |
fn csi_no_params() { |
| 134 |
assert_eq!( |
| 135 |
run(b"\x1b[m"), |
| 136 |
vec!["csi(params=[], intermediates=[], ignore=false, action='m')"] |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
#[test] |
| 141 |
fn csi_with_intermediate_space_q() { |
| 142 |
|
| 143 |
assert_eq!( |
| 144 |
run(b"\x1b[2 q"), |
| 145 |
vec!["csi(params=[[2]], intermediates=[32], ignore=false, action='q')"] |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
#[test] |
| 152 |
fn osc_st_terminated() { |
| 153 |
assert_eq!( |
| 154 |
run(b"\x1b]0;title\x1b\\"), |
| 155 |
vec![ |
| 156 |
"osc(params=[[48], [116, 105, 116, 108, 101]], bell=false)", |
| 157 |
|
| 158 |
"esc(intermediates=[], ignore=false, byte=0x5c)" |
| 159 |
] |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
#[test] |
| 164 |
fn osc_bel_terminated() { |
| 165 |
assert_eq!( |
| 166 |
run(b"\x1b]2;shop\x07"), |
| 167 |
vec!["osc(params=[[50], [115, 104, 111, 112]], bell=true)"] |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
#[test] |
| 174 |
fn apc_st_terminated() { |
| 175 |
let events = run(b"\x1b_Ga=T,f=32;PAYLOAD\x1b\\"); |
| 176 |
assert_eq!(events.len(), 2); |
| 177 |
assert!(events[0].starts_with("apc(")); |
| 178 |
assert!( |
| 179 |
events[0].contains("71"), |
| 180 |
"payload should carry 'G' (0x47=71)" |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
#[test] |
| 185 |
fn apc_bel_terminated() { |
| 186 |
let events = run(b"\x1b_hello\x07"); |
| 187 |
assert_eq!(events, vec!["apc([104, 101, 108, 108, 111])"]); |
| 188 |
} |
| 189 |
|
| 190 |
#[test] |
| 191 |
fn apc_survives_chunk_boundary() { |
| 192 |
let mut p = Parser::new(); |
| 193 |
let mut r = Rec::default(); |
| 194 |
p.advance(&mut r, b"\x1b_Ga="); |
| 195 |
assert!(r.0.is_empty(), "no dispatch mid-APC"); |
| 196 |
p.advance(&mut r, b"T;X\x07"); |
| 197 |
assert_eq!(r.0, vec!["apc([71, 97, 61, 84, 59, 88])"]); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
#[test] |
| 203 |
fn esc_bare_letter() { |
| 204 |
assert_eq!( |
| 205 |
run(b"\x1bM"), |
| 206 |
vec!["esc(intermediates=[], ignore=false, byte=0x4d)"] |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
#[test] |
| 213 |
fn dcs_passthrough_st_terminated() { |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
let events = run(b"\x1bP1$r0m\x1b\\"); |
| 218 |
let names: Vec<&str> = events |
| 219 |
.iter() |
| 220 |
.map(|s| s.split('(').next().unwrap()) |
| 221 |
.collect(); |
| 222 |
assert_eq!(names, vec!["hook", "put", "put", "unhook", "esc"]); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
#[test] |
| 228 |
fn cancel_aborts_escape() { |
| 229 |
|
| 230 |
let events = run(b"\x1b\x18X"); |
| 231 |
assert_eq!(events, vec!["exec(0x18)", "print('X')"]); |
| 232 |
} |
| 233 |
|
| 234 |
#[test] |
| 235 |
fn csi_ignoring_after_extra_intermediates() { |
| 236 |
|
| 237 |
let events = run(b"\x1b[!\"# X"); |
| 238 |
|
| 239 |
assert!( |
| 240 |
events.last().is_some_and(|s| s.contains("ignore=true")), |
| 241 |
"dispatch should carry ignore=true, got {events:?}" |
| 242 |
); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
#[test] |
| 251 |
fn csi_past_the_parameter_cap_is_ignored_not_buffered() { |
| 252 |
let mut p = Parser::new(); |
| 253 |
let mut r = Rec::default(); |
| 254 |
let mut seq = b"\x1b[".to_vec(); |
| 255 |
seq.extend(std::iter::repeat_n(b';', 100_000)); |
| 256 |
seq.push(b'm'); |
| 257 |
p.advance(&mut r, &seq); |
| 258 |
|
| 259 |
let event = r.0.last().expect("a dispatch").clone(); |
| 260 |
assert!(event.contains("ignore=true"), "got {event}"); |
| 261 |
assert!( |
| 262 |
p.buffered_bytes() < 8 * 1024, |
| 263 |
"parser kept {} bytes for 100k separators", |
| 264 |
p.buffered_bytes() |
| 265 |
); |
| 266 |
assert!(p.in_ground()); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
#[test] |
| 272 |
fn csi_at_the_parameter_cap_still_dispatches_every_slot() { |
| 273 |
let params: Vec<String> = (1..=MAX_PARAMS).map(|n| n.to_string()).collect(); |
| 274 |
let events = run(format!("\x1b[{}m", params.join(";")).as_bytes()); |
| 275 |
|
| 276 |
let event = events.last().expect("a dispatch"); |
| 277 |
assert!(event.contains("ignore=false"), "got {event}"); |
| 278 |
assert!(event.contains(&format!("[{MAX_PARAMS}]")), "got {event}"); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
#[test] |
| 288 |
fn an_oversized_osc_body_is_dropped_and_the_buffer_shrinks() { |
| 289 |
let mut p = Parser::new(); |
| 290 |
let mut r = Rec::default(); |
| 291 |
let mut seq = b"\x1b]0;".to_vec(); |
| 292 |
seq.extend(std::iter::repeat_n(b'A', MAX_STRING_BYTES + 1)); |
| 293 |
seq.push(0x07); |
| 294 |
p.advance(&mut r, &seq); |
| 295 |
|
| 296 |
assert!( |
| 297 |
r.0.is_empty(), |
| 298 |
"a body over the cap should dispatch nothing, got {:?}", |
| 299 |
r.0 |
| 300 |
); |
| 301 |
assert!( |
| 302 |
p.buffered_bytes() < 8 * 1024, |
| 303 |
"buffers stayed at {} bytes after the body ended", |
| 304 |
p.buffered_bytes() |
| 305 |
); |
| 306 |
assert!(p.in_ground()); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
#[test] |
| 312 |
fn an_ordinary_osc_body_still_dispatches() { |
| 313 |
let title = "t".repeat(100_000); |
| 314 |
let events = run(format!("\x1b]0;{title}\x07").as_bytes()); |
| 315 |
assert_eq!(events.len(), 1, "got {events:?}"); |
| 316 |
assert!(events[0].starts_with("osc("), "got {events:?}"); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
#[test] |
| 322 |
fn osc_separators_alone_do_not_grow_the_field_table() { |
| 323 |
let mut p = Parser::new(); |
| 324 |
let mut r = Rec::default(); |
| 325 |
let mut seq = b"\x1b]".to_vec(); |
| 326 |
seq.extend(std::iter::repeat_n(b';', 100_000)); |
| 327 |
seq.push(0x07); |
| 328 |
p.advance(&mut r, &seq); |
| 329 |
|
| 330 |
assert!(r.0.is_empty(), "got {:?}", r.0); |
| 331 |
assert!( |
| 332 |
p.buffered_bytes() < 64 * 1024, |
| 333 |
"parser kept {} bytes for 100k separators", |
| 334 |
p.buffered_bytes() |
| 335 |
); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
#[test] |
| 342 |
fn an_oversized_apc_body_is_dropped() { |
| 343 |
let mut p = Parser::new(); |
| 344 |
let mut r = Rec::default(); |
| 345 |
let mut seq = b"\x1b_G".to_vec(); |
| 346 |
seq.extend(std::iter::repeat_n(b'A', MAX_STRING_BYTES + 1)); |
| 347 |
seq.extend_from_slice(b"\x1b\\"); |
| 348 |
p.advance(&mut r, &seq); |
| 349 |
|
| 350 |
assert!(!r.0.iter().any(|e| e.starts_with("apc(")), "got {:?}", r.0); |
| 351 |
assert!(p.buffered_bytes() < 8 * 1024); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
#[test] |
| 362 |
fn the_bounds_are_the_numbers_the_module_documents() { |
| 363 |
assert_eq!(MAX_PARAMS, 32, "vte's number, so we are a drop-in for it"); |
| 364 |
assert_eq!(MAX_STRING_BYTES, 8_388_608, "8 MiB"); |
| 365 |
assert_eq!(MAX_OSC_PARAMS, 1024); |
| 366 |
assert_eq!(INITIAL_BODY_CAPACITY, 2048); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
#[test] |
| 376 |
fn params_len_and_is_empty_track_the_open_groups() { |
| 377 |
let mut p = Params::default(); |
| 378 |
assert!(p.is_empty()); |
| 379 |
assert_eq!(p.len(), 0); |
| 380 |
|
| 381 |
assert!(p.new_param()); |
| 382 |
assert!(!p.is_empty()); |
| 383 |
assert_eq!(p.len(), 1); |
| 384 |
|
| 385 |
assert!(p.new_param()); |
| 386 |
assert_eq!(p.len(), 2); |
| 387 |
|
| 388 |
p.clear(); |
| 389 |
assert!(p.is_empty()); |
| 390 |
assert_eq!(p.len(), 0); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
#[test] |
| 398 |
fn subparameters_consume_slots_so_the_cap_still_binds() { |
| 399 |
let mut p = Params::default(); |
| 400 |
assert!(p.new_param(), "the first slot"); |
| 401 |
for i in 1..MAX_PARAMS { |
| 402 |
assert!(p.new_subparam(), "slot {} of {MAX_PARAMS}", i + 1); |
| 403 |
} |
| 404 |
assert!( |
| 405 |
!p.new_subparam(), |
| 406 |
"slot {} is past the cap and must be refused", |
| 407 |
MAX_PARAMS + 1 |
| 408 |
); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
#[test] |
| 419 |
fn footprint_counts_the_outer_vec_and_every_group() { |
| 420 |
let p = Params::default(); |
| 421 |
assert_eq!(p.footprint(), 0, "an unused list holds nothing"); |
| 422 |
|
| 423 |
let mut p = Params::default(); |
| 424 |
assert!(p.new_param()); |
| 425 |
assert!(p.new_subparam()); |
| 426 |
assert!(p.new_param()); |
| 427 |
|
| 428 |
let outer = p.inner.capacity(); |
| 429 |
let groups: usize = p.inner.iter().map(Vec::capacity).sum(); |
| 430 |
assert!(outer > 0 && groups > 0, "the case has to be non-degenerate"); |
| 431 |
assert_eq!( |
| 432 |
p.footprint(), |
| 433 |
outer * std::mem::size_of::<Vec<u16>>() + groups * std::mem::size_of::<u16>() |
| 434 |
); |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
#[test] |
| 443 |
fn in_ground_is_false_while_a_sequence_is_open() { |
| 444 |
let mut p = Parser::new(); |
| 445 |
let mut r = Rec::default(); |
| 446 |
assert!(p.in_ground(), "a fresh parser holds nothing"); |
| 447 |
|
| 448 |
p.advance(&mut r, b"\x1b[1"); |
| 449 |
assert!(!p.in_ground(), "mid-CSI"); |
| 450 |
|
| 451 |
p.advance(&mut r, b"m"); |
| 452 |
assert!(p.in_ground(), "the sequence terminated"); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
#[test] |
| 458 |
fn buffered_bytes_sums_every_buffer_the_parser_holds() { |
| 459 |
let mut p = Parser::new(); |
| 460 |
let mut r = Rec::default(); |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
p.advance(&mut r, b"\x1bP1;2;3"); |
| 466 |
assert!(p.params.footprint() > 0, "the parameter term must be live"); |
| 467 |
|
| 468 |
let expected = p.osc_buf.capacity() |
| 469 |
+ p.apc_buf.capacity() |
| 470 |
+ p.osc_params.capacity() * std::mem::size_of::<(usize, usize)>() |
| 471 |
+ p.params.footprint(); |
| 472 |
assert!(expected > 1, "the case has to distinguish 0 and 1"); |
| 473 |
assert_eq!(p.buffered_bytes(), expected); |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
#[test] |
| 480 |
fn a_fresh_sequence_does_not_inherit_the_last_ones_parameters() { |
| 481 |
assert_eq!( |
| 482 |
run(b"\x1b[1;2m\x1b[m"), |
| 483 |
vec![ |
| 484 |
"csi(params=[[1], [2]], intermediates=[], ignore=false, action='m')", |
| 485 |
"csi(params=[], intermediates=[], ignore=false, action='m')", |
| 486 |
] |
| 487 |
); |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
#[test] |
| 495 |
fn can_and_sub_execute_in_ground() { |
| 496 |
assert_eq!(run(b"\x18"), vec!["exec(0x18)"]); |
| 497 |
assert_eq!(run(b"\x1a"), vec!["exec(0x1a)"]); |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
#[test] |
| 503 |
fn a_c0_inside_escape_executes_without_ending_the_sequence() { |
| 504 |
assert_eq!( |
| 505 |
run(b"\x1b\rB"), |
| 506 |
vec![ |
| 507 |
"exec(0x0d)", |
| 508 |
"esc(intermediates=[], ignore=false, byte=0x42)" |
| 509 |
] |
| 510 |
); |
| 511 |
} |
| 512 |
|
| 513 |
#[test] |
| 514 |
fn an_escape_intermediate_reaches_the_dispatch() { |
| 515 |
assert_eq!( |
| 516 |
run(b"\x1b(B"), |
| 517 |
vec!["esc(intermediates=[40], ignore=false, byte=0x42)"] |
| 518 |
); |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
#[test] |
| 523 |
fn escape_intermediates_accumulate() { |
| 524 |
assert_eq!( |
| 525 |
run(b"\x1b($B"), |
| 526 |
vec!["esc(intermediates=[40, 36], ignore=false, byte=0x42)"] |
| 527 |
); |
| 528 |
} |
| 529 |
|
| 530 |
#[test] |
| 531 |
fn a_c0_inside_an_escape_intermediate_executes() { |
| 532 |
assert_eq!( |
| 533 |
run(b"\x1b(\rB"), |
| 534 |
vec![ |
| 535 |
"exec(0x0d)", |
| 536 |
"esc(intermediates=[40], ignore=false, byte=0x42)" |
| 537 |
] |
| 538 |
); |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
#[test] |
| 546 |
fn sos_and_pm_open_a_string_and_dispatch_it() { |
| 547 |
assert_eq!( |
| 548 |
run(b"\x1bXhi\x1b\\"), |
| 549 |
vec![ |
| 550 |
"apc([104, 105])", |
| 551 |
"esc(intermediates=[], ignore=false, byte=0x5c)" |
| 552 |
] |
| 553 |
); |
| 554 |
assert_eq!( |
| 555 |
run(b"\x1b^hi\x1b\\"), |
| 556 |
vec![ |
| 557 |
"apc([104, 105])", |
| 558 |
"esc(intermediates=[], ignore=false, byte=0x5c)" |
| 559 |
] |
| 560 |
); |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
#[test] |
| 566 |
fn a_c0_inside_csi_entry_executes() { |
| 567 |
assert_eq!( |
| 568 |
run(b"\x1b[\rm"), |
| 569 |
vec![ |
| 570 |
"exec(0x0d)", |
| 571 |
"csi(params=[], intermediates=[], ignore=false, action='m')" |
| 572 |
] |
| 573 |
); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
#[test] |
| 580 |
fn a_leading_colon_opens_a_subparameter_slot() { |
| 581 |
assert_eq!( |
| 582 |
run(b"\x1b[:m"), |
| 583 |
vec!["csi(params=[[0]], intermediates=[], ignore=false, action='m')"] |
| 584 |
); |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
#[test] |
| 591 |
fn opening_a_slot_that_succeeds_does_not_mark_the_sequence_ignored() { |
| 592 |
assert_eq!( |
| 593 |
run(b"\x1b[:m"), |
| 594 |
vec!["csi(params=[[0]], intermediates=[], ignore=false, action='m')"] |
| 595 |
); |
| 596 |
assert_eq!( |
| 597 |
run(b"\x1b[;m"), |
| 598 |
vec!["csi(params=[[]], intermediates=[], ignore=false, action='m')"] |
| 599 |
); |
| 600 |
} |
| 601 |
|
| 602 |
#[test] |
| 603 |
fn a_c0_inside_csi_param_executes() { |
| 604 |
assert_eq!( |
| 605 |
run(b"\x1b[1\rm"), |
| 606 |
vec![ |
| 607 |
"exec(0x0d)", |
| 608 |
"csi(params=[[1]], intermediates=[], ignore=false, action='m')" |
| 609 |
] |
| 610 |
); |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
#[test] |
| 616 |
fn a_private_marker_after_a_parameter_abandons_the_sequence() { |
| 617 |
assert_eq!(run(b"\x1b[1<m"), Vec::<String>::new()); |
| 618 |
} |
| 619 |
|
| 620 |
#[test] |
| 621 |
fn a_c0_inside_a_csi_intermediate_executes() { |
| 622 |
assert_eq!( |
| 623 |
run(b"\x1b[ \rm"), |
| 624 |
vec![ |
| 625 |
"exec(0x0d)", |
| 626 |
"csi(params=[], intermediates=[32], ignore=false, action='m')" |
| 627 |
] |
| 628 |
); |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
#[test] |
| 634 |
fn a_parameter_after_an_intermediate_abandons_the_sequence() { |
| 635 |
assert_eq!(run(b"\x1b[ 1m"), Vec::<String>::new()); |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
#[test] |
| 642 |
fn an_abandoned_csi_still_executes_c0s_and_still_ends() { |
| 643 |
assert_eq!(run(b"\x1b[1<\rma"), vec!["exec(0x0d)", "print('a')"]); |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
#[test] |
| 649 |
fn a_bare_dcs_hooks_and_unhooks() { |
| 650 |
assert_eq!( |
| 651 |
run(b"\x1bPq\x1b\\"), |
| 652 |
vec![ |
| 653 |
"hook(params=[], intermediates=[], ignore=false, action='q')", |
| 654 |
"unhook", |
| 655 |
"esc(intermediates=[], ignore=false, byte=0x5c)", |
| 656 |
] |
| 657 |
); |
| 658 |
} |
| 659 |
|
| 660 |
#[test] |
| 661 |
fn dcs_passthrough_delivers_the_body_and_a_bell_terminates_it() { |
| 662 |
assert_eq!( |
| 663 |
run(b"\x1bPqAB\x07"), |
| 664 |
vec![ |
| 665 |
"hook(params=[], intermediates=[], ignore=false, action='q')", |
| 666 |
"put(0x41)", |
| 667 |
"put(0x42)", |
| 668 |
"unhook", |
| 669 |
] |
| 670 |
); |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
#[test] |
| 676 |
fn dcs_parameters_parse_as_numbers() { |
| 677 |
assert_eq!( |
| 678 |
run(b"\x1bP1q\x1b\\")[0], |
| 679 |
"hook(params=[[1]], intermediates=[], ignore=false, action='q')" |
| 680 |
); |
| 681 |
assert_eq!( |
| 682 |
run(b"\x1bP12q\x1b\\")[0], |
| 683 |
"hook(params=[[12]], intermediates=[], ignore=false, action='q')" |
| 684 |
); |
| 685 |
assert_eq!( |
| 686 |
run(b"\x1bP1;2q\x1b\\")[0], |
| 687 |
"hook(params=[[1], [2]], intermediates=[], ignore=false, action='q')" |
| 688 |
); |
| 689 |
} |
| 690 |
|
| 691 |
#[test] |
| 692 |
fn a_dcs_intermediate_reaches_the_hook() { |
| 693 |
assert_eq!( |
| 694 |
run(b"\x1bP$q\x1b\\")[0], |
| 695 |
"hook(params=[], intermediates=[36], ignore=false, action='q')" |
| 696 |
); |
| 697 |
assert_eq!( |
| 698 |
run(b"\x1bP1$q\x1b\\")[0], |
| 699 |
"hook(params=[[1]], intermediates=[36], ignore=false, action='q')" |
| 700 |
); |
| 701 |
assert_eq!( |
| 702 |
run(b"\x1bP$$q\x1b\\")[0], |
| 703 |
"hook(params=[], intermediates=[36, 36], ignore=false, action='q')" |
| 704 |
); |
| 705 |
} |
| 706 |
|
| 707 |
#[test] |
| 708 |
fn a_dcs_private_marker_reaches_the_hook() { |
| 709 |
assert_eq!( |
| 710 |
run(b"\x1bP?q\x1b\\")[0], |
| 711 |
"hook(params=[], intermediates=[63], ignore=false, action='q')" |
| 712 |
); |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
#[test] |
| 718 |
fn a_leading_dcs_separator_opens_an_empty_parameter() { |
| 719 |
assert_eq!( |
| 720 |
run(b"\x1bP;q\x1b\\")[0], |
| 721 |
"hook(params=[[]], intermediates=[], ignore=false, action='q')" |
| 722 |
); |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
#[test] |
| 729 |
fn an_illegal_dcs_prelude_abandons_the_sequence() { |
| 730 |
for seq in [ |
| 731 |
&b"\x1bP:q\x1b\\"[..], |
| 732 |
&b"\x1bP1:q\x1b\\"[..], |
| 733 |
&b"\x1bP1<q\x1b\\"[..], |
| 734 |
&b"\x1bP$1q\x1b\\"[..], |
| 735 |
] { |
| 736 |
let log = run(seq); |
| 737 |
assert!( |
| 738 |
!log.iter().any(|e| e.starts_with("hook(")), |
| 739 |
"{seq:?} must not hook, got {log:?}" |
| 740 |
); |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
#[test] |
| 750 |
fn the_first_dcs_digit_is_a_value_and_not_a_quotient() { |
| 751 |
assert_eq!( |
| 752 |
run(b"\x1bP2q\x1b\\")[0], |
| 753 |
"hook(params=[[2]], intermediates=[], ignore=false, action='q')" |
| 754 |
); |
| 755 |
assert_eq!( |
| 756 |
run(b"\x1bP9q\x1b\\")[0], |
| 757 |
"hook(params=[[9]], intermediates=[], ignore=false, action='q')" |
| 758 |
); |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
#[test] |
| 768 |
fn an_abandoned_dcs_swallows_everything_that_is_not_the_terminator() { |
| 769 |
assert_eq!(run(b"\x1bP:qB"), Vec::<String>::new()); |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
#[test] |
| 776 |
fn an_abandoned_dcs_ends_on_the_terminator_and_prints_again() { |
| 777 |
assert_eq!( |
| 778 |
run(b"\x1bP:q\x1b\\a"), |
| 779 |
vec![ |
| 780 |
"esc(intermediates=[], ignore=false, byte=0x5c)", |
| 781 |
"print('a')" |
| 782 |
] |
| 783 |
); |
| 784 |
} |
| 785 |
|