diff --git a/src/stat/test_stat.rs b/src/stat/test_stat.rs new file mode 100644 index 000000000..816ad680a --- /dev/null +++ b/src/stat/test_stat.rs @@ -0,0 +1,70 @@ +pub use super::*; + +#[test] +fn test_scanutil() { + assert_eq!(Some((-5, 2)), "-5zxc".scan_num::()); + assert_eq!(Some((51, 2)), "51zxc".scan_num::()); + assert_eq!(Some((192, 4)), "+192zxc".scan_num::()); + assert_eq!(None, "z192zxc".scan_num::()); + + assert_eq!(Some(('a', 3)), "141zxc".scan_char(8)); + assert_eq!(Some(('\n', 2)), "12qzxc".scan_char(8)); + assert_eq!(Some(('\r', 1)), "dqzxc".scan_char(16)); + assert_eq!(None, "z2qzxc".scan_char(8)); +} + +#[cfg(test)] +mod test_generate_tokens { + use super::*; + + #[test] + fn test_normal_format() { + let s = "%10.2ac%-5.w\n"; + let expected = vec![Token::Directive { + flag: 0, + width: 10, + precision: 2, + format: 'a', + }, + Token::Char('c'), + Token::Directive { + flag: F_LEFT, + width: 5, + precision: 0, + format: 'w', + }, + Token::Char('\n')]; + assert_eq!(&expected, &Stater::generate_tokens(s, false).unwrap()); + } + + #[test] + fn test_printf_format() { + let s = "%-# 15a\\r\\\"\\\\\\a\\b\\e\\f\\v%+020.-23w\\x12\\167\\132\\112\\n"; + let expected = vec![Token::Directive { + flag: F_LEFT | F_ALTER | F_SPACE, + width: 15, + precision: -1, + format: 'a', + }, + Token::Char('\r'), + Token::Char('"'), + Token::Char('\\'), + Token::Char('\x07'), + Token::Char('\x08'), + Token::Char('\x1B'), + Token::Char('\x0C'), + Token::Char('\x0B'), + Token::Directive { + flag: F_SIGN | F_ZERO, + width: 20, + precision: -1, + format: 'w', + }, + Token::Char('\x12'), + Token::Char('w'), + Token::Char('Z'), + Token::Char('J'), + Token::Char('\n')]; + assert_eq!(&expected, &Stater::generate_tokens(s, true).unwrap()); + } +} diff --git a/tests/test_stat.rs b/tests/test_stat.rs new file mode 100644 index 000000000..baf924528 --- /dev/null +++ b/tests/test_stat.rs @@ -0,0 +1,98 @@ +use common::util::*; + +static UTIL_NAME: &'static str = "stat"; + +use std::process::Command; + +#[test] +fn test_invalid_option() { + let (_, mut ucmd) = testing(UTIL_NAME); + ucmd.arg("-w").arg("-q").arg("/"); + ucmd.fails(); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_terse_fs_format() { + let (_, mut ucmd) = testing(UTIL_NAME); + let args = ["-f", "-t", "/proc"]; + ucmd.args(&args); + assert_eq!(ucmd.run().stdout, expected_result(&args)); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_fs_format() { + let (_, mut ucmd) = testing(UTIL_NAME); + let args = ["-f", "--format=%n %i 0x%t %T", "/dev/shm"]; + ucmd.args(&args); + assert_eq!(ucmd.run().stdout, "/dev/shm 0 0x1021994 tmpfs\n"); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_terse_normal_format() { + let (_, mut ucmd) = testing(UTIL_NAME); + let args = ["-t", "/"]; + ucmd.args(&args); + assert_eq!(ucmd.run().stdout, expected_result(&args)); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_normal_format() { + let (_, mut ucmd) = testing(UTIL_NAME); + let args = ["/boot"]; + ucmd.args(&args); + assert_eq!(ucmd.run().stdout, expected_result(&args)); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_follow_symlink() { + let (_, mut ucmd) = testing(UTIL_NAME); + let args = ["-L", "/dev/cdrom"]; + ucmd.args(&args); + assert_eq!(ucmd.run().stdout, expected_result(&args)); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_symlink() { + let (_, mut ucmd) = testing(UTIL_NAME); + let args = ["/dev/cdrom"]; + ucmd.args(&args); + assert_eq!(ucmd.run().stdout, expected_result(&args)); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_char() { + let (_, mut ucmd) = testing(UTIL_NAME); + let args = ["/dev/zero"]; + ucmd.args(&args); + assert_eq!(ucmd.run().stdout, expected_result(&args)); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_multi_files() { + let (_, mut ucmd) = testing(UTIL_NAME); + let args = ["/dev", "/usr/lib", "/etc/fstab", "/var"]; + ucmd.args(&args); + assert_eq!(ucmd.run().stdout, expected_result(&args)); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_printf() { + let (_, mut ucmd) = testing(UTIL_NAME); + let args = ["--printf=123%-# 15q\\r\\\"\\\\\\a\\b\\e\\f\\v%+020.23m\\x12\\167\\132\\112\\n", "/"]; + ucmd.args(&args); + assert_eq!(ucmd.run().stdout, "123?\r\"\\\x07\x08\x1B\x0C\x0B /\x12wZJ\n"); +} + +fn expected_result(args: &[&str]) -> String { + let output = Command::new(UTIL_NAME).args(args).output().unwrap(); + String::from_utf8_lossy(&output.stdout).into_owned() +}