From 600db0ec01809c8750513a435b162a45e1e06f66 Mon Sep 17 00:00:00 2001 From: Penwing Date: Tue, 23 Jan 2024 18:15:18 +0100 Subject: [PATCH] added tool tests --- src/calcifer.rs | 94 +++++++++++++++++----------------- src/tools/mod.rs | 124 +++++++++++++++++++++++---------------------- src/tools/tests.rs | 85 +++++++++++++++++++++++++++++++ 3 files changed, 196 insertions(+), 107 deletions(-) create mode 100644 src/tools/tests.rs diff --git a/src/calcifer.rs b/src/calcifer.rs index 4118552..b78f14b 100644 --- a/src/calcifer.rs +++ b/src/calcifer.rs @@ -29,8 +29,8 @@ impl super::Calcifer { }); } - pub fn draw_tree_panel(&mut self, ctx: &egui::Context) { - egui::SidePanel::left("file_tree_panel").show(ctx, |ui| { + pub fn draw_tree_panel(&mut self, ctx: &egui::Context) { + egui::SidePanel::left("file_tree_panel").show(ctx, |ui| { ui.heading("Bookshelf"); if ui.add(egui::Button::new("open file")).clicked() { if let Some(path) = rfd::FileDialog::new().pick_file() { @@ -41,29 +41,29 @@ impl super::Calcifer { let _ = self.list_files(ui, Path::new("/home/penwing/Documents/")); ui.separator(); }); - } + } - pub fn draw_terminal_panel(&mut self, ctx: &egui::Context) { - egui::TopBottomPanel::bottom("terminal") - .default_height(super::TERMINAL_HEIGHT.clone()) - .min_height(0.0) - .show(ctx, |ui| { - ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| { - ui.label(""); - ui.horizontal(|ui| { + pub fn draw_terminal_panel(&mut self, ctx: &egui::Context) { + egui::TopBottomPanel::bottom("terminal") + .default_height(super::TERMINAL_HEIGHT.clone()) + .min_height(0.0) + .show(ctx, |ui| { + ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| { + ui.label(""); + ui.horizontal(|ui| { ui.style_mut().visuals.extreme_bg_color = egui::Color32::from_hex(self.theme.bg).expect("Could not convert color"); - let Self { command, .. } = self; - ui.label(format!("{}>", env::current_dir().expect("Could not find Shell Environnment").file_name().expect("Could not get Shell Environnment Name").to_string_lossy().to_string())); - let response = ui.add(egui::TextEdit::singleline(command).desired_width(f32::INFINITY).lock_focus(true)); + let Self { command, .. } = self; + ui.label(format!("{}>", env::current_dir().expect("Could not find Shell Environnment").file_name().expect("Could not get Shell Environnment Name").to_string_lossy().to_string())); + let response = ui.add(egui::TextEdit::singleline(command).desired_width(f32::INFINITY).lock_focus(true)); - if response.lost_focus() && ctx.input(|i| i.key_pressed(egui::Key::Enter)) { - self.command_history.push(tools::run_command(self.command.clone())); - self.command = "".into(); - response.request_focus(); - } - }); - ui.separator(); - egui::ScrollArea::vertical().stick_to_bottom(true).show(ui, |ui| { + if response.lost_focus() && ctx.input(|i| i.key_pressed(egui::Key::Enter)) { + self.command_history.push(tools::run_command(self.command.clone())); + self.command = "".into(); + response.request_focus(); + } + }); + ui.separator(); + egui::ScrollArea::vertical().stick_to_bottom(true).show(ui, |ui| { ui.with_layout(egui::Layout::top_down(egui::Align::LEFT), |ui| { ui.separator(); ui.horizontal_wrapped(|ui| { @@ -82,12 +82,12 @@ impl super::Calcifer { } }); }); - }); - }); - }); - } - - pub fn draw_tab_panel(&mut self, ctx: &egui::Context) { + }); + }); + }); + } + + pub fn draw_tab_panel(&mut self, ctx: &egui::Context) { egui::TopBottomPanel::top("tabs") .resizable(false) .show(ctx, |ui| { @@ -123,7 +123,7 @@ impl super::Calcifer { pub fn draw_content_panel(&mut self, ctx: &egui::Context) { egui::CentralPanel::default().show(ctx, |ui| { - ui.horizontal(|ui| { + ui.horizontal(|ui| { ui.label("Picked file:"); ui.monospace(self.tabs[self.selected_tab.to_index()].path.to_string_lossy().to_string()); }); @@ -133,9 +133,9 @@ impl super::Calcifer { } self.draw_code_file(ui); - }); - } - + }); + } + fn draw_code_file(&mut self, ui: &mut egui::Ui) { let current_tab = &mut self.tabs[self.selected_tab.to_index()]; let lines = current_tab.code.chars().filter(|&c| c == '\n').count() + 1; @@ -143,8 +143,8 @@ impl super::Calcifer { if !self.search.result_selected && self.search.tab_selected { override_cursor = Some(CCursorRange::two( - CCursor::new(self.search.get_cursor_start()), - CCursor::new(self.search.get_cursor_end()), + CCursor::new(self.search.get_cursor_start()), + CCursor::new(self.search.get_cursor_end()), )); self.search.result_selected = true; } @@ -158,7 +158,7 @@ impl super::Calcifer { .show(ui, &mut current_tab.code, &mut current_tab.saved, &mut current_tab.last_cursor, &mut current_tab.scroll_offset, override_cursor); } - pub fn save_tab(&self) -> Option { + pub fn save_tab(&self) -> Option { if self.tabs[self.selected_tab.to_index()].path.file_name().expect("Could not get Tab Name").to_string_lossy().to_string() == "untitled" { return self.save_tab_as(); } else { @@ -197,8 +197,8 @@ impl super::Calcifer { tabs: Vec::new(), ..Default::default() }; - - for path in app_state.tabs { + + for path in app_state.tabs { if path.file_name().expect("Could not get Tab Name").to_string_lossy().to_string() != "untitled" { new.open_file(&path); } @@ -208,10 +208,10 @@ impl super::Calcifer { new.new_tab(); } - new - } - - pub fn save_state(&self) { + new + } + + pub fn save_state(&self) { let mut state_theme : usize = 0; if let Some(theme) = DEFAULT_THEMES.iter().position(|&r| r == self.theme) { state_theme = theme; @@ -223,14 +223,14 @@ impl super::Calcifer { state_tabs.push(tab.path.clone()); } let app_state = tools::AppState { - tabs: state_tabs, - theme: state_theme, - }; + tabs: state_tabs, + theme: state_theme, + }; let _ = tools::save_state(&app_state, super::SAVE_PATH); } - - fn list_files(&mut self, ui: &mut egui::Ui, path: &Path) -> io::Result<()> { + + fn list_files(&mut self, ui: &mut egui::Ui, path: &Path) -> io::Result<()> { if let Some(name) = path.file_name() { if path.is_dir() { egui::CollapsingHeader::new(name.to_string_lossy()).show(ui, |ui| { @@ -262,7 +262,7 @@ impl super::Calcifer { let new_tab = tools::Tab { path: path.into(), - code: fs::read_to_string(path).expect("Not able to read the file").replace(" ", "\t"), + code: fs::read_to_string(path).expect("Not able to read the file").replace(" ", "\t"), language: path.to_str().unwrap().split('.').last().unwrap().into(), saved: true, ..tools::Tab::default() diff --git a/src/tools/mod.rs b/src/tools/mod.rs index f438f60..cee1314 100644 --- a/src/tools/mod.rs +++ b/src/tools/mod.rs @@ -9,21 +9,21 @@ pub mod search; pub trait View { - fn ui(&mut self, ui: &mut egui::Ui, tabs: &mut Vec, selected_tab: &mut TabNumber); + fn ui(&mut self, ui: &mut egui::Ui, tabs: &mut Vec, selected_tab: &mut TabNumber); } /// Something to view pub trait Demo { - /// Is the demo enabled for this integraton? - fn is_enabled(&self, _ctx: &egui::Context) -> bool { - true - } + /// Is the demo enabled for this integraton? + fn is_enabled(&self, _ctx: &egui::Context) -> bool { + true + } - /// `&'static` so we can also use it as a key to store open/close state. - fn name(&self) -> &str; //'static + /// `&'static` so we can also use it as a key to store open/close state. + fn name(&self) -> &str; //'static - /// Show windows, etc - fn show(&mut self, ctx: &egui::Context, open: &mut bool, tabs: &mut Vec, selected_tab: &mut TabNumber); + /// Show windows, etc + fn show(&mut self, ctx: &egui::Context, open: &mut bool, tabs: &mut Vec, selected_tab: &mut TabNumber); } @@ -43,30 +43,30 @@ pub enum TabNumber { impl TabNumber { - pub fn from_index(n : usize) -> TabNumber { - match n { - 0 => TabNumber::Zero, - 1 => TabNumber::One, - 2 => TabNumber::Two, - 3 => TabNumber::Three, - 4 => TabNumber::Four, - 5 => TabNumber::Five, - 6 => TabNumber::Six, - 7 => TabNumber::Seven, - _ => TabNumber::None, - } - } - pub fn to_index(&self) -> usize { + pub fn from_index(n : usize) -> TabNumber { + match n { + 0 => TabNumber::Zero, + 1 => TabNumber::One, + 2 => TabNumber::Two, + 3 => TabNumber::Three, + 4 => TabNumber::Four, + 5 => TabNumber::Five, + 6 => TabNumber::Six, + 7 => TabNumber::Seven, + _ => TabNumber::None, + } + } + pub fn to_index(&self) -> usize { match self { TabNumber::Zero => 0, - TabNumber::One => 1, - TabNumber::Two => 2, - TabNumber::Three => 3, - TabNumber::Four => 4, - TabNumber::Five => 5, - TabNumber::Six => 6, - TabNumber::Seven => 7, - _ => 0, + TabNumber::One => 1, + TabNumber::Two => 2, + TabNumber::Three => 3, + TabNumber::Four => 4, + TabNumber::Five => 5, + TabNumber::Six => 6, + TabNumber::Seven => 7, + _ => 0, } } } @@ -111,37 +111,37 @@ pub struct CommandEntry { impl Default for CommandEntry { - fn default() -> Self { - Self { - env: env::current_dir().expect("Could not find Shell Environnment").file_name().expect("Could not get Shell Environnment Name").to_string_lossy().to_string(), - command : "".into(), + fn default() -> Self { + Self { + env: env::current_dir().expect("Could not find Shell Environnment").file_name().expect("Could not get Shell Environnment Name").to_string_lossy().to_string(), + command : "".into(), output : "".into(), error : "".into(), - } - } + } + } } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct AppState { - pub tabs: Vec, - pub theme: usize, + pub tabs: Vec, + pub theme: usize, } pub fn save_state(state: &AppState, file_path: &str) -> Result<(), std::io::Error> { - let serialized_state = serde_json::to_string(state)?; + let serialized_state = serde_json::to_string(state)?; - write(file_path, serialized_state)?; + write(file_path, serialized_state)?; - Ok(()) + Ok(()) } pub fn load_state(file_path: &str) -> Result { - let serialized_state = read_to_string(file_path)?; + let serialized_state = read_to_string(file_path)?; - Ok(serde_json::from_str(&serialized_state)?) + Ok(serde_json::from_str(&serialized_state)?) } @@ -180,10 +180,10 @@ pub fn to_syntax(language : &str) -> Syntax { pub fn run_command(cmd : String) -> CommandEntry { let mut entry = CommandEntry::default(); let output = Command::new("sh") - .arg("-c") - .arg(cmd.clone()) - .output() - .expect("failed to execute process"); + .arg("-c") + .arg(cmd.clone()) + .output() + .expect("failed to execute process"); entry.command = cmd; entry.output = (&String::from_utf8_lossy(&output.stdout)).to_string(); @@ -194,16 +194,20 @@ pub fn run_command(cmd : String) -> CommandEntry { pub fn sort_directories_first(a: &std::fs::DirEntry, b: &std::fs::DirEntry) -> Ordering { - let a_is_dir = a.path().is_dir(); - let b_is_dir = b.path().is_dir(); + let a_is_dir = a.path().is_dir(); + let b_is_dir = b.path().is_dir(); - // Directories come first, then files - if a_is_dir && !b_is_dir { - Ordering::Less - } else if !a_is_dir && b_is_dir { - Ordering::Greater - } else { - // Both are either directories or files, sort alphabetically - a.path().cmp(&b.path()) - } + // Directories come first, then files + if a_is_dir && !b_is_dir { + Ordering::Less + } else if !a_is_dir && b_is_dir { + Ordering::Greater + } else { + // Both are either directories or files, sort alphabetically + a.path().cmp(&b.path()) + } } + + +#[cfg(test)] +mod tests; \ No newline at end of file diff --git a/src/tools/tests.rs b/src/tools/tests.rs new file mode 100644 index 0000000..ad4187b --- /dev/null +++ b/src/tools/tests.rs @@ -0,0 +1,85 @@ +#[cfg(test)] + + +mod tests { + + use crate::tools::*; + + #[test] + fn test_tab_number_conversions() { + let tab_num = TabNumber::from_index(3); + assert_eq!(tab_num, TabNumber::Three); + assert_eq!(tab_num.to_index(), 3); + } + + #[test] + fn test_default_tab() { + let default_tab = Tab::default(); + assert_eq!(default_tab.path, PathBuf::from("untitled")); + assert_eq!(default_tab.code, "// Hello there, Master"); + assert_eq!(default_tab.language, "rs"); + assert!(!default_tab.saved); + assert_eq!(default_tab.scroll_offset, 0.0); + assert_eq!(default_tab.last_cursor, None); + } + + #[test] + fn test_get_tab_name() { + let tab = Tab { + path: PathBuf::from("/path/to/file.rs"), + code: String::from(""), + language: String::from("rs"), + saved: true, + scroll_offset: 0.0, + last_cursor: None, + }; + assert_eq!(tab.get_name(), "file.rs"); + } + + #[test] + fn test_default_command_entry() { + let default_entry = CommandEntry::default(); + assert_eq!( + default_entry.env, + env::current_dir() + .expect("Could not find Shell Environnment") + .file_name() + .expect("Could not get Shell Environnment Name") + .to_string_lossy() + .to_string() + ); + assert_eq!(default_entry.command, ""); + assert_eq!(default_entry.output, ""); + assert_eq!(default_entry.error, ""); + } + + #[test] + fn test_save_and_load_state() { + let tabs = vec![ + PathBuf::from("/path/to/file1.rs"), + PathBuf::from("/path/to/file2.py"), + ]; + let theme = 42; + let original_state = AppState { tabs, theme }; + + // Save state to a temporary file + let temp_file_path = "/tmp/test_state.json"; + save_state(&original_state, temp_file_path).expect("Failed to save state"); + + // Load state from the temporary file + let loaded_state = load_state(temp_file_path).expect("Failed to load state"); + + assert_eq!(original_state, loaded_state); + } + + #[test] + fn test_run_command() { + let cmd = "echo hello".to_string(); + let entry = run_command(cmd); + assert_eq!(entry.command, "echo hello"); + assert_eq!(entry.output.trim(), "hello"); + assert_eq!(entry.error, ""); + } + + // Add more tests as needed for other functions +} \ No newline at end of file