feat: add tests for conf

This commit is contained in:
alban 2023-06-03 23:27:12 +02:00
parent ae14cb600d
commit 312a48ff5b
4 changed files with 56 additions and 11 deletions

View File

@ -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<u8>,
dac_url: Option<String>,
pub laser_id: u8,
pub debug: bool,
pub redis_url: String,
pub dac_family: DacFamily,
pub dac_id: Option<u8>,
pub dac_url: Option<String>,
}
pub fn load_config(path: &str) -> Result<Conf, Box<dyn std::error::Error>> {
let settings = Config::builder()
.add_source(config::File::with_name(path))
@ -24,5 +23,6 @@ pub fn load_config(path: &str) -> Result<Conf, Box<dyn std::error::Error>> {
let conf = settings.try_deserialize::<Conf>()?;
Ok(conf)
}

View File

19
tests/settings/valid.toml Normal file
View File

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

View File

@ -1,6 +1,32 @@
use lj_rust::conf;
use lj_rust::conf::{load_config, DacFamily};
#[test]
fn it_adds_two() {
assert_eq!(4, 4);
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")));
}