diff --git a/Cargo.lock b/Cargo.lock index 2c202e1de6..e0ce004172 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3129,6 +3129,7 @@ dependencies = [ name = "gpui_platform" version = "0.1.0" dependencies = [ + "gpui", "gpui_mac", ] diff --git a/crates/gpui/src/text_layout.rs b/crates/gpui/src/text_layout.rs index 25b5c3f430..26d7291a82 100644 --- a/crates/gpui/src/text_layout.rs +++ b/crates/gpui/src/text_layout.rs @@ -528,7 +528,7 @@ pub struct ShapedBoundary { } impl Boundary { - fn new(ix: usize, next_indent: u32) -> Self { + pub fn new(ix: usize, next_indent: u32) -> Self { Self { ix, next_indent } } } @@ -727,129 +727,3 @@ impl LineWrapper { .width } } - -#[cfg(test)] -mod tests { - use super::*; - use crate::fonts::{Properties, Weight}; - - #[crate::test(self)] - fn test_wrap_line(cx: &mut crate::AppContext) { - let font_cache = cx.font_cache().clone(); - let font_system = cx.platform().fonts(); - let family = font_cache - .load_family(&["Courier"], &Default::default()) - .unwrap(); - let font_id = font_cache.select_font(family, &Default::default()).unwrap(); - - let mut wrapper = LineWrapper::new(font_id, 16., font_system); - assert_eq!( - wrapper - .wrap_line("aa bbb cccc ddddd eeee", 72.0) - .collect::>(), - &[ - Boundary::new(7, 0), - Boundary::new(12, 0), - Boundary::new(18, 0) - ], - ); - assert_eq!( - wrapper - .wrap_line("aaa aaaaaaaaaaaaaaaaaa", 72.0) - .collect::>(), - &[ - Boundary::new(4, 0), - Boundary::new(11, 0), - Boundary::new(18, 0) - ], - ); - assert_eq!( - wrapper.wrap_line(" aaaaaaa", 72.).collect::>(), - &[ - Boundary::new(7, 5), - Boundary::new(9, 5), - Boundary::new(11, 5), - ] - ); - assert_eq!( - wrapper - .wrap_line(" ", 72.) - .collect::>(), - &[ - Boundary::new(7, 0), - Boundary::new(14, 0), - Boundary::new(21, 0) - ] - ); - assert_eq!( - wrapper - .wrap_line(" aaaaaaaaaaaaaa", 72.) - .collect::>(), - &[ - Boundary::new(7, 0), - Boundary::new(14, 3), - Boundary::new(18, 3), - Boundary::new(22, 3), - ] - ); - } - - #[crate::test(self, retries = 5)] - fn test_wrap_shaped_line(cx: &mut crate::AppContext) { - // This is failing intermittently on CI and we don't have time to figure it out - let font_cache = cx.font_cache().clone(); - let font_system = cx.platform().fonts(); - let text_layout_cache = TextLayoutCache::new(font_system.clone()); - - let family = font_cache - .load_family(&["Helvetica"], &Default::default()) - .unwrap(); - let font_id = font_cache.select_font(family, &Default::default()).unwrap(); - let normal = RunStyle { - font_id, - color: Default::default(), - underline: Default::default(), - }; - let bold = RunStyle { - font_id: font_cache - .select_font( - family, - &Properties { - weight: Weight::BOLD, - ..Default::default() - }, - ) - .unwrap(), - color: Default::default(), - underline: Default::default(), - }; - - let text = "aa bbb cccc ddddd eeee"; - let line = text_layout_cache.layout_str( - text, - 16.0, - &[(4, normal), (5, bold), (6, normal), (1, bold), (7, normal)], - ); - - let mut wrapper = LineWrapper::new(font_id, 16., font_system); - assert_eq!( - wrapper - .wrap_shaped_line(text, &line, 72.0) - .collect::>(), - &[ - ShapedBoundary { - run_ix: 1, - glyph_ix: 3 - }, - ShapedBoundary { - run_ix: 2, - glyph_ix: 3 - }, - ShapedBoundary { - run_ix: 4, - glyph_ix: 2 - } - ], - ); - } -} diff --git a/crates/gpui_platform/Cargo.toml b/crates/gpui_platform/Cargo.toml index a183d161f9..518059def8 100644 --- a/crates/gpui_platform/Cargo.toml +++ b/crates/gpui_platform/Cargo.toml @@ -5,5 +5,11 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +test-support = ["gpui/test-support"] + +[dependencies] +gpui = {path = "../gpui", optional = true} + [target.'cfg(target_os = "macos")'.dependencies] current_platform = {path = "../gpui_mac", package = "gpui_mac"} diff --git a/crates/gpui_platform/src/lib.rs b/crates/gpui_platform/src/lib.rs index 16f464b3e5..3adf78be5e 100644 --- a/crates/gpui_platform/src/lib.rs +++ b/crates/gpui_platform/src/lib.rs @@ -1 +1,3 @@ pub use current_platform::*; +#[cfg(any(test, feature = "test-support"))] +mod tests; diff --git a/crates/gpui_platform/src/tests/mod.rs b/crates/gpui_platform/src/tests/mod.rs new file mode 100644 index 0000000000..949c9c3e27 --- /dev/null +++ b/crates/gpui_platform/src/tests/mod.rs @@ -0,0 +1,125 @@ + + + use gpui::text_layout::*; + use gpui::fonts::{Properties, Weight}; + + #[gpui::test] + fn test_wrap_line(cx: &mut gpui::AppContext) { + let font_cache = cx.font_cache().clone(); + let font_system = cx.platform().fonts(); + let family = font_cache + .load_family(&["Courier"], &Default::default()) + .unwrap(); + let font_id = font_cache.select_font(family, &Default::default()).unwrap(); + + let mut wrapper = LineWrapper::new(font_id, 16., font_system); + assert_eq!( + wrapper + .wrap_line("aa bbb cccc ddddd eeee", 72.0) + .collect::>(), + &[ + Boundary::new(7, 0), + Boundary::new(12, 0), + Boundary::new(18, 0) + ], + ); + assert_eq!( + wrapper + .wrap_line("aaa aaaaaaaaaaaaaaaaaa", 72.0) + .collect::>(), + &[ + Boundary::new(4, 0), + Boundary::new(11, 0), + Boundary::new(18, 0) + ], + ); + assert_eq!( + wrapper.wrap_line(" aaaaaaa", 72.).collect::>(), + &[ + Boundary::new(7, 5), + Boundary::new(9, 5), + Boundary::new(11, 5), + ] + ); + assert_eq!( + wrapper + .wrap_line(" ", 72.) + .collect::>(), + &[ + Boundary::new(7, 0), + Boundary::new(14, 0), + Boundary::new(21, 0) + ] + ); + assert_eq!( + wrapper + .wrap_line(" aaaaaaaaaaaaaa", 72.) + .collect::>(), + &[ + Boundary::new(7, 0), + Boundary::new(14, 3), + Boundary::new(18, 3), + Boundary::new(22, 3), + ] + ); + } + + #[gpui::test(retries = 5)] + fn test_wrap_shaped_line(cx: &mut gpui::AppContext) { + // This is failing intermittently on CI and we don't have time to figure it out + let font_cache = cx.font_cache().clone(); + let font_system = cx.platform().fonts(); + let text_layout_cache = TextLayoutCache::new(font_system.clone()); + + let family = font_cache + .load_family(&["Helvetica"], &Default::default()) + .unwrap(); + let font_id = font_cache.select_font(family, &Default::default()).unwrap(); + let normal = RunStyle { + font_id, + color: Default::default(), + underline: Default::default(), + }; + let bold = RunStyle { + font_id: font_cache + .select_font( + family, + &Properties { + weight: Weight::BOLD, + ..Default::default() + }, + ) + .unwrap(), + color: Default::default(), + underline: Default::default(), + }; + + let text = "aa bbb cccc ddddd eeee"; + let line = text_layout_cache.layout_str( + text, + 16.0, + &[(4, normal), (5, bold), (6, normal), (1, bold), (7, normal)], + ); + + let mut wrapper = LineWrapper::new(font_id, 16., font_system); + assert_eq!( + wrapper + .wrap_shaped_line(text, &line, 72.0) + .collect::>(), + &[ + ShapedBoundary { + run_ix: 1, + glyph_ix: 3 + }, + ShapedBoundary { + run_ix: 2, + glyph_ix: 3 + }, + ShapedBoundary { + run_ix: 4, + glyph_ix: 2 + } + ], + ); + +}