2021-07-03 23:22:11 +02:00
|
|
|
// https://github.com/rust-lang/rust-clippy/issues/7422
|
|
|
|
#![allow(clippy::nonstandard_macro_braces)]
|
|
|
|
|
2020-10-02 11:13:27 +02:00
|
|
|
#[cfg(feature = "macros")]
|
2022-03-13 16:37:11 +01:00
|
|
|
use teloxide::utils::command::{BotCommands, ParseError};
|
2020-06-06 19:14:42 +02:00
|
|
|
|
2020-06-06 20:32:51 +02:00
|
|
|
// We put tests here because macro expand in unit tests in module
|
|
|
|
// teloxide::utils::command was a failure
|
2020-06-06 19:14:42 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-10-02 11:13:27 +02:00
|
|
|
#[cfg(feature = "macros")]
|
2020-06-06 19:14:42 +02:00
|
|
|
fn parse_command_with_args() {
|
2022-03-13 16:37:11 +01:00
|
|
|
#[derive(BotCommands, Debug, PartialEq)]
|
2021-03-13 18:05:50 +01:00
|
|
|
#[command(rename = "lowercase")]
|
2020-06-06 19:14:42 +02:00
|
|
|
enum DefaultCommands {
|
|
|
|
Start(String),
|
|
|
|
Help,
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = "/start arg1 arg2";
|
|
|
|
let expected = DefaultCommands::Start("arg1 arg2".to_string());
|
|
|
|
let actual = DefaultCommands::parse(data, "").unwrap();
|
|
|
|
assert_eq!(actual, expected)
|
|
|
|
}
|
|
|
|
|
2020-08-28 23:33:16 +02:00
|
|
|
#[test]
|
2020-10-02 11:13:27 +02:00
|
|
|
#[cfg(feature = "macros")]
|
2020-08-28 23:33:16 +02:00
|
|
|
fn parse_command_with_non_string_arg() {
|
2022-03-13 16:37:11 +01:00
|
|
|
#[derive(BotCommands, Debug, PartialEq)]
|
2021-03-13 18:05:50 +01:00
|
|
|
#[command(rename = "lowercase")]
|
2020-08-28 23:33:16 +02:00
|
|
|
enum DefaultCommands {
|
|
|
|
Start(i32),
|
|
|
|
Help,
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = "/start -50";
|
|
|
|
let expected = DefaultCommands::Start("-50".parse().unwrap());
|
|
|
|
let actual = DefaultCommands::parse(data, "").unwrap();
|
|
|
|
assert_eq!(actual, expected)
|
|
|
|
}
|
|
|
|
|
2020-06-06 19:14:42 +02:00
|
|
|
#[test]
|
2020-10-02 11:13:27 +02:00
|
|
|
#[cfg(feature = "macros")]
|
2020-06-06 19:14:42 +02:00
|
|
|
fn attribute_prefix() {
|
2022-03-13 16:37:11 +01:00
|
|
|
#[derive(BotCommands, Debug, PartialEq)]
|
2021-03-13 18:05:50 +01:00
|
|
|
#[command(rename = "lowercase")]
|
2020-06-06 19:14:42 +02:00
|
|
|
enum DefaultCommands {
|
|
|
|
#[command(prefix = "!")]
|
|
|
|
Start(String),
|
|
|
|
Help,
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = "!start arg1 arg2";
|
|
|
|
let expected = DefaultCommands::Start("arg1 arg2".to_string());
|
|
|
|
let actual = DefaultCommands::parse(data, "").unwrap();
|
|
|
|
assert_eq!(actual, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-10-02 11:13:27 +02:00
|
|
|
#[cfg(feature = "macros")]
|
2020-06-06 19:14:42 +02:00
|
|
|
fn many_attributes() {
|
2022-03-13 16:37:11 +01:00
|
|
|
#[derive(BotCommands, Debug, PartialEq)]
|
2021-03-15 04:22:10 +01:00
|
|
|
#[command(rename = "lowercase")]
|
2020-06-06 19:14:42 +02:00
|
|
|
enum DefaultCommands {
|
|
|
|
#[command(prefix = "!", description = "desc")]
|
|
|
|
Start,
|
|
|
|
Help,
|
|
|
|
}
|
|
|
|
|
2020-07-26 19:47:02 +02:00
|
|
|
assert_eq!(DefaultCommands::Start, DefaultCommands::parse("!start", "").unwrap());
|
2022-03-14 15:11:57 +01:00
|
|
|
assert_eq!(DefaultCommands::descriptions().to_string(), "!start — desc\n/help");
|
2020-06-06 19:14:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-10-02 11:13:27 +02:00
|
|
|
#[cfg(feature = "macros")]
|
2020-06-06 19:14:42 +02:00
|
|
|
fn global_attributes() {
|
2022-03-13 16:37:11 +01:00
|
|
|
#[derive(BotCommands, Debug, PartialEq)]
|
2021-03-15 04:22:10 +01:00
|
|
|
#[command(prefix = "!", rename = "lowercase", description = "Bot commands")]
|
2020-06-06 19:14:42 +02:00
|
|
|
enum DefaultCommands {
|
|
|
|
#[command(prefix = "/")]
|
|
|
|
Start,
|
|
|
|
Help,
|
|
|
|
}
|
|
|
|
|
2020-07-26 19:47:02 +02:00
|
|
|
assert_eq!(DefaultCommands::Start, DefaultCommands::parse("/start", "MyNameBot").unwrap());
|
|
|
|
assert_eq!(DefaultCommands::Help, DefaultCommands::parse("!help", "MyNameBot").unwrap());
|
2022-03-14 15:11:57 +01:00
|
|
|
assert_eq!(DefaultCommands::descriptions().to_string(), "Bot commands\n\n/start\n!help");
|
2020-06-06 19:14:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-10-02 11:13:27 +02:00
|
|
|
#[cfg(feature = "macros")]
|
2020-06-06 19:14:42 +02:00
|
|
|
fn parse_command_with_bot_name() {
|
2022-03-13 16:37:11 +01:00
|
|
|
#[derive(BotCommands, Debug, PartialEq)]
|
2021-03-15 04:22:10 +01:00
|
|
|
#[command(rename = "lowercase")]
|
2020-06-06 19:14:42 +02:00
|
|
|
enum DefaultCommands {
|
|
|
|
#[command(prefix = "/")]
|
|
|
|
Start,
|
|
|
|
Help,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
DefaultCommands::Start,
|
|
|
|
DefaultCommands::parse("/start@MyNameBot", "MyNameBot").unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-10-02 11:13:27 +02:00
|
|
|
#[cfg(feature = "macros")]
|
2020-06-06 19:14:42 +02:00
|
|
|
fn parse_with_split() {
|
2022-03-13 16:37:11 +01:00
|
|
|
#[derive(BotCommands, Debug, PartialEq)]
|
2020-06-06 19:14:42 +02:00
|
|
|
#[command(rename = "lowercase")]
|
|
|
|
#[command(parse_with = "split")]
|
|
|
|
enum DefaultCommands {
|
|
|
|
Start(u8, String),
|
|
|
|
Help,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
DefaultCommands::Start(10, "hello".to_string()),
|
|
|
|
DefaultCommands::parse("/start 10 hello", "").unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-10-02 11:13:27 +02:00
|
|
|
#[cfg(feature = "macros")]
|
2020-06-06 19:14:42 +02:00
|
|
|
fn parse_with_split2() {
|
2022-03-13 16:37:11 +01:00
|
|
|
#[derive(BotCommands, Debug, PartialEq)]
|
2020-06-06 19:14:42 +02:00
|
|
|
#[command(rename = "lowercase")]
|
|
|
|
#[command(parse_with = "split", separator = "|")]
|
|
|
|
enum DefaultCommands {
|
|
|
|
Start(u8, String),
|
|
|
|
Help,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
DefaultCommands::Start(10, "hello".to_string()),
|
|
|
|
DefaultCommands::parse("/start 10|hello", "").unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-10-02 11:13:27 +02:00
|
|
|
#[cfg(feature = "macros")]
|
2020-06-06 19:14:42 +02:00
|
|
|
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),
|
2020-07-26 19:47:02 +02:00
|
|
|
_ => return Err(ParseError::IncorrectFormat("might be 2 arguments!".into())),
|
2020-06-06 19:14:42 +02:00
|
|
|
};
|
2020-07-26 19:47:02 +02:00
|
|
|
left.parse::<u8>()
|
|
|
|
.map(|res| (res, (*right).to_string()))
|
|
|
|
.map_err(|_| ParseError::Custom("First argument must be a integer!".to_owned().into()))
|
2020-06-06 19:14:42 +02:00
|
|
|
}
|
|
|
|
|
2022-03-13 16:37:11 +01:00
|
|
|
#[derive(BotCommands, Debug, PartialEq)]
|
2021-03-13 18:05:50 +01:00
|
|
|
#[command(rename = "lowercase")]
|
2020-06-06 19:14:42 +02:00
|
|
|
enum DefaultCommands {
|
|
|
|
#[command(parse_with = "custom_parse_function")]
|
|
|
|
Start(u8, String),
|
|
|
|
Help,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
DefaultCommands::Start(10, "hello".to_string()),
|
|
|
|
DefaultCommands::parse("/start 10 hello", "").unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-10-02 11:13:27 +02:00
|
|
|
#[cfg(feature = "macros")]
|
2020-06-06 19:14:42 +02:00
|
|
|
fn parse_named_fields() {
|
2022-03-13 16:37:11 +01:00
|
|
|
#[derive(BotCommands, Debug, PartialEq)]
|
2020-06-06 19:14:42 +02:00
|
|
|
#[command(rename = "lowercase")]
|
|
|
|
#[command(parse_with = "split")]
|
|
|
|
enum DefaultCommands {
|
|
|
|
Start { num: u8, data: String },
|
|
|
|
Help,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
DefaultCommands::Start { num: 10, data: "hello".to_string() },
|
|
|
|
DefaultCommands::parse("/start 10 hello", "").unwrap()
|
|
|
|
);
|
|
|
|
}
|
2020-07-04 09:44:42 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-10-02 11:13:27 +02:00
|
|
|
#[cfg(feature = "macros")]
|
2020-07-04 09:44:42 +02:00
|
|
|
fn descriptions_off() {
|
2022-03-13 16:37:11 +01:00
|
|
|
#[derive(BotCommands, Debug, PartialEq)]
|
2021-03-13 18:05:50 +01:00
|
|
|
#[command(rename = "lowercase")]
|
2020-07-04 09:44:42 +02:00
|
|
|
enum DefaultCommands {
|
|
|
|
#[command(description = "off")]
|
|
|
|
Start,
|
|
|
|
Help,
|
|
|
|
}
|
|
|
|
|
2022-03-14 15:11:57 +01:00
|
|
|
assert_eq!(DefaultCommands::descriptions().to_string(), "/help".to_owned());
|
2020-07-04 09:44:42 +02:00
|
|
|
}
|
2022-03-23 13:23:39 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "macros")]
|
|
|
|
fn rename_rules() {
|
2022-03-27 11:47:24 +02:00
|
|
|
#[derive(BotCommands, Debug, PartialEq)]
|
2022-03-23 13:23:39 +01:00
|
|
|
enum DefaultCommands {
|
|
|
|
#[command(rename = "lowercase")]
|
|
|
|
AaaAaa,
|
|
|
|
#[command(rename = "UPPERCASE")]
|
|
|
|
BbbBbb,
|
|
|
|
#[command(rename = "PascalCase")]
|
|
|
|
CccCcc,
|
|
|
|
#[command(rename = "camelCase")]
|
|
|
|
DddDdd,
|
|
|
|
#[command(rename = "snake_case")]
|
|
|
|
EeeEee,
|
|
|
|
#[command(rename = "SCREAMING_SNAKE_CASE")]
|
|
|
|
FffFff,
|
|
|
|
#[command(rename = "kebab-case")]
|
|
|
|
GggGgg,
|
|
|
|
#[command(rename = "SCREAMING-KEBAB-CASE")]
|
|
|
|
HhhHhh,
|
2022-05-26 19:48:43 +02:00
|
|
|
#[command(rename = "Bar")]
|
|
|
|
Foo,
|
2022-03-23 13:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(DefaultCommands::AaaAaa, DefaultCommands::parse("/aaaaaa", "").unwrap());
|
|
|
|
assert_eq!(DefaultCommands::BbbBbb, DefaultCommands::parse("/BBBBBB", "").unwrap());
|
|
|
|
assert_eq!(DefaultCommands::CccCcc, DefaultCommands::parse("/CccCcc", "").unwrap());
|
|
|
|
assert_eq!(DefaultCommands::DddDdd, DefaultCommands::parse("/dddDdd", "").unwrap());
|
|
|
|
assert_eq!(DefaultCommands::EeeEee, DefaultCommands::parse("/eee_eee", "").unwrap());
|
|
|
|
assert_eq!(DefaultCommands::FffFff, DefaultCommands::parse("/FFF_FFF", "").unwrap());
|
|
|
|
assert_eq!(DefaultCommands::GggGgg, DefaultCommands::parse("/ggg-ggg", "").unwrap());
|
|
|
|
assert_eq!(DefaultCommands::HhhHhh, DefaultCommands::parse("/HHH-HHH", "").unwrap());
|
2022-05-26 19:48:43 +02:00
|
|
|
assert_eq!(DefaultCommands::Foo, DefaultCommands::parse("/Bar", "").unwrap());
|
2022-03-23 13:23:39 +01:00
|
|
|
|
|
|
|
assert_eq!(
|
2022-05-26 19:48:43 +02:00
|
|
|
"/aaaaaa\n/BBBBBB\n/CccCcc\n/dddDdd\n/eee_eee\n/FFF_FFF\n/ggg-ggg\n/HHH-HHH\n/Bar",
|
2022-03-27 11:47:24 +02:00
|
|
|
DefaultCommands::descriptions().to_string()
|
2022-03-23 13:23:39 +01:00
|
|
|
);
|
|
|
|
}
|