test(explorer): add integration tests

pull/9/head
wongjiahau 2 years ago
parent 6321dc9ade
commit 7b63fda7d2

80
Cargo.lock generated

@ -114,6 +114,20 @@ dependencies = [
"num-traits",
]
[[package]]
name = "build-fs-tree"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85199b032e7d08f84570a62dc4b59d4ef37e094939d634e9dddd161515ec3ba9"
dependencies = [
"derive_more",
"pipe-trait",
"serde",
"serde_yaml",
"text-block-macros",
"thiserror",
]
[[package]]
name = "bumpalo"
version = "3.11.1"
@ -235,6 +249,12 @@ dependencies = [
"memchr",
]
[[package]]
name = "convert_case"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
[[package]]
name = "core-foundation-sys"
version = "0.8.3"
@ -352,6 +372,19 @@ dependencies = [
"parking_lot_core 0.9.4",
]
[[package]]
name = "derive_more"
version = "0.99.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321"
dependencies = [
"convert_case",
"proc-macro2",
"quote",
"rustc_version",
"syn",
]
[[package]]
name = "diff"
version = "0.1.13"
@ -1214,6 +1247,7 @@ version = "0.6.0"
dependencies = [
"anyhow",
"arc-swap",
"build-fs-tree",
"chrono",
"content_inspector",
"crossterm",
@ -1701,6 +1735,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pipe-trait"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1be1ec9e59f0360aefe84efa6f699198b685ab0d5718081e9f72aa2344289e2"
[[package]]
name = "pretty_assertions"
version = "1.3.0"
@ -1849,6 +1889,15 @@ dependencies = [
"str_indices",
]
[[package]]
name = "rustc_version"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
dependencies = [
"semver",
]
[[package]]
name = "rustversion"
version = "1.0.9"
@ -1882,6 +1931,12 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8132065adcfd6e02db789d9285a0deb2f3fcb04002865ab67d5fb103533898"
[[package]]
name = "semver"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58bc9567378fc7690d6b2addae4e60ac2eeea07becb2c64b9f218b53865cba2a"
[[package]]
name = "serde"
version = "1.0.152"
@ -1933,6 +1988,19 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_yaml"
version = "0.9.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fb06d4b6cdaef0e0c51fa881acb721bed3c924cfaa71d9c94a3b771dfdf6567"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]]
name = "sha1_smol"
version = "1.0.0"
@ -2093,6 +2161,12 @@ dependencies = [
"dirs-next",
]
[[package]]
name = "text-block-macros"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f8b59b4da1c1717deaf1de80f0179a9d8b4ac91c986d5fd9f4a8ff177b84049"
[[package]]
name = "textwrap"
version = "0.16.0"
@ -2336,6 +2410,12 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]]
name = "unsafe-libyaml"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc7ed8ba44ca06be78ea1ad2c3682a43349126c8818054231ee6f4748012aed2"
[[package]]
name = "url"
version = "2.3.1"

@ -41,7 +41,7 @@ New:
- [x] fix(filter): crash
- [x] fix(explorer/preview): panic if not tall enough
- [x] explorer(preview): content not sorted
- [] add integration test for Explorer
- [x] add integration test for Explorer
- [] search highlight matching word
- [] Error didn't clear
- [] bind "o" to open/close file/folder
@ -50,3 +50,5 @@ New:
- [] Sticky ancestors
- [] explorer(preview): overflow where bufferline is there
- [] explorer(preview): implement scrolling C-j/C-k
- [] symlink not showing
- [] remove unwrap and expect

@ -1 +1,2 @@
/target
test-explorer

@ -81,3 +81,4 @@ smallvec = "1.10"
indoc = "2.0.0"
tempfile = "3.3.0"
pretty_assertions = "1.3.0"
build-fs-tree = "0.4.1"

File diff suppressed because it is too large Load Diff

@ -315,8 +315,6 @@ pub struct TreeView<T: TreeViewItem> {
/// For implementing vertical scroll
winline: usize,
previous_area: Rect,
/// For implementing horizontal scoll
column: usize,
@ -324,10 +322,16 @@ pub struct TreeView<T: TreeViewItem> {
max_len: usize,
count: usize,
tree_symbol_style: String,
#[allow(clippy::type_complexity)]
pre_render: Option<Box<dyn Fn(&mut Self, Rect) + 'static>>,
#[allow(clippy::type_complexity)]
on_opened_fn: Option<Box<dyn FnMut(&mut T, &mut Context, &mut T::Params) -> TreeOp + 'static>>,
#[allow(clippy::type_complexity)]
on_folded_fn: Option<Box<dyn FnMut(&mut T, &mut Context, &mut T::Params) + 'static>>,
#[allow(clippy::type_complexity)]
on_next_key: Option<Box<dyn FnMut(&mut Context, &mut Self, &KeyEvent)>>,
}
@ -343,8 +347,8 @@ impl<T: TreeViewItem> TreeView<T> {
column: 0,
max_len: 0,
count: 0,
previous_area: Rect::new(0, 0, 0, 0),
tree_symbol_style: "ui.text".into(),
pre_render: None,
on_opened_fn: None,
on_folded_fn: None,
on_next_key: None,
@ -388,10 +392,11 @@ impl<T: TreeViewItem> TreeView<T> {
/// ```
/// vec!["helix-term", "src", "ui", "tree.rs"]
/// ```
pub fn reveal_item(&mut self, segments: Vec<&str>, filter: &String) -> Result<()> {
pub fn reveal_item(&mut self, segments: Vec<String>, filter: &String) -> Result<()> {
self.refresh_with_filter(filter)?;
// Expand the tree
let root = self.tree.item.name();
segments.iter().fold(
Ok(&mut self.tree),
|current_tree, segment| match current_tree {
@ -409,9 +414,8 @@ impl<T: TreeViewItem> TreeView<T> {
Ok(tree)
}
None => Err(anyhow::anyhow!(format!(
"Unable to find path: '{}'. current_segment = {}",
"Unable to find path: '{}'. current_segment = '{segment}'. current_root = '{root}'",
segments.join("/"),
segment
))),
}
}
@ -437,7 +441,9 @@ impl<T: TreeViewItem> TreeView<T> {
}
fn align_view_center(&mut self) {
self.winline = self.previous_area.height as usize / 2
self.pre_render = Some(Box::new(|tree, area| {
tree.winline = area.height as usize / 2
}))
}
fn align_view_top(&mut self) {
@ -445,7 +451,7 @@ impl<T: TreeViewItem> TreeView<T> {
}
fn align_view_bottom(&mut self) {
self.winline = self.previous_area.height as usize
self.pre_render = Some(Box::new(|tree, area| tree.winline = area.height as usize))
}
fn regenerate_index(&mut self) {
@ -492,11 +498,6 @@ impl<T: TreeViewItem> TreeView<T> {
self.move_down(usize::MAX / 2)
}
#[cfg(test)]
fn set_previous_area(&mut self, area: Rect) {
self.previous_area = area
}
fn move_leftmost(&mut self) {
self.move_left(usize::MAX / 2);
}
@ -608,7 +609,7 @@ impl<T: TreeViewItem> TreeView<T> {
self.search_previous(&self.search_str.clone())
}
fn move_down(&mut self, rows: usize) {
pub fn move_down(&mut self, rows: usize) {
let len = self.tree.len();
if len > 0 {
self.set_selected(std::cmp::min(self.selected + rows, len.saturating_sub(1)))
@ -647,7 +648,7 @@ impl<T: TreeViewItem> TreeView<T> {
}
}
fn move_up(&mut self, rows: usize) {
pub fn move_up(&mut self, rows: usize) {
let len = self.tree.len();
if len > 0 {
self.set_selected(self.selected.saturating_sub(rows).max(0))
@ -659,27 +660,34 @@ impl<T: TreeViewItem> TreeView<T> {
}
fn move_right(&mut self, cols: usize) {
let max_scroll = self
.max_len
.saturating_sub(self.previous_area.width as usize)
.saturating_add(1);
self.column = max_scroll.min(self.column + cols);
self.pre_render = Some(Box::new(move |tree, area| {
let max_scroll = tree.max_len.saturating_sub(area.width as usize);
tree.column = max_scroll.min(tree.column + cols);
}));
}
fn move_down_half_page(&mut self) {
self.move_down(self.previous_area.height as usize / 2)
self.pre_render = Some(Box::new(|tree, area| {
tree.move_down((area.height / 2) as usize);
}));
}
fn move_up_half_page(&mut self) {
self.move_up(self.previous_area.height as usize / 2);
self.pre_render = Some(Box::new(|tree, area| {
tree.move_up((area.height / 2) as usize);
}));
}
fn move_down_page(&mut self) {
self.move_down(self.previous_area.height as usize);
self.pre_render = Some(Box::new(|tree, area| {
tree.move_down((area.height) as usize);
}));
}
fn move_up_page(&mut self) {
self.move_up(self.previous_area.height as usize);
self.pre_render = Some(Box::new(|tree, area| {
tree.move_up((area.height) as usize);
}));
}
fn save_view(&mut self) {
@ -856,8 +864,7 @@ impl<T: TreeViewItem + Clone> TreeView<T> {
}
#[cfg(test)]
fn render_to_string(&mut self, filter: &String) -> String {
let area = self.previous_area;
pub fn render_to_string(&mut self, area: Rect, filter: &String) -> String {
let lines = self.render_lines(area, filter);
lines
.into_iter()
@ -876,10 +883,11 @@ impl<T: TreeViewItem + Clone> TreeView<T> {
}
fn render_lines(&mut self, area: Rect, filter: &String) -> Vec<RenderedLine> {
self.previous_area = area;
self.winline = self
.winline
.min(self.previous_area.height.saturating_sub(1) as usize);
if let Some(pre_render) = self.pre_render.take() {
pre_render(self, area);
}
self.winline = self.winline.min(area.height.saturating_sub(1) as usize);
let skip = self.selected.saturating_sub(self.winline);
let params = RenderTreeParams {
tree: &self.tree,
@ -902,7 +910,7 @@ impl<T: TreeViewItem + Clone> TreeView<T> {
.max()
.unwrap_or(0);
let max_width = self.previous_area.width as usize;
let max_width = area.width as usize;
lines
.into_iter()
@ -1185,9 +1193,8 @@ mod test_tree_view {
}
fn dummy_tree_view<'a>() -> TreeView<Item<'a>> {
let root = item("who_lives_in_a_pineapple_under_the_sea");
let mut view = TreeView::new(
root,
TreeView::new(
item("who_lives_in_a_pineapple_under_the_sea"),
vec_to_tree(vec![
item("gary_the_snail"),
item("krabby_patty"),
@ -1200,11 +1207,7 @@ mod test_tree_view {
item("karen"),
item("plankton"),
]),
);
view.set_previous_area(dummy_area());
view
)
}
fn dummy_area() -> Rect {
@ -1212,7 +1215,7 @@ mod test_tree_view {
}
fn render<'a>(view: &mut TreeView<Item<'a>>) -> String {
view.render_to_string(&"".to_string())
view.render_to_string(dummy_area(), &"".to_string())
}
#[test]
@ -1393,7 +1396,6 @@ mod test_tree_view {
fn test_move_half() {
let mut view = dummy_tree_view();
view.move_down_half_page();
assert_eq!(view.selected, 2);
assert_eq!(
render(&mut view),
"
@ -1534,7 +1536,10 @@ mod test_tree_view {
#[test]
fn test_move_left_right() {
let mut view = dummy_tree_view();
view.set_previous_area(dummy_area().with_width(20));
fn render<'a>(view: &mut TreeView<Item<'a>>) -> String {
view.render_to_string(dummy_area().with_width(20), &"".to_string())
}
assert_eq!(
render(&mut view),
@ -1627,7 +1632,7 @@ krabby_patty
);
view.move_rightmost();
assert_eq!(render(&mut view), "(apple_under_the_sea)\n\n\n\n");
assert_eq!(render(&mut view), "(eapple_under_the_sea)\n\n\n\n");
}
#[test]
@ -1921,6 +1926,8 @@ krabby_patty
fn test_jump_backward() {
let mut view = dummy_tree_view();
view.move_down_half_page();
render(&mut view);
view.move_down_half_page();
assert_eq!(
render(&mut view),

Loading…
Cancel
Save