#![cfg(feature = "std")]
#[test]
fn readme_examples_compile_and_run() -> Result<(), bcrypt_rust::BcryptError> {
let hash = bcrypt_rust::hash(b"hunter2", 4)?;
assert_eq!(hash.len(), 60);
assert!(bcrypt_rust::verify(b"hunter2", &hash)?);
assert!(!bcrypt_rust::verify(b"hunter3", &hash)?);
let passwords: [&[u8]; 3] = [b"alpha", b"bravo", b"charlie"];
let hashes = bcrypt_rust::hash_many(&passwords, 4)?;
for (password, hash) in passwords.iter().zip(&hashes) {
assert!(bcrypt_rust::verify(password, hash)?);
}
Ok(())
}
#[test]
fn transcription_still_matches_the_readme() {
let readme = std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))
.expect("README.md must be readable from the manifest dir");
let this_file =
std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/readme.rs"))
.expect("this test file must be readable");
fn normalize(line: &str) -> String {
let code = match line.find("//") {
Some(i) => &line[..i],
None => line,
};
code.split_whitespace().collect::<Vec<_>>().join(" ")
}
let transcribed: Vec<String> = this_file.lines().map(normalize).collect();
let mut in_rust_block = false;
let mut blocks = 0usize;
let mut checked = 0usize;
for line in readme.lines() {
if line.trim_start().starts_with("```") {
if in_rust_block {
in_rust_block = false;
} else if line.trim() == "```rust" {
in_rust_block = true;
blocks += 1;
}
continue;
}
if !in_rust_block {
continue;
}
let want = normalize(line);
if want.is_empty() {
continue; }
assert!(
transcribed.contains(&want),
"README.md has a line of Rust that this file does not transcribe, so it is \
compiled by nothing:\n {}\nAdd it to `readme_examples_compile_and_run`.",
line.trim()
);
checked += 1;
}
assert_eq!(
blocks, 2,
"README gained or lost a ```rust block; transcribe it here too"
);
assert!(
checked >= 8,
"extraction found suspiciously little code: {checked} lines"
);
}