зеркало из
https://github.com/glebtv/tui-textarea.git
synced 2026-09-08 00:15:50 +03:00
add more tests for search feature
Этот коммит содержится в:
@@ -151,3 +151,20 @@ impl Search {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn matches() {
|
||||||
|
let mut s = Search::default();
|
||||||
|
s.set_pattern("fo+").unwrap();
|
||||||
|
|
||||||
|
let m: Vec<_> = s.matches("fo foo bar fooo").unwrap().collect();
|
||||||
|
assert_eq!(m, [(0, 2), (3, 6), (11, 15)]);
|
||||||
|
|
||||||
|
s.set_pattern("").unwrap();
|
||||||
|
assert!(s.matches("fo foo bar fooo").is_none());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
use tui_textarea::{CursorMove, TextArea};
|
use tui_textarea::{CursorMove, TextArea};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn search_forward() {
|
fn search_lines_forward() {
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
let mut textarea = TextArea::from([
|
let mut textarea = TextArea::from([
|
||||||
"fooo foo",
|
"fooo foo",
|
||||||
@@ -20,13 +20,13 @@ fn search_forward() {
|
|||||||
for (i, pos) in expected.into_iter().enumerate() {
|
for (i, pos) in expected.into_iter().enumerate() {
|
||||||
let moved = textarea.search_forward(false);
|
let moved = textarea.search_forward(false);
|
||||||
let cursor = textarea.cursor();
|
let cursor = textarea.cursor();
|
||||||
assert!(moved, "{}th move didn't happen: {:?}", i, cursor);
|
assert!(moved, "{}th move didn't happen: {:?}", i + 1, cursor);
|
||||||
assert_eq!(pos, cursor, "{}th move is unexpected", i);
|
assert_eq!(pos, cursor, "{}th position is unexpected", i + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn search_backward() {
|
fn search_lines_backward() {
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
let mut textarea = TextArea::from([
|
let mut textarea = TextArea::from([
|
||||||
"fooo foo",
|
"fooo foo",
|
||||||
@@ -43,8 +43,44 @@ fn search_backward() {
|
|||||||
for (i, pos) in expected.into_iter().enumerate() {
|
for (i, pos) in expected.into_iter().enumerate() {
|
||||||
let moved = textarea.search_back(false);
|
let moved = textarea.search_back(false);
|
||||||
let cursor = textarea.cursor();
|
let cursor = textarea.cursor();
|
||||||
assert!(moved, "{}th move didn't happen: {:?}", i, cursor);
|
assert!(moved, "{}th move didn't happen: {:?}", i + 1, cursor);
|
||||||
assert_eq!(pos, cursor, "{}th move is unexpected", i);
|
assert_eq!(pos, cursor, "{}th position is unexpected", i + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn search_forward_within_line() {
|
||||||
|
let mut textarea = TextArea::from(["foo fo foo fooo"]);
|
||||||
|
|
||||||
|
// Move to 'f' on 'fo'
|
||||||
|
textarea.move_cursor(CursorMove::Jump(0, 4));
|
||||||
|
|
||||||
|
textarea.set_search_pattern("fo+").unwrap();
|
||||||
|
|
||||||
|
let expected = [(0, 7), (0, 11), (0, 0), (0, 4)];
|
||||||
|
for (i, pos) in expected.into_iter().enumerate() {
|
||||||
|
let moved = textarea.search_forward(false);
|
||||||
|
let cursor = textarea.cursor();
|
||||||
|
assert!(moved, "{}th move didn't happen: {:?}", i + 1, cursor);
|
||||||
|
assert_eq!(pos, cursor, "{}th position is unexpected", i + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn search_backward_within_line() {
|
||||||
|
let mut textarea = TextArea::from(["foo fo foo fooo"]);
|
||||||
|
|
||||||
|
// Move to 'f' on 'fo'
|
||||||
|
textarea.move_cursor(CursorMove::Jump(0, 4));
|
||||||
|
|
||||||
|
textarea.set_search_pattern("fo+").unwrap();
|
||||||
|
|
||||||
|
let expected = [(0, 0), (0, 11), (0, 7), (0, 4)];
|
||||||
|
for (i, pos) in expected.into_iter().enumerate() {
|
||||||
|
let moved = textarea.search_back(false);
|
||||||
|
let cursor = textarea.cursor();
|
||||||
|
assert!(moved, "{}th move didn't happen: {:?}", i + 1, cursor);
|
||||||
|
assert_eq!(pos, cursor, "{}th position is unexpected", i + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,13 +107,26 @@ fn accept_cursor_position() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn set_search_pattern() {
|
fn set_search_pattern() {
|
||||||
let mut textarea = TextArea::default();
|
let mut textarea = TextArea::from(["foo"]);
|
||||||
|
|
||||||
assert!(textarea.search_pattern().is_none());
|
assert!(textarea.search_pattern().is_none());
|
||||||
|
assert!(!textarea.search_forward(true));
|
||||||
|
assert!(!textarea.search_forward(false));
|
||||||
|
assert!(!textarea.search_back(true));
|
||||||
|
assert!(!textarea.search_back(false));
|
||||||
|
|
||||||
textarea.set_search_pattern("(foo").unwrap_err();
|
textarea.set_search_pattern("(foo").unwrap_err();
|
||||||
assert!(textarea.search_pattern().is_none());
|
assert!(textarea.search_pattern().is_none());
|
||||||
|
|
||||||
textarea.set_search_pattern("(fo+)ba+r").unwrap();
|
textarea.set_search_pattern("(fo+)ba+r").unwrap();
|
||||||
let pat = textarea.search_pattern().unwrap();
|
let pat = textarea.search_pattern().unwrap();
|
||||||
assert_eq!(pat.as_str(), "(fo+)ba+r");
|
assert_eq!(pat.as_str(), "(fo+)ba+r");
|
||||||
|
|
||||||
|
textarea.set_search_pattern("fo+").unwrap();
|
||||||
|
textarea.set_search_pattern("").unwrap();
|
||||||
|
assert!(textarea.search_pattern().is_none());
|
||||||
|
assert!(!textarea.search_forward(true));
|
||||||
|
assert!(!textarea.search_forward(false));
|
||||||
|
assert!(!textarea.search_back(true));
|
||||||
|
assert!(!textarea.search_back(false));
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user