1. Box<Chat> -> Chat; 2. assert_eq!( //much code//, //same// ) -> expected & actual; 3. User { user: User } -> User(User)

This commit is contained in:
Mr-Andersen 2019-09-07 21:05:18 +03:00
parent 620ffe5580
commit 0d6bb9840b
2 changed files with 68 additions and 50 deletions

View file

@ -86,25 +86,24 @@ mod tests {
#[test] #[test]
fn channel_de() { fn channel_de() {
assert_eq!( let expected = Chat {
Chat { id: -1,
id: -1, kind: ChatKind::NonPrivate {
kind: ChatKind::NonPrivate { title: None,
title: None, kind: NonPrivateChatKind::Channel {
kind: NonPrivateChatKind::Channel { username: Some("channelname".into()),
username: Some("channelname".into())
},
description: None,
invite_link: None,
pinned_message: None
}, },
photo: None, description: None,
invite_link: None,
pinned_message: None,
}, },
from_str( photo: None,
r#"{"chat_id":-1,"type":"channel","username":"channelname"}"# };
) let actual = from_str(
.unwrap() r#"{"chat_id":-1,"type":"channel","username":"channelname"}"#,
); )
.unwrap();
assert_eq!(expected, actual);
} }
#[test] #[test]
@ -129,22 +128,4 @@ mod tests {
fn private_chat_de_wrong_type_field() { fn private_chat_de_wrong_type_field() {
assert!(from_str::<Chat>(r#"{"chat_id":0,"type":"WRONG"}"#).is_err()); assert!(from_str::<Chat>(r#"{"chat_id":0,"type":"WRONG"}"#).is_err());
} }
/*#[test]
fn private_chat_ser() {
assert_eq!(
to_string(&Chat {
id: 0,
type_: ChatKind::Private {
type_: (),
username: None,
first_name: None,
last_name: None
},
photo: None
})
.unwrap(),
r#"{"chat_id":0,"type":"private"}"#.to_owned()
);
}*/
} }

View file

@ -9,7 +9,7 @@ pub struct Message {
#[serde(rename = "message_id")] #[serde(rename = "message_id")]
pub id: i64, pub id: i64,
pub date: i32, pub date: i32,
pub chat: Box<Chat>, pub chat: Chat,
#[serde(flatten)] #[serde(flatten)]
pub message_kind: MessageKind, pub message_kind: MessageKind,
} }
@ -77,7 +77,7 @@ pub enum ForwardKind {
#[serde(rename = "forward_date")] #[serde(rename = "forward_date")]
date: i32, date: i32,
#[serde(rename = "forward_from_chat")] #[serde(rename = "forward_from_chat")]
chat: Box<Chat>, chat: Chat,
#[serde(rename = "forward_from_message_id")] #[serde(rename = "forward_from_message_id")]
message_id: i64, message_id: i64,
#[serde(rename = "forward_signature")] #[serde(rename = "forward_signature")]
@ -95,16 +95,11 @@ pub enum ForwardKind {
} }
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)] #[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
#[serde(untagged)]
pub enum ForwardedFrom { pub enum ForwardedFrom {
User { #[serde(rename = "forward_from")]
#[serde(rename = "forward_from")] User(User),
user: User, #[serde(rename = "forward_sender_name")]
}, SenderName(String),
SenderName {
#[serde(rename = "forward_sender_name")]
sender_name: String,
},
} }
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)] #[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
@ -167,11 +162,11 @@ mod tests {
use serde_json::from_str; use serde_json::from_str;
#[test] #[test]
fn incoming_origin_de() { fn origin_de() {
let expected = Message { let expected = Message {
id: 0, id: 0,
date: 0, date: 0,
chat: Box::new(Chat { chat: Chat {
id: 0, id: 0,
kind: ChatKind::Private { kind: ChatKind::Private {
type_: (), type_: (),
@ -180,7 +175,7 @@ mod tests {
last_name: None, last_name: None,
}, },
photo: None, photo: None,
}), },
message_kind: MessageKind::IncomingMessage { message_kind: MessageKind::IncomingMessage {
forward_kind: ForwardKind::Origin { forward_kind: ForwardKind::Origin {
reply_to_message: None, reply_to_message: None,
@ -193,7 +188,49 @@ mod tests {
reply_markup: None, reply_markup: None,
}, },
}; };
let actual = from_str(r#"{"message_id":0,"date":0,"chat":{"chat_id":0,"type":"private"},"text":"Hello","entities":[]}"#).unwrap(); let actual = from_str::<Message>(r#"{"message_id":0,"date":0,"chat":{"chat_id":0,"type":"private"},"text":"Hello","entities":[]}"#).unwrap();
assert_eq!(expected, actual);
}
#[test]
fn forward_de() {
let expected = Message {
id: 1,
date: 1,
chat: Chat {
id: 1,
kind: ChatKind::Private {
type_: (),
username: None,
first_name: None,
last_name: None,
},
photo: None,
},
message_kind: MessageKind::IncomingMessage {
forward_kind: ForwardKind::NonChannelForward {
date: 1,
from: ForwardedFrom::User(User {
id: 123,
is_bot: false,
first_name: "Name".to_string(),
last_name: None,
username: None,
language_code: None,
}),
},
edit_date: None,
media_kind: MediaKind::Text {
text: "Message".into(),
entities: vec![],
},
reply_markup: None,
},
};
let actual = from_str::<Message>(
r#"{"message_id":1,"date":1,"chat":{"chat_id":1,"type":"private"},"forward_date":1,"forward_from":{"id":123,"is_bot":false,"first_name":"Name"},"text":"Message","entities":[]}"#,
)
.unwrap();
assert_eq!(expected, actual); assert_eq!(expected, actual);
} }
} }