use assert_cmd::prelude::*; // Add methods on commands use predicates::prelude::*; // Used for writing assertions use std::process::Command; // Run programs use assert_fs::prelude::*; use rust_demo_rgrep::{answer, find_matches}; #[test] fn check_answer_validity() { assert_eq!(answer(), 42); } #[test] fn find_a_match() { let mut result = Vec::new(); find_matches("lorem ipsum\ndolor sit amet", "lorem", &mut result); assert_eq!(result, b"lorem ipsum\n"); } #[test] fn file_doesnt_exist() -> Result<(), Box> { let mut cmd = Command::cargo_bin("rust-demo-rgrep")?; cmd.arg("main").arg("src/main.rs"); cmd.assert() .failure() .stderr(predicate::str::contains("could not read file")); Ok(()) } #[test] fn find_content_in_file() -> Result<(), Box> { let file = assert_fs::NamedTempFile::new("sample.txt")?; file.write_str("A test\nActual content\nMore content\nAnother test")?; let mut cmd = Command::cargo_bin("rust-demo-rgrep")?; cmd.arg("test").arg(file.path()); cmd.assert() .success() .stdout(predicate::str::contains("A test\nAnother test")); Ok(()) }