From 1f25982b84e986b7cb6e7d2d3d1ccd7a50b0235f Mon Sep 17 00:00:00 2001 From: Bauke Date: Sat, 2 Apr 2022 15:19:19 +0200 Subject: [PATCH] Rewrite tests to use snapshots. --- tests/lib.rs | 58 ++++++++----------- tests/snapshots/lib__generate-gravatar.snap | 8 +++ tests/snapshots/lib__generate-libravatar.snap | 8 +++ tests/snapshots/lib__hash-bauke-casing.snap | 5 ++ .../snapshots/lib__hash-bauke-whitespace.snap | 5 ++ tests/snapshots/lib__hash-bauke.snap | 5 ++ tests/snapshots/lib__hash-holllo-casing.snap | 5 ++ .../lib__hash-holllo-whitespace.snap | 5 ++ tests/snapshots/lib__hash-holllo.snap | 5 ++ 9 files changed, 71 insertions(+), 33 deletions(-) create mode 100644 tests/snapshots/lib__generate-gravatar.snap create mode 100644 tests/snapshots/lib__generate-libravatar.snap create mode 100644 tests/snapshots/lib__hash-bauke-casing.snap create mode 100644 tests/snapshots/lib__hash-bauke-whitespace.snap create mode 100644 tests/snapshots/lib__hash-bauke.snap create mode 100644 tests/snapshots/lib__hash-holllo-casing.snap create mode 100644 tests/snapshots/lib__hash-holllo-whitespace.snap create mode 100644 tests/snapshots/lib__hash-holllo.snap diff --git a/tests/lib.rs b/tests/lib.rs index 41ea5a0..6bac2c7 100644 --- a/tests/lib.rs +++ b/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); } } diff --git a/tests/snapshots/lib__generate-gravatar.snap b/tests/snapshots/lib__generate-gravatar.snap new file mode 100644 index 0000000..3226a1f --- /dev/null +++ b/tests/snapshots/lib__generate-gravatar.snap @@ -0,0 +1,8 @@ +--- +source: tests/lib.rs +expression: urls +--- +[ + "https://www.gravatar.com/avatar/ecd836ee843ff0ab75d4720bd40c2baf", + "https://www.gravatar.com/avatar/ebff9105dce4954b1bdb57fdab079ff3", +] diff --git a/tests/snapshots/lib__generate-libravatar.snap b/tests/snapshots/lib__generate-libravatar.snap new file mode 100644 index 0000000..42a259e --- /dev/null +++ b/tests/snapshots/lib__generate-libravatar.snap @@ -0,0 +1,8 @@ +--- +source: tests/lib.rs +expression: urls +--- +[ + "https://cdn.libravatar.org/avatar/ecd836ee843ff0ab75d4720bd40c2baf", + "https://cdn.libravatar.org/avatar/ebff9105dce4954b1bdb57fdab079ff3", +] diff --git a/tests/snapshots/lib__hash-bauke-casing.snap b/tests/snapshots/lib__hash-bauke-casing.snap new file mode 100644 index 0000000..3014d41 --- /dev/null +++ b/tests/snapshots/lib__hash-bauke-casing.snap @@ -0,0 +1,5 @@ +--- +source: tests/lib.rs +expression: "Generator::hash_email(&email.to_uppercase())" +--- +ecd836ee843ff0ab75d4720bd40c2baf diff --git a/tests/snapshots/lib__hash-bauke-whitespace.snap b/tests/snapshots/lib__hash-bauke-whitespace.snap new file mode 100644 index 0000000..02de210 --- /dev/null +++ b/tests/snapshots/lib__hash-bauke-whitespace.snap @@ -0,0 +1,5 @@ +--- +source: tests/lib.rs +expression: "Generator::hash_email(&format!(\" {email} \"))" +--- +ecd836ee843ff0ab75d4720bd40c2baf diff --git a/tests/snapshots/lib__hash-bauke.snap b/tests/snapshots/lib__hash-bauke.snap new file mode 100644 index 0000000..421b045 --- /dev/null +++ b/tests/snapshots/lib__hash-bauke.snap @@ -0,0 +1,5 @@ +--- +source: tests/lib.rs +expression: "Generator::hash_email(email)" +--- +ecd836ee843ff0ab75d4720bd40c2baf diff --git a/tests/snapshots/lib__hash-holllo-casing.snap b/tests/snapshots/lib__hash-holllo-casing.snap new file mode 100644 index 0000000..a331c20 --- /dev/null +++ b/tests/snapshots/lib__hash-holllo-casing.snap @@ -0,0 +1,5 @@ +--- +source: tests/lib.rs +expression: "Generator::hash_email(&email.to_uppercase())" +--- +ebff9105dce4954b1bdb57fdab079ff3 diff --git a/tests/snapshots/lib__hash-holllo-whitespace.snap b/tests/snapshots/lib__hash-holllo-whitespace.snap new file mode 100644 index 0000000..20a7ff8 --- /dev/null +++ b/tests/snapshots/lib__hash-holllo-whitespace.snap @@ -0,0 +1,5 @@ +--- +source: tests/lib.rs +expression: "Generator::hash_email(&format!(\" {email} \"))" +--- +ebff9105dce4954b1bdb57fdab079ff3 diff --git a/tests/snapshots/lib__hash-holllo.snap b/tests/snapshots/lib__hash-holllo.snap new file mode 100644 index 0000000..b0b18a7 --- /dev/null +++ b/tests/snapshots/lib__hash-holllo.snap @@ -0,0 +1,5 @@ +--- +source: tests/lib.rs +expression: "Generator::hash_email(email)" +--- +ebff9105dce4954b1bdb57fdab079ff3