You are browsing as a guest. Sign up (or log in) to start making projects!

2h 43m 20s logged

Testing Tests

Hello again! I can’t believe we’re on devlog #8, considering I have been doing pretty sparse devlogs!

if true { allTestsPass }

IT’S UNIT TEST TIIIIIME!!!
I added 13 unit tests to my big change_leading_zeros() function. They cover most, if not all, possible edge cases.

Rename file11 -> file11

I discovered that in change_leading_zeros(), if the resulting new filename was the same as the old, would still do the rename, and it would count as a renamed file, so that the actual number of renamed files would be too large.

Hating on for loops as much as the functional bros.

I realised that my get_filenames() fn, which still has no unit tests, could be swapped to an iterator structure.

old:

pub fn get_filenames(path: &Path) -> Vec<String> {
    let mut names = Vec::new();
    if let Ok(read_dir) = fs::read_dir(path) {
        for file in read_dir.flatten() {
            names.push(
                file.file_name()
                    .into_string()
                    .unwrap_or("unknown name".into()),
            );
        }
    }
    names
}
pub fn get_filenames(path: &Path) -> Vec<String> {
    fs::read_dir(path)
        .into_iter()
        .flatten()
        .flatten()
        .map(|f| f.file_name().into_string().unwrap_or("unknown name".into()))
        .collect()
}

The two functions behave in exactly the same way, no unnecesary unwrap()s that may cause panics

See y’all next devlog!

0
17

Comments 0

No comments yet. Be the first!