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:
parent
84650b2af1
commit
3b07b4768a
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -248,6 +248,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"reqwest",
|
||||
"scraper",
|
||||
"tempfile",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
|
@ -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"
|
||||
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user