Skip to content

Commit

Permalink
watch: Add test caes for interval implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
maxer137 committed Apr 1, 2024
1 parent 13e7508 commit 155c5a1
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/uu/watch/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!())
Expand Down Expand Up @@ -184,3 +239,5 @@ pub fn uu_app() -> Command {
.help("Pass command to exec instead of 'sh -c'"),
)
}


47 changes: 47 additions & 0 deletions tests/by-util/test_watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 155c5a1

Please sign in to comment.