1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use core::sync::atomic::{AtomicI32, Ordering};
use chrono::{DateTime, Utc};
use teloxide::types::*;
use super::chat::MockPrivateChat;
use crate::{proc_macros::Changeable, MockUser};
macro_rules! Message {
(
#[derive($($derive:meta),*)]
$pub:vis struct $name:ident {
$($fpub:vis $field:ident : $type:ty,)*
}
) => {
#[derive($($derive),*)]
$pub struct $name { // This is basically a template
pub id: MessageId,
pub thread_id: Option<ThreadId>,
pub from: Option<User>,
pub sender_chat: Option<Chat>,
pub date: DateTime<Utc>,
pub chat: Chat,
pub is_topic_message: bool,
pub via_bot: Option<User>,
pub sender_business_bot: Option<User>,
$($fpub $field : $type,)*
}
impl $name {
pub const ID: i32 = 1;
pub(crate) fn new_message($($field:$type,)*) -> Self{
Self { // To not repeat this over and over again
id: MessageId($name::ID),
thread_id: None,
from: Some(MockUser::new().build()),
sender_chat: None,
date: Utc::now(),
chat: MockPrivateChat::new().build(),
is_topic_message: false,
via_bot: None,
sender_business_bot: None,
$($field,)*
}
}
pub(crate) fn build_message(self, message_kind: MessageKind) -> Message {
Message {
id: self.id,
thread_id: self.thread_id,
from: self.from,
sender_chat: self.sender_chat,
date: self.date,
chat: self.chat,
is_topic_message: self.is_topic_message,
via_bot: self.via_bot,
kind: message_kind,
sender_business_bot: self.sender_business_bot,
}
}
}
impl crate::dataset::IntoUpdate for $name {
/// Converts the mock message into an updates vector
///
/// # Example
/// ```
/// use teloxide_tests::IntoUpdate;
/// use teloxide::types::{UpdateId, UpdateKind::Message};
/// use std::sync::atomic::AtomicI32;
///
/// let mock_message = teloxide_tests::MockMessageText::new();
/// let update = mock_message.clone().into_update(&AtomicI32::new(42))[0].clone();
///
/// assert_eq!(update.id, UpdateId(42));
/// assert_eq!(update.kind, Message(mock_message.build()));
/// ```
///
fn into_update(self, id: &AtomicI32) -> Vec<Update> {
vec![Update {
id: UpdateId(id.fetch_add(1, Ordering::Relaxed) as u32),
kind: UpdateKind::Message(self.build()),
}]
}
}
}
}
pub(crate) use Message;
#[derive(Clone, Debug, PartialEq)]
pub struct MockEditedMessage(Message);
impl MockEditedMessage {
/// Creates a new MockEditedMessage wrapper.
///
/// This is useful for testing the `UpdateKind::EditedMessage` variant.
///
/// # Example
/// ```
/// use chrono::Utc;
///
/// let message = teloxide_tests::MockMessageText::new().edit_date(Utc::now()).build();
/// let edited_message = teloxide_tests::MockEditedMessage::new(message.clone());
/// assert_eq!(edited_message.message(), &message);
pub fn new(mut message: Message) -> Self {
if let MessageKind::Common(ref mut common) = message.kind {
common.edit_date = common.edit_date.or(Some(Utc::now()));
}
Self(message)
}
pub fn message(&self) -> &Message {
&self.0
}
}
impl crate::dataset::IntoUpdate for MockEditedMessage {
/// Converts the edited Message into an updates vector
///
/// # Example
/// ```
/// use chrono::Utc;
/// use teloxide_tests::IntoUpdate;
/// use teloxide::types::{UpdateId, UpdateKind};
/// use std::sync::atomic::AtomicI32;
///
/// let message = teloxide_tests::MockMessageText::new().edit_date(Utc::now()).build();
/// let edited_message = teloxide_tests::MockEditedMessage::new(message.clone());
/// let update = edited_message.into_update(&AtomicI32::new(42))[0].clone();
///
/// assert_eq!(update.id, UpdateId(42));
/// assert_eq!(update.kind, UpdateKind::EditedMessage(message));
/// ```
///
fn into_update(self, id: &AtomicI32) -> Vec<Update> {
vec![Update {
id: UpdateId(id.fetch_add(1, Ordering::Relaxed) as u32),
kind: UpdateKind::EditedMessage(self.0),
}]
}
}
// More messages like Webapp data is needed
Message! {
#[derive(Changeable, Clone)]
pub struct MockMessageDice {
pub value: u8,
pub emoji: DiceEmoji,
}
}
impl MockMessageDice {
pub const VALUE: u8 = 1;
pub const EMOJI: DiceEmoji = DiceEmoji::Dice;
/// Creates a new easily changable message dice builder
///
/// # Example
/// ```
/// let message = teloxide_tests::MockMessageDice::new()
/// .value(2)
/// .build();
/// assert_eq!(message.dice().unwrap().value, 2);
/// ```
///
pub fn new() -> Self {
Self::new_message(Self::VALUE, Self::EMOJI)
}
/// Builds the message dice
///
/// # Example
/// ```
/// let mock_message = teloxide_tests::MockMessageDice::new();
/// let message = mock_message.build();
/// assert_eq!(message.dice().unwrap().emoji, teloxide_tests::MockMessageDice::EMOJI); // EMOJI is a default value
/// ```
///
pub fn build(self) -> Message {
self.clone().build_message(MessageKind::Dice(MessageDice {
dice: Dice {
emoji: self.emoji,
value: self.value,
},
}))
}
}
Message! {
#[derive(Changeable, Clone)]
pub struct MockMessageInvoice {
pub title: String,
pub description: String,
pub start_parameter: String,
pub currency: String,
pub total_amount: u32,
}
}
impl MockMessageInvoice {
pub const TITLE: &'static str = "Title of Invoice";
pub const DESCRIPTION: &'static str = "Description of Invoice";
pub const START_PARAMETER: &'static str = "Start parameter of Invoice";
pub const CURRENCY: &'static str = "XTR";
pub const TOTAL_AMOUNT: u32 = 0;
/// Creates a new easily changable message invoice builder
///
/// # Example
/// ```
/// let message = teloxide_tests::MockMessageInvoice::new()
/// .title("Some title")
/// .build();
/// assert_eq!(message.invoice().unwrap().title, "Some title".to_owned());
/// ```
///
pub fn new() -> Self {
Self::new_message(
Self::TITLE.to_owned(),
Self::DESCRIPTION.to_owned(),
Self::START_PARAMETER.to_owned(),
Self::CURRENCY.to_owned(),
Self::TOTAL_AMOUNT,
)
}
/// Builds the message dice
///
/// # Example
/// ```
/// let mock_message = teloxide_tests::MockMessageInvoice::new();
/// let message = mock_message.build();
/// assert_eq!(message.invoice().unwrap().currency, teloxide_tests::MockMessageInvoice::CURRENCY); // CURRENCY is a default value
/// ```
///
pub fn build(self) -> Message {
self.clone()
.build_message(MessageKind::Invoice(MessageInvoice {
invoice: Invoice {
title: self.title,
description: self.description,
start_parameter: self.start_parameter,
currency: self.currency,
total_amount: self.total_amount,
},
}))
}
}
Message! {
#[derive(Changeable, Clone)]
pub struct MockMessageNewChatMembers {
pub new_chat_members: Vec<User>,
}
}
impl MockMessageNewChatMembers {
/// Creates a new easily changeable new chat member message builder
///
/// # Example
/// ```
/// let message = teloxide_tests::MockMessageNewChatMembers::new()
/// .new_chat_members(vec![teloxide_tests::MockUser::new().id(123).build()])
/// .build();
/// assert_eq!(message.new_chat_members().unwrap()[0].id.0, 123);
pub fn new() -> Self {
Self::new_message(vec![MockUser::new().build()])
}
/// Builds the new chat member message
///
/// # Example
/// ```
/// let mock_message = teloxide_tests::MockMessageNewChatMembers::new();
/// let message = mock_message.build();
/// assert_eq!(message.new_chat_members().unwrap().len(), 1); // Contains a single MockUser by default
/// ```
///
pub fn build(self) -> Message {
self.clone()
.build_message(MessageKind::NewChatMembers(MessageNewChatMembers {
new_chat_members: self.new_chat_members,
}))
}
}