#[cfg(test)]
mod tests {
use r3bl_rs_utils_core::*;
use crate::*;
#[test]
fn editor_delete() {
let mut buffer = EditorBuffer::default();
let mut engine = make_editor_engine();
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::InsertString("abc".into()),
EditorBufferCommand::InsertNewLine,
EditorBufferCommand::InsertString("ab".into()),
EditorBufferCommand::InsertNewLine,
EditorBufferCommand::InsertString("a".into()),
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 1, row: 2)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::MoveCaret(CaretDirection::Left),
EditorBufferCommand::Delete,
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 2)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::MoveCaret(CaretDirection::Up),
EditorBufferCommand::MoveCaret(CaretDirection::Right),
EditorBufferCommand::MoveCaret(CaretDirection::Right),
EditorBufferCommand::Delete,
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(buffer.get_lines().len(), 2);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 2, row: 1)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::MoveCaret(CaretDirection::Up),
EditorBufferCommand::MoveCaret(CaretDirection::Right),
EditorBufferCommand::Delete,
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(buffer.get_lines().len(), 1);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 3, row: 0)
);
assert::line_at_caret(&buffer, &engine, "abcab");
}
#[test]
fn editor_backspace() {
let mut buffer = EditorBuffer::default();
let mut engine = make_editor_engine();
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::InsertString("abc".into()),
EditorBufferCommand::InsertNewLine,
EditorBufferCommand::InsertString("ab".into()),
EditorBufferCommand::InsertNewLine,
EditorBufferCommand::InsertString("a".into()),
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 1, row: 2)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::Backspace],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 2)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::Backspace],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 2, row: 1)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::MoveCaret(CaretDirection::Left),
EditorBufferCommand::MoveCaret(CaretDirection::Left),
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 1)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::Backspace],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(buffer.get_lines().len(), 1);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 3, row: 0)
);
assert::line_at_caret(&buffer, &engine, "abcab");
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::MoveCaret(CaretDirection::Right),
EditorBufferCommand::MoveCaret(CaretDirection::Right),
EditorBufferCommand::InsertString("😃".into()),
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 7, row: 0)
);
EditorBuffer::apply_editor_event(
&mut engine,
&mut buffer,
EditorBufferCommand::Backspace,
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert::line_at_caret(&buffer, &engine, "abcab");
}
#[test]
fn editor_validate_caret_position_on_up() {
let mut buffer = EditorBuffer::default();
let mut engine = make_editor_engine();
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::InsertString("😀".into()),
EditorBufferCommand::InsertNewLine,
EditorBufferCommand::InsertChar('1'),
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 1, row: 1)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::MoveCaret(CaretDirection::Up)],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 2, row: 0)
);
}
#[test]
fn editor_validate_caret_position_on_down() {
let mut buffer = EditorBuffer::default();
let mut engine = make_editor_engine();
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::InsertChar('1'),
EditorBufferCommand::InsertNewLine,
EditorBufferCommand::InsertString("😀".into()),
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 2, row: 1)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::MoveCaret(CaretDirection::Up),
EditorBufferCommand::MoveCaret(CaretDirection::Right),
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 1)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::MoveCaret(CaretDirection::Down)],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 2, row: 1)
);
}
#[test]
fn editor_move_caret_up_down() {
let mut buffer = EditorBuffer::default();
let mut engine = make_editor_engine();
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::InsertString("abc".into()),
EditorBufferCommand::InsertNewLine,
EditorBufferCommand::InsertString("ab".into()),
EditorBufferCommand::InsertNewLine,
EditorBufferCommand::InsertString("a".into()),
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 1, row: 2)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::MoveCaret(CaretDirection::Down),
EditorBufferCommand::MoveCaret(CaretDirection::Down),
EditorBufferCommand::MoveCaret(CaretDirection::Down),
EditorBufferCommand::MoveCaret(CaretDirection::Down),
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 1, row: 2)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::MoveCaret(CaretDirection::Up)],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 1, row: 1)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::MoveCaret(CaretDirection::Up)],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 1, row: 0)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::MoveCaret(CaretDirection::Up),
EditorBufferCommand::MoveCaret(CaretDirection::Up),
EditorBufferCommand::MoveCaret(CaretDirection::Up),
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 0)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::MoveCaret(CaretDirection::Right),
EditorBufferCommand::MoveCaret(CaretDirection::Right),
EditorBufferCommand::MoveCaret(CaretDirection::Down),
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 2, row: 1)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::MoveCaret(CaretDirection::Down)],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 1, row: 2)
);
}
#[test]
fn editor_insert_new_line() {
let mut buffer = EditorBuffer::default();
let mut engine = make_editor_engine();
assert_eq2!(buffer.get_lines().len(), 1);
assert_eq2!(buffer.get_lines().len(), 1);
assert::none_is_at_caret(&buffer, &engine);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::InsertChar('a')],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert::none_is_at_caret(&buffer, &engine);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 1, row: 0)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::InsertNewLine],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(buffer.get_lines().len(), 2);
assert::none_is_at_caret(&buffer, &engine);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 1)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::InsertChar('a')],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::MoveCaret(CaretDirection::Left)],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert::str_is_at_caret(&buffer, &engine, "a");
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::InsertNewLine],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(buffer.get_lines().len(), 3);
assert::str_is_at_caret(&buffer, &engine, "a");
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 2)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::MoveCaret(CaretDirection::Right),
EditorBufferCommand::InsertChar('b'),
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert::none_is_at_caret(&buffer, &engine);
assert_eq2!(
get_content::line_at_caret_to_string(&buffer, &engine)
.unwrap()
.string,
"ab"
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::MoveCaret(CaretDirection::Left),
EditorBufferCommand::InsertNewLine,
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert::str_is_at_caret(&buffer, &engine, "b");
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 3)
);
assert_eq2!(buffer.get_lines().len(), 4);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::MoveCaret(CaretDirection::Up),
EditorBufferCommand::MoveCaret(CaretDirection::Right),
EditorBufferCommand::InsertNewLine,
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(buffer.get_lines().len(), 5);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 3)
);
}
#[test]
fn editor_move_caret_left_right() {
let mut buffer = EditorBuffer::default();
let mut engine = make_editor_engine();
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::InsertChar('a')],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert::none_is_at_caret(&buffer, &engine);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::MoveCaret(CaretDirection::Left),
EditorBufferCommand::MoveCaret(CaretDirection::Left), ],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert::str_is_at_caret(&buffer, &engine, "a");
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::InsertChar('1')],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
get_content::line_at_caret_to_string(&buffer, &engine)
.unwrap()
.string,
"1a"
);
assert::str_is_at_caret(&buffer, &engine, "a");
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::MoveCaret(CaretDirection::Left)],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert::str_is_at_caret(&buffer, &engine, "1");
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::MoveCaret(CaretDirection::Right)],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert::str_is_at_caret(&buffer, &engine, "a");
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::InsertChar('2')],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert::str_is_at_caret(&buffer, &engine, "a");
assert_eq2!(
get_content::line_at_caret_to_string(&buffer, &engine)
.unwrap()
.string,
"12a"
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::MoveCaret(CaretDirection::Right),
EditorBufferCommand::MoveCaret(CaretDirection::Right), ],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert::none_is_at_caret(&buffer, &engine);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 3, row: 0)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::MoveCaret(CaretDirection::Left),
EditorBufferCommand::MoveCaret(CaretDirection::Left),
EditorBufferCommand::MoveCaret(CaretDirection::Left),
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 0)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::MoveCaret(CaretDirection::Right),
EditorBufferCommand::MoveCaret(CaretDirection::Right),
EditorBufferCommand::MoveCaret(CaretDirection::Right),
EditorBufferCommand::InsertNewLine,
EditorBufferCommand::MoveCaret(CaretDirection::Left),
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 3, row: 0)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::MoveCaret(CaretDirection::Right)],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 1)
);
}
#[test]
fn editor_empty_state() {
let buffer = EditorBuffer::default();
assert_eq2!(buffer.get_lines().len(), 1);
assert!(!buffer.is_empty());
}
#[test]
fn editor_insertion() {
let mut buffer = EditorBuffer::default();
let mut engine = make_editor_engine();
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 0)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::InsertChar('a')],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(*buffer.get_lines(), vec![UnicodeString::from("a")]);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 1, row: 0)
);
mut_content::insert_new_line_at_caret(EditorArgsMut {
buffer: &mut buffer,
engine: &mut engine,
});
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::InsertChar('b')],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
*buffer.get_lines(),
vec![UnicodeString::from("a"), UnicodeString::from("b")]
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 1, row: 1)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::InsertNewLine,
EditorBufferCommand::InsertNewLine,
EditorBufferCommand::InsertChar('😀'),
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
*buffer.get_lines(),
vec![
UnicodeString::from("a"),
UnicodeString::from("b"),
UnicodeString::from(""),
UnicodeString::from("😀")
]
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 2, row: 3)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::InsertChar('d')],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
*buffer.get_lines(),
vec![
UnicodeString::from("a"),
UnicodeString::from("b"),
UnicodeString::from(""),
UnicodeString::from("😀d")
]
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 3, row: 3)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::InsertString("🙏🏽".into())],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
*buffer.get_lines(),
vec![
UnicodeString::from("a"),
UnicodeString::from("b"),
UnicodeString::from(""),
UnicodeString::from("😀d🙏🏽")
]
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 7, row: 3)
);
}
#[test]
fn editor_move_caret_home_end() {
let mut buffer = EditorBuffer::default();
let mut engine = make_editor_engine();
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::InsertString("hello".to_string()),
EditorBufferCommand::Home,
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 0)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::End],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 5, row: 0)
);
}
#[test]
fn editor_move_caret_page_up_page_down() {
let mut buffer = EditorBuffer::default();
let mut engine = make_editor_engine();
let max_lines = 20;
let mut count = max_lines;
while count > 0 {
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::InsertString(format!("{}: {}", count, "hello")),
EditorBufferCommand::InsertNewLine,
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
count -= 1;
}
assert_eq2!(buffer.len(), ch!(max_lines + 1));
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::PageUp],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 10)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::PageUp],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 0)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::PageUp],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 0)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::PageDown],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 10)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::PageDown],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 20)
);
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::PageDown],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 20)
);
}
#[test]
fn editor_scroll_vertical() {
let mut buffer = EditorBuffer::default();
let mut engine = make_editor_engine();
let max_lines = 20;
for count in 1..=max_lines {
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![
EditorBufferCommand::InsertString(format!("{}: {}", count, "hello")),
EditorBufferCommand::InsertNewLine,
],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
}
assert_eq2!(buffer.len(), ch!(max_lines + 1));
for _ in 1..12 {
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::MoveCaret(CaretDirection::Up)],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
}
assert_eq2!(buffer.get_caret(CaretKind::Raw), position!(col: 0, row: 0));
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 9)
);
assert_eq2!(buffer.get_scroll_offset(), position!(col: 0, row: 9));
for _ in 1..9 {
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::MoveCaret(CaretDirection::Down)],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
}
assert_eq2!(buffer.get_caret(CaretKind::Raw), position!(col: 0, row: 8));
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 0, row: 17)
);
assert_eq2!(buffer.get_scroll_offset(), position!(col: 0, row: 9));
}
#[test]
fn editor_scroll_horizontal() {
let mut buffer = EditorBuffer::default();
let mut engine = make_editor_engine();
let max_cols = 15;
for count in 1..=max_cols {
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::InsertString(format!("{}", count))],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
}
assert_eq2!(buffer.len(), ch!(1));
assert_eq2!(buffer.get_caret(CaretKind::Raw), position!(col: 9, row: 0));
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 21, row: 0)
);
assert_eq2!(buffer.get_scroll_offset(), position!(col: 12, row: 0));
for _ in 1..5 {
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::MoveCaret(CaretDirection::Left)],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
}
assert_eq2!(buffer.get_caret(CaretKind::Raw), position!(col: 5, row: 0));
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 17, row: 0)
);
assert_eq2!(buffer.get_scroll_offset(), position!(col: 12, row: 0));
for _ in 1..3 {
EditorBuffer::apply_editor_events(
&mut engine,
&mut buffer,
vec![EditorBufferCommand::MoveCaret(CaretDirection::Right)],
&make_shared_tw_data(),
&mut make_component_registry(),
"",
);
}
assert_eq2!(buffer.get_caret(CaretKind::Raw), position!(col: 7, row: 0));
assert_eq2!(
buffer.get_caret(CaretKind::ScrollAdjusted),
position!(col: 19, row: 0)
);
assert_eq2!(buffer.get_scroll_offset(), position!(col: 12, row: 0));
}
mod mock_real_objects {
use super::*;
pub fn make_shared_tw_data() -> SharedTWData {
use std::sync::Arc;
use tokio::sync::RwLock;
let shared_tw_data: SharedTWData = Arc::new(RwLock::new(TWData::default()));
shared_tw_data
}
pub fn make_component_registry() -> ComponentRegistry<String, String> {
let component_registry: ComponentRegistry<String, String> = ComponentRegistry::default();
component_registry
}
pub fn make_editor_engine() -> EditorRenderEngine {
EditorRenderEngine {
current_box: FlexBox {
style_adjusted_bounds_size: size!( cols: 10, rows: 10 ),
style_adjusted_origin_pos: position!( col: 0, row: 0 ),
..Default::default()
},
}
}
}
use mock_real_objects::*;
mod assert {
use super::*;
pub fn none_is_at_caret(buffer: &EditorBuffer, engine: &EditorRenderEngine) {
assert_eq2!(get_content::string_at_caret(buffer, engine), None);
}
pub fn str_is_at_caret(
editor_buffer: &EditorBuffer,
engine: &EditorRenderEngine,
expected: &str,
) {
match get_content::string_at_caret(editor_buffer, engine) {
Some(UnicodeStringSegmentSliceResult {
unicode_string_seg: s,
..
}) => assert_eq2!(s.string, expected),
None => panic!("Expected string at caret, but got None."),
}
}
pub fn line_at_caret(
editor_buffer: &EditorBuffer,
engine: &EditorRenderEngine,
expected: &str,
) {
assert_eq2!(
get_content::line_at_caret_to_string(editor_buffer, engine)
.unwrap()
.string,
expected
);
}
}
}