Skip to content

Commit

Permalink
Added several unit tests and some refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
RRArny committed Nov 24, 2024
1 parent b603071 commit 3e6bca2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 32 deletions.
48 changes: 16 additions & 32 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,23 @@ fn read_config_file(config_filepath: Option<String>) -> Config {
let contents = contents.parse::<Table>().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));
}
}
}

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;
}
}
Expand All @@ -135,49 +131,37 @@ fn read_config_file(config_filepath: Option<String>) -> 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)
{
Expand Down
26 changes: 26 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
16 changes: 16 additions & 0 deletions src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 3e6bca2

Please sign in to comment.