Add tests for manipulate_lines()

This commit is contained in:
Joseph T. Lyons 2023-07-25 15:17:16 -04:00
parent bf2ca57f55
commit 4085df5146

View file

@ -2501,82 +2501,153 @@ fn test_join_lines_with_multi_selection(cx: &mut TestAppContext) {
} }
#[gpui::test] #[gpui::test]
fn test_manipulate_lines_with_single_selection(cx: &mut TestAppContext) { async fn test_manipulate_lines_with_single_selection(cx: &mut TestAppContext) {
init_test(cx, |_| {}); init_test(cx, |_| {});
cx.add_window(|cx| { let mut cx = EditorTestContext::new(cx).await;
let buffer = MultiBuffer::build_simple("dddd\nccc\nbb\na\n\n", cx);
let mut editor = build_editor(buffer.clone(), cx);
let buffer = buffer.read(cx).as_singleton().unwrap();
editor.change_selections(None, cx, |s| { // Test sort_lines_case_insensitive()
s.select_ranges([Point::new(0, 1)..Point::new(0, 1)]) cx.set_state(indoc! {"
}); «z
editor.sort_lines_case_sensitive(&SortLinesCaseSensitive, cx); y
assert_eq!( x
buffer.read(cx).text(), Z
"dddd\nccc\nbb\na\n\n", Y
"no sorting when single cursor parked on single line" »
); "});
assert_eq!( cx.update_editor(|e, cx| e.sort_lines_case_insensitive(&SortLinesCaseInsensitive, cx));
editor.selections.ranges::<Point>(cx), cx.assert_editor_state(indoc! {"
&[Point::new(0, 1)..Point::new(0, 2)] «x
); X
y
Y
z
»
"});
editor.change_selections(None, cx, |s| { // Test reverse_lines()
s.select_ranges([Point::new(0, 2)..Point::new(5, 0)]) cx.set_state(indoc! {"
}); «5
//editor.sort_lines(); 4
assert_eq!( 3
buffer.read(cx).text(), 2
"a\nbb\nccc\ndddd\n\n", 1ˇ»
"single selection is sorted" "});
); cx.update_editor(|e, cx| e.reverse_lines(&ReverseLines, cx));
assert_eq!( cx.assert_editor_state(indoc! {"
editor.selections.ranges::<Point>(cx), «1
&[Point::new(0, 0)..Point::new(5, 1)] 2
); 3
4
5ˇ»
"});
editor // Skip testing shuffle_line()
});
// From here on out, test more complex cases of manipulate_lines() with a single driver method: sort_lines_case_sensitive()
// Since all methods calling manipulate_lines() are doing the exact same general thing (reordering lines)
// Don't manipulate when cursor is on single line, but expand the selection
cx.set_state(indoc! {"
ddˇdd
ccc
bb
a
"});
cx.update_editor(|e, cx| e.sort_lines_case_sensitive(&SortLinesCaseSensitive, cx));
cx.assert_editor_state(indoc! {"
«ddddˇ»
ccc
bb
a
"});
// Basic manipulate case
// Start selection moves to column 0
// End of selection shrinks to fit shorter line
cx.set_state(indoc! {"
dd«d
ccc
bb
aaaaaˇ»
"});
cx.update_editor(|e, cx| e.sort_lines_case_sensitive(&SortLinesCaseSensitive, cx));
cx.assert_editor_state(indoc! {"
«aaaaa
bb
ccc
dddˇ»
"});
// Manipulate case with newlines
cx.set_state(indoc! {"
dd«d
ccc
bb
aaaaa
ˇ»
"});
cx.update_editor(|e, cx| e.sort_lines_case_sensitive(&SortLinesCaseSensitive, cx));
cx.assert_editor_state(indoc! {"
«
aaaaa
bb
ccc
dddˇ»
"});
} }
#[gpui::test] #[gpui::test]
fn test_manipulate_lines_with_multi_selection(cx: &mut TestAppContext) { async fn test_manipulate_lines_with_multi_selection(cx: &mut TestAppContext) {
init_test(cx, |_| {}); init_test(cx, |_| {});
cx.add_window(|cx| { let mut cx = EditorTestContext::new(cx).await;
let buffer = MultiBuffer::build_simple("dddd\nccc\nbb\na\n\n3\n2\n1\n\n", cx);
let mut editor = build_editor(buffer.clone(), cx);
let buffer = buffer.read(cx).as_singleton().unwrap();
editor.change_selections(None, cx, |s| { // Manipulate with multiple selections on a single line
s.select_ranges([ cx.set_state(indoc! {"
Point::new(0, 2)..Point::new(3, 2), dd«dd
Point::new(5, 0)..Point::new(7, 1), »c«c
]) bb
}); aaaˇ»aa
"});
cx.update_editor(|e, cx| e.sort_lines_case_sensitive(&SortLinesCaseSensitive, cx));
cx.assert_editor_state(indoc! {"
«aaaaa
bb
ccc
ddddˇ»
"});
editor.sort_lines_case_sensitive(&SortLinesCaseSensitive, cx); // Manipulate with multiple disjoin selections
assert_eq!(buffer.read(cx).text(), "a\nbb\nccc\ndddd\n\n1\n2\n3\n\n"); cx.set_state(indoc! {"
assert_eq!( 5«
editor.selections.ranges::<Point>(cx), 4
&[Point::new(0, 5)..Point::new(2, 2)] 3
); 2
assert_eq!( 1ˇ»
editor.selections.ranges::<Point>(cx),
&[Point::new(0, 5)..Point::new(2, 2)]
);
// assert_eq!( dd«dd
// editor.selections.ranges::<Point>(cx), ccc
// [ bb
// Point::new(0, 7)..Point::new(0, 7), aaaˇ»aa
// Point::new(1, 3)..Point::new(1, 3) "});
// ] cx.update_editor(|e, cx| e.sort_lines_case_sensitive(&SortLinesCaseSensitive, cx));
// ); cx.assert_editor_state(indoc! {"
editor «1
}); 2
3
4
5ˇ»
«aaaaa
bb
ccc
ddddˇ»
"});
} }
#[gpui::test] #[gpui::test]