use text_document::{MoveMode, MoveOperation, SelectionType, TextDocument};
fn doc_with(text: &str) -> TextDocument {
let doc = TextDocument::new();
doc.set_plain_text(text).unwrap();
doc
}
const THREE: &str = "One is first. Two is second. Three is third.";
#[test]
fn select_sentence_under_cursor_takes_the_whole_sentence() {
let doc = doc_with(THREE);
let cursor = doc.cursor_at(16); cursor.select(SelectionType::SentenceUnderCursor);
assert_eq!(cursor.selected_text().unwrap(), "Two is second.");
}
#[test]
fn the_selection_excludes_the_space_after_the_terminator() {
let doc = doc_with(THREE);
let cursor = doc.cursor_at(2);
cursor.select(SelectionType::SentenceUnderCursor);
let text = cursor.selected_text().unwrap();
assert_eq!(text, "One is first.");
assert!(!text.ends_with(' '));
}
#[test]
fn selecting_in_an_empty_block_is_a_no_op() {
let doc = doc_with("Text.\n\nMore.");
let blank = "Text.\n".chars().count();
let cursor = doc.cursor_at(blank);
cursor.select(SelectionType::SentenceUnderCursor);
assert!(!cursor.has_selection());
assert_eq!(cursor.position(), blank);
}
#[test]
fn the_content_locale_tailors_the_selection() {
let doc = doc_with("Mr. Smith went home.");
let cursor = doc.cursor_at(6);
assert_eq!(cursor.content_locale(), None, "untailored by default");
cursor.select(SelectionType::SentenceUnderCursor);
assert_eq!(cursor.selected_text().unwrap(), "Smith went home.");
let cursor = doc.cursor_at(6);
cursor.set_content_locale(Some("en-US"));
assert_eq!(cursor.content_locale().as_deref(), Some("en-US"));
cursor.select(SelectionType::SentenceUnderCursor);
assert_eq!(cursor.selected_text().unwrap(), "Mr. Smith went home.");
}
#[test]
fn a_cloned_cursor_inherits_the_content_locale() {
let doc = doc_with("M. Dupont est parti.");
let cursor = doc.cursor_at(5);
cursor.set_content_locale(Some("fr-FR"));
let clone = cursor.clone();
assert_eq!(clone.content_locale().as_deref(), Some("fr-FR"));
clone.select(SelectionType::SentenceUnderCursor);
assert_eq!(clone.selected_text().unwrap(), "M. Dupont est parti.");
}
#[test]
fn start_and_end_of_sentence_reach_its_edges() {
let doc = doc_with(THREE);
let cursor = doc.cursor_at(20);
cursor.move_position(MoveOperation::StartOfSentence, MoveMode::MoveAnchor, 1);
assert_eq!(cursor.position(), 14, "the start of \"Two is second.\"");
let cursor = doc.cursor_at(20);
cursor.move_position(MoveOperation::EndOfSentence, MoveMode::MoveAnchor, 1);
assert_eq!(cursor.position(), 28, "just past its terminator");
}
#[test]
fn repeating_start_of_sentence_walks_backwards() {
let doc = doc_with(THREE);
let cursor = doc.cursor_at(30);
cursor.move_position(MoveOperation::StartOfSentence, MoveMode::MoveAnchor, 1);
assert_eq!(cursor.position(), 29);
cursor.move_position(MoveOperation::StartOfSentence, MoveMode::MoveAnchor, 1);
assert_eq!(cursor.position(), 14);
cursor.move_position(MoveOperation::StartOfSentence, MoveMode::MoveAnchor, 1);
assert_eq!(cursor.position(), 0);
cursor.move_position(MoveOperation::StartOfSentence, MoveMode::MoveAnchor, 1);
assert_eq!(cursor.position(), 0);
}
#[test]
fn next_and_previous_sentence_step_between_them() {
let doc = doc_with(THREE);
let cursor = doc.cursor_at(0);
cursor.move_position(MoveOperation::NextSentence, MoveMode::MoveAnchor, 1);
assert_eq!(cursor.position(), 14);
cursor.move_position(MoveOperation::NextSentence, MoveMode::MoveAnchor, 1);
assert_eq!(cursor.position(), 29);
cursor.move_position(MoveOperation::PreviousSentence, MoveMode::MoveAnchor, 1);
assert_eq!(cursor.position(), 14);
cursor.move_position(MoveOperation::PreviousSentence, MoveMode::MoveAnchor, 1);
assert_eq!(cursor.position(), 0);
}
#[test]
fn the_repeat_count_applies() {
let doc = doc_with(THREE);
let cursor = doc.cursor_at(0);
cursor.move_position(MoveOperation::NextSentence, MoveMode::MoveAnchor, 2);
assert_eq!(cursor.position(), 29);
}
#[test]
fn keep_anchor_extends_the_selection_by_sentences() {
let doc = doc_with(THREE);
let cursor = doc.cursor_at(0);
cursor.move_position(MoveOperation::EndOfSentence, MoveMode::KeepAnchor, 1);
assert!(cursor.has_selection());
assert_eq!(cursor.selected_text().unwrap(), "One is first.");
}
#[test]
fn movement_stops_at_the_document_edges() {
let doc = doc_with(THREE);
let total = doc.character_count();
let cursor = doc.cursor_at(0);
cursor.move_position(MoveOperation::NextSentence, MoveMode::MoveAnchor, 99);
assert!(cursor.position() <= total, "ran past the end");
let cursor = doc.cursor_at(total);
cursor.move_position(MoveOperation::EndOfSentence, MoveMode::MoveAnchor, 99);
assert!(cursor.position() <= total, "ran past the end");
}
#[test]
fn movement_respects_block_boundaries() {
let doc = doc_with("First block.\nSecond block.");
let cursor = doc.cursor_at(2);
cursor.move_position(MoveOperation::EndOfSentence, MoveMode::MoveAnchor, 1);
assert_eq!(cursor.position(), 12, "stops at the end of its own block");
}