Feat: Add test case for find_matches
Slight find_matches rewrite to use Write trait.
This commit is contained in:
+13
-5
@@ -19,21 +19,29 @@ fn main() -> Result<()> {
|
||||
let content = std::fs::read_to_string(&args.path)
|
||||
.with_context(|| format!("could not read file `{}`.", args.path.display()))?;
|
||||
|
||||
|
||||
find_matches(content.as_str(), args.pattern.as_str());
|
||||
find_matches(
|
||||
content.as_str(),
|
||||
args.pattern.as_str(),
|
||||
&mut std::io::stdout(),
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn find_matches(content: &str, pattern: &str) {
|
||||
fn find_matches(content: &str, pattern: &str, mut writer: impl std::io::Write) {
|
||||
for line in content.lines() {
|
||||
if line.contains(pattern) {
|
||||
println!("{}", line)
|
||||
writeln!(writer, "{}", line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn find_a_match() {
|
||||
let mut result = Vec::new();
|
||||
find_matches("lorem ipsum\ndolar sit amet", "lorem", &mut result);
|
||||
assert_eq!(result, b"lorem ipsum\n")
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn answer() -> i32 {
|
||||
|
||||
Reference in New Issue
Block a user