added tool tests

This commit is contained in:
Penwing 2024-01-23 18:15:18 +01:00
parent 59342fa980
commit 600db0ec01
3 changed files with 196 additions and 107 deletions

View file

@ -29,8 +29,8 @@ impl super::Calcifer {
}); });
} }
pub fn draw_tree_panel(&mut self, ctx: &egui::Context) { pub fn draw_tree_panel(&mut self, ctx: &egui::Context) {
egui::SidePanel::left("file_tree_panel").show(ctx, |ui| { egui::SidePanel::left("file_tree_panel").show(ctx, |ui| {
ui.heading("Bookshelf"); ui.heading("Bookshelf");
if ui.add(egui::Button::new("open file")).clicked() { if ui.add(egui::Button::new("open file")).clicked() {
if let Some(path) = rfd::FileDialog::new().pick_file() { 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/")); let _ = self.list_files(ui, Path::new("/home/penwing/Documents/"));
ui.separator(); ui.separator();
}); });
} }
pub fn draw_terminal_panel(&mut self, ctx: &egui::Context) { pub fn draw_terminal_panel(&mut self, ctx: &egui::Context) {
egui::TopBottomPanel::bottom("terminal") egui::TopBottomPanel::bottom("terminal")
.default_height(super::TERMINAL_HEIGHT.clone()) .default_height(super::TERMINAL_HEIGHT.clone())
.min_height(0.0) .min_height(0.0)
.show(ctx, |ui| { .show(ctx, |ui| {
ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| { ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| {
ui.label(""); ui.label("");
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.style_mut().visuals.extreme_bg_color = egui::Color32::from_hex(self.theme.bg).expect("Could not convert color"); ui.style_mut().visuals.extreme_bg_color = egui::Color32::from_hex(self.theme.bg).expect("Could not convert color");
let Self { command, .. } = self; 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())); 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 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)) { 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_history.push(tools::run_command(self.command.clone()));
self.command = "".into(); self.command = "".into();
response.request_focus(); response.request_focus();
} }
}); });
ui.separator(); ui.separator();
egui::ScrollArea::vertical().stick_to_bottom(true).show(ui, |ui| { egui::ScrollArea::vertical().stick_to_bottom(true).show(ui, |ui| {
ui.with_layout(egui::Layout::top_down(egui::Align::LEFT), |ui| { ui.with_layout(egui::Layout::top_down(egui::Align::LEFT), |ui| {
ui.separator(); ui.separator();
ui.horizontal_wrapped(|ui| { 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") egui::TopBottomPanel::top("tabs")
.resizable(false) .resizable(false)
.show(ctx, |ui| { .show(ctx, |ui| {
@ -123,7 +123,7 @@ impl super::Calcifer {
pub fn draw_content_panel(&mut self, ctx: &egui::Context) { pub fn draw_content_panel(&mut self, ctx: &egui::Context) {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("Picked file:"); ui.label("Picked file:");
ui.monospace(self.tabs[self.selected_tab.to_index()].path.to_string_lossy().to_string()); ui.monospace(self.tabs[self.selected_tab.to_index()].path.to_string_lossy().to_string());
}); });
@ -133,8 +133,8 @@ impl super::Calcifer {
} }
self.draw_code_file(ui); self.draw_code_file(ui);
}); });
} }
fn draw_code_file(&mut self, ui: &mut egui::Ui) { fn draw_code_file(&mut self, ui: &mut egui::Ui) {
let current_tab = &mut self.tabs[self.selected_tab.to_index()]; let current_tab = &mut self.tabs[self.selected_tab.to_index()];
@ -143,8 +143,8 @@ impl super::Calcifer {
if !self.search.result_selected && self.search.tab_selected { if !self.search.result_selected && self.search.tab_selected {
override_cursor = Some(CCursorRange::two( override_cursor = Some(CCursorRange::two(
CCursor::new(self.search.get_cursor_start()), CCursor::new(self.search.get_cursor_start()),
CCursor::new(self.search.get_cursor_end()), CCursor::new(self.search.get_cursor_end()),
)); ));
self.search.result_selected = true; 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); .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<PathBuf> { pub fn save_tab(&self) -> Option<PathBuf> {
if self.tabs[self.selected_tab.to_index()].path.file_name().expect("Could not get Tab Name").to_string_lossy().to_string() == "untitled" { 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(); return self.save_tab_as();
} else { } else {
@ -198,7 +198,7 @@ impl super::Calcifer {
..Default::default() ..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" { if path.file_name().expect("Could not get Tab Name").to_string_lossy().to_string() != "untitled" {
new.open_file(&path); new.open_file(&path);
} }
@ -208,10 +208,10 @@ impl super::Calcifer {
new.new_tab(); new.new_tab();
} }
new new
} }
pub fn save_state(&self) { pub fn save_state(&self) {
let mut state_theme : usize = 0; let mut state_theme : usize = 0;
if let Some(theme) = DEFAULT_THEMES.iter().position(|&r| r == self.theme) { if let Some(theme) = DEFAULT_THEMES.iter().position(|&r| r == self.theme) {
state_theme = theme; state_theme = theme;
@ -223,14 +223,14 @@ impl super::Calcifer {
state_tabs.push(tab.path.clone()); state_tabs.push(tab.path.clone());
} }
let app_state = tools::AppState { let app_state = tools::AppState {
tabs: state_tabs, tabs: state_tabs,
theme: state_theme, theme: state_theme,
}; };
let _ = tools::save_state(&app_state, super::SAVE_PATH); 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 let Some(name) = path.file_name() {
if path.is_dir() { if path.is_dir() {
egui::CollapsingHeader::new(name.to_string_lossy()).show(ui, |ui| { egui::CollapsingHeader::new(name.to_string_lossy()).show(ui, |ui| {
@ -262,7 +262,7 @@ impl super::Calcifer {
let new_tab = tools::Tab { let new_tab = tools::Tab {
path: path.into(), 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(), language: path.to_str().unwrap().split('.').last().unwrap().into(),
saved: true, saved: true,
..tools::Tab::default() ..tools::Tab::default()

View file

@ -9,21 +9,21 @@ pub mod search;
pub trait View { pub trait View {
fn ui(&mut self, ui: &mut egui::Ui, tabs: &mut Vec<Tab>, selected_tab: &mut TabNumber); fn ui(&mut self, ui: &mut egui::Ui, tabs: &mut Vec<Tab>, selected_tab: &mut TabNumber);
} }
/// Something to view /// Something to view
pub trait Demo { pub trait Demo {
/// Is the demo enabled for this integraton? /// Is the demo enabled for this integraton?
fn is_enabled(&self, _ctx: &egui::Context) -> bool { fn is_enabled(&self, _ctx: &egui::Context) -> bool {
true true
} }
/// `&'static` so we can also use it as a key to store open/close state. /// `&'static` so we can also use it as a key to store open/close state.
fn name(&self) -> &str; //'static fn name(&self) -> &str; //'static
/// Show windows, etc /// Show windows, etc
fn show(&mut self, ctx: &egui::Context, open: &mut bool, tabs: &mut Vec<Tab>, selected_tab: &mut TabNumber); fn show(&mut self, ctx: &egui::Context, open: &mut bool, tabs: &mut Vec<Tab>, selected_tab: &mut TabNumber);
} }
@ -43,30 +43,30 @@ pub enum TabNumber {
impl TabNumber { impl TabNumber {
pub fn from_index(n : usize) -> TabNumber { pub fn from_index(n : usize) -> TabNumber {
match n { match n {
0 => TabNumber::Zero, 0 => TabNumber::Zero,
1 => TabNumber::One, 1 => TabNumber::One,
2 => TabNumber::Two, 2 => TabNumber::Two,
3 => TabNumber::Three, 3 => TabNumber::Three,
4 => TabNumber::Four, 4 => TabNumber::Four,
5 => TabNumber::Five, 5 => TabNumber::Five,
6 => TabNumber::Six, 6 => TabNumber::Six,
7 => TabNumber::Seven, 7 => TabNumber::Seven,
_ => TabNumber::None, _ => TabNumber::None,
} }
} }
pub fn to_index(&self) -> usize { pub fn to_index(&self) -> usize {
match self { match self {
TabNumber::Zero => 0, TabNumber::Zero => 0,
TabNumber::One => 1, TabNumber::One => 1,
TabNumber::Two => 2, TabNumber::Two => 2,
TabNumber::Three => 3, TabNumber::Three => 3,
TabNumber::Four => 4, TabNumber::Four => 4,
TabNumber::Five => 5, TabNumber::Five => 5,
TabNumber::Six => 6, TabNumber::Six => 6,
TabNumber::Seven => 7, TabNumber::Seven => 7,
_ => 0, _ => 0,
} }
} }
} }
@ -111,37 +111,37 @@ pub struct CommandEntry {
impl Default for CommandEntry { impl Default for CommandEntry {
fn default() -> Self { fn default() -> Self {
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(), 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(), command : "".into(),
output : "".into(), output : "".into(),
error : "".into(), error : "".into(),
} }
} }
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct AppState { pub struct AppState {
pub tabs: Vec<PathBuf>, pub tabs: Vec<PathBuf>,
pub theme: usize, pub theme: usize,
} }
pub fn save_state(state: &AppState, file_path: &str) -> Result<(), std::io::Error> { 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<AppState, std::io::Error> { pub fn load_state(file_path: &str) -> Result<AppState, std::io::Error> {
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 { pub fn run_command(cmd : String) -> CommandEntry {
let mut entry = CommandEntry::default(); let mut entry = CommandEntry::default();
let output = Command::new("sh") let output = Command::new("sh")
.arg("-c") .arg("-c")
.arg(cmd.clone()) .arg(cmd.clone())
.output() .output()
.expect("failed to execute process"); .expect("failed to execute process");
entry.command = cmd; entry.command = cmd;
entry.output = (&String::from_utf8_lossy(&output.stdout)).to_string(); 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 { pub fn sort_directories_first(a: &std::fs::DirEntry, b: &std::fs::DirEntry) -> Ordering {
let a_is_dir = a.path().is_dir(); let a_is_dir = a.path().is_dir();
let b_is_dir = b.path().is_dir(); let b_is_dir = b.path().is_dir();
// Directories come first, then files // Directories come first, then files
if a_is_dir && !b_is_dir { if a_is_dir && !b_is_dir {
Ordering::Less Ordering::Less
} else if !a_is_dir && b_is_dir { } else if !a_is_dir && b_is_dir {
Ordering::Greater Ordering::Greater
} else { } else {
// Both are either directories or files, sort alphabetically // Both are either directories or files, sort alphabetically
a.path().cmp(&b.path()) a.path().cmp(&b.path())
} }
} }
#[cfg(test)]
mod tests;

85
src/tools/tests.rs Normal file
View file

@ -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
}