1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-01-05 16:10:31 +02:00

Add unit tests to Safe FFI exercise (#588)

This commit is contained in:
Martin Geisler 2023-05-04 12:20:47 +02:00 committed by GitHub
parent 84650b2af1
commit 3b07b4768a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 0 deletions

1
Cargo.lock generated
View File

@ -248,6 +248,7 @@ version = "0.1.0"
dependencies = [
"reqwest",
"scraper",
"tempfile",
"thiserror",
]

View File

@ -44,3 +44,6 @@ path = "concurrency/link-checker.rs"
reqwest = { version = "0.11.12", features = ["blocking"] }
scraper = "0.15.0"
thiserror = "1.0.37"
[dev-dependencies]
tempfile = "3.5.0"

View File

@ -123,3 +123,42 @@ fn main() -> Result<(), String> {
Ok(())
}
// ANCHOR_END: main
#[cfg(test)]
mod tests {
use super::*;
use std::error::Error;
#[test]
fn test_nonexisting_directory() {
let iter = DirectoryIterator::new("no-such-directory");
assert!(iter.is_err());
}
#[test]
fn test_empty_directory() -> Result<(), Box<dyn Error>> {
let tmp = tempfile::TempDir::new()?;
let iter = DirectoryIterator::new(
tmp.path().to_str().ok_or("Non UTF-8 character in path")?,
)?;
let mut entries = iter.collect::<Vec<_>>();
entries.sort();
assert_eq!(entries, &[".", ".."]);
Ok(())
}
#[test]
fn test_nonempty_directory() -> Result<(), Box<dyn Error>> {
let tmp = tempfile::TempDir::new()?;
std::fs::write(tmp.path().join("foo.txt"), "The Foo Diaries\n")?;
std::fs::write(tmp.path().join("bar.png"), "<PNG>\n")?;
std::fs::write(tmp.path().join("crab.rs"), "//! Crab\n")?;
let iter = DirectoryIterator::new(
tmp.path().to_str().ok_or("Non UTF-8 character in path")?,
)?;
let mut entries = iter.collect::<Vec<_>>();
entries.sort();
assert_eq!(entries, &[".", "..", "bar.png", "crab.rs", "foo.txt"]);
Ok(())
}
}