From 312a48ff5b9346872eddace40da72121d507cab2 Mon Sep 17 00:00:00 2001 From: alban Date: Sat, 3 Jun 2023 23:27:12 +0200 Subject: [PATCH] feat: add tests for conf --- src/conf.rs | 14 +++++++------- tests/settings/empty.toml | 0 tests/settings/valid.toml | 19 +++++++++++++++++++ tests/test_conf.rs | 34 ++++++++++++++++++++++++++++++---- 4 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 tests/settings/empty.toml create mode 100644 tests/settings/valid.toml diff --git a/src/conf.rs b/src/conf.rs index 051cd58..02ea741 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -9,14 +9,13 @@ pub enum DacFamily { #[derive(Deserialize, Debug)] pub struct Conf { - laser_id: u8, - debug: bool, - redis_url: String, - dac_family: DacFamily, - dac_id: Option, - dac_url: Option, + pub laser_id: u8, + pub debug: bool, + pub redis_url: String, + pub dac_family: DacFamily, + pub dac_id: Option, + pub dac_url: Option, } - pub fn load_config(path: &str) -> Result> { let settings = Config::builder() .add_source(config::File::with_name(path)) @@ -24,5 +23,6 @@ pub fn load_config(path: &str) -> Result> { let conf = settings.try_deserialize::()?; + Ok(conf) } diff --git a/tests/settings/empty.toml b/tests/settings/empty.toml new file mode 100644 index 0000000..e69de29 diff --git a/tests/settings/valid.toml b/tests/settings/valid.toml new file mode 100644 index 0000000..1d9689c --- /dev/null +++ b/tests/settings/valid.toml @@ -0,0 +1,19 @@ +# file: valid.toml + +# The main key of your laser in LJ +laser_id = 0 + +# Activate for more debug +debug = "true" + +# Redis URL as IP:port +redis_url = "127.0.0.1" + +# Either Helios or Etherdream +dac_family = "Helios" + +# For Helios. USB Device Id of the DAC +dac_id = 0 + +# For Etherdream. IP of the DAC +dac_url = "192.168.1.68" diff --git a/tests/test_conf.rs b/tests/test_conf.rs index 1305e13..a366a11 100644 --- a/tests/test_conf.rs +++ b/tests/test_conf.rs @@ -1,6 +1,32 @@ -use lj_rust::conf; +use lj_rust::conf::{load_config, DacFamily}; #[test] -fn it_adds_two() { - assert_eq!(4, 4); -} \ No newline at end of file +fn it_loads_a_valid_conf() { + let result = load_config("tests/settings/valid"); + assert!(result.is_ok()); +} + +#[test] +fn it_fails_invalid_conf() { + let result = load_config("tests/settings/empty"); + assert!(result.is_err()); +} + +#[test] +fn it_finds_struct_fields() { + let config = match load_config("tests/settings/valid") { + Ok(c) => c, + Err(err) => { + panic!("Unable to load config file: {:?}", err) + } + }; + assert_eq!(config.laser_id, u8::from(0)); + assert_eq!(config.debug, true); + assert_eq!(config.redis_url, String::from("127.0.0.1")); + assert!(match config.dac_family { + DacFamily::Helios => true, + _ => false, + }); + assert_eq!(config.dac_id, Some(0)); + assert_eq!(config.dac_url, Some(String::from("192.168.1.68"))); +}