pub mod searcher;
pub mod options;
#[cfg(test)]
mod tests {
use crate::searcher::Searcher;
use crate::options::Options;
#[test]
fn exclude() {
let options = Options {
exclude: true,
..Options::default()
};
let text = String::from("Steve Jobs Passed Away\nGates thrilled");
let searcher = Searcher::new(String::from("Jobs"), text, options, None);
let assert_text: String = String::from("Gates thrilled");
let mut return_text: String = searcher.search();
if return_text.contains("\n") {
return_text.remove(return_text.find("\n").unwrap());
}
assert_eq!(assert_text, return_text);
}
#[test]
fn include() {
let options = Options::default();
let text = String::from("Steve Jobs Passed Away\nGates thrilled");
let searcher = Searcher::new(String::from("Jobs"), text, options, None);
let assert_text: String = String::from("Steve Jobs Passed Away");
let mut return_text: String = searcher.search();
if return_text.contains("\n") {
return_text.remove(return_text.find("\n").unwrap());
}
assert_eq!(assert_text, return_text);
}
#[test]
fn include_before() {
let options = Options {
include_before: true,
..Options::default()
};
let text = String::from("Steve Jobs Passed Away\nGates thrilled\nApple Fans Devastated\nGates Thrilled and Devastated");
let searcher = Searcher::new(String::from("Gates"), text, options, None);
let assert_text: String = String::from("Steve Jobs Passed Away Gates thrilled Gates Thrilled and Devastated");
let mut return_text: String = searcher.search();
if return_text.contains("\n") {
return_text = return_text.replace("\n", " ");
if return_text.ends_with(" ") {
return_text.remove(return_text.len() - 1);
}
if return_text.starts_with(" ") {
return_text.remove(0);
}
println!("test: {}", return_text);
}
assert_eq!(assert_text, return_text);
}
#[test]
fn include_after() {
let options = Options {
include_after: true,
..Options::default()
};
let text = String::from("Steve Jobs Passed Away\nGates thrilled\nApple Fans Devastated");
let searcher = Searcher::new(String::from("Gates"), text, options, None);
let assert_text: String = String::from("Gates thrilled Apple Fans Devastated");
let mut return_text: String = searcher.search();
if return_text.contains("\n") {
return_text = return_text.replace("\n", " ");
if return_text.ends_with(" ") {
return_text.remove(return_text.len() - 1);
}
if return_text.starts_with(" ") {
return_text.remove(0);
}
println!("test: {}", return_text);
}
assert_eq!(assert_text, return_text);
}
#[test]
fn case_insensitive() {
let options = Options {
case_insensitive: true,
..Options::default()
};
let text = String::from("Steve Jobs Passed Away\nGates thrilled\n Steve jobs hasn't passed away");
let searcher = Searcher::new(String::from("Jobs"), text, options, None);
let assert_text: String = String::from("Steve Jobs Passed Away Steve jobs hasn't passed away").to_lowercase();
let mut return_text: String = searcher.search();
if return_text.contains("\n") {
return_text.remove(return_text.find("\n").unwrap());
}
if return_text.ends_with("\n") {
return_text.remove(return_text.len()-1);
}
assert_eq!(assert_text, return_text);
}
#[test]
fn regex() {
let options = Options {
regex: true,
..Options::default()
};
let text = String::from("Steve Jobs Passed Away on 2020-05-18\nHe passed away at exactly midnight as he didn't like being late");
let searcher = Searcher::new(String::from(r"\d"), text, options, None);
let assert_text = String::from("Steve Jobs Passed Away on 2020-05-18");
let mut return_text: String = searcher.search();
if return_text.contains("\n") {
return_text.remove(return_text.find("\n").unwrap());
}
if return_text.ends_with("\n") {
return_text.remove(return_text.len()-1);
}
assert_eq!(assert_text, return_text);
}
#[test]
fn custom_delim() {
let options = Options::default();
let text = String::from("Steve Jobs Passed Away\nGates thrilled");
let searcher = Searcher::new(String::from("Jobs"), text, options, Some(String::from(" ")));
let assert_text: String = String::from("Jobs");
let mut return_text: String = searcher.search();
if return_text.contains("\n") {
return_text.remove(return_text.find("\n").unwrap());
}
assert_eq!(assert_text, return_text);
}
#[test]
fn show_line() {
let options = Options {
show_line: true,
..Options::default()
};
let text = String::from("Steve Jobs Passed Away\nGates thrilled");
let searcher = Searcher::new(String::from("Jobs"), text, options, None);
let assert_text: String = String::from("found at line: 0\nSteve Jobs Passed Away\n");
let mut return_text: String = searcher.search();
assert_eq!(assert_text, return_text);
}
}