Skip to content

Commit

Permalink
[tui] Add editor component content cache tests
Browse files Browse the repository at this point in the history
Signed-off-by: Harshil Jani <[email protected]>
  • Loading branch information
Harshil-Jani authored and nazmulidris committed Dec 17, 2023
1 parent 1ea58c9 commit 6232240
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 7 deletions.
15 changes: 8 additions & 7 deletions tui/src/tui/editor/editor_buffer/editor_buffer_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,18 @@ use crate::*;
/// - The value is the [SelectionRange].
#[derive(Clone, PartialEq, Serialize, Deserialize, GetSize, Default)]
pub struct EditorBuffer {
editor_content: EditorContent,
pub(crate) editor_content: EditorContent,
pub(crate) history: EditorBufferHistory,
pub render_cache: HashMap<String, RenderOps>,
pub(crate) render_cache: HashMap<String, RenderOps>,
}

#[derive(Clone, PartialEq, Serialize, Deserialize, GetSize, Default)]
pub struct EditorContent {
lines: Vec<UnicodeString>,
caret_display_position: Position,
scroll_offset: ScrollOffset,
maybe_file_extension: Option<String>,
selection_map: SelectionMap,
pub(crate) lines: Vec<UnicodeString>,
pub(crate) caret_display_position: Position,
pub(crate) scroll_offset: ScrollOffset,
pub(crate) maybe_file_extension: Option<String>,
pub(crate) selection_map: SelectionMap,
}

#[derive(Clone, PartialEq, Serialize, Deserialize, GetSize)]
Expand Down Expand Up @@ -594,6 +594,7 @@ pub mod cache {
editor_buffer.render_cache.insert(key, render_ops.clone());
}
}

pub enum CaretKind {
Raw,
ScrollAdjusted,
Expand Down
127 changes: 127 additions & 0 deletions tui/src/tui/editor/editor_engine/editor_engine_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,3 +677,130 @@ mod no_syn_hi_path {
render_ops.push(RenderOp::ResetColor);
}
}

#[cfg(test)]
mod test_cache {
use std::collections::HashMap;

use super::*;

#[test]
fn test_render_content() {
let mut render_ops = &mut render_ops!();

let editor_buffer = &mut EditorBuffer::default();
let editor_engine = &mut EditorEngine::default();
let window_size = Size {
col_count: ch!(70),
row_count: ch!(15),
};
let has_focus = &mut HasFocus::default();

// Creating expected cache data structure
let mut cache = HashMap::new();

// The very first request to cache is always missed since cache is empty.
cache::render_content(
editor_buffer,
editor_engine,
window_size,
has_focus,
&mut render_ops,
);
test_cache_miss(editor_buffer, window_size, render_ops, &mut cache);

// Render the caret to screen. This should not change the content and result in a cache hit.
EditorEngineApi::render_caret(
RenderArgs {
editor_buffer,
editor_engine,
has_focus,
},
&mut render_ops,
);
cache::render_content(
editor_buffer,
editor_engine,
window_size,
has_focus,
&mut render_ops,
);
test_cache_hit(editor_buffer, &mut cache);

// Change in window size should invalidate the cache and result in a cache miss.
let window_size = Size {
col_count: ch!(50),
row_count: ch!(15),
};
cache::render_content(
editor_buffer,
editor_engine,
window_size,
has_focus,
&mut render_ops,
);
test_cache_miss(editor_buffer, window_size, render_ops, &mut cache);

// Render the selection of text to screen. This should not change the content and result in a cache hit.
EditorEngineApi::render_selection(
RenderArgs {
editor_buffer,
editor_engine,
has_focus,
},
&mut render_ops,
);
cache::render_content(
editor_buffer,
editor_engine,
window_size,
has_focus,
&mut render_ops,
);
test_cache_hit(editor_buffer, &mut cache);

// Change in scroll_offset should invalidate the cache and result in a cache miss.
editor_buffer.editor_content.scroll_offset = ScrollOffset {
col_index: ch!(1),
row_index: ch!(1),
};
cache::render_content(
editor_buffer,
editor_engine,
window_size,
has_focus,
&mut render_ops,
);
test_cache_miss(editor_buffer, window_size, render_ops, &mut cache);

// Change in content should invalidate the cache and result in a cache miss.
editor_buffer.set_lines(vec!["r3bl".to_string()]);
cache::render_content(
editor_buffer,
editor_engine,
window_size,
has_focus,
&mut render_ops,
);
test_cache_miss(editor_buffer, window_size, render_ops, &mut cache);
}

fn test_cache_miss(
editor_buffer: &mut EditorBuffer,
window_size: Size,
render_ops: &mut RenderOps,
cache: &mut HashMap<String, RenderOps>,
) {
cache.clear(); // invalidating cache
let key = format!("{}{}", editor_buffer.get_scroll_offset(), window_size); // generating key
cache.insert(key, render_ops.clone()); // enter the new entry into cache
assert_eq2!(editor_buffer.render_cache, cache.clone());
}

fn test_cache_hit(
editor_buffer: &mut EditorBuffer,
cache: &mut HashMap<String, RenderOps>,
) {
assert_eq2!(editor_buffer.render_cache, cache.clone());
}
}

0 comments on commit 6232240

Please sign in to comment.