use super::slice_ansi;
#[test]
fn plain_slice() {
assert_eq!(slice_ansi("hello world", 0, Some(5)), "hello");
assert_eq!(slice_ansi("hello world", 6, None), "world");
}
#[test]
fn colored_slice_reopen_close() {
assert_eq!(
slice_ansi("\x1b[31mhello\x1b[39m", 1, Some(3)),
"\x1b[31mel\x1b[39m"
);
}
#[test]
fn mid_string_picks_up_active_styles() {
assert_eq!(
slice_ansi("\x1b[31mhello world\x1b[39m", 6, Some(11)),
"\x1b[31mworld\x1b[39m"
);
}
#[test]
fn cjk_straddle_excluded() {
assert_eq!(slice_ansi("a中b", 0, Some(2)), "a");
assert_eq!(slice_ansi("a中b", 0, Some(3)), "a中");
}
#[test]
fn emoji_zwj_not_split() {
let family = "👨👩👧👦";
assert_eq!(slice_ansi(&format!("{family}x"), 0, Some(2)), family);
assert_eq!(
slice_ansi(&format!("{family}x"), 0, Some(3)),
format!("{family}x")
);
}
#[test]
fn flag_emoji_regional_indicators() {
assert_eq!(slice_ansi("🇸🇪x", 0, Some(2)), "🇸🇪");
}
#[test]
fn hyperlink_included() {
let input = "\x1b]8;;https://x.com\x07link\x1b]8;;\x07";
assert_eq!(slice_ansi(input, 0, Some(4)), input);
}
#[test]
fn hyperlink_excluded_and_discarded() {
let input = "ab\x1b]8;;https://x.com\x07link\x1b]8;;\x07";
assert_eq!(slice_ansi(input, 0, Some(2)), "ab");
}
#[test]
fn hyperlink_empty_discarded() {
let input = "\x1b]8;;https://x.com\x07\x1b]8;;\x07ab";
assert_eq!(slice_ansi(input, 0, Some(2)), "ab");
}
#[test]
fn hyperlink_closed_at_end() {
let input = "\x1b]8;;https://x.com\x07link text\x1b]8;;\x07";
assert_eq!(
slice_ansi(input, 0, Some(4)),
"\x1b]8;;https://x.com\x07link\x1b]8;;\x07"
);
}
#[test]
fn color256_preserved() {
assert_eq!(
slice_ansi("\x1b[38;5;200mhello\x1b[39m", 0, Some(3)),
"\x1b[38;5;200mhel\x1b[39m"
);
}
#[test]
fn truecolor_preserved() {
assert_eq!(
slice_ansi("\x1b[38;2;255;0;0mhello\x1b[39m", 0, Some(3)),
"\x1b[38;2;255;0;0mhel\x1b[39m"
);
}
#[test]
fn end_none_to_end() {
assert_eq!(slice_ansi("hello world", 6, None), "world");
}
#[test]
fn start_beyond_string() {
assert_eq!(slice_ansi("hello", 10, Some(20)), "");
}
#[test]
fn ansi_split_grapheme_fallback() {
let input = "e\x1b[31m\u{301}x";
assert_eq!(slice_ansi(input, 0, Some(1)), "e\x1b[31m\u{301}\x1b[39m");
}
#[test]
fn control_sequence_before_visible_dropped() {
assert_eq!(slice_ansi("\x1b]0;title\x07hello", 0, Some(3)), "hel");
}
#[test]
fn control_sequence_inline_passthrough() {
assert_eq!(
slice_ansi("ab\x1b]0;t\x07cd", 0, Some(4)),
"ab\x1b]0;t\x07cd"
);
}
#[test]
fn pending_sgr_rollback() {
assert_eq!(slice_ansi("ab\x1b[31mcd\x1b[39m", 0, Some(2)), "ab");
}
#[test]
fn c1_csi_sequences() {
assert_eq!(
slice_ansi("\u{9b}31mhello\u{9b}39m", 1, Some(3)),
"\u{9b}31mel\x1b[39m"
);
}
#[test]
fn reset_mid_string_clears() {
assert_eq!(
slice_ansi("\x1b[31mhe\x1b[0mllo", 0, Some(4)),
"\x1b[31mhe\x1b[0mll"
);
}
#[test]
fn bold_dim_map_collision() {
assert_eq!(
slice_ansi("\x1b[1m\x1b[2mhi\x1b[22m", 0, Some(2)),
"\x1b[2mhi\x1b[22m"
);
}
#[test]
fn distinct_modifiers_coexist() {
assert_eq!(
slice_ansi("\x1b[1m\x1b[4mhi\x1b[24m\x1b[22m", 0, Some(2)),
"\x1b[1m\x1b[4mhi\x1b[24m\x1b[22m"
);
}
#[test]
fn trailing_ansi_no_closing_effect_dropped() {
assert_eq!(slice_ansi("ab\x1b[39m", 0, Some(2)), "ab");
}
#[test]
fn trailing_ansi_closes_active_style() {
assert_eq!(
slice_ansi("\x1b[31mab\x1b[39m", 0, Some(2)),
"\x1b[31mab\x1b[39m"
);
}
#[test]
fn empty_input() {
assert_eq!(slice_ansi("", 0, Some(5)), "");
}
#[test]
fn zero_width_slice() {
assert_eq!(slice_ansi("hello", 0, Some(0)), "");
}
#[test]
fn generic_osc_control_string_is_zero_width() {
let t = "\x1b]0;window title\x07visible after";
assert_eq!(slice_ansi(t, 12, Some(13)), "r");
assert_eq!(slice_ansi(t, 0, Some(2)), "vi");
}
#[test]
fn slice_ansi_begin_greater_than_end_returns_empty() {
assert_eq!(slice_ansi("\x1b[31mabcdef\x1b[39m", 4, Some(2)), "");
assert_eq!(slice_ansi("hello world", 5, Some(2)), "");
}