Skip to content

Commit

Permalink
basic report test
Browse files Browse the repository at this point in the history
  • Loading branch information
helio-frota committed Jun 12, 2024
1 parent 7047eff commit d57cfb1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ edition = "2021"

[dependencies]
strsim = "0.11"

21 changes: 13 additions & 8 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ mod tests {
let content = util::content(&path_as_str);
let blocks = util::code_blocks(content.unwrap().as_str());
assert!(!blocks[0].is_empty());

let mut use_found = false;
for b in blocks {
if b.contains("use") {
use_found = true;
}
}
assert!(!use_found);
}

#[test]
Expand Down Expand Up @@ -62,4 +54,17 @@ mod tests {
assert!(s.2 > 0.10);
}
}

#[test]
fn test_report() {
let files = util::rust_files(".").expect("rust files not found.");
let path_as_str = files[0].to_string_lossy();
let content = util::content(&path_as_str);
let blocks = util::code_blocks(content.unwrap().as_str());
// Intentionally lowering the threshold to test something
let similar_found = util::similar(blocks, 0.10);
let out = util::report(similar_found);
assert!(out.contains("Duplicrabs"));
assert!(out.contains("Almost the same"));
}
}
46 changes: 20 additions & 26 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn rust_files(dir: &str) -> Result<Vec<PathBuf>, io::Error> {
if path.is_dir() {
if let Some(file_name) = path.file_name() {
if let Some(file_name_str) = file_name.to_str() {
if file_name_str.contains("target") || file_name_str.contains("test") {
if file_name_str.contains("target") {
continue;
}
}
Expand Down Expand Up @@ -80,18 +80,6 @@ pub fn code_blocks(content: &str) -> Vec<String> {
continue;
}

// discards comments
if lines[i].trim().contains("//") {
i += 1;
continue;
}

// discards comments
if lines[i].trim().contains("///") {
i += 1;
continue;
}

// Creates a temporary code-block vector of lines.
let mut block_lines: Vec<String> = Vec::new();
block_lines.push(lines[i].clone());
Expand Down Expand Up @@ -145,32 +133,38 @@ pub fn similar(blocks: Vec<String>, threshold: f64) -> Vec<(String, String, f64)
result
}

pub fn report(similar_blocks: Vec<(String, String, f64)>) {
pub fn report(similar_blocks: Vec<(String, String, f64)>) -> String {
let mut out = String::new();
if !similar_blocks.is_empty() {
println!("# Duplicrabs\n");
out.push_str("# Duplicrabs\n\n");

for (idx, s) in similar_blocks.iter().enumerate() {
let mut b1: Vec<&str> = s.0.split('\n').collect();
let f1 = b1.pop();
let mut b2: Vec<&str> = s.1.split('\n').collect();
let f2 = b2.pop();

println!("### crab {}\n", idx + 1);
out.push_str(format!("### \u{1F980} {}\n\n", idx + 1).as_str());

if s.2 == 1.0 {
println!("> [!TIP]\n> Exactly the same\n");
out.push_str("> [!TIP]\n> Exactly the same\n\n");
} else {
println!("> [!WARNING]\n> Almost the same\n");
out.push_str("> [!WARNING]\n> Almost the same\n\n");
}

println!("```rust");
println!("{}", b1.join("\n"));
println!("```\n");
println!("`{}`\n", f1.unwrap_or("n/a"));
println!("```rust");
println!("{}", b2.join("\n"));
println!("```\n");
println!("`{}`\n", f2.unwrap_or("n/a"));
out.push_str("```rust\n");
out.push_str(format!("{}\n", b1.join("\n").as_str()).as_str());
out.push_str("```\n\n");

out.push_str(format!("`{}`\n\n", f1.unwrap_or("n/a")).as_str());

out.push_str("```rust\n");
out.push_str(format!("{}\n", b2.join("\n").as_str()).as_str());
out.push_str("```\n\n");

out.push_str(format!("`{}`\n\n", f2.unwrap_or("n/a")).as_str());
}
}
println!("{}", out);
out
}

0 comments on commit d57cfb1

Please sign in to comment.