use super::*;
use test_support::production;
#[test]
fn raw_and_paste_are_owned_by_one_guard() {
let src = production();
assert_eq!(
src.matches("enable_raw_mode()").count(),
0,
"raw mode comes from RawModeGuard, never from crossterm directly"
);
assert_eq!(
src.matches("disable_raw_mode();").count(),
0,
"…and is released by the field, never by a statement here"
);
assert!(
src.contains("_raw: RawModeGuard"),
"RawPasteGuard must HOLD a RawModeGuard — composition, not a \
reimplementation"
);
assert_eq!(
src.matches("EnableBracketedPaste)").count(),
1,
"bracketed paste is enabled in exactly one place"
);
assert_eq!(
src.matches("DisableBracketedPaste)").count(),
1,
"…and disabled in exactly one place"
);
assert!(
src.contains("impl Drop for RawPasteGuard"),
"the restore must be a Drop obligation, not a method someone has \
to remember to call — which is what #1411 cleared this site for"
);
}
#[test]
fn the_guard_releases_paste_before_raw_mode() {
let src = production();
let drop_impl = src
.split_once("impl Drop for RawPasteGuard")
.expect("the guard must restore from Drop")
.1;
let body = &drop_impl[..drop_impl.find("\n}").unwrap_or(drop_impl.len())];
assert!(
body.contains("DisableBracketedPaste"),
"Drop must release bracketed paste in its own body"
);
assert!(
!body.contains("disable_raw_mode();"),
"releasing raw mode in the body would run BEFORE the field drops, \
handing line discipline back with paste markers still armed"
);
assert!(
src.contains("_raw: RawModeGuard"),
"raw mode must be a FIELD, so it drops after the body"
);
}
#[test]
fn a_drop_body_runs_before_its_fields_drop() {
use std::cell::RefCell;
thread_local! {
static ORDER: RefCell<Vec<&'static str>> = const { RefCell::new(Vec::new()) };
}
struct Field;
impl Drop for Field {
fn drop(&mut self) {
ORDER.with(|o| o.borrow_mut().push("field"));
}
}
struct Outer {
_f: Field,
}
impl Drop for Outer {
fn drop(&mut self) {
ORDER.with(|o| o.borrow_mut().push("body"));
}
}
drop(Outer { _f: Field });
ORDER.with(|o| assert_eq!(*o.borrow(), ["body", "field"]));
}