use super::*;
fn make_buffer(content: &str) -> Buffer {
Buffer::from_string(content)
}
fn make_cursor(line: usize, column: usize) -> Cursor {
Cursor::new(Position::new(line, column))
}
#[test]
fn test_char_motion_forward() {
let buffer = make_buffer("hello");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Char(Direction::Forward), 1);
assert_eq!(pos, Some(Position::new(0, 1)));
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Char(Direction::Forward), 3);
assert_eq!(pos, Some(Position::new(0, 3)));
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Char(Direction::Forward), 10);
assert_eq!(pos, Some(Position::new(0, 4))); }
#[test]
fn test_char_motion_backward() {
let buffer = make_buffer("hello");
let cursor = make_cursor(0, 4);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Char(Direction::Backward), 1);
assert_eq!(pos, Some(Position::new(0, 3)));
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Char(Direction::Backward), 10);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_line_motion() {
let buffer = make_buffer("line1\nline2\nline3");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Line(Direction::Forward), 1);
assert_eq!(pos, Some(Position::new(1, 0)));
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Line(Direction::Forward), 2);
assert_eq!(pos, Some(Position::new(2, 0)));
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Line(Direction::Forward), 10);
assert_eq!(pos, Some(Position::new(2, 0)));
}
#[test]
fn test_line_position() {
let buffer = make_buffer(" hello world ");
let cursor = make_cursor(0, 5);
let pos =
MotionEngine::calculate(&buffer, &cursor, Motion::LinePosition(LinePosition::Start), 1);
assert_eq!(pos, Some(Position::new(0, 0)));
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::LinePosition(LinePosition::FirstNonBlank),
1,
);
assert_eq!(pos, Some(Position::new(0, 2)));
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::LinePosition(LinePosition::End), 1);
assert_eq!(pos, Some(Position::new(0, 14)));
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::LinePosition(LinePosition::LastNonBlank),
1,
);
assert_eq!(pos, Some(Position::new(0, 12))); }
#[test]
fn test_word_forward() {
let buffer = make_buffer("hello world foo");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 6))); }
#[test]
fn test_word_backward() {
let buffer = make_buffer("hello world foo");
let cursor = make_cursor(0, 12);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 6))); }
#[test]
fn test_word_end() {
let buffer = make_buffer("hello world foo");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 4))); }
#[test]
fn test_jump_line() {
let buffer = make_buffer("line0\nline1\nline2");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::JumpLine(None), 1);
assert_eq!(pos, Some(Position::new(2, 0)));
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::JumpLine(Some(1)), 1);
assert_eq!(pos, Some(Position::new(1, 0)));
}
#[test]
fn test_match_bracket() {
let buffer = make_buffer("(hello)");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 6)));
let cursor = make_cursor(0, 6);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 0))); }
#[test]
fn test_match_bracket_nested() {
let buffer = make_buffer("((inner))");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 8))); }
#[test]
fn test_find_char() {
let buffer = make_buffer("hello world");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::FindChar {
char: 'w',
direction: Direction::Forward,
till: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 6)));
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::FindChar {
char: 'w',
direction: Direction::Forward,
till: true,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 5)));
}
#[test]
fn test_paragraph_motion() {
let buffer = make_buffer("para1\n\npara2\npara2b\n\npara3");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Paragraph(Direction::Forward), 1);
assert_eq!(pos.map(|p| p.line), Some(2));
let cursor = make_cursor(5, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Paragraph(Direction::Backward), 1);
assert_eq!(pos.map(|p| p.line), Some(2)); }
#[test]
fn test_calculate_with_desired_col_vertical() {
let buffer = make_buffer("hello\nhi\nworld");
let cursor = make_cursor(0, 4);
let (new_pos, desired_col) = MotionEngine::calculate_with_desired_col(
&buffer,
&cursor,
Motion::Line(Direction::Forward),
1,
);
assert_eq!(new_pos, Some(Position::new(1, 1))); assert_eq!(desired_col, Some(4)); }
#[test]
fn test_calculate_with_desired_col_horizontal() {
let buffer = make_buffer("hello");
let cursor = make_cursor(0, 0);
let (new_pos, desired_col) = MotionEngine::calculate_with_desired_col(
&buffer,
&cursor,
Motion::Char(Direction::Forward),
1,
);
assert_eq!(new_pos, Some(Position::new(0, 1)));
assert_eq!(desired_col, None); }
#[test]
fn test_calculate_with_desired_col_with_preferred() {
let buffer = make_buffer("hello\nhi\nworld");
let mut cursor = make_cursor(0, 4);
cursor.preferred_column = Some(4);
let (new_pos, desired_col) = MotionEngine::calculate_with_desired_col(
&buffer,
&cursor,
Motion::Line(Direction::Forward),
1,
);
assert_eq!(new_pos, Some(Position::new(1, 1)));
assert_eq!(desired_col, Some(4));
}
#[test]
fn test_big_word_forward() {
let buffer = make_buffer("hello.world foo");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::BigWord,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 12))); }
#[test]
fn test_big_word_end() {
let buffer = make_buffer("hello.world foo");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::BigWord,
end: true,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 10))); }
#[test]
fn test_big_word_backward() {
let buffer = make_buffer("hello.world foo");
let cursor = make_cursor(0, 14);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::BigWord,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 12))); }
#[test]
fn test_word_end_backward() {
let buffer = make_buffer("hello world foo");
let cursor = make_cursor(0, 12);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 10))); }
#[test]
fn test_word_end_backward_at_start() {
let buffer = make_buffer("hello world");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert!(pos.is_some());
}
#[test]
fn test_word_end_backward_empty_buffer() {
let buffer = Buffer::new();
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_word_end_backward_multiline() {
let buffer = make_buffer("hello\n\nworld");
let cursor = make_cursor(2, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 4))); }
#[test]
fn test_paragraph_backward_multiple_count() {
let buffer = make_buffer("para1\n\npara2\n\npara3");
let cursor = make_cursor(4, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Paragraph(Direction::Backward), 2);
assert_eq!(pos.map(|p| p.line), Some(0)); }
#[test]
fn test_paragraph_forward_multiple_count() {
let buffer = make_buffer("para1\n\npara2\n\npara3");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Paragraph(Direction::Forward), 2);
assert_eq!(pos.map(|p| p.line), Some(4)); }
#[test]
fn test_paragraph_empty_buffer() {
let buffer = Buffer::new();
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Paragraph(Direction::Forward), 1);
assert_eq!(pos, None);
}
#[test]
fn test_find_char_backward_hello() {
let buffer = make_buffer("hello world");
let cursor = make_cursor(0, 10);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::FindChar {
char: 'o',
direction: Direction::Backward,
till: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 7))); }
#[test]
fn test_find_char_till_backward() {
let buffer = make_buffer("hello world");
let cursor = make_cursor(0, 10);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::FindChar {
char: 'o',
direction: Direction::Backward,
till: true,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 8))); }
#[test]
fn test_find_char_not_found() {
let buffer = make_buffer("hello");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::FindChar {
char: 'z',
direction: Direction::Forward,
till: false,
},
1,
);
assert_eq!(pos, None);
}
#[test]
fn test_find_char_with_count() {
let buffer = make_buffer("aabaa");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::FindChar {
char: 'a',
direction: Direction::Forward,
till: false,
},
2,
);
assert_eq!(pos, Some(Position::new(0, 3))); }
#[test]
fn test_match_bracket_search_forward() {
let buffer = make_buffer("hello (world)");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 12))); }
#[test]
fn test_match_bracket_multiline() {
let buffer = make_buffer("(\n content\n)");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(2, 0)));
}
#[test]
fn test_match_bracket_multiline_backward() {
let buffer = make_buffer("(\n content\n)");
let cursor = make_cursor(2, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_match_bracket_square() {
let buffer = make_buffer("[hello]");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 6)));
let cursor = make_cursor(0, 6);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_match_bracket_curly() {
let buffer = make_buffer("{hello}");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 6)));
let cursor = make_cursor(0, 6);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_match_bracket_no_bracket() {
let buffer = make_buffer("hello world");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, None);
}
#[test]
fn test_word_forward_punctuation() {
let buffer = make_buffer("foo::bar baz");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 3))); }
#[test]
fn test_word_backward_punctuation() {
let buffer = make_buffer("foo::bar");
let cursor = make_cursor(0, 7);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 5))); }
#[test]
fn test_word_end_punctuation() {
let buffer = make_buffer("foo::bar");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 2))); }
#[test]
fn test_line_motion_backward() {
let buffer = make_buffer("line1\nline2\nline3");
let cursor = make_cursor(2, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Line(Direction::Backward), 1);
assert_eq!(pos, Some(Position::new(1, 0)));
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Line(Direction::Backward), 10);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_line_motion_empty_buffer() {
let buffer = Buffer::new();
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Line(Direction::Forward), 1);
assert_eq!(pos, None);
}
#[test]
fn test_line_motion_empty_target_line() {
let buffer = make_buffer("hello\n\nworld");
let cursor = make_cursor(0, 3);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Line(Direction::Forward), 1);
assert_eq!(pos, Some(Position::new(1, 0))); }
#[test]
fn test_jump_line_with_indented_target() {
let buffer = make_buffer("hello\n world");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::JumpLine(Some(1)), 1);
assert_eq!(pos, Some(Position::new(1, 2))); }
#[test]
fn test_jump_line_beyond_end() {
let buffer = make_buffer("line1\nline2");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::JumpLine(Some(100)), 1);
assert_eq!(pos, Some(Position::new(1, 0))); }
#[test]
fn test_jump_line_empty_buffer() {
let buffer = Buffer::new();
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::JumpLine(None), 1);
assert_eq!(pos, None);
}
#[test]
fn test_word_forward_multiline() {
let buffer = make_buffer("hello\nworld");
let cursor = make_cursor(0, 4);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(1, 0))); }
#[test]
fn test_word_backward_multiline() {
let buffer = make_buffer("hello\nworld");
let cursor = make_cursor(1, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 0))); }
#[test]
fn test_word_forward_from_whitespace() {
let buffer = make_buffer(" hello");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 2))); }
#[test]
fn test_count_zero_treated_as_one() {
let buffer = make_buffer("hello");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Char(Direction::Forward), 0);
assert_eq!(pos, Some(Position::new(0, 1))); }
#[test]
fn test_line_position_all_whitespace() {
let buffer = make_buffer(" ");
let cursor = make_cursor(0, 1);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::LinePosition(LinePosition::FirstNonBlank),
1,
);
assert_eq!(pos, Some(Position::new(0, 0)));
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::LinePosition(LinePosition::LastNonBlank),
1,
);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_word_forward_bigword_from_whitespace() {
let buffer = make_buffer(" hello.world");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::BigWord,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 2)));
}
#[test]
fn test_word_forward_punctuation_skip() {
let buffer = make_buffer("foo::bar baz");
let cursor = make_cursor(0, 3);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 5)));
}
#[test]
fn test_word_forward_across_whitespace_only_line() {
let buffer = make_buffer("hello\n \nworld");
let cursor = make_cursor(0, 4);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(2, 0)));
}
#[test]
fn test_word_forward_at_end_of_buffer() {
let buffer = make_buffer("hello");
let cursor = make_cursor(0, 4);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 4)));
}
#[test]
fn test_word_end_cross_line() {
let buffer = make_buffer("hello\nworld");
let cursor = make_cursor(0, 4);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert_eq!(pos, Some(Position::new(1, 4)));
}
#[test]
fn test_word_end_empty_buffer() {
let buffer = Buffer::new();
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_word_end_across_whitespace_only_line() {
let buffer = make_buffer("hello\n \nworld");
let cursor = make_cursor(0, 4);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert_eq!(pos, Some(Position::new(2, 4)));
}
#[test]
fn test_word_end_on_punctuation() {
let buffer = make_buffer("foo::bar");
let cursor = make_cursor(0, 2);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 4))); }
#[test]
fn test_word_backward_empty_buffer() {
let buffer = Buffer::new();
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_word_backward_across_whitespace_line() {
let buffer = make_buffer("hello\n \nworld");
let cursor = make_cursor(2, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_word_backward_bigword() {
let buffer = make_buffer("hello foo.bar baz");
let cursor = make_cursor(0, 14);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::BigWord,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 6)));
}
#[test]
fn test_word_backward_from_punct() {
let buffer = make_buffer("foo::bar");
let cursor = make_cursor(0, 4);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 3)));
}
#[test]
fn test_word_end_backward_basic() {
let buffer = make_buffer("hello world");
let cursor = make_cursor(0, 10);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert!(pos.is_some());
}
#[test]
fn test_word_end_backward_cross_line() {
let buffer = make_buffer("hello\nworld");
let cursor = make_cursor(1, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 4)));
}
#[test]
fn test_word_end_backward_whitespace_only_line() {
let buffer = make_buffer("hello\n \nworld");
let cursor = make_cursor(2, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 4)));
}
#[test]
fn test_paragraph_backward_count2() {
let buffer = make_buffer("para1\n\npara2\n\npara3");
let cursor = make_cursor(4, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Paragraph(Direction::Backward), 2);
assert!(pos.is_some());
assert_eq!(pos.unwrap().line, 0);
}
#[test]
fn test_paragraph_backward_from_empty_line() {
let buffer = make_buffer("para1\n\npara2");
let cursor = make_cursor(1, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Paragraph(Direction::Backward), 1);
assert!(pos.is_some());
assert_eq!(pos.unwrap().line, 0);
}
#[test]
fn test_paragraph_forward_count2() {
let buffer = make_buffer("para1\n\npara2\n\npara3");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Paragraph(Direction::Forward), 2);
assert!(pos.is_some());
assert_eq!(pos.unwrap().line, 4);
}
#[test]
fn test_find_char_backward() {
let buffer = make_buffer("abcabc");
let cursor = make_cursor(0, 5);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::FindChar {
char: 'a',
direction: Direction::Backward,
till: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 3)));
}
#[test]
fn test_find_char_backward_till() {
let buffer = make_buffer("abcabc");
let cursor = make_cursor(0, 5);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::FindChar {
char: 'a',
direction: Direction::Backward,
till: true,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 4)));
}
#[test]
fn test_find_char_backward_count2() {
let buffer = make_buffer("abcabc");
let cursor = make_cursor(0, 5);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::FindChar {
char: 'a',
direction: Direction::Backward,
till: false,
},
2,
);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_match_bracket_closing_search_forward() {
let buffer = make_buffer("x ) ( y");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, None);
}
#[test]
fn test_match_bracket_multiline_forward() {
let buffer = make_buffer("(\nhello\n)");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(2, 0)));
}
#[test]
fn test_match_bracket_multiline_backward_newline() {
let buffer = make_buffer("(\nhello\n)");
let cursor = make_cursor(2, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_match_bracket_square_simple() {
let buffer = make_buffer("[hello]");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 6)));
}
#[test]
fn test_match_bracket_curly_simple() {
let buffer = make_buffer("{hello}");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 6)));
}
#[test]
fn test_calculate_with_desired_col_line_motion() {
let buffer = make_buffer("hello\nworld");
let cursor = make_cursor(0, 3);
let (pos, desired) = MotionEngine::calculate_with_desired_col(
&buffer,
&cursor,
Motion::Line(Direction::Forward),
1,
);
assert_eq!(pos, Some(Position::new(1, 3)));
assert_eq!(desired, Some(3));
}
#[test]
fn test_calculate_with_desired_col_char_motion() {
let buffer = make_buffer("hello");
let cursor = make_cursor(0, 0);
let (pos, desired) = MotionEngine::calculate_with_desired_col(
&buffer,
&cursor,
Motion::Char(Direction::Forward),
1,
);
assert_eq!(pos, Some(Position::new(0, 1)));
assert_eq!(desired, None); }
#[test]
fn test_word_forward_empty_buffer() {
let buffer = Buffer::new();
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_word_end_at_last_position() {
let buffer = make_buffer("hello");
let cursor = make_cursor(0, 4);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 4)));
}
#[test]
fn test_paragraph_backward_starting_empty() {
let buffer = make_buffer("\npara1\n\npara2");
let cursor = make_cursor(3, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Paragraph(Direction::Backward), 1);
assert!(pos.is_some());
assert!(pos.unwrap().line <= 1);
}
#[test]
fn test_backward_bracket_multiline_nested() {
let buffer = make_buffer("(\n inner\n (\n deep\n )\n)");
let cursor = make_cursor(5, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_word_end_backward_whitespace_at_line_start() {
let buffer = make_buffer(" hello\nworld");
let cursor = make_cursor(1, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 6)));
}
#[test]
fn test_paragraph_backward_line0_empty() {
let buffer = make_buffer("\n\npara1");
let cursor = make_cursor(2, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Paragraph(Direction::Backward), 1);
assert!(pos.is_some());
assert_eq!(pos.unwrap().line, 0);
}
#[test]
fn test_match_bracket_forward_search_finds_close() {
let buffer = make_buffer("abc ) xyz");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert!(pos.is_none());
}
#[test]
fn test_word_end_at_end_of_last_line() {
let buffer = make_buffer("hello world");
let cursor = make_cursor(0, 10);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 10)));
}
#[test]
fn test_word_backward_whitespace_start() {
let buffer = make_buffer(" hello");
let cursor = make_cursor(0, 1);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_paragraph_forward_past_end() {
let buffer = make_buffer("hello");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Paragraph(Direction::Forward), 1);
assert!(pos.is_some());
}
#[test]
fn test_paragraph_backward_at_line0() {
let buffer = make_buffer("hello\nworld");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Paragraph(Direction::Backward), 1);
assert!(pos.is_some());
assert_eq!(pos.unwrap().line, 0);
}
#[test]
fn test_word_end_backward_at_origin() {
let buffer = make_buffer(" hello");
let cursor = make_cursor(0, 1);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert!(pos.is_some());
assert_eq!(pos.unwrap().column, 0);
}
#[test]
fn test_word_forward_through_empty_line() {
let buffer = make_buffer("hello\n\nworld");
let cursor = make_cursor(0, 4);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert!(pos.is_some());
}
#[test]
fn test_calculate_with_desired_col_count_zero() {
let buffer = make_buffer("hello\nworld");
let cursor = make_cursor(0, 0);
let (pos, desired) = MotionEngine::calculate_with_desired_col(
&buffer,
&cursor,
Motion::Line(Direction::Forward),
0,
);
assert_eq!(pos, Some(Position::new(1, 0)));
assert_eq!(desired, Some(0));
}
#[test]
fn test_word_forward_multiline_bigword() {
let buffer = make_buffer("hello.world\n foo");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::BigWord,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(1, 2)));
}
#[test]
fn test_word_backward_wrap_previous_line() {
let buffer = make_buffer("hello world\nfoo");
let cursor = make_cursor(1, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 6))); }
#[test]
fn test_word_forward_past_buffer_end_bigword() {
let buffer = make_buffer("hello\nworld");
let cursor = make_cursor(1, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::BigWord,
end: false,
},
1,
);
assert!(pos.is_some());
let p = pos.unwrap();
assert_eq!(p.line, 1);
assert_eq!(p.column, 4); }
#[test]
fn test_word_end_at_last_line_boundary_trailing_spaces() {
let buffer = make_buffer("hello\nworld ");
let cursor = make_cursor(1, 4);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
assert!(pos.is_some());
let p = pos.unwrap();
assert_eq!(p.line, 1);
}
#[test]
fn test_word_backward_multi_punctuation() {
let buffer = make_buffer("foo:::bar");
let cursor = make_cursor(0, 6);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 3))); }
#[test]
fn test_find_forward_bracket_no_closing_match() {
let buffer = make_buffer("(hello world");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, None);
}
#[test]
fn test_backward_bracket_nested_depth_on_first_line() {
let buffer = make_buffer("(())");
let cursor = make_cursor(0, 3);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 0))); }
#[test]
fn test_word_forward_overshoot_past_buffer_end() {
let buffer = make_buffer("a b");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: false,
},
10,
);
assert!(pos.is_some());
}
#[test]
fn test_word_forward_next_line_all_whitespace() {
let buffer = make_buffer("hello\n \n ");
let cursor = make_cursor(0, 4);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert!(pos.is_some());
}
#[test]
fn test_word_end_overshoot_past_buffer_end() {
let buffer = make_buffer("a b");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: true,
},
10,
);
assert!(pos.is_some());
}
#[test]
fn test_word_end_backward_on_empty_first_line() {
let buffer = make_buffer("\nhello");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: true,
},
1,
);
let _ = pos;
}
#[test]
fn test_paragraph_forward_past_end_of_buffer() {
let buffer = make_buffer("hello\n\nworld\n\n");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Paragraph(Direction::Forward), 10);
assert!(pos.is_some());
}
#[test]
fn test_match_bracket_cursor_not_on_any_bracket() {
let buffer = make_buffer("hello");
let cursor = make_cursor(0, 2);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, None);
}
#[test]
fn test_word_forward_high_count_two_line_buffer() {
let buffer = make_buffer("ab\ncd");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: false,
},
10,
);
assert!(pos.is_some());
let p = pos.unwrap();
assert_eq!(p.line, 1);
}
#[test]
fn test_word_forward_bigword_high_count_small_buffer() {
let buffer = make_buffer("x\ny");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::BigWord,
end: false,
},
20,
);
assert!(pos.is_some());
let p = pos.unwrap();
assert_eq!(p.line, 1);
assert_eq!(p.column, 0);
}
#[test]
fn test_word_forward_multiple_whitespace_lines_then_content() {
let buffer = make_buffer("hello\n \n \nworld");
let cursor = make_cursor(0, 4);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(3, 0)));
}
#[test]
fn test_word_forward_bigword_whitespace_line_fallthrough() {
let buffer = make_buffer("abc\n \ndef");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::BigWord,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(2, 0)));
}
#[test]
fn test_word_end_high_count_two_line_buffer() {
let buffer = make_buffer("ab\ncd");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::Word,
end: true,
},
10,
);
assert!(pos.is_some());
let p = pos.unwrap();
assert_eq!(p.line, 1);
}
#[test]
fn test_word_end_bigword_high_count_small_buffer() {
let buffer = make_buffer("x\ny");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Forward,
boundary: WordBoundary::BigWord,
end: true,
},
20,
);
assert!(pos.is_some());
let p = pos.unwrap();
assert_eq!(p.line, 1);
assert_eq!(p.column, 0);
}
#[test]
fn test_word_backward_single_punctuation() {
let buffer = make_buffer("a.b");
let cursor = make_cursor(0, 2);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 1)));
}
#[test]
fn test_word_backward_punctuation_at_line_start() {
let buffer = make_buffer(".hello");
let cursor = make_cursor(0, 1);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_word_backward_through_mixed_punct_word() {
let buffer = make_buffer("foo.bar{baz");
let cursor = make_cursor(0, 8);
let pos = MotionEngine::calculate(
&buffer,
&cursor,
Motion::Word {
direction: Direction::Backward,
boundary: WordBoundary::Word,
end: false,
},
1,
);
assert_eq!(pos, Some(Position::new(0, 7)));
}
#[test]
fn test_match_bracket_non_bracket_char_with_bracket_ahead() {
let buffer = make_buffer("x(hello)");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 7)));
}
#[test]
fn test_match_bracket_letter_before_bracket() {
let buffer = make_buffer("abc{def}");
let cursor = make_cursor(0, 1);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 7)));
}
#[test]
fn test_backward_bracket_close_at_col_zero() {
let buffer = make_buffer("(hello\n)");
let cursor = make_cursor(1, 0); let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_backward_bracket_nested_col_zero() {
let buffer = make_buffer("(\n (inner)\n)");
let cursor = make_cursor(2, 0); let pos = MotionEngine::calculate(&buffer, &cursor, Motion::MatchBracket, 1);
assert_eq!(pos, Some(Position::new(0, 0)));
}
#[test]
fn test_paragraph_forward_single_paragraph_no_blank() {
let buffer = make_buffer("aaa\nbbb\nccc");
let cursor = make_cursor(0, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Paragraph(Direction::Forward), 1);
assert!(pos.is_some());
let p = pos.unwrap();
assert_eq!(p.line, 2);
}
#[test]
fn test_paragraph_backward_multiple_paragraphs() {
let buffer = make_buffer("aaa\nbbb\n\nccc\nddd\n\neee\nfff");
let cursor = make_cursor(7, 0); let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Paragraph(Direction::Backward), 2);
assert!(pos.is_some());
let p = pos.unwrap();
assert_eq!(p.line, 0);
}
#[test]
fn test_paragraph_backward_high_count() {
let buffer = make_buffer("aaa\n\nbbb\n\nccc");
let cursor = make_cursor(4, 0);
let pos = MotionEngine::calculate(&buffer, &cursor, Motion::Paragraph(Direction::Backward), 10);
assert!(pos.is_some());
assert_eq!(pos.unwrap().line, 0);
}