From b926de06d6d8c778b3848f142ddcb2537d402fa3 Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Thu, 19 Sep 2024 08:34:56 +0200 Subject: [PATCH] test(json): assert_str (#221) * test(json): assert_str * build(json): also build on test for other tests * build: allow dead code for tests --- src/client_reqwest.rs | 5 +- src/client_ureq.rs | 277 +++++++++++------------------------------- src/json.rs | 28 +++++ src/lib.rs | 2 +- src/parse_mode.rs | 6 +- 5 files changed, 103 insertions(+), 215 deletions(-) diff --git a/src/client_reqwest.rs b/src/client_reqwest.rs index 83a9a6c..4390bea 100644 --- a/src/client_reqwest.rs +++ b/src/client_reqwest.rs @@ -147,6 +147,7 @@ impl AsyncTelegramApi for AsyncApi { mod async_tests { use super::*; use crate::api_params::SendMessageParams; + use crate::json; #[tokio::test] async fn async_send_message_success() { @@ -165,9 +166,7 @@ mod async_tests { let api = AsyncApi::new_url(server.url()); let response = api.send_message(¶ms).await.unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[tokio::test] diff --git a/src/client_ureq.rs b/src/client_ureq.rs index 7841744..48e2ad1 100644 --- a/src/client_ureq.rs +++ b/src/client_ureq.rs @@ -164,6 +164,7 @@ mod tests { SetMyCommandsParams, SetWebhookParams, StopMessageLiveLocationParams, StopPollParams, UnbanChatMemberParams, UnpinChatMessageParams, }; + use crate::json; use crate::objects::{ AllowedUpdate, BotCommand, ChatPermissions, InlineQueryResultVenue, InputPollOption, }; @@ -191,14 +192,10 @@ mod tests { let api = Api::new_url(server.url()); let response = api.get_updates(¶ms).unwrap(); + json::assert_str(&response, response_string); - let json = serde_json::to_string(&response).unwrap(); - - assert_eq!(response_string, json); assert_eq!(1, response.result.len()); - let update = &response.result[0]; - assert_eq!(379656753, update.update_id); } @@ -218,9 +215,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_message(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -259,9 +254,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.set_webhook(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -278,9 +271,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.delete_webhook(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -295,9 +286,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.get_webhook_info().unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -312,9 +301,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.get_me().unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -329,9 +316,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.log_out().unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -346,10 +331,8 @@ mod tests { .create(); let api = Api::new_url(server.url()); - let response = api.close().unwrap_err(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + let response = api.close().err(); + json::assert_str(&response, response_string); } #[test] @@ -364,9 +347,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.close().unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -387,9 +368,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.forward_message(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -410,9 +389,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.copy_message(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -433,9 +410,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_location(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -457,9 +432,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.edit_message_live_location(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -479,9 +452,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.stop_message_live_location(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -504,9 +475,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_venue(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -527,9 +496,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_contact(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -553,9 +520,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_poll(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -572,9 +537,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_dice(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -594,9 +557,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_chat_action(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -615,9 +576,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.get_user_profile_photos(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -636,9 +595,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.get_file(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -658,9 +615,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.ban_chat_member(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -680,9 +635,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.unban_chat_member(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -707,9 +660,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.restrict_chat_member(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -730,9 +681,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.promote_chat_member(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -753,9 +702,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.set_chat_administrator_custom_title(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -779,9 +726,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.set_chat_permissions(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -801,9 +746,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.export_chat_invite_link(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -823,9 +766,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.create_chat_invite_link(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -846,9 +787,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.edit_chat_invite_link(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -869,9 +808,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.revoke_chat_invite_link(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -890,9 +827,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.delete_chat_photo(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -913,9 +848,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_photo(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -935,9 +868,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_audio(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -959,9 +890,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_audio(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -984,9 +913,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_audio(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1008,9 +935,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_document(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1032,9 +957,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_video(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1056,9 +979,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_animation(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1078,9 +999,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_voice(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1100,9 +1019,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_video_note(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1125,9 +1042,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.set_chat_photo(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1147,9 +1062,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.set_chat_title(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1169,9 +1082,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.set_chat_description(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1191,9 +1102,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.pin_chat_message(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1210,9 +1119,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.unpin_chat_message(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1229,9 +1136,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.leave_chat(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1248,9 +1153,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.get_chat(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1269,9 +1172,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.get_chat_administrators(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1290,9 +1191,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.get_chat_member_count(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1312,9 +1211,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.get_chat_member(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1334,9 +1231,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.set_chat_sticker_set(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1355,9 +1250,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.delete_chat_sticker_set(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1377,9 +1270,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.answer_callback_query(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1407,9 +1298,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.set_my_commands(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1438,9 +1327,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.set_my_commands(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1463,9 +1350,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.delete_my_commands(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1482,9 +1367,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.get_my_commands(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1505,9 +1388,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.get_my_commands(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1539,9 +1420,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.answer_inline_query(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1563,9 +1442,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.edit_message_text(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1587,9 +1464,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.edit_message_caption(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1610,9 +1485,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.stop_poll(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1633,9 +1506,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.delete_message(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1657,9 +1528,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_sticker(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1675,9 +1544,7 @@ mod tests { .create(); let api = Api::new_url(server.url()); let response = api.get_sticker_set(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1703,9 +1570,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.send_media_group(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] @@ -1731,9 +1596,7 @@ mod tests { let api = Api::new_url(server.url()); let response = api.edit_message_media(¶ms).unwrap(); - - let json = serde_json::to_string(&response).unwrap(); - assert_eq!(response_string, json); + json::assert_str(&response, response_string); } #[test] diff --git a/src/json.rs b/src/json.rs index f172f1a..389ca4a 100644 --- a/src/json.rs +++ b/src/json.rs @@ -15,3 +15,31 @@ where { serde_json::to_string(value).map_err(|error| Error::Encode(format!("{error:?} : {value:?}"))) } + +#[cfg(test)] +#[track_caller] +pub fn assert_str(value: &T, expected: &str) +where + T: serde::ser::Serialize, +{ + let actual = serde_json::to_string(value).expect("Should be able to stringify to JSON"); + assert!( + expected == actual, + "value should equal the expected JSON\n expected: {expected}\n actual: {actual}" + ); +} + +#[test] +fn assert_str_works() { + let value = serde_json::json!({"code": 42}); + let expected = r#"{"code":42}"#; + assert_str(&value, expected); +} + +#[test] +#[should_panic = "value should equal the expected JSON"] +fn assert_str_fails() { + let value = serde_json::json!({"code": 42}); + let expected = r#"{"foo":"bar"}"#; + assert_str(&value, expected); +} diff --git a/src/lib.rs b/src/lib.rs index 91b287e..0bf5232 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,7 +26,7 @@ mod client_reqwest; #[cfg(feature = "http-client")] mod client_ureq; mod error; -#[cfg(any(feature = "http-client", feature = "async-http-client"))] +#[cfg(any(test, feature = "http-client", feature = "async-http-client"))] mod json; mod macros; pub mod objects; diff --git a/src/parse_mode.rs b/src/parse_mode.rs index 970e5f8..8e86059 100644 --- a/src/parse_mode.rs +++ b/src/parse_mode.rs @@ -51,12 +51,10 @@ impl Display for ParseMode { #[test] fn serde_markdown_works() { - let json = serde_json::to_string(&ParseMode::MarkdownV2).unwrap(); - assert_eq!(json, r#""MarkdownV2""#); + crate::json::assert_str(&ParseMode::MarkdownV2, r#""MarkdownV2""#); } #[test] fn serde_html_works() { - let json = serde_json::to_string(&ParseMode::Html).unwrap(); - assert_eq!(json, r#""HTML""#); + crate::json::assert_str(&ParseMode::Html, r#""HTML""#); }