diff options
| author | lukeflo | 2025-02-23 12:35:56 +0100 |
|---|---|---|
| committer | lukeflo | 2025-02-23 12:35:56 +0100 |
| commit | d8bb353580f8d26e1e3dc2422e421ce64aa85417 (patch) | |
| tree | ab5f2d888ab0a46d2593bea95ce1225453ae8bf7 | |
| parent | 63d4410fdfe7712faec287aee2f5c0ca288dc996 (diff) | |
| download | bibiman-d8bb353580f8d26e1e3dc2422e421ce64aa85417.tar.gz bibiman-d8bb353580f8d26e1e3dc2422e421ce64aa85417.zip | |
test module for config crate
| -rw-r--r-- | Cargo.lock | 2 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/config.rs | 50 |
3 files changed, 50 insertions, 4 deletions
@@ -470,7 +470,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8cb01cd46b0cf372153850f4c6c272d9cbea2da513e07538405148f95bd789f3" dependencies = [ "atomic", + "parking_lot", "serde", + "tempfile", "toml", "uncased", "version_check", @@ -37,4 +37,4 @@ regex = "1.11.1" ureq = "2.12.1" # config = { version = "0.15.8", default-features = false, features = ["async", "async-trait", "convert-case", "convert_case", "toml"] } serde = { version = "1.0.217", features = ["serde_derive"] } -figment = { version = "0.10.19", features = [ "toml" ]} +figment = { version = "0.10.19", features = [ "toml", "test" ]} diff --git a/src/config.rs b/src/config.rs index e2c34b9..91bd1e8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -28,14 +28,14 @@ use serde::{Deserialize, Serialize}; use crate::cliargs::CLIArgs; /// Main struct of the config file. Contains substructs/headings in toml -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct BibiConfig { pub general: General, pub colors: Colors, } /// Substruct [general] in config.toml -#[derive(Debug, Clone, Deserialize, Serialize)] +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] pub struct General { pub bibfiles: Option<Vec<PathBuf>>, pub editor: Option<String>, @@ -44,7 +44,7 @@ pub struct General { } /// Substruct [colors] in config.toml -#[derive(Debug, Clone, Deserialize, Serialize)] +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] pub struct Colors { pub main_text_color: Color, pub highlight_text_color: Color, @@ -118,3 +118,47 @@ fn select_opener() -> String { _ => panic!("Couldn't detect OS for setting correct opener"), } } + +#[cfg(test)] +mod tests { + use figment::{ + providers::{Format, Toml}, + Figment, + }; + + use super::BibiConfig; + + #[test] + fn parse_default_config() { + figment::Jail::expect_with(|jail| { + jail.create_file( + "bibiman.toml", + r#" + [general] + pdf_opener = "xdg-open" + url_opener = "xdg-open" + + [colors] + main_text_color = "250" + highlight_text_color = "254" + entry_color = "36" + keyword_color = "101" + info_color = "99" + confirm_color = "47" + warn_color = "124" + bar_bg_color = "235" + popup_bg_color = "234" + selected_row_bg_color = "237" + "#, + )?; + + let config: BibiConfig = Figment::new().merge(Toml::file("bibiman.toml")).extract()?; + + let default_config: BibiConfig = BibiConfig::default(); + + assert_eq!(config, default_config); + + Ok(()) + }); + } +} |
