mirror of
https://github.com/arabianq/pipewire-soundpad.git
synced 2026-06-19 12:13:32 +00:00
feat: better testing (#131)
* add tests * update github actions to include testing step * optimization
This commit is contained in:
committed by
GitHub
parent
0476329798
commit
e91465365d
@@ -217,3 +217,70 @@ impl SoundpadGui {
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use egui::{Key, Modifiers};
|
||||
|
||||
#[test]
|
||||
fn test_chord_from_event() {
|
||||
// Valid modifier + key
|
||||
let mut mods = Modifiers::NONE;
|
||||
mods.ctrl = true;
|
||||
let chord = chord_from_event(&mods, &Key::A);
|
||||
assert_eq!(chord, Some("Ctrl+A".to_string()));
|
||||
|
||||
// Multiple modifiers
|
||||
mods.shift = true;
|
||||
let chord = chord_from_event(&mods, &Key::F1);
|
||||
assert_eq!(chord, Some("Ctrl+Shift+F1".to_string()));
|
||||
|
||||
// Missing modifiers (requires at least one modifier)
|
||||
let no_mods = Modifiers::NONE;
|
||||
let chord = chord_from_event(&no_mods, &Key::A);
|
||||
assert_eq!(chord, None);
|
||||
|
||||
// Invalid keys (e.g. Escape or Enter shouldn't be accepted by chord_from_event)
|
||||
mods.shift = false;
|
||||
let chord = chord_from_event(&mods, &Key::Escape);
|
||||
assert_eq!(chord, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_chord() {
|
||||
// Valid Ctrl+A
|
||||
let res = parse_chord("Ctrl+A");
|
||||
assert!(res.is_some());
|
||||
let (mods, key) = res.unwrap();
|
||||
assert!(mods.ctrl);
|
||||
assert!(!mods.alt);
|
||||
assert!(!mods.shift);
|
||||
assert_eq!(key, Key::A);
|
||||
|
||||
// Valid Ctrl+Shift+F12
|
||||
let res = parse_chord("Ctrl+Shift+F12");
|
||||
assert!(res.is_some());
|
||||
let (mods, key) = res.unwrap();
|
||||
assert!(mods.ctrl);
|
||||
assert!(mods.shift);
|
||||
assert!(!mods.alt);
|
||||
assert_eq!(key, Key::F12);
|
||||
|
||||
// Valid Ctrl+Alt+Shift+Super+B
|
||||
let res = parse_chord("Ctrl+Alt+Shift+Super+B");
|
||||
assert!(res.is_some());
|
||||
let (mods, key) = res.unwrap();
|
||||
assert!(mods.ctrl);
|
||||
assert!(mods.alt);
|
||||
assert!(mods.shift);
|
||||
assert!(mods.command); // Super maps to command in egui Modifiers
|
||||
assert_eq!(key, Key::B);
|
||||
|
||||
// Invalid keys/chords
|
||||
assert!(parse_chord("").is_none());
|
||||
assert!(parse_chord("Ctrl+").is_none());
|
||||
assert!(parse_chord("Ctrl+Escape").is_none());
|
||||
assert!(parse_chord("Invalid+A").is_none());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,3 +294,45 @@ pub async fn run() -> Result<()> {
|
||||
Err(e) => Err(anyhow!(e.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_get_filtered_files() {
|
||||
let mut gui = SoundpadGui {
|
||||
app_state: AppState::default(),
|
||||
config: GuiConfig::default(),
|
||||
audio_player_state: AudioPlayerState::default(),
|
||||
audio_player_state_shared: Arc::new(Mutex::new(AudioPlayerState::default())),
|
||||
};
|
||||
|
||||
// Create some dummy paths
|
||||
// We will mock path properties using standard Rust PathBuf
|
||||
let dir_a = PathBuf::from("a_dir");
|
||||
let file_b = PathBuf::from("b_file.mp3");
|
||||
let file_c = PathBuf::from("c_file.wav");
|
||||
let file_txt = PathBuf::from("invalid.txt");
|
||||
|
||||
gui.app_state.listed_files.insert(dir_a.clone());
|
||||
gui.app_state.listed_files.insert(file_b.clone());
|
||||
gui.app_state.listed_files.insert(file_c.clone());
|
||||
gui.app_state.listed_files.insert(file_txt.clone());
|
||||
|
||||
// Note: is_dir() check in get_filtered_files relies on physical filesystem properties.
|
||||
// On the real OS filesystem, these paths don't exist, so they are treated as files.
|
||||
// Unsupported extensions (like .txt) will be filtered out.
|
||||
// So we expect only file_b and file_c, sorted alphabetically.
|
||||
let filtered = gui.get_filtered_files();
|
||||
assert_eq!(filtered.len(), 2);
|
||||
assert_eq!(filtered[0], file_b);
|
||||
assert_eq!(filtered[1], file_c);
|
||||
|
||||
// Test search query
|
||||
gui.app_state.search_query = "c_fi".to_string();
|
||||
let filtered_search = gui.get_filtered_files();
|
||||
assert_eq!(filtered_search.len(), 1);
|
||||
assert_eq!(filtered_search[0], file_c);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,3 +30,26 @@ impl SoundpadGui {
|
||||
self.draw_footer(ui);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_get_volume_icon() {
|
||||
assert_eq!(SoundpadGui::get_volume_icon(0.8), ICON_VOLUME_UP.codepoint);
|
||||
assert_eq!(SoundpadGui::get_volume_icon(0.0), ICON_VOLUME_OFF.codepoint);
|
||||
assert_eq!(
|
||||
SoundpadGui::get_volume_icon(-0.1),
|
||||
ICON_VOLUME_OFF.codepoint
|
||||
);
|
||||
assert_eq!(
|
||||
SoundpadGui::get_volume_icon(0.2),
|
||||
ICON_VOLUME_MUTE.codepoint
|
||||
);
|
||||
assert_eq!(
|
||||
SoundpadGui::get_volume_icon(0.5),
|
||||
ICON_VOLUME_DOWN.codepoint
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user