duplicate_code 0.8.1

A tool for parsing directories scanning all the files within to find duplicate segments of code across files.
use crate::model::raw_file::RawFile;

#[macro_use]
mod macros;

#[test]
fn test_empty_hashed_files() {
    // Given
    let raw_files = vec![];

    // When/Then
    assert_duplicates!("empty_hashed_files", raw_files);
}

#[test]
fn test_hashed_file_with_no_lines() {
    // Given
    let raw_files = vec![RawFile {
        filename: "raw_file_filename".to_string(),
        lines: vec![],
    }];

    // When/Then
    assert_duplicates!("hashed_file_with_no_lines", raw_files);
}

#[test]
fn test_hashed_files_with_no_lines() {
    // Given
    let raw_files = vec![
        RawFile {
            filename: "raw_file_filename_1".to_string(),
            lines: vec![],
        },
        RawFile {
            filename: "raw_file_filename_2".to_string(),
            lines: vec![],
        },
    ];

    // When/Then
    assert_duplicates!("hashed_files_with_no_lines", raw_files);
}

#[test]
fn test_hashed_files_with_no_duplicate() {
    // Given
    let raw_files = vec![
        RawFile {
            filename: "raw_file_filename_1".to_string(),
            lines: vec![
                "line 1".to_string(),
                "line 2".to_string(),
                "line 3".to_string(),
            ],
        },
        RawFile {
            filename: "raw_file_filename_2".to_string(),
            lines: vec![
                "line 4".to_string(),
                "line 5".to_string(),
                "line 6".to_string(),
            ],
        },
    ];

    // When/Then
    assert_duplicates!("hashed_files_with_no_duplicate", raw_files);
}

#[test]
fn test_hashed_files_with_duplicate() {
    // Given
    let raw_files = vec![
        RawFile {
            filename: "raw_file_filename_1".to_string(),
            lines: vec![
                "line 1".to_string(),
                "line 2".to_string(),
                "line 3".to_string(),
            ],
        },
        RawFile {
            filename: "raw_file_filename_2".to_string(),
            lines: vec![
                "line 1".to_string(),
                "line 2".to_string(),
                "line 3".to_string(),
            ],
        },
    ];

    // When/Then
    assert_duplicates!("hashed_files_with_duplicate", raw_files);
}

#[test]
fn test_hashed_files_with_duplicate_on_different_lines() {
    // Given
    let raw_files = vec![
        RawFile {
            filename: "raw_file_filename_1".to_string(),
            lines: vec![
                "precursor 1".to_string(),
                "precursor 2".to_string(),
                "line 1".to_string(),
                "line 2".to_string(),
                "line 3".to_string(),
                "line 4".to_string(),
            ],
        },
        RawFile {
            filename: "raw_file_filename_2".to_string(),
            lines: vec![
                "line 1".to_string(),
                "line 2".to_string(),
                "line 3".to_string(),
                "successor 1".to_string(),
            ],
        },
    ];

    // When/Then
    assert_duplicates!("hashed_files_with_duplicate_on_different_lines", raw_files);
}

#[test]
fn test_hashed_files_and_empty_with_duplicate_on_different_lines() {
    // Given
    let raw_files = vec![
        RawFile {
            filename: "raw_file_filename_1".to_string(),
            lines: vec![
                "precursor 1".to_string(),
                "precursor 2".to_string(),
                "line 1".to_string(),
                "line 2".to_string(),
                "line 3".to_string(),
                "line 4".to_string(),
            ],
        },
        RawFile {
            filename: "raw_file_filename_2".to_string(),
            lines: vec![],
        },
        RawFile {
            filename: "raw_file_filename_3".to_string(),
            lines: vec![
                "line 1".to_string(),
                "line 2".to_string(),
                "line 3".to_string(),
                "successor 1".to_string(),
            ],
        },
    ];

    // When/Then
    assert_duplicates!(
        "hashed_files_and_empty_with_duplicate_on_different_lines",
        raw_files
    );
}

#[test]
fn test_hashed_files_with_multiple_duplicate_in_different_files() {
    // Given
    let raw_files = vec![
        RawFile {
            filename: "raw_file_filename_1".to_string(),
            lines: vec![
                "precursor 1".to_string(),
                "precursor 2".to_string(),
                "line 1".to_string(),
                "line 2".to_string(),
                "line 3".to_string(),
                "line 4".to_string(),
            ],
        },
        RawFile {
            filename: "raw_file_filename_2".to_string(),
            lines: vec![
                "line 1".to_string(),
                "line 2".to_string(),
                "line 3".to_string(),
                "successor 1".to_string(),
            ],
        },
        RawFile {
            filename: "raw_file_filename_3".to_string(),
            lines: vec![
                "precursor 1".to_string(),
                "line 1".to_string(),
                "line 2".to_string(),
                "line 3".to_string(),
                "successor 1".to_string(),
            ],
        },
    ];

    // When/Then
    assert_duplicates!(
        "hashed_files_with_multiple_duplicate_in_different_files",
        raw_files
    );
}

#[test]
fn test_hashed_files_with_ignore_line_regex() {
    // Given
    let raw_files = vec![
        RawFile {
            filename: "raw_file_filename_1".to_string(),
            lines: vec![
                "precursor 1".to_string(),
                "precursor 2".to_string(),
                "line 1".to_string(),
                "line 2".to_string(),
                "line 3".to_string(),
                "line 4".to_string(),
            ],
        },
        RawFile {
            filename: "raw_file_filename_2".to_string(),
            lines: vec![
                "line 1".to_string(),
                "line 2".to_string(),
                "line 3".to_string(),
                "successor 1".to_string(),
            ],
        },
    ];
    let ignore_line_regex = vec!["^precursor".to_string(), "^successor".to_string()];

    // When/Then
    assert_duplicates_with_ignore_line_regex!(
        "hashed_files_with_duplicate_on_different_lines",
        ignore_line_regex,
        raw_files
    );
}

#[test]
fn test_hashed_files_with_duplicate_with_multiple_occurrences() {
    // Given
    let raw_files = vec![
        RawFile {
            filename: "raw_file_filename_1".to_string(),
            lines: vec![
                "precursor 1".to_string(),
                "line 1".to_string(),
                "line 2".to_string(),
                "line 3".to_string(),
            ],
        },
        RawFile {
            filename: "raw_file_filename_2".to_string(),
            lines: vec![
                "line 1".to_string(),
                "line 2".to_string(),
                "line 3".to_string(),
                "breaker 1".to_string(),
                "line 1".to_string(),
                "line 2".to_string(),
                "line 3".to_string(),
            ],
        },
    ];

    // When/Then
    assert_duplicates!(
        "hashed_files_with_duplicate_with_multiple_occurrences",
        raw_files
    );
}