Add test for word boundary movement/selection/deletion

This commit is contained in:
Antonio Scandurra 2021-04-30 10:13:45 +02:00
parent f352cfb686
commit 9c3216507b
2 changed files with 187 additions and 2 deletions

View file

@ -2357,6 +2357,188 @@ mod tests {
}); });
} }
#[test]
fn test_prev_next_word_boundary() {
App::test((), |app| {
let buffer = app
.add_model(|ctx| Buffer::new(0, "use std::str::{foo, bar}\n\n {baz.qux()}", ctx));
let settings = settings::channel(&app.font_cache()).unwrap().1;
let (_, view) = app.add_window(|ctx| BufferView::for_buffer(buffer, settings, ctx));
view.update(app, |view, ctx| {
view.select_display_ranges(
&[
DisplayPoint::new(0, 11)..DisplayPoint::new(0, 11),
DisplayPoint::new(2, 4)..DisplayPoint::new(2, 4),
],
ctx,
)
.unwrap();
});
view.update(app, |view, ctx| {
view.move_to_previous_word_boundary(&(), ctx)
});
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
&[
DisplayPoint::new(0, 9)..DisplayPoint::new(0, 9),
DisplayPoint::new(2, 3)..DisplayPoint::new(2, 3),
]
);
view.update(app, |view, ctx| {
view.move_to_previous_word_boundary(&(), ctx)
});
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
&[
DisplayPoint::new(0, 7)..DisplayPoint::new(0, 7),
DisplayPoint::new(2, 2)..DisplayPoint::new(2, 2),
]
);
view.update(app, |view, ctx| {
view.move_to_previous_word_boundary(&(), ctx)
});
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
&[
DisplayPoint::new(0, 4)..DisplayPoint::new(0, 4),
DisplayPoint::new(2, 0)..DisplayPoint::new(2, 0),
]
);
view.update(app, |view, ctx| {
view.move_to_previous_word_boundary(&(), ctx)
});
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
&[
DisplayPoint::new(0, 3)..DisplayPoint::new(0, 3),
DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0),
]
);
view.update(app, |view, ctx| {
view.move_to_previous_word_boundary(&(), ctx)
});
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
&[
DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0),
DisplayPoint::new(0, 24)..DisplayPoint::new(0, 24),
]
);
view.update(app, |view, ctx| {
view.move_to_previous_word_boundary(&(), ctx)
});
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
&[
DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0),
DisplayPoint::new(0, 23)..DisplayPoint::new(0, 23),
]
);
view.update(app, |view, ctx| view.move_to_next_word_boundary(&(), ctx));
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
&[
DisplayPoint::new(0, 3)..DisplayPoint::new(0, 3),
DisplayPoint::new(0, 24)..DisplayPoint::new(0, 24),
]
);
view.update(app, |view, ctx| view.move_to_next_word_boundary(&(), ctx));
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
&[
DisplayPoint::new(0, 4)..DisplayPoint::new(0, 4),
DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0),
]
);
view.update(app, |view, ctx| view.move_to_next_word_boundary(&(), ctx));
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
&[
DisplayPoint::new(0, 7)..DisplayPoint::new(0, 7),
DisplayPoint::new(2, 0)..DisplayPoint::new(2, 0),
]
);
view.update(app, |view, ctx| view.move_to_next_word_boundary(&(), ctx));
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
&[
DisplayPoint::new(0, 9)..DisplayPoint::new(0, 9),
DisplayPoint::new(2, 2)..DisplayPoint::new(2, 2),
]
);
view.update(app, |view, ctx| {
view.move_right(&(), ctx);
view.select_to_previous_word_boundary(&(), ctx);
});
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
&[
DisplayPoint::new(0, 10)..DisplayPoint::new(0, 9),
DisplayPoint::new(2, 3)..DisplayPoint::new(2, 2),
]
);
view.update(app, |view, ctx| {
view.select_to_previous_word_boundary(&(), ctx)
});
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
&[
DisplayPoint::new(0, 10)..DisplayPoint::new(0, 7),
DisplayPoint::new(2, 3)..DisplayPoint::new(2, 0),
]
);
view.update(app, |view, ctx| view.select_to_next_word_boundary(&(), ctx));
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
&[
DisplayPoint::new(0, 10)..DisplayPoint::new(0, 9),
DisplayPoint::new(2, 3)..DisplayPoint::new(2, 2),
]
);
view.update(app, |view, ctx| view.delete_to_next_word_boundary(&(), ctx));
assert_eq!(
view.read(app).text(app.as_ref()),
"use std::s::{foo, bar}\n\n {az.qux()}"
);
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
&[
DisplayPoint::new(0, 10)..DisplayPoint::new(0, 10),
DisplayPoint::new(2, 3)..DisplayPoint::new(2, 3),
]
);
view.update(app, |view, ctx| {
view.delete_to_previous_word_boundary(&(), ctx)
});
assert_eq!(
view.read(app).text(app.as_ref()),
"use std::::{foo, bar}\n\n az.qux()}"
);
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
&[
DisplayPoint::new(0, 9)..DisplayPoint::new(0, 9),
DisplayPoint::new(2, 2)..DisplayPoint::new(2, 2),
]
);
});
}
#[test] #[test]
fn test_backspace() { fn test_backspace() {
App::test((), |app| { App::test((), |app| {

View file

@ -119,7 +119,7 @@ pub fn next_word_boundary(
) -> Result<DisplayPoint> { ) -> Result<DisplayPoint> {
let mut prev_c = None; let mut prev_c = None;
for c in map.chars_at(point, app)? { for c in map.chars_at(point, app)? {
if prev_c.is_some() && char_kind(prev_c.unwrap()) != char_kind(c) { if prev_c.is_some() && (c == '\n' || char_kind(prev_c.unwrap()) != char_kind(c)) {
break; break;
} }
@ -136,13 +136,16 @@ pub fn next_word_boundary(
#[derive(Copy, Clone, Eq, PartialEq)] #[derive(Copy, Clone, Eq, PartialEq)]
enum CharKind { enum CharKind {
Newline,
Whitespace, Whitespace,
Punctuation, Punctuation,
Word, Word,
} }
fn char_kind(c: char) -> CharKind { fn char_kind(c: char) -> CharKind {
if c.is_whitespace() { if c == '\n' {
CharKind::Newline
} else if c.is_whitespace() {
CharKind::Whitespace CharKind::Whitespace
} else if c.is_alphanumeric() || c == '_' { } else if c.is_alphanumeric() || c == '_' {
CharKind::Word CharKind::Word