mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-24 09:16:12 +01:00
Add tests for MessageEntity
parsing
This commit is contained in:
parent
06c265cd11
commit
312af99aa0
2 changed files with 131 additions and 0 deletions
|
@ -52,6 +52,7 @@ vecrem = { version = "0.1", optional = true }
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
pretty_env_logger = "0.4"
|
pretty_env_logger = "0.4"
|
||||||
tokio = { version = "1.8.0", features = ["fs", "macros"] }
|
tokio = { version = "1.8.0", features = ["fs", "macros"] }
|
||||||
|
cool_asserts = "2.0.3"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["native-tls"]
|
default = ["native-tls"]
|
||||||
|
|
|
@ -27,6 +27,7 @@ pub struct MessageEntity {
|
||||||
/// mostly work with UTF-**8**. In order to use an entity we need to convert
|
/// mostly work with UTF-**8**. In order to use an entity we need to convert
|
||||||
/// UTF-16 offsets to UTF-8 ones. This type represents a message entity with
|
/// UTF-16 offsets to UTF-8 ones. This type represents a message entity with
|
||||||
/// converted offsets and a reference to the text.
|
/// converted offsets and a reference to the text.
|
||||||
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||||
pub struct MessageEntityRef<'a> {
|
pub struct MessageEntityRef<'a> {
|
||||||
message: &'a str,
|
message: &'a str,
|
||||||
range: Range<usize>,
|
range: Range<usize>,
|
||||||
|
@ -267,6 +268,9 @@ pub enum MessageEntityKind {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use cool_asserts::assert_matches;
|
||||||
|
use MessageEntity;
|
||||||
|
use MessageEntityKind::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn recursive_kind() {
|
fn recursive_kind() {
|
||||||
|
@ -320,4 +324,130 @@ mod tests {
|
||||||
None
|
None
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_быба() {
|
||||||
|
let parsed = MessageEntityRef::parse(
|
||||||
|
"быба",
|
||||||
|
&[
|
||||||
|
MessageEntity {
|
||||||
|
kind: Strikethrough,
|
||||||
|
offset: 0,
|
||||||
|
length: 1,
|
||||||
|
},
|
||||||
|
MessageEntity {
|
||||||
|
kind: Bold,
|
||||||
|
offset: 1,
|
||||||
|
length: 1,
|
||||||
|
},
|
||||||
|
MessageEntity {
|
||||||
|
kind: Italic,
|
||||||
|
offset: 2,
|
||||||
|
length: 1,
|
||||||
|
},
|
||||||
|
MessageEntity {
|
||||||
|
kind: Code,
|
||||||
|
offset: 3,
|
||||||
|
length: 1,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_matches!(
|
||||||
|
parsed,
|
||||||
|
[
|
||||||
|
entity if entity.text() == "б" && entity.kind() == &Strikethrough,
|
||||||
|
entity if entity.text() == "ы" && entity.kind() == &Bold,
|
||||||
|
entity if entity.text() == "б" && entity.kind() == &Italic,
|
||||||
|
entity if entity.text() == "а" && entity.kind() == &Code,
|
||||||
|
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_symbol_24bit() {
|
||||||
|
let parsed = MessageEntityRef::parse(
|
||||||
|
"xx আ #tt",
|
||||||
|
&[MessageEntity {
|
||||||
|
kind: Hashtag,
|
||||||
|
offset: 5,
|
||||||
|
length: 3,
|
||||||
|
}],
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_matches!(
|
||||||
|
parsed,
|
||||||
|
[entity if entity.text() == "#tt" && entity.kind() == &Hashtag]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_enclosed() {
|
||||||
|
let parsed = MessageEntityRef::parse(
|
||||||
|
"b i b",
|
||||||
|
// For some reason this is how telegram encodes <b>b <i>i<i/> b<b/>
|
||||||
|
&[
|
||||||
|
MessageEntity {
|
||||||
|
kind: Bold,
|
||||||
|
offset: 0,
|
||||||
|
length: 2,
|
||||||
|
},
|
||||||
|
MessageEntity {
|
||||||
|
kind: Bold,
|
||||||
|
offset: 2,
|
||||||
|
length: 3,
|
||||||
|
},
|
||||||
|
MessageEntity {
|
||||||
|
kind: Italic,
|
||||||
|
offset: 2,
|
||||||
|
length: 1,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_matches!(
|
||||||
|
parsed,
|
||||||
|
[
|
||||||
|
entity if entity.text() == "b " && entity.kind() == &Bold,
|
||||||
|
entity if entity.text() == "i b" && entity.kind() == &Bold,
|
||||||
|
entity if entity.text() == "i" && entity.kind() == &Italic,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_nothing() {
|
||||||
|
let parsed = MessageEntityRef::parse("a", &[]);
|
||||||
|
assert_eq!(parsed, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_empty() {
|
||||||
|
// It should be impossible for this to be returned from telegram, but just to be
|
||||||
|
// sure
|
||||||
|
let parsed = MessageEntityRef::parse(
|
||||||
|
"",
|
||||||
|
&[
|
||||||
|
MessageEntity {
|
||||||
|
kind: Bold,
|
||||||
|
offset: 0,
|
||||||
|
length: 0,
|
||||||
|
},
|
||||||
|
MessageEntity {
|
||||||
|
kind: Italic,
|
||||||
|
offset: 0,
|
||||||
|
length: 0,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_matches!(
|
||||||
|
parsed,
|
||||||
|
[
|
||||||
|
entity if entity.text() == "" && entity.kind() == &Bold,
|
||||||
|
entity if entity.text() == "" && entity.kind() == &Italic,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue