1
Fork 0

Rewrite tests to use snapshots.

This commit is contained in:
Bauke 2022-04-02 15:19:19 +02:00
parent 430b653a2c
commit 1f25982b84
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
9 changed files with 71 additions and 33 deletions

View File

@ -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
insta::assert_snapshot!(
format!("hash-{name}-whitespace"),
Generator::hash_email(&format!(" {email} "))
);
assert_eq!(
Generator::hash_email(&HOLLLO_EMAIL.to_uppercase()),
HOLLLO_HASH
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);
}
}

View File

@ -0,0 +1,8 @@
---
source: tests/lib.rs
expression: urls
---
[
"https://www.gravatar.com/avatar/ecd836ee843ff0ab75d4720bd40c2baf",
"https://www.gravatar.com/avatar/ebff9105dce4954b1bdb57fdab079ff3",
]

View File

@ -0,0 +1,8 @@
---
source: tests/lib.rs
expression: urls
---
[
"https://cdn.libravatar.org/avatar/ecd836ee843ff0ab75d4720bd40c2baf",
"https://cdn.libravatar.org/avatar/ebff9105dce4954b1bdb57fdab079ff3",
]

View File

@ -0,0 +1,5 @@
---
source: tests/lib.rs
expression: "Generator::hash_email(&email.to_uppercase())"
---
ecd836ee843ff0ab75d4720bd40c2baf

View File

@ -0,0 +1,5 @@
---
source: tests/lib.rs
expression: "Generator::hash_email(&format!(\" {email} \"))"
---
ecd836ee843ff0ab75d4720bd40c2baf

View File

@ -0,0 +1,5 @@
---
source: tests/lib.rs
expression: "Generator::hash_email(email)"
---
ecd836ee843ff0ab75d4720bd40c2baf

View File

@ -0,0 +1,5 @@
---
source: tests/lib.rs
expression: "Generator::hash_email(&email.to_uppercase())"
---
ebff9105dce4954b1bdb57fdab079ff3

View File

@ -0,0 +1,5 @@
---
source: tests/lib.rs
expression: "Generator::hash_email(&format!(\" {email} \"))"
---
ebff9105dce4954b1bdb57fdab079ff3

View File

@ -0,0 +1,5 @@
---
source: tests/lib.rs
expression: "Generator::hash_email(email)"
---
ebff9105dce4954b1bdb57fdab079ff3