From 4b01c8e699d044d572da4bc24c70beb1aed787ac Mon Sep 17 00:00:00 2001 From: azzamsa Date: Tue, 18 Jun 2024 09:18:12 +0700 Subject: [PATCH] test: add time format tests --- tests/integration.rs | 68 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 8 deletions(-) diff --git a/tests/integration.rs b/tests/integration.rs index ef395c8..6f854f5 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1,7 +1,8 @@ use std::{env, error::Error, process::Command}; use assert_cmd::{crate_name, prelude::*}; -use assert_fs::{prelude::*, TempDir}; +use assert_fs::fixture::FileWriteStr; +use assert_fs::{fixture::ChildPath, prelude::*, TempDir}; use predicates::prelude::*; #[test] @@ -16,7 +17,8 @@ fn help() -> Result<(), Box> { #[test] fn all() -> Result<(), Box> { - let temp_dir = setup_config()?; + let (temp_dir, config) = setup_config()?; + config.write_str(&config_base())?; env::set_var( "BILAL_CONFIG", format!("{}/config.toml", temp_dir.path().display()), @@ -27,12 +29,17 @@ fn all() -> Result<(), Box> { cmd.assert() .success() .stdout(predicate::str::contains("Fajr")); + // Make sure it is not 12H format + cmd.assert() + .success() + .stdout(predicate::str::contains("AM").not()); Ok(()) } #[test] fn current() -> Result<(), Box> { - let temp_dir = setup_config()?; + let (temp_dir, config) = setup_config()?; + config.write_str(&config_base())?; env::set_var( "BILAL_CONFIG", format!("{}/config.toml", temp_dir.path().display()), @@ -49,7 +56,8 @@ fn current() -> Result<(), Box> { #[test] fn next() -> Result<(), Box> { - let temp_dir = setup_config()?; + let (temp_dir, config) = setup_config()?; + config.write_str(&config_base())?; env::set_var( "BILAL_CONFIG", format!("{}/config.toml", temp_dir.path().display()), @@ -64,11 +72,44 @@ fn next() -> Result<(), Box> { Ok(()) } -fn setup_config() -> Result> { - let temp_dir = assert_fs::TempDir::new()?; +#[test] +fn all_12h_format() -> Result<(), Box> { + let (temp_dir, config) = setup_config()?; + config.write_str(&config_12h())?; + env::set_var( + "BILAL_CONFIG", + format!("{}/config.toml", temp_dir.path().display()), + ); + + let mut cmd = Command::cargo_bin(crate_name!())?; + cmd.arg("all"); + cmd.assert() + .success() + .stdout(predicate::str::contains("AM")); + Ok(()) +} + +#[test] +fn next_12h_format() -> Result<(), Box> { + let (temp_dir, config) = setup_config()?; + config.write_str(&config_12h())?; + env::set_var( + "BILAL_CONFIG", + format!("{}/config.toml", temp_dir.path().display()), + ); + + let mut cmd = Command::cargo_bin(crate_name!())?; + cmd.arg("next"); + cmd.assert() + .success() + .stdout(predicate::str::contains("AM").or(predicate::str::contains("PM"))); + Ok(()) +} + +fn setup_config() -> Result<(TempDir, ChildPath), Box> { + let temp_dir = TempDir::new()?; let config = temp_dir.child("config.toml"); - config.write_str(&config_base())?; - Ok(temp_dir) + Ok((temp_dir, config)) } fn config_base() -> String { @@ -80,3 +121,14 @@ method = "Egyptian" "#; content.to_string() } + +fn config_12h() -> String { + let content = r#" +latitude = -6.18233995 +longitude = 106.84287154 +madhab = "Shafi" +method = "Egyptian" +time_format = "12H" +"#; + content.to_string() +}