From ccecda4f666a8a1167bdb6a0b2f3ef9c331a59f4 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Fri, 30 Jul 2021 08:37:13 -0700 Subject: Add more unit tests for pos_at_screen_coords. Specifically, test cases for double-width characters and grapheme clusters. --- helix-view/src/view.rs | 95 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 68 insertions(+), 27 deletions(-) (limited to 'helix-view') diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index 1b350172..62ab2642 100644 --- a/helix-view/src/view.rs +++ b/helix-view/src/view.rs @@ -245,76 +245,117 @@ impl View { mod tests { use super::*; use helix_core::Rope; + const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter #[test] fn test_text_pos_at_screen_coords() { let mut view = View::new(DocumentId::default()); view.area = Rect::new(40, 40, 40, 40); - let text = Rope::from_str("abc\n\tdef"); + let rope = Rope::from_str("abc\n\tdef"); + let text = rope.slice(..); + + assert_eq!(view.text_pos_at_screen_coords(&text, 40, 2, 4), None); + + assert_eq!(view.text_pos_at_screen_coords(&text, 40, 41, 4), None); + + assert_eq!(view.text_pos_at_screen_coords(&text, 0, 2, 4), None); + + assert_eq!(view.text_pos_at_screen_coords(&text, 0, 49, 4), None); + + assert_eq!(view.text_pos_at_screen_coords(&text, 0, 41, 4), None); + + assert_eq!(view.text_pos_at_screen_coords(&text, 40, 81, 4), None); + + assert_eq!(view.text_pos_at_screen_coords(&text, 78, 41, 4), None); assert_eq!( - view.text_pos_at_screen_coords(&text.slice(..), 40, 2, 4), - None + view.text_pos_at_screen_coords(&text, 40, 40 + OFFSET + 3, 4), + Some(3) ); + assert_eq!(view.text_pos_at_screen_coords(&text, 40, 80, 4), Some(3)); + assert_eq!( - view.text_pos_at_screen_coords(&text.slice(..), 40, 41, 4), - None + view.text_pos_at_screen_coords(&text, 41, 40 + OFFSET + 1, 4), + Some(5) ); assert_eq!( - view.text_pos_at_screen_coords(&text.slice(..), 0, 2, 4), - None + view.text_pos_at_screen_coords(&text, 41, 40 + OFFSET + 4, 4), + Some(5) ); assert_eq!( - view.text_pos_at_screen_coords(&text.slice(..), 0, 49, 4), - None + view.text_pos_at_screen_coords(&text, 41, 40 + OFFSET + 7, 4), + Some(8) ); + assert_eq!(view.text_pos_at_screen_coords(&text, 41, 80, 4), Some(8)); + } + + #[test] + fn test_text_pos_at_screen_coords_cjk() { + let mut view = View::new(DocumentId::default()); + view.area = Rect::new(40, 40, 40, 40); + let rope = Rope::from_str("Hi! こんにちは皆さん"); + let text = rope.slice(..); + assert_eq!( - view.text_pos_at_screen_coords(&text.slice(..), 0, 41, 4), - None + view.text_pos_at_screen_coords(&text, 40, 40 + OFFSET + 0, 4), + Some(0) ); assert_eq!( - view.text_pos_at_screen_coords(&text.slice(..), 40, 81, 4), - None + view.text_pos_at_screen_coords(&text, 40, 40 + OFFSET + 5, 4), + Some(5) ); assert_eq!( - view.text_pos_at_screen_coords(&text.slice(..), 78, 41, 4), - None + view.text_pos_at_screen_coords(&text, 40, 40 + OFFSET + 6, 4), + Some(5) ); assert_eq!( - view.text_pos_at_screen_coords(&text.slice(..), 40, 40 + 7 + 3, 4), - Some(3) + view.text_pos_at_screen_coords(&text, 40, 40 + OFFSET + 7, 4), + Some(6) ); assert_eq!( - view.text_pos_at_screen_coords(&text.slice(..), 40, 80, 4), - Some(3) + view.text_pos_at_screen_coords(&text, 40, 40 + OFFSET + 8, 4), + Some(6) ); + } + + #[test] + fn test_text_pos_at_screen_coords_graphemes() { + let mut view = View::new(DocumentId::default()); + view.area = Rect::new(40, 40, 40, 40); + let rope = Rope::from_str("Hèl̀l̀ò world!"); + let text = rope.slice(..); assert_eq!( - view.text_pos_at_screen_coords(&text.slice(..), 41, 40 + 7 + 1, 4), - Some(5) + view.text_pos_at_screen_coords(&text, 40, 40 + OFFSET + 0, 4), + Some(0) ); assert_eq!( - view.text_pos_at_screen_coords(&text.slice(..), 41, 40 + 7 + 4, 4), - Some(5) + view.text_pos_at_screen_coords(&text, 40, 40 + OFFSET + 1, 4), + Some(1) ); assert_eq!( - view.text_pos_at_screen_coords(&text.slice(..), 41, 40 + 7 + 7, 4), - Some(8) + view.text_pos_at_screen_coords(&text, 40, 40 + OFFSET + 2, 4), + Some(3) ); assert_eq!( - view.text_pos_at_screen_coords(&text.slice(..), 41, 80, 4), - Some(8) + view.text_pos_at_screen_coords(&text, 40, 40 + OFFSET + 3, 4), + Some(5) + ); + + assert_eq!( + view.text_pos_at_screen_coords(&text, 40, 40 + OFFSET + 4, 4), + Some(7) ); } } -- cgit v1.2.3-70-g09d2