mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Test teloxide-macros with a parse_with
bug fix
This commit is contained in:
parent
d6d819afb8
commit
78afa99b79
2 changed files with 27 additions and 11 deletions
|
@ -58,7 +58,7 @@ full = [
|
|||
|
||||
[dependencies]
|
||||
teloxide-core = { version = "0.7.0", default-features = false }
|
||||
teloxide-macros = { version = "0.6.2", optional = true }
|
||||
# teloxide-macros = { version = "0.6.2", optional = true }
|
||||
|
||||
serde_json = "1.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
@ -67,6 +67,7 @@ dptree = "0.3.0"
|
|||
|
||||
# These lines are used only for development.
|
||||
# teloxide-core = { git = "https://github.com/teloxide/teloxide-core", rev = "b13393d", default-features = false }
|
||||
teloxide-macros = { git = "https://github.com/teloxide/teloxide-macros", rev = "44d91c5", optional = true }
|
||||
# dptree = { git = "https://github.com/teloxide/dptree", rev = "df578e4" }
|
||||
|
||||
tokio = { version = "1.8", features = ["fs"] }
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#![allow(clippy::nonstandard_macro_braces)]
|
||||
|
||||
#[cfg(feature = "macros")]
|
||||
use teloxide::utils::command::{BotCommands, ParseError};
|
||||
use teloxide::utils::command::BotCommands;
|
||||
|
||||
// We put tests here because macro expand in unit tests in module
|
||||
// teloxide::utils::command was a failure
|
||||
|
@ -141,22 +141,33 @@ fn parse_with_split2() {
|
|||
#[test]
|
||||
#[cfg(feature = "macros")]
|
||||
fn parse_custom_parser() {
|
||||
fn custom_parse_function(s: String) -> Result<(u8, String), ParseError> {
|
||||
let vec = s.split_whitespace().collect::<Vec<_>>();
|
||||
let (left, right) = match vec.as_slice() {
|
||||
[l, r] => (l, r),
|
||||
_ => return Err(ParseError::IncorrectFormat("might be 2 arguments!".into())),
|
||||
};
|
||||
left.parse::<u8>()
|
||||
.map(|res| (res, (*right).to_string()))
|
||||
.map_err(|_| ParseError::Custom("First argument must be a integer!".to_owned().into()))
|
||||
mod parser {
|
||||
use teloxide::utils::command::ParseError;
|
||||
|
||||
pub fn custom_parse_function(s: String) -> Result<(u8, String), ParseError> {
|
||||
let vec = s.split_whitespace().collect::<Vec<_>>();
|
||||
let (left, right) = match vec.as_slice() {
|
||||
[l, r] => (l, r),
|
||||
_ => return Err(ParseError::IncorrectFormat("might be 2 arguments!".into())),
|
||||
};
|
||||
left.parse::<u8>().map(|res| (res, (*right).to_string())).map_err(|_| {
|
||||
ParseError::Custom("First argument must be a integer!".to_owned().into())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
use parser::custom_parse_function;
|
||||
|
||||
#[derive(BotCommands, Debug, PartialEq)]
|
||||
#[command(rename = "lowercase")]
|
||||
enum DefaultCommands {
|
||||
#[command(parse_with = "custom_parse_function")]
|
||||
Start(u8, String),
|
||||
|
||||
// Test <https://github.com/teloxide/teloxide/issues/668>.
|
||||
#[command(parse_with = "parser::custom_parse_function")]
|
||||
TestPath(u8, String),
|
||||
|
||||
Help,
|
||||
}
|
||||
|
||||
|
@ -164,6 +175,10 @@ fn parse_custom_parser() {
|
|||
DefaultCommands::Start(10, "hello".to_string()),
|
||||
DefaultCommands::parse("/start 10 hello", "").unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
DefaultCommands::TestPath(10, "hello".to_string()),
|
||||
DefaultCommands::parse("/testpath 10 hello", "").unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in a new issue