From 0f767c40a0517e4c635e462c6084e4cb16e57478 Mon Sep 17 00:00:00 2001 From: Vika Date: Thu, 6 May 2021 17:13:38 +0300 Subject: Turns out assert!() can have a second argument with a panic reason. Cool! --- src/micropub/post.rs | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) (limited to 'src/micropub') diff --git a/src/micropub/post.rs b/src/micropub/post.rs index 39a06ce..5b85ad4 100644 --- a/src/micropub/post.rs +++ b/src/micropub/post.rs @@ -509,15 +509,15 @@ mod tests { }); let (uid, post) = normalize_mf2(mf2, &User::new("https://fireburn.ru/", "https://quill.p3k.io/", "create update media")); - assert!(post["properties"]["published"].as_array().unwrap().len() > 0); - DateTime::parse_from_rfc3339(post["properties"]["published"][0].as_str().unwrap()).unwrap(); - assert!(post["properties"]["url"].as_array().unwrap().len() > 0); - assert!(post["properties"]["uid"].as_array().unwrap().len() > 0); - assert_eq!(post["properties"]["uid"][0].as_str().unwrap(), &uid); - assert!(uid.starts_with("https://fireburn.ru/posts/")); - assert_eq!(post["properties"]["content"][0]["html"].as_str().unwrap().trim(), "

This is content!

"); - assert_eq!(post["properties"]["channel"][0], "https://fireburn.ru/feeds/main"); - assert_eq!(post["properties"]["author"][0], "https://fireburn.ru/"); + assert_eq!(post["properties"]["published"].as_array().expect("post['published'] is undefined").len(), 1, "Post doesn't have a published time"); + DateTime::parse_from_rfc3339(post["properties"]["published"][0].as_str().unwrap()).expect("Couldn't parse date from rfc3339"); + assert!(post["properties"]["url"].as_array().expect("post['url'] is undefined").len() > 0, "Post doesn't have any URLs"); + assert_eq!(post["properties"]["uid"].as_array().expect("post['uid'] is undefined").len(), 1, "Post doesn't have a single UID"); + assert_eq!(post["properties"]["uid"][0], uid, "UID of a post and its supposed location don't match"); + assert!(uid.starts_with("https://fireburn.ru/posts/"), "The post namespace is incorrect"); + assert_eq!(post["properties"]["content"][0]["html"].as_str().expect("Post doesn't have a rich content object").trim(), "

This is content!

", "Parsed Markdown content doesn't match expected HTML"); + assert_eq!(post["properties"]["channel"][0], "https://fireburn.ru/feeds/main", "Post isn't posted to the main channel"); + assert_eq!(post["properties"]["author"][0], "https://fireburn.ru/", "Post author is unknown"); } #[test] @@ -536,11 +536,9 @@ mod tests { .unwrap() .iter() .map(|i| i.as_str().unwrap()) - .any(|i| i == "https://fireburn.ru/posts/hello-post") - ); - if let Some(_) = post["properties"]["mp-slug"].as_array() { - panic!("mp-slug wasn't deleted from the array!") - } + .any(|i| i == "https://fireburn.ru/posts/hello-post"), + "Didn't found an URL pointing to the location expected by the mp-slug semantics"); + assert!(post["properties"]["mp-slug"].as_array().is_none(), "mp-slug wasn't deleted from the array!") } #[test] @@ -554,16 +552,15 @@ mod tests { }); let (uid, post) = normalize_mf2(mf2, &User::new("https://fireburn.ru/", "https://quill.p3k.io/", "create update media")); - assert_eq!(post["properties"]["uid"][0].as_str().unwrap(), &uid); + assert_eq!(post["properties"]["uid"][0], uid, "UID of a post and its supposed location don't match"); assert_eq!(post["properties"]["author"][0], "https://fireburn.ru/"); assert!(post["properties"]["url"] .as_array() .unwrap() .iter() .map(|i| i.as_str().unwrap()) - .any(|i| i == "https://fireburn.ru/feeds/main")); - if let Some(_) = post["properties"]["mp-slug"].as_array() { - panic!("mp-slug wasn't deleted from the array!") - } + .any(|i| i == "https://fireburn.ru/feeds/main"), + "Didn't found an URL pointing to the location expected by the mp-slug semantics"); + assert!(post["properties"]["mp-slug"].as_array().is_none(), "mp-slug wasn't deleted from the array!") } } \ No newline at end of file -- cgit 1.4.1