Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ serde_json = { version = "1.0.149", default-features = false, features = [
] }
clap = { version = "4.5", features = ["derive"] }

[dev-dependencies]
tempfile = "3.15"

[profile.release]
opt-level = 3
lto = true
Expand Down
116 changes: 116 additions & 0 deletions src/tests/config_roundtrip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#[cfg(test)]
mod config_roundtrip_tests {
use crate::config::user::UserConfig;
use crate::hyprland::SearchOptions;
use crate::ui::types::{ColumnVisibility, Theme};
use std::fs;
use tempfile::TempDir;

fn setup_temp_config(temp_dir: &TempDir) -> std::path::PathBuf {
let config_path = temp_dir.path().join("hyprbind_test.json");
config_path
}

/// Ensures default config can be serialized and deserialized without data loss
#[test]
fn test_default_roundtrip() {
let temp_dir = TempDir::new().unwrap();
let config_path = setup_temp_config(&temp_dir);

let original = UserConfig::default();
let json = serde_json::to_string_pretty(&original).unwrap();
fs::write(&config_path, json).unwrap();

let loaded_json = fs::read_to_string(&config_path).unwrap();
let loaded: UserConfig = serde_json::from_str(&loaded_json).unwrap();

assert_eq!(
serde_json::to_string(&original).unwrap(),
serde_json::to_string(&loaded).unwrap()
);
}

/// Validates custom config values survive serialization roundtrip
#[test]
fn test_custom_config_roundtrip() {
let temp_dir = TempDir::new().unwrap();
let config_path = setup_temp_config(&temp_dir);

let original = UserConfig {
theme: Theme::Light,
column_visibility: ColumnVisibility {
keybind: false,
command: true,
description: false,
},
search_options: SearchOptions {
keybind: true,
command: true,
description: false,
},
zen_mode: true,
};

let json = serde_json::to_string_pretty(&original).unwrap();
fs::write(&config_path, json).unwrap();

let loaded_json = fs::read_to_string(&config_path).unwrap();
let loaded: UserConfig = serde_json::from_str(&loaded_json).unwrap();

assert_eq!(
serde_json::to_string(&original).unwrap(),
serde_json::to_string(&loaded).unwrap()
);
}

/// Checks that serialized JSON contains all expected fields
#[test]
fn test_serialization_format_stability() {
let config = UserConfig::default();
let json = serde_json::to_string_pretty(&config).unwrap();

assert!(json.contains("\"theme\""));
assert!(json.contains("\"column_visibility\""));
assert!(json.contains("\"search_options\""));
assert!(json.contains("\"zen_mode\""));
}

/// Verifies default UserConfig values match specification
#[test]
fn test_default_values() {
let config = UserConfig::default();
assert!(matches!(config.theme, Theme::Dark));
assert!(!config.zen_mode);
assert!(config.column_visibility.keybind);
assert!(config.column_visibility.description);
assert!(!config.column_visibility.command);
}

/// Ensures partial JSON config can be successfully deserialized
#[test]
fn test_partial_deserialization() {
let temp_dir = TempDir::new().unwrap();
let config_path = setup_temp_config(&temp_dir);

let partial_json = r#"{
"theme": "Light",
"column_visibility": {
"keybind": true,
"command": true,
"description": true
},
"search_options": {
"keybind": true,
"command": true,
"description": true
},
"zen_mode": false
}"#;

fs::write(&config_path, partial_json).unwrap();
let loaded_json = fs::read_to_string(&config_path).unwrap();
let loaded: Result<UserConfig, _> = serde_json::from_str(&loaded_json);

assert!(loaded.is_ok());
}
}
1 change: 1 addition & 0 deletions src/tests/icons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod icons_tests {
use crate::ui::styling::icons::get_icon;

/// Tests icon mapping for various keys and modifiers
#[test]
fn test_get_icon() {
let cases: [(&str, &str); 28] = [
Expand Down
3 changes: 3 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
mod config_roundtrip;
mod icons;
mod models;
mod parser;
mod parser_edge;
mod source_error;
mod table;
1 change: 1 addition & 0 deletions src/tests/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod models_tests {
use crate::hyprland::{KeyBindEntry, KeyBindings};

/// Validates dmenu format export with icon mapping
#[test]
fn test_to_dmenu() {
// 1. No modifier, with description
Expand Down
3 changes: 3 additions & 0 deletions src/tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod parser_tests {
use crate::hyprland::parser::parse_binds_output;

/// Tests modmask bitmask to human-readable string conversion
#[test]
fn test_modmask_conversion() {
let sample = r"bind
Expand All @@ -19,6 +20,7 @@ mod parser_tests {
assert_eq!(kb.entries[0].modifiers, "SUPER");
}

/// Validates parsing of binds with multiple modifier keys
#[test]
fn test_multiple_modifiers() {
let sample = r"bind
Expand All @@ -37,6 +39,7 @@ mod parser_tests {
assert_eq!(kb.entries[0].description, "Kill window");
}

/// Tests complete parsing of a single bind block with all fields
#[test]
fn test_parse_bind_block() {
let block = r"bind
Expand Down
Loading