Integration testing
This commit is contained in:
parent
d718d0c46e
commit
95eb8fcf0a
2 changed files with 44 additions and 8 deletions
40
src/lib.rs
40
src/lib.rs
|
|
@ -69,4 +69,44 @@ pub fn search(config: Config, file_content: String) -> Result<Vec<String>, &'sta
|
|||
Ok(str_result)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
fn run_search(args: Vec<String>) -> Vec<String> {
|
||||
let config = Config::build(args).unwrap();
|
||||
|
||||
let file_content = std::fs::read_to_string(&config.file_path)
|
||||
.expect("Failed to read file");
|
||||
|
||||
let result = search(config, file_content).unwrap();
|
||||
assert!(!result.is_empty(), "Expected matches but found none!");
|
||||
result
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_search() {
|
||||
let args: Vec<String> = vec!["target/debug/m-grep", "Who", "poem.txt"]
|
||||
.iter().map(|&x| x.into()).collect();
|
||||
|
||||
let result = run_search(args);
|
||||
|
||||
for line in result {
|
||||
println!("{line}");
|
||||
assert_eq!(line, format!("I'm nobody! {} are you?", "Who".green()));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_search_ignorecase() {
|
||||
let args: Vec<String> = vec!["target/debug/m-grep", "who", "poem.txt", "-i"]
|
||||
.iter().map(|&x| x.into()).collect();
|
||||
|
||||
let result = run_search(args);
|
||||
|
||||
for line in result {
|
||||
assert_eq!(line, format!("I'm nobody! {} are you?", "Who".green()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
12
src/main.rs
12
src/main.rs
|
|
@ -1,12 +1,11 @@
|
|||
use colored::Colorize;
|
||||
use std::io::Read;
|
||||
use std::process;
|
||||
|
||||
use m_grep::{Config, search};
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
|
||||
|
||||
if let Err(err) = run(args) {
|
||||
eprintln!("{}: {}", "Error".red().bold(), err.to_string().red());
|
||||
process::exit(1);
|
||||
|
|
@ -16,16 +15,13 @@ fn main() {
|
|||
fn run(args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let config = Config::build(args)?;
|
||||
let file_path = &config.file_path;
|
||||
|
||||
|
||||
if !std::path::Path::new(&file_path).exists() {
|
||||
return Err("File not exists!".into());
|
||||
}
|
||||
|
||||
let mut file = std::fs::File::open(file_path).map_err(|_| "Failed to open file")?;
|
||||
let mut file_content = String::new();
|
||||
|
||||
file.read_to_string(&mut file_content)
|
||||
.map_err(|_| "Failed to read file")?;
|
||||
let file_content = std::fs::read_to_string(file_path)
|
||||
.expect("Failed to read file");
|
||||
|
||||
let result = search(config, file_content)?;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue