From 3e6bca2c3a43cde6a638e65e747ee9be29610af4 Mon Sep 17 00:00:00 2001 From: Robin Arnold <1297660+RRArny@users.noreply.github.com> Date: Sun, 24 Nov 2024 17:27:14 +0100 Subject: [PATCH] Added several unit tests and some refactoring. --- src/config.rs | 48 ++++++++++++++++-------------------------------- src/main.rs | 26 ++++++++++++++++++++++++++ src/position.rs | 16 ++++++++++++++++ 3 files changed, 58 insertions(+), 32 deletions(-) diff --git a/src/config.rs b/src/config.rs index a133ea1..72eb159 100644 --- a/src/config.rs +++ b/src/config.rs @@ -94,10 +94,11 @@ fn read_config_file(config_filepath: Option) -> Config { let contents = contents.parse::().expect(msg); if contents.contains_key("position") { - if let Some(airfield) = contents["position"].get("airfield").and_then(Value::as_str) { + let position = &contents["position"]; + if let Some(airfield) = position.get("airfield").and_then(Value::as_str) { config.position = Position::Airfield(airfield.to_string()); } - if let Some(lat) = contents["position"].get("lat").and_then(Value::as_float) { + if let Some(lat) = position.get("lat").and_then(Value::as_float) { if let Some(lon) = contents["position"].get("lon").and_then(Value::as_float) { config.position = Position::LatLong(LatLong(lat, lon)); } @@ -105,16 +106,11 @@ fn read_config_file(config_filepath: Option) -> Config { } if contents.contains_key("clouds") { - if let Some(minimum) = contents["clouds"] - .get("cloud_minimum") - .and_then(Value::as_integer) - { + let clouds = &contents["clouds"]; + if let Some(minimum) = clouds.get("cloud_minimum").and_then(Value::as_integer) { config.cloud_minimum = minimum; } - if let Some(marginal) = contents["clouds"] - .get("cloud_marginal") - .and_then(Value::as_integer) - { + if let Some(marginal) = clouds.get("cloud_marginal").and_then(Value::as_integer) { config.cloud_marginal = marginal; } } @@ -135,49 +131,37 @@ fn read_config_file(config_filepath: Option) -> Config { } if contents.contains_key("wind") { - if let Some(var_maximum) = contents["wind"] - .get("wind_var_maximum") - .and_then(Value::as_integer) - { + let wind = &contents["wind"]; + if let Some(var_maximum) = wind.get("wind_var_maximum").and_then(Value::as_integer) { config.wind_var_maximum = var_maximum; } - if let Some(maximum) = contents["wind"] - .get("wind_maximum") - .and_then(Value::as_integer) - { + if let Some(maximum) = wind.get("wind_maximum").and_then(Value::as_integer) { config.wind_maximum = maximum; } - if let Some(gust_maximum) = contents["wind"] - .get("gust_maximum") - .and_then(Value::as_integer) - { + if let Some(gust_maximum) = wind.get("gust_maximum").and_then(Value::as_integer) { config.gust_maximum = gust_maximum; } } if contents.contains_key("age") { - if let Some(maximum) = contents["age"] - .get("age_maximum") - .and_then(Value::as_integer) - { + let age = &contents["age"]; + if let Some(maximum) = age.get("age_maximum").and_then(Value::as_integer) { config.age_maximum = TimeDelta::seconds(maximum); } - if let Some(marginal) = contents["age"] - .get("age_marginal") - .and_then(Value::as_integer) - { + if let Some(marginal) = age.get("age_marginal").and_then(Value::as_integer) { config.age_marginal = TimeDelta::seconds(marginal); } } if contents.contains_key("visibility") { - if let Some(minimum) = contents["visibility"] + let visibility = &contents["visibility"]; + if let Some(minimum) = visibility .get("visibility_minimum") .and_then(Value::as_integer) { config.visibility_minimum = minimum; } - if let Some(marginal) = contents["visibility"] + if let Some(marginal) = visibility .get("visibility_marginal") .and_then(Value::as_integer) { diff --git a/src/main.rs b/src/main.rs index aba4026..f7ee3a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,3 +84,29 @@ async fn main() { println!("{wx_string}"); } + +#[cfg(test)] +mod test { + use std::fs; + + use super::*; + + #[tokio::test] + async fn test_get_weather_from_file() { + for entry in fs::read_dir("tests/testdata").unwrap() { + let path = entry.unwrap().path(); + let result = get_weather_from_file(path.into_os_string().into_string().unwrap()); + assert!(result.is_object()); + } + } + + #[tokio::test] + async fn test_get_weather_from_file_metar() { + for entry in fs::read_dir("tests/testdata").unwrap() { + let path = entry.unwrap().path(); + let json = get_weather_from_file(path.into_os_string().into_string().unwrap()); + let metar = Metar::from_json(&json, &Config::default()); + assert!(metar.is_some()); + } + } +} diff --git a/src/position.rs b/src/position.rs index 81224b9..1db4715 100644 --- a/src/position.rs +++ b/src/position.rs @@ -76,4 +76,20 @@ mod test { let actual = latlon.to_string(); assert_eq!(expected, actual); } + + #[tokio::test] + async fn test_get_location_str_latlong() { + let latlon = Position::LatLong(LatLong(51.4, 8.5)); + let expected = "51.4,8.5"; + let actual = latlon.get_location_str().await; + assert_eq!(expected, actual); + } + + #[tokio::test] + async fn test_get_location_str_airfield() { + let airport = Position::Airfield("EDRK".to_string()); + let expected = "EDRK"; + let actual = airport.get_location_str().await; + assert_eq!(expected, actual); + } }