From 78afa99b794d72405566b4ce120005630a6796f6 Mon Sep 17 00:00:00 2001 From: Hirrolot Date: Tue, 19 Jul 2022 21:47:09 +0600 Subject: [PATCH] Test teloxide-macros with a `parse_with` bug fix --- Cargo.toml | 3 ++- tests/command.rs | 35 +++++++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e5f1364b..d7b160ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/tests/command.rs b/tests/command.rs index b3e6609e..4b59ac48 100644 --- a/tests/command.rs +++ b/tests/command.rs @@ -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::>(); - let (left, right) = match vec.as_slice() { - [l, r] => (l, r), - _ => return Err(ParseError::IncorrectFormat("might be 2 arguments!".into())), - }; - left.parse::() - .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::>(); + let (left, right) = match vec.as_slice() { + [l, r] => (l, r), + _ => return Err(ParseError::IncorrectFormat("might be 2 arguments!".into())), + }; + left.parse::().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 . + #[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]