From b4cba64fd618900d0147c4ebdf55cb8573f03c8b Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Tue, 22 Aug 2023 00:30:52 +0200 Subject: [PATCH] Start fixing up platform tests --- Cargo.lock | 1 + crates/editor/Cargo.toml | 5 ++- crates/gpui/src/font_cache.rs | 45 ------------------- crates/gpui/src/gpui.rs | 2 + crates/gpui/src/platform.rs | 2 + .../src => gpui/src/platform}/test.rs | 34 +++++++------- crates/gpui/src/test.rs | 7 +-- crates/gpui_mac/src/fonts.rs | 35 +++++++++++++++ crates/gpui_mac/src/lib.rs | 9 +++- crates/gpui_macros/src/gpui_macros.rs | 4 +- 10 files changed, 72 insertions(+), 72 deletions(-) rename crates/{gpui_mac/src => gpui/src/platform}/test.rs (92%) diff --git a/Cargo.lock b/Cargo.lock index a866637492..fc45243cba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2270,6 +2270,7 @@ dependencies = [ "fuzzy", "git", "gpui", + "gpui_mac", "indoc", "itertools", "language", diff --git a/crates/editor/Cargo.toml b/crates/editor/Cargo.toml index 2fdeee56c1..93abae008d 100644 --- a/crates/editor/Cargo.toml +++ b/crates/editor/Cargo.toml @@ -18,7 +18,8 @@ test-support = [ "util/test-support", "workspace/test-support", "tree-sitter-rust", - "tree-sitter-typescript" + "tree-sitter-typescript", + "gpui_mac" ] [dependencies] @@ -44,6 +45,7 @@ theme = { path = "../theme" } util = { path = "../util" } sqlez = { path = "../sqlez" } workspace = { path = "../workspace" } +gpui_mac = { path = "../gpui_mac", optional = true} aho-corasick = "0.7" anyhow.workspace = true @@ -79,6 +81,7 @@ project = { path = "../project", features = ["test-support"] } settings = { path = "../settings", features = ["test-support"] } workspace = { path = "../workspace", features = ["test-support"] } + ctor.workspace = true env_logger.workspace = true rand.workspace = true diff --git a/crates/gpui/src/font_cache.rs b/crates/gpui/src/font_cache.rs index 4f0d4fd461..a21e61fb5a 100644 --- a/crates/gpui/src/font_cache.rs +++ b/crates/gpui/src/font_cache.rs @@ -278,48 +278,3 @@ impl DerefMut for LineWrapperHandle { self.wrapper.as_mut().unwrap() } } - -#[cfg(test)] -mod tests { - use super::*; - use crate::{ - fonts::{Style, Weight}, - platform::{test, Platform as _}, - }; - - #[test] - fn test_select_font() { - let platform = test::platform(); - let fonts = FontCache::new(platform.fonts()); - let arial = fonts - .load_family( - &["Arial"], - &Features { - calt: Some(false), - ..Default::default() - }, - ) - .unwrap(); - let arial_regular = fonts.select_font(arial, &Properties::new()).unwrap(); - let arial_italic = fonts - .select_font(arial, Properties::new().style(Style::Italic)) - .unwrap(); - let arial_bold = fonts - .select_font(arial, Properties::new().weight(Weight::BOLD)) - .unwrap(); - assert_ne!(arial_regular, arial_italic); - assert_ne!(arial_regular, arial_bold); - assert_ne!(arial_italic, arial_bold); - - let arial_with_calt = fonts - .load_family( - &["Arial"], - &Features { - calt: Some(true), - ..Default::default() - }, - ) - .unwrap(); - assert_ne!(arial_with_calt, arial); - } -} diff --git a/crates/gpui/src/gpui.rs b/crates/gpui/src/gpui.rs index 1f7a946f3f..1ac625fdf0 100644 --- a/crates/gpui/src/gpui.rs +++ b/crates/gpui/src/gpui.rs @@ -27,6 +27,8 @@ pub mod platform; pub use gpui_macros::{test, Element}; pub use window::{Axis, RectFExt, SizeConstraint, Vector2FExt, WindowContext}; +#[cfg(any(test, feature="test-support"))] +pub mod test; pub use anyhow; pub use serde_json; diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs index c5be71ff23..73b5508f0a 100644 --- a/crates/gpui/src/platform.rs +++ b/crates/gpui/src/platform.rs @@ -1,4 +1,6 @@ mod event; +#[cfg(any(test, feature = "test-support"))] +pub(super) mod test; use crate::{ executor, diff --git a/crates/gpui_mac/src/test.rs b/crates/gpui/src/platform/test.rs similarity index 92% rename from crates/gpui_mac/src/test.rs rename to crates/gpui/src/platform/test.rs index 81e941ecdc..2e2244c7cd 100644 --- a/crates/gpui_mac/src/test.rs +++ b/crates/gpui/src/platform/test.rs @@ -1,7 +1,7 @@ use super::{AppVersion, CursorStyle, WindowBounds}; use anyhow::{anyhow, Result}; use collections::VecDeque; -use gpui::{ +use crate::{ geometry::{ rect::RectF, vector::{vec2f, Vector2F}, @@ -64,7 +64,7 @@ impl super::ForegroundPlatform for ForegroundPlatform { fn on_resign_active(&self, _: Box) {} fn on_quit(&self, _: Box) {} fn on_reopen(&self, _: Box) {} - fn on_event(&self, _: Box bool>) {} + fn on_event(&self, _: Box bool>) {} fn on_open_urls(&self, _: Box)>) {} fn run(&self, _on_finish_launching: Box) { @@ -93,10 +93,6 @@ impl super::ForegroundPlatform for ForegroundPlatform { fn reveal_path(&self, _: &Path) {} } -pub fn platform() -> Platform { - Platform::new() -} - pub struct Platform { dispatcher: Arc, fonts: Arc, @@ -106,10 +102,10 @@ pub struct Platform { } impl Platform { - fn new() -> Self { + pub fn new(fonts: Arc) -> Self { Self { dispatcher: Arc::new(Dispatcher), - fonts: Arc::new(super::current::FontSystem::new()), + fonts, current_clipboard_item: Default::default(), cursor: Mutex::new(CursorStyle::Arrow), active_window: Default::default(), @@ -136,11 +132,11 @@ impl super::Platform for Platform { fn quit(&self) {} - fn screen_by_id(&self, _id: uuid::Uuid) -> Option> { + fn screen_by_id(&self, _id: uuid::Uuid) -> Option> { None } - fn screens(&self) -> Vec> { + fn screens(&self) -> Vec> { Default::default() } @@ -165,7 +161,7 @@ impl super::Platform for Platform { self.active_window.lock().clone() } - fn add_status_item(&self, handle: AnyWindowHandle) -> Box { + fn add_status_item(&self, handle: AnyWindowHandle) -> Box { Box::new(Window::new( handle, vec2f(24., 24.), @@ -263,7 +259,7 @@ pub struct Window { handle: AnyWindowHandle, pub(crate) size: Vector2F, scale_factor: f32, - current_scene: Option, + current_scene: Option, event_handlers: Vec bool>>, pub(crate) resize_handlers: Vec>, pub(crate) moved_handlers: Vec>, @@ -324,11 +320,11 @@ impl super::Window for Window { 24. } - fn appearance(&self) -> gpui::platform::Appearance { - gpui::platform::Appearance::Light + fn appearance(&self) -> super::Appearance { + super::Appearance::Light } - fn screen(&self) -> Rc { + fn screen(&self) -> Rc { Rc::new(Screen) } @@ -336,11 +332,11 @@ impl super::Window for Window { self } - fn set_input_handler(&mut self, _: Box) {} + fn set_input_handler(&mut self, _: Box) {} fn prompt( &self, - _: gpui::platform::PromptLevel, + _: super::PromptLevel, _: &str, _: &[&str], ) -> oneshot::Receiver { @@ -367,13 +363,13 @@ impl super::Window for Window { fn zoom(&self) {} - fn present_scene(&mut self, scene: gpui::Scene) { + fn present_scene(&mut self, scene: crate::Scene) { self.current_scene = Some(scene); } fn toggle_full_screen(&self) {} - fn on_event(&mut self, callback: Box bool>) { + fn on_event(&mut self, callback: Box bool>) { self.event_handlers.push(callback); } diff --git a/crates/gpui/src/test.rs b/crates/gpui/src/test.rs index def8ba2ce5..6329cb7bd5 100644 --- a/crates/gpui/src/test.rs +++ b/crates/gpui/src/test.rs @@ -35,6 +35,7 @@ fn init_logger() { // static ALLOC: dhat::Alloc = dhat::Alloc; pub fn run_test( + fonts: fn() -> std::sync::Arc, mut num_iterations: u64, mut starting_seed: u64, max_retries: usize, @@ -66,10 +67,10 @@ pub fn run_test( loop { let result = panic::catch_unwind(|| { + let fonts = fonts(); let foreground_platform = Rc::new(platform::test::foreground_platform()); - let platform = Arc::new(platform::test::platform()); - let font_system = platform.fonts(); - let font_cache = Arc::new(FontCache::new(font_system)); + let platform = Arc::new(platform::test::Platform::new(fonts.clone())); + let font_cache = Arc::new(FontCache::new(fonts.clone())); let mut prev_runnable_history: Option> = None; for _ in 0..num_iterations { diff --git a/crates/gpui_mac/src/fonts.rs b/crates/gpui_mac/src/fonts.rs index 5ebdf67ac3..081bf9cb8b 100644 --- a/crates/gpui_mac/src/fonts.rs +++ b/crates/gpui_mac/src/fonts.rs @@ -668,4 +668,39 @@ mod tests { // There's no glyph for \u{feff} assert_eq!(layout.runs[0].glyphs[1].id, 69); // b } + #[test] + fn test_select_font() { + let platform = test::platform(FontSystem::new()); + let fonts = FontCache::new(platform.fonts()); + let arial = fonts + .load_family( + &["Arial"], + &Features { + calt: Some(false), + ..Default::default() + }, + ) + .unwrap(); + let arial_regular = fonts.select_font(arial, &Properties::new()).unwrap(); + let arial_italic = fonts + .select_font(arial, Properties::new().style(Style::Italic)) + .unwrap(); + let arial_bold = fonts + .select_font(arial, Properties::new().weight(Weight::BOLD)) + .unwrap(); + assert_ne!(arial_regular, arial_italic); + assert_ne!(arial_regular, arial_bold); + assert_ne!(arial_italic, arial_bold); + + let arial_with_calt = fonts + .load_family( + &["Arial"], + &Features { + calt: Some(true), + ..Default::default() + }, + ) + .unwrap(); + assert_ne!(arial_with_calt, arial); + } } diff --git a/crates/gpui_mac/src/lib.rs b/crates/gpui_mac/src/lib.rs index 8fa9764ed9..8ee61340ec 100644 --- a/crates/gpui_mac/src/lib.rs +++ b/crates/gpui_mac/src/lib.rs @@ -10,8 +10,7 @@ mod renderer; mod screen; mod sprite_cache; mod status_item; -#[cfg(test)] -mod test; + mod window; use cocoa::{ @@ -104,3 +103,9 @@ unsafe impl objc::Encode for NSRange { unsafe fn ns_string(string: &str) -> id { NSString::alloc(nil).init_str(string).autorelease() } + +#[doc(hidden)] +#[allow(dead_code)] +pub fn font_system() -> Arc { + Arc::new(FontSystem::new()) +} diff --git a/crates/gpui_macros/src/gpui_macros.rs b/crates/gpui_macros/src/gpui_macros.rs index 2e6c725872..d68e0ecb7b 100644 --- a/crates/gpui_macros/src/gpui_macros.rs +++ b/crates/gpui_macros/src/gpui_macros.rs @@ -157,8 +157,8 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream { #[test] fn #outer_fn_name() { #inner_fn - #namespace::test::run_test( + gpui_mac::font_system, #num_iterations as u64, #starting_seed as u64, #max_retries, @@ -234,8 +234,8 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream { #[test] fn #outer_fn_name() { #inner_fn - #namespace::test::run_test( + gpui_mac::font_system, #num_iterations as u64, #starting_seed as u64, #max_retries,