teloxide/tests/command.rs

299 lines
7.7 KiB
Rust
Raw Normal View History

2022-09-29 12:08:27 +02:00
//! Test for `teloxide-macros`
2022-10-03 14:28:45 +02:00
use teloxide_macros::BotCommands;
// Import only trait _methods_, such that we can call `parse`, but we also test
2022-10-03 16:28:57 +02:00
// that proc macros work without the trait being imported.
2022-10-03 14:28:45 +02:00
use teloxide::utils::command::BotCommands as _;
2022-09-29 12:08:27 +02:00
#[test]
fn parse_command_with_args() {
#[derive(BotCommands, Debug, PartialEq)]
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "lowercase")]
2022-09-29 12:08:27 +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)
}
#[test]
fn parse_command_with_non_string_arg() {
#[derive(BotCommands, Debug, PartialEq)]
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "lowercase")]
2022-09-29 12:08:27 +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)
}
#[test]
fn attribute_prefix() {
#[derive(BotCommands, Debug, PartialEq)]
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "lowercase")]
2022-09-29 12:08:27 +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]
fn many_attributes() {
#[derive(BotCommands, Debug, PartialEq)]
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "lowercase")]
2022-09-29 12:08:27 +02:00
enum DefaultCommands {
#[command(prefix = "!", description = "desc")]
Start,
Help,
}
assert_eq!(
DefaultCommands::Start,
DefaultCommands::parse("!start", "").unwrap()
);
assert_eq!(
DefaultCommands::descriptions().to_string(),
"!start — desc\n/help"
);
}
#[test]
fn global_attributes() {
#[derive(BotCommands, Debug, PartialEq)]
2022-10-02 14:24:28 +02:00
#[command(
prefix = "!",
rename_rule = "lowercase",
description = "Bot commands"
)]
2022-09-29 12:08:27 +02:00
enum DefaultCommands {
#[command(prefix = "/")]
Start,
Help,
}
assert_eq!(
DefaultCommands::Start,
DefaultCommands::parse("/start", "MyNameBot").unwrap()
);
assert_eq!(
DefaultCommands::Help,
DefaultCommands::parse("!help", "MyNameBot").unwrap()
);
assert_eq!(
DefaultCommands::descriptions().to_string(),
"Bot commands\n\n/start\n!help"
);
}
#[test]
fn parse_command_with_bot_name() {
#[derive(BotCommands, Debug, PartialEq)]
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "lowercase")]
2022-09-29 12:08:27 +02:00
enum DefaultCommands {
#[command(prefix = "/")]
Start,
Help,
}
assert_eq!(
DefaultCommands::Start,
DefaultCommands::parse("/start@MyNameBot", "MyNameBot").unwrap()
);
}
#[test]
fn parse_with_split() {
#[derive(BotCommands, Debug, PartialEq)]
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "lowercase")]
2022-09-29 12:08:27 +02:00
#[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]
fn parse_with_split2() {
#[derive(BotCommands, Debug, PartialEq)]
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "lowercase")]
2022-09-29 12:08:27 +02:00
#[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]
fn parse_custom_parser() {
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)]
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "lowercase")]
2022-09-29 12:08:27 +02:00
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,
}
assert_eq!(
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]
fn parse_named_fields() {
#[derive(BotCommands, Debug, PartialEq)]
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "lowercase")]
2022-09-29 12:08:27 +02:00
#[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()
);
}
#[test]
fn descriptions_off() {
#[derive(BotCommands, Debug, PartialEq)]
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "lowercase")]
2022-09-29 12:08:27 +02:00
enum DefaultCommands {
#[command(description = "off")]
Start,
Help,
}
assert_eq!(DefaultCommands::descriptions().to_string(), "/help".to_owned());
}
#[test]
fn rename_rules() {
#[derive(BotCommands, Debug, PartialEq)]
enum DefaultCommands {
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "lowercase")]
2022-09-29 12:08:27 +02:00
AaaAaa,
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "UPPERCASE")]
2022-09-29 12:08:27 +02:00
BbbBbb,
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "PascalCase")]
2022-09-29 12:08:27 +02:00
CccCcc,
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "camelCase")]
2022-09-29 12:08:27 +02:00
DddDdd,
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "snake_case")]
2022-09-29 12:08:27 +02:00
EeeEee,
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "SCREAMING_SNAKE_CASE")]
2022-09-29 12:08:27 +02:00
FffFff,
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "kebab-case")]
2022-09-29 12:08:27 +02:00
GggGgg,
2022-10-02 14:24:28 +02:00
#[command(rename_rule = "SCREAMING-KEBAB-CASE")]
2022-09-29 12:08:27 +02:00
HhhHhh,
2022-10-02 16:13:54 +02:00
#[command(rename = "Bar")]
Foo,
2022-09-29 12:08:27 +02: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-10-02 16:13:54 +02:00
assert_eq!(
DefaultCommands::Foo,
DefaultCommands::parse("/Bar", "").unwrap()
);
2022-09-29 12:08:27 +02:00
assert_eq!(
"/aaaaaa\n/BBBBBB\n/CccCcc\n/dddDdd\n/eee_eee\n/FFF_FFF\n/ggg-ggg\n/\
2022-10-02 16:13:54 +02:00
HHH-HHH\n/Bar",
2022-09-29 12:08:27 +02:00
DefaultCommands::descriptions().to_string()
);
}