Rewrite tests to use snapshots.
This commit is contained in:
parent
430b653a2c
commit
1f25982b84
58
tests/lib.rs
58
tests/lib.rs
|
@ -1,49 +1,41 @@
|
|||
use gravatar_rs::Generator;
|
||||
|
||||
const BAUKE_EMAIL: &str = "me@bauke.xyz";
|
||||
const BAUKE_HASH: &str = "ecd836ee843ff0ab75d4720bd40c2baf";
|
||||
|
||||
const HOLLLO_EMAIL: &str = "helllo@holllo.cc";
|
||||
const HOLLLO_HASH: &str = "ebff9105dce4954b1bdb57fdab079ff3";
|
||||
|
||||
#[test]
|
||||
fn test_hash_email() {
|
||||
assert_eq!(Generator::hash_email(BAUKE_EMAIL), BAUKE_HASH);
|
||||
assert_eq!(Generator::hash_email(HOLLLO_EMAIL), HOLLLO_HASH);
|
||||
let samples = [("bauke", BAUKE_EMAIL), ("holllo", HOLLLO_EMAIL)];
|
||||
|
||||
// Make sure leading and trailing whitespace is removed.
|
||||
assert_eq!(
|
||||
Generator::hash_email(&format!(" {BAUKE_EMAIL} ")),
|
||||
BAUKE_HASH
|
||||
);
|
||||
assert_eq!(
|
||||
Generator::hash_email(&format!(" {HOLLLO_EMAIL} ")),
|
||||
HOLLLO_HASH
|
||||
);
|
||||
for (name, email) in samples {
|
||||
insta::assert_snapshot!(
|
||||
format!("hash-{name}"),
|
||||
Generator::hash_email(email)
|
||||
);
|
||||
|
||||
// Make sure casing doesn't matter.
|
||||
assert_eq!(
|
||||
Generator::hash_email(&BAUKE_EMAIL.to_uppercase()),
|
||||
BAUKE_HASH
|
||||
);
|
||||
assert_eq!(
|
||||
Generator::hash_email(&HOLLLO_EMAIL.to_uppercase()),
|
||||
HOLLLO_HASH
|
||||
);
|
||||
insta::assert_snapshot!(
|
||||
format!("hash-{name}-whitespace"),
|
||||
Generator::hash_email(&format!(" {email} "))
|
||||
);
|
||||
|
||||
insta::assert_snapshot!(
|
||||
format!("hash-{name}-casing"),
|
||||
Generator::hash_email(&email.to_uppercase())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generator() {
|
||||
let base_urls = [&Generator::default().base_url, "cdn.libravatar.org"];
|
||||
let samples = [(BAUKE_EMAIL, BAUKE_HASH), (HOLLLO_EMAIL, HOLLLO_HASH)];
|
||||
let emails = [BAUKE_EMAIL, HOLLLO_EMAIL];
|
||||
let samples = [
|
||||
("gravatar", Generator::default().base_url),
|
||||
("libravatar", "cdn.libravatar.org".to_string()),
|
||||
];
|
||||
|
||||
for base_url in base_urls {
|
||||
let generator = Generator::default().set_base_url(base_url);
|
||||
|
||||
for (email, hash) in samples {
|
||||
let actual = generator.generate(email);
|
||||
let expected = format!("https://{base_url}/avatar/{hash}");
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
for (name, base_url) in samples {
|
||||
let generator = Generator::default().set_base_url(&base_url);
|
||||
let urls = emails.map(|email| generator.generate(email));
|
||||
insta::assert_debug_snapshot!(format!("generate-{name}"), urls);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
source: tests/lib.rs
|
||||
expression: urls
|
||||
---
|
||||
[
|
||||
"https://www.gravatar.com/avatar/ecd836ee843ff0ab75d4720bd40c2baf",
|
||||
"https://www.gravatar.com/avatar/ebff9105dce4954b1bdb57fdab079ff3",
|
||||
]
|
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
source: tests/lib.rs
|
||||
expression: urls
|
||||
---
|
||||
[
|
||||
"https://cdn.libravatar.org/avatar/ecd836ee843ff0ab75d4720bd40c2baf",
|
||||
"https://cdn.libravatar.org/avatar/ebff9105dce4954b1bdb57fdab079ff3",
|
||||
]
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
source: tests/lib.rs
|
||||
expression: "Generator::hash_email(&email.to_uppercase())"
|
||||
---
|
||||
ecd836ee843ff0ab75d4720bd40c2baf
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
source: tests/lib.rs
|
||||
expression: "Generator::hash_email(&format!(\" {email} \"))"
|
||||
---
|
||||
ecd836ee843ff0ab75d4720bd40c2baf
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
source: tests/lib.rs
|
||||
expression: "Generator::hash_email(email)"
|
||||
---
|
||||
ecd836ee843ff0ab75d4720bd40c2baf
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
source: tests/lib.rs
|
||||
expression: "Generator::hash_email(&email.to_uppercase())"
|
||||
---
|
||||
ebff9105dce4954b1bdb57fdab079ff3
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
source: tests/lib.rs
|
||||
expression: "Generator::hash_email(&format!(\" {email} \"))"
|
||||
---
|
||||
ebff9105dce4954b1bdb57fdab079ff3
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
source: tests/lib.rs
|
||||
expression: "Generator::hash_email(email)"
|
||||
---
|
||||
ebff9105dce4954b1bdb57fdab079ff3
|
Loading…
Reference in New Issue