From 3402ea2a8c91e187afdb5e8ca085d779f5877674 Mon Sep 17 00:00:00 2001 From: 53hornet <53hornet@gmail.com> Date: Mon, 25 Feb 2019 21:57:25 -0500 Subject: Added first test. --- minigrep/src/lib.rs | 31 ++++++++++++++++++++++++++++++- minigrep/src/main.rs | 2 -- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/minigrep/src/lib.rs b/minigrep/src/lib.rs index 78610ba..b55e924 100644 --- a/minigrep/src/lib.rs +++ b/minigrep/src/lib.rs @@ -23,7 +23,36 @@ impl Config { pub fn run(config: Config) -> Result<(), Box> { let contents = fs::read_to_string(config.filename)?; - dbg!(contents); Ok(()) } +fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + + for line in contents.lines() { + if line.contains(query) { + results.push(line); + } + } + + results +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn one_result() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three."; + + assert_eq!( + vec!["safe, fast, productive."], + search(query, contents) + ); + } +} diff --git a/minigrep/src/main.rs b/minigrep/src/main.rs index fc5ae9a..b1581c9 100644 --- a/minigrep/src/main.rs +++ b/minigrep/src/main.rs @@ -10,8 +10,6 @@ fn main() { println!("Problem parsing arguments: {}", err); process::exit(1); }); - println!("\nSearching for {}", config.query); - println!(" in file {}", config.filename); if let Err(e) = minigrep::run(config) { println!("Application error: {}", e); process::exit(1); -- cgit v1.2.3