Skip to content

Commit

Permalink
Wrote basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthvk committed Jan 20, 2024
1 parent 4b16a62 commit 62c0268
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ pub struct Cli {

/// Filters files which start with a given pattern. To specify multiple filters -s "foo" -s "bar"
#[arg(short='s', long="starts-with", allow_hyphen_values=true, number_of_values=1, value_name="STRING")]
pub starts_with_list: Vec<String>
pub starts_with_list: Vec<String>,

/// Filters entries with the given regex
#[arg(short='f', long="filter", allow_hyphen_values=true, value_name="FILTER REGEX")]
pub filter_regex: Option<String>
}
24 changes: 24 additions & 0 deletions src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,27 @@ pub fn generate_startswith_filter(starts_with_list: &Vec<&str>) -> Result<Regex,
let regular_expression = format!("^({})", opts.join("|"));
Regex::new(&regular_expression)
}

#[cfg(test)]
mod tests {
use super::generate_startswith_filter;

#[test]
fn test_single_element(){
let re = generate_startswith_filter(&vec!["abc"]).unwrap();
println!("{}",re.as_str());
assert!(re.as_str() == "^(abc)");
}
#[test]
fn test_multiple_elements(){
let re = generate_startswith_filter(&vec!["abc", "de8", "fgh", "123"]).unwrap();
println!("{}",re.as_str());
assert!(re.as_str() == "^(abc|de8|fgh|123)");
}
#[test]
fn test_escaping(){
let re = generate_startswith_filter(&vec!["^^^", ".-+"]).unwrap();
println!("{}",re.as_str());
assert!(re.as_str() == "^(\\^\\^\\^|\\.\\-\\+)");
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ mod operations;
use clap::Parser;
fn main() {
let cli = cli::Cli::parse();
println!("{:#?}", cli);
// println!("{:#?}", cli);
rename::rename(cli);
}
16 changes: 14 additions & 2 deletions src/operations.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

use std::io::{self, Write};

use regex::Regex;
use colored::*;

use regex::Regex;

pub struct RegexReplacement<'a> {
pub filename: &'a str,
Expand Down Expand Up @@ -45,3 +44,16 @@ pub fn ask_confirmation() -> bool {
Err(err) => panic!("{:#?}", err),
}
}

#[cfg(test)]
mod tests {
use super::regex_replace;
use regex::Regex;

#[test]
fn test_regex_replacement() {
let re = Regex::new("te").unwrap();
let replaced = regex_replace("testte.txt", "foo", &re);
assert!(replaced.replaced == "foostte.txt");
}
}
7 changes: 6 additions & 1 deletion src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ pub fn rename(config: Cli) {
filters.push(generate_startswith_filter(&startswith).unwrap())
}

for file in files {
'outer: for file in files {
for filter in &filters {
if !filter.is_match(&file) {
continue 'outer;
}
}
if re.is_match(&file) {
let replacement = regex_replace(&file, &config.replace_expr, &re);
match_display(&replacement);
Expand Down

0 comments on commit 62c0268

Please sign in to comment.