diff --git a/src/lib.rs b/src/lib.rs index c9f12be..20ca087 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,7 @@ pub struct Cli { ignore_extras: bool, } -pub fn skip(cli: &Cli, mut out: F) -> io::Result<()> +pub fn skip(cli: &Cli, out: F) -> io::Result<()> where F: FnMut(String) -> (), { @@ -49,7 +49,6 @@ fn skip_file_lines(cli: &Cli, mut out: F, file: &PathBuf) -> io::Result<()> where F: FnMut(String) -> (), { - eprintln!("skip_file_lines"); let content = fs::read_to_string(file).expect("Could not read file"); let mut counter = 0usize; for current_line in content.lines() { @@ -65,7 +64,6 @@ fn skip_file_lines_matching(cli: &Cli, mut out: F, file: &PathBuf, line: &str where F: FnMut(String) -> (), { - eprintln!("skip_file_lines_matching"); let content = fs::read_to_string(file).expect("Could not read file"); let mut counter = 0usize; for current_line in content.lines() { @@ -83,7 +81,6 @@ fn skip_file_tokens(cli: &Cli, mut out: F, file: &PathBuf, token: &str) -> io where F: FnMut(String) -> (), { - eprintln!("skip_file_lines_tokens"); let content = fs::read_to_string(file).expect("Could not read file"); let mut counter = 0usize; for current_line in content.lines() { @@ -91,7 +88,12 @@ where out(String::from(current_line)); } if current_line.contains(&token) { - counter += 1; + if cli.ignore_extras { + counter += 1; + } else { + let occurances = current_line.matches(&token).count(); + counter += occurances; + } } } Ok(()) @@ -162,4 +164,94 @@ mod tests { Ok(()) } + #[test] + fn skip_three_matching_tokens() -> io::Result<()> { + //given + let cli = Cli { + lines: 3, + file: Some(PathBuf::from("tests/poem.txt")), + line: None, + token: Some(String::from("one")), + ignore_extras: false, + }; + let mut lines: Vec = Vec::new(); + + //when + skip(&cli, |line| lines.push(line))?; + + //then + assert_eq!( + lines, + vec![ + "Or help one fainting robin", + "Unto his nest again,", + "I shall not live in vain." + ] + ); + Ok(()) + } + + #[test] + fn skip_three_matching_tokens_include_extras() -> io::Result<()> { + //given + let cli = Cli { + lines: 4, + file: Some(PathBuf::from("tests/lorem.txt")), + line: None, + token: Some(String::from("or")), + ignore_extras: false, + }; + let mut lines: Vec = Vec::new(); + + //when + skip(&cli, |line| lines.push(line))?; + + //then + assert_eq!( + lines, + vec![ + //Lorem ipsum dolor sit amet, -- +2 = 2 + //consectetur adipiscing elit, + //sed do eiusmod tempor incididunt -- +1 = 3 + //ut labore et dolore magna aliqua. -- +2 = 5 + "Ut enim ad minim veniam,", + "quis nostrud exercitation ullamco", + "laboris nisi ut aliquip ex ea", + "commodo consequat." + ] + ); + Ok(()) + } + + #[test] + fn skip_three_matching_tokens_ignore_extras() -> io::Result<()> { + //given + let cli = Cli { + lines: 4, + file: Some(PathBuf::from("tests/lorem.txt")), + line: None, + token: Some(String::from("or")), + ignore_extras: true, + }; + let mut lines: Vec = Vec::new(); + + //when + skip(&cli, |line| lines.push(line))?; + + //then + assert_eq!( + lines, + vec![ + //Lorem ipsum dolor sit amet, -- 1 + //consectetur adipiscing elit, + //sed do eiusmod tempor incididunt -- 2 + //ut labore et dolore magna aliqua. -- 3 + //Ut enim ad minim veniam, + //quis nostrud exercitation ullamco + //laboris nisi ut aliquip ex ea -- 4 + "commodo consequat." + ] + ); + Ok(()) + } } diff --git a/tests/lorem.txt b/tests/lorem.txt new file mode 100644 index 0000000..61f2dc3 --- /dev/null +++ b/tests/lorem.txt @@ -0,0 +1,8 @@ +Lorem ipsum dolor sit amet, +consectetur adipiscing elit, +sed do eiusmod tempor incididunt +ut labore et dolore magna aliqua. +Ut enim ad minim veniam, +quis nostrud exercitation ullamco +laboris nisi ut aliquip ex ea +commodo consequat. diff --git a/tests/poem.txt b/tests/poem.txt new file mode 100644 index 0000000..5203679 --- /dev/null +++ b/tests/poem.txt @@ -0,0 +1,7 @@ +If I can stop one heart from breaking, +I shall not live in vain; +If I can ease one life the aching, +Or cool one pain, +Or help one fainting robin +Unto his nest again, +I shall not live in vain.