use text_document::TextDocument;
fn new_doc(text: &str) -> TextDocument {
let doc = TextDocument::new();
doc.set_plain_text(text).unwrap();
doc
}
fn slice(doc: &TextDocument, range: (usize, usize)) -> String {
doc.to_plain_text()
.unwrap()
.chars()
.skip(range.0)
.take(range.1 - range.0)
.collect()
}
#[test]
fn offsets_are_absolute_not_block_relative() {
let doc = new_doc("First para. Still first.\nSecond para. Still second.");
let second_block_start = "First para. Still first.\n".chars().count();
let range = doc
.sentence_at(second_block_start + 2, Some("en"))
.expect("a sentence");
assert_eq!(slice(&doc, range), "Second para.");
assert!(
range.0 >= second_block_start,
"offsets must be absolute: got {range:?}, block starts at {second_block_start}"
);
}
#[test]
fn a_sentence_never_crosses_a_block_boundary() {
let doc = new_doc("An unfinished line\nA second line.");
let range = doc.sentence_at(3, Some("en")).expect("a sentence");
assert_eq!(slice(&doc, range), "An unfinished line");
}
#[test]
fn every_caret_position_in_a_document_resolves_or_declines_cleanly() {
let doc = new_doc("One. Two.\n\nThree? \"Yes,\" he said.\n");
let total = doc.character_count() + 3; for pos in 0..=total {
if let Some((start, end)) = doc.sentence_at(pos, Some("en")) {
assert!(start < end, "empty range at {pos}: {start}..{end}");
assert!(
end <= doc.to_plain_text().unwrap().chars().count(),
"range past the document at {pos}: {start}..{end}"
);
}
}
}
#[test]
fn an_empty_block_has_no_sentence() {
let doc = new_doc("Text.\n\nMore text.");
let blank = "Text.\n".chars().count();
assert_eq!(doc.sentence_at(blank, Some("en")), None);
}
#[test]
fn an_empty_document_has_no_sentence() {
let doc = new_doc("");
assert_eq!(doc.sentence_at(0, Some("en")), None);
}
#[test]
fn the_locale_argument_changes_the_answer_for_the_same_document() {
let doc = new_doc("Mr. Smith went home.");
assert_eq!(
slice(&doc, doc.sentence_at(0, Some("en")).unwrap()),
"Mr. Smith went home."
);
assert_eq!(slice(&doc, doc.sentence_at(0, None).unwrap()), "Mr.");
}
#[test]
fn offsets_are_char_based_over_multibyte_text() {
let doc = new_doc("Émile hésita. « Vraiment ? » demanda-t-il.");
let range = doc.sentence_at(20, Some("fr")).expect("a sentence");
assert_eq!(slice(&doc, range), "« Vraiment ? » demanda-t-il.");
}
#[test]
fn the_query_follows_the_document_across_an_edit() {
let doc = new_doc("One. Two.");
assert_eq!(slice(&doc, doc.sentence_at(6, Some("en")).unwrap()), "Two.");
doc.set_plain_text("Zero. One. Two.").unwrap();
let range = doc.sentence_at(12, Some("en")).unwrap();
assert_eq!(slice(&doc, range), "Two.");
assert_eq!(range.0, 11, "the sentence moved with the text before it");
}