From 155c5a18d84f9c610659cacabf3c059ddb002ac8 Mon Sep 17 00:00:00 2001 From: Max van Ling Date: Mon, 1 Apr 2024 17:29:40 +0200 Subject: [PATCH] watch: Add test caes for interval implementation --- src/uu/watch/src/watch.rs | 57 +++++++++++++++++++++++++++++++++++++ tests/by-util/test_watch.rs | 47 ++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/src/uu/watch/src/watch.rs b/src/uu/watch/src/watch.rs index 99b4389e..0c65dd14 100644 --- a/src/uu/watch/src/watch.rs +++ b/src/uu/watch/src/watch.rs @@ -90,6 +90,61 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { Ok(()) } +#[cfg(test)] +mod parse_interval_tests { + use super::*; + + #[test] + fn test_comma_parse() { + let interval = parse_interval("1,5"); + assert_eq!(Ok(Duration::from_millis(1500)), interval); + } + + #[test] + fn test_different_nanos_length() { + let interval = parse_interval("1.12345"); + assert_eq!(Ok(Duration::new(1, 123450000)), interval); + let interval = parse_interval("1.1234"); + assert_eq!(Ok(Duration::new(1, 123400000)), interval); + } + + #[test] + fn test_period_parse() { + let interval = parse_interval("1.5"); + assert_eq!(Ok(Duration::from_millis(1500)), interval); + } + + #[test] + fn test_empty_seconds_interval() { + let interval = parse_interval(".5"); + assert_eq!(Ok(Duration::from_millis(500)), interval); + } + + #[test] + fn test_seconds_only() { + let interval = parse_interval("7"); + assert_eq!(Ok(Duration::from_secs(7)), interval); + } + + #[test] + fn test_empty_nanoseconds_interval() { + let interval = parse_interval("1."); + assert_eq!(Ok(Duration::from_millis(1000)), interval); + } + + #[test] + fn test_too_many_nanos() { + let interval = parse_interval("1.00000000009"); + assert_eq!(Ok(Duration::from_secs(1)), interval); + } + + #[test] + fn test_invalid_nano() { + let interval = parse_interval("1.00000000000a"); + assert!(interval.is_err()) + } +} + pub fn uu_app() -> Command { Command::new(uucore::util_name()) .version(crate_version!()) @@ -184,3 +239,5 @@ pub fn uu_app() -> Command { .help("Pass command to exec instead of 'sh -c'"), ) } + + diff --git a/tests/by-util/test_watch.rs b/tests/by-util/test_watch.rs index c3afeaf6..27557283 100644 --- a/tests/by-util/test_watch.rs +++ b/tests/by-util/test_watch.rs @@ -10,3 +10,50 @@ use crate::common::util::TestScenario; fn test_invalid_arg() { new_ucmd!().arg("--definitely-invalid").fails().code_is(1); } + +#[test] +fn test_invalid_interval() { + let args = vec!["-n", "definitely-not-valid", "true"]; + new_ucmd!().args(&args).fails(); +} + +#[test] +fn test_no_interval() { + let mut p = new_ucmd!() + .arg("true") + .run_no_wait(); + p.make_assertion_with_delay(500).is_alive(); + p.kill() + .make_assertion() + .with_all_output() + .no_stderr() + .no_stdout(); +} + +#[test] +fn test_valid_interval() { + let args = vec!["-n", "1.5", "true"]; + let mut p = new_ucmd!() + .args(&args) + .run_no_wait(); + p.make_assertion_with_delay(500).is_alive(); + p.kill() + .make_assertion() + .with_all_output() + .no_stderr() + .no_stdout(); +} + +#[test] +fn test_valid_interval_comma() { + let args = vec!["-n", "1,5", "true"]; + let mut p = new_ucmd!() + .args(&args) + .run_no_wait(); + p.make_assertion_with_delay(1000).is_alive(); + p.kill() + .make_assertion() + .with_all_output() + .no_stderr() + .no_stdout(); +}