#[test]
fn event() {
use crate::alt::ColoredAlt;
use crate::event::*;
use crate::value_map;
use crate::prelude::WalleError;
fn test<T>(event: (&str, Event, T))
where
T: TryFrom<Event, Error = WalleError> + std::fmt::Debug + PartialEq,
{
assert_eq!(serde_json::from_str::<Event>(event.0).unwrap(), event.1);
assert_eq!(
serde_json::from_str::<Event>(&serde_json::to_string(&event.1).unwrap()).unwrap(),
event.1
);
assert_eq!(T::try_from(event.1.clone()).unwrap(), event.2);
println!("{}", event.1.colored_alt());
}
test((
r#"{
"id": "b6e65187-5ac0-489c-b431-53078e9d2bbb",
"impl": "go_onebot_qq",
"platform": "qq",
"self_id": "123234",
"time": 1632847927,
"type": "message",
"detail_type": "private",
"sub_type": "",
"message_id": "6283",
"message": [
{
"type": "text",
"data": {
"text": "OneBot is not a bot"
}
},
{
"type": "image",
"data": {
"file_id": "e30f9684-3d54-4f65-b2da-db291a477f16"
}
}
],
"alt_message": "OneBot is not a bot[图片]",
"user_id": "123456788"
}"#,
Event {
id: "b6e65187-5ac0-489c-b431-53078e9d2bbb".to_string(),
implt: "go_onebot_qq".to_string(),
platform: "qq".to_string(),
self_id: "123234".to_string(),
time: 1632847927.0,
ty: "message".to_string(),
detail_type: "private".to_string(),
sub_type: "".to_string(),
extra: value_map! {
"message_id": "6283",
"message": [
{
"type": "text",
"data": {
"text": "OneBot is not a bot"
}
},
{
"type": "image",
"data": {
"file_id": "e30f9684-3d54-4f65-b2da-db291a477f16"
}
}
],
"alt_message": "OneBot is not a bot[图片]",
"user_id": "123456788"
},
},
new_event(
"b6e65187-5ac0-489c-b431-53078e9d2bbb".to_string(),
1632847927.0,
"123234".to_string(),
Message {
message: vec![
crate::segment::MessageSegment {
ty: "text".to_string(),
data: value_map! {
"text": "OneBot is not a bot"
},
},
crate::segment::MessageSegment {
ty: "image".to_string(),
data: value_map! {
"file_id": "e30f9684-3d54-4f65-b2da-db291a477f16"
},
},
],
message_id: "6283".to_string(),
alt_message: "OneBot is not a bot[图片]".to_string(),
user_id: "123456788".to_string(),
},
Private {},
(),
(),
(),
value_map!(),
),
));
}
#[test]
fn message() {
use crate::segment::*;
use crate::prelude::WalleError;
use crate::util::value::Value;
use crate::{value_map, value};
fn test<T>(message: (Value, MessageSegment, T))
where
T: TryFrom<MessageSegment, Error = WalleError> + std::fmt::Debug + PartialEq,
{
assert_eq!(MessageSegment::try_from(message.0).unwrap(), message.1);
assert_eq!(T::try_from(message.1.clone()).unwrap(), message.2);
println!("{}", message.1.alt());
}
test((
value!({"type": "text",
"data": {
"text": "这是一个纯文本消息段"
}
}),
MessageSegment {
ty: "text".to_string(),
data: value_map! {
"text": "这是一个纯文本消息段"
},
},
Text {
text: "这是一个纯文本消息段".to_string(),
},
));
test((
value!({"type": "image",
"data": {
"file_id": "e30f9684-3d54-4f65-b2da-db291a477f16",
"url": "https://example.com"
}
}),
MessageSegment {
ty: "image".to_string(),
data: value_map! {
"file_id": "e30f9684-3d54-4f65-b2da-db291a477f16",
"url": "https://example.com"
},
},
BaseSegment {
segment: Image {
file_id: "e30f9684-3d54-4f65-b2da-db291a477f16".to_string(),
},
extra: value_map! {
"url": "https://example.com"
},
},
));
}
#[test]
fn extendedmap_test() {
use crate::util::{ValueMap, Value};
let mut map = ValueMap::new();
map.insert("key1".to_owned(), Value::Null);
println!("{}", serde_json::to_string(&map).unwrap());
let d = r#"{"key":3}"#;
let map: ValueMap = serde_json::from_str(d).unwrap();
println!("{:?}", map);
}
#[test]
fn enum_action() {
use crate::action::*;
use crate::prelude::value_map;
use crate::prelude::WalleResult;
use walle_macro::_OneBot as OneBot;
#[derive(Debug, OneBot)]
#[action]
pub enum MyAction {
GetUserInfo(GetUserInfo),
GetGroupInfo { group_id: String },
}
let raw_action = Action {
action: "get_user_info".to_string(),
params: value_map! {
"user_id": "abab"
},
};
let action: WalleResult<BaseAction<MyAction>> = raw_action.try_into();
println!("{:?}", action);
}
#[test]
fn option_action() {
use crate::action::Action;
use walle_macro::{_OneBot as OneBot, _PushToValueMap as PushToValueMap};
#[derive(Debug, OneBot, PushToValueMap)]
#[action]
pub struct MySeg {
pub text: Option<String>,
}
println!(
"{:?}",
MySeg::try_from(
serde_json::from_str::<Action>(r#"{"action":"my_seg", "params": {"text": "text"}}"#)
.unwrap()
)
);
println!(
"{:?}",
MySeg::try_from(
serde_json::from_str::<Action>(r#"{"action":"my_seg", "params": {}}"#).unwrap()
)
)
}