mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-21 14:29:01 +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]
|
[dependencies]
|
||||||
teloxide-core = { version = "0.7.0", default-features = false }
|
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_json = "1.0"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
@ -67,6 +67,7 @@ dptree = "0.3.0"
|
||||||
|
|
||||||
# These lines are used only for development.
|
# These lines are used only for development.
|
||||||
# teloxide-core = { git = "https://github.com/teloxide/teloxide-core", rev = "b13393d", default-features = false }
|
# 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" }
|
# dptree = { git = "https://github.com/teloxide/dptree", rev = "df578e4" }
|
||||||
|
|
||||||
tokio = { version = "1.8", features = ["fs"] }
|
tokio = { version = "1.8", features = ["fs"] }
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
#![allow(clippy::nonstandard_macro_braces)]
|
#![allow(clippy::nonstandard_macro_braces)]
|
||||||
|
|
||||||
#[cfg(feature = "macros")]
|
#[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
|
// We put tests here because macro expand in unit tests in module
|
||||||
// teloxide::utils::command was a failure
|
// teloxide::utils::command was a failure
|
||||||
|
@ -141,22 +141,33 @@ fn parse_with_split2() {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "macros")]
|
#[cfg(feature = "macros")]
|
||||||
fn parse_custom_parser() {
|
fn parse_custom_parser() {
|
||||||
fn custom_parse_function(s: String) -> Result<(u8, String), ParseError> {
|
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 vec = s.split_whitespace().collect::<Vec<_>>();
|
||||||
let (left, right) = match vec.as_slice() {
|
let (left, right) = match vec.as_slice() {
|
||||||
[l, r] => (l, r),
|
[l, r] => (l, r),
|
||||||
_ => return Err(ParseError::IncorrectFormat("might be 2 arguments!".into())),
|
_ => return Err(ParseError::IncorrectFormat("might be 2 arguments!".into())),
|
||||||
};
|
};
|
||||||
left.parse::<u8>()
|
left.parse::<u8>().map(|res| (res, (*right).to_string())).map_err(|_| {
|
||||||
.map(|res| (res, (*right).to_string()))
|
ParseError::Custom("First argument must be a integer!".to_owned().into())
|
||||||
.map_err(|_| ParseError::Custom("First argument must be a integer!".to_owned().into()))
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
use parser::custom_parse_function;
|
||||||
|
|
||||||
#[derive(BotCommands, Debug, PartialEq)]
|
#[derive(BotCommands, Debug, PartialEq)]
|
||||||
#[command(rename = "lowercase")]
|
#[command(rename = "lowercase")]
|
||||||
enum DefaultCommands {
|
enum DefaultCommands {
|
||||||
#[command(parse_with = "custom_parse_function")]
|
#[command(parse_with = "custom_parse_function")]
|
||||||
Start(u8, String),
|
Start(u8, String),
|
||||||
|
|
||||||
|
// Test <https://github.com/teloxide/teloxide/issues/668>.
|
||||||
|
#[command(parse_with = "parser::custom_parse_function")]
|
||||||
|
TestPath(u8, String),
|
||||||
|
|
||||||
Help,
|
Help,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,6 +175,10 @@ fn parse_custom_parser() {
|
||||||
DefaultCommands::Start(10, "hello".to_string()),
|
DefaultCommands::Start(10, "hello".to_string()),
|
||||||
DefaultCommands::parse("/start 10 hello", "").unwrap()
|
DefaultCommands::parse("/start 10 hello", "").unwrap()
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
DefaultCommands::TestPath(10, "hello".to_string()),
|
||||||
|
DefaultCommands::parse("/testpath 10 hello", "").unwrap()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Reference in a new issue