15852
|
1 |
use crate::{
|
|
2 |
parser::HwProtocolError,
|
16026
|
3 |
parser::{malformed_message, message, server_message},
|
15852
|
4 |
types::GameCfg,
|
|
5 |
};
|
|
6 |
|
|
7 |
#[test]
|
|
8 |
fn parse_test() {
|
|
9 |
use crate::messages::HwProtocolMessage::*;
|
16026
|
10 |
use nom::Err::Incomplete;
|
|
11 |
|
|
12 |
assert!(matches!(
|
|
13 |
dbg!(message(b"CHAT\nWhat the")),
|
|
14 |
Err(Incomplete(_))
|
|
15 |
));
|
|
16 |
assert!(matches!(
|
|
17 |
dbg!(malformed_message(b"CHAT\nWhat the \xF0\x9F\xA6\x94\n\nBYE")),
|
|
18 |
Ok((b"BYE", _))
|
|
19 |
));
|
15852
|
20 |
|
|
21 |
assert_eq!(message(b"PING\n\n"), Ok((&b""[..], Ping)));
|
|
22 |
assert_eq!(message(b"START_GAME\n\n"), Ok((&b""[..], StartGame)));
|
|
23 |
assert_eq!(
|
|
24 |
message(b"NICK\nit's me\n\n"),
|
|
25 |
Ok((&b""[..], Nick("it's me".to_string())))
|
|
26 |
);
|
|
27 |
assert_eq!(message(b"PROTO\n51\n\n"), Ok((&b""[..], Proto(51))));
|
|
28 |
assert_eq!(
|
|
29 |
message(b"QUIT\nbye-bye\n\n"),
|
|
30 |
Ok((&b""[..], Quit(Some("bye-bye".to_string()))))
|
|
31 |
);
|
|
32 |
assert_eq!(message(b"QUIT\n\n"), Ok((&b""[..], Quit(None))));
|
|
33 |
assert_eq!(
|
|
34 |
message(b"CMD\nwatch 49471\n\n"),
|
|
35 |
Ok((&b""[..], Watch(49471)))
|
|
36 |
);
|
|
37 |
assert_eq!(
|
|
38 |
message(b"BAN\nme\nbad\n77\n\n"),
|
|
39 |
Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77)))
|
|
40 |
);
|
|
41 |
|
|
42 |
assert_eq!(message(b"CMD\nPART\n\n"), Ok((&b""[..], Part(None))));
|
|
43 |
assert_eq!(
|
|
44 |
message(b"CMD\nPART _msg_\n\n"),
|
|
45 |
Ok((&b""[..], Part(Some("_msg_".to_string()))))
|
|
46 |
);
|
|
47 |
|
|
48 |
assert_eq!(message(b"CMD\nRND\n\n"), Ok((&b""[..], Rnd(vec![]))));
|
|
49 |
assert_eq!(
|
|
50 |
message(b"CMD\nRND A B\n\n"),
|
|
51 |
Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")])))
|
|
52 |
);
|
|
53 |
|
|
54 |
assert_eq!(
|
|
55 |
message(b"CFG\nSCHEME\na\nA\n\n"),
|
|
56 |
Ok((
|
|
57 |
&b""[..],
|
|
58 |
Cfg(GameCfg::Scheme("a".to_string(), vec!["A".to_string()]))
|
|
59 |
))
|
|
60 |
);
|
|
61 |
|
|
62 |
assert_eq!(
|
|
63 |
message(b"QUIT\n1\n2\n\n"),
|
|
64 |
Err(nom::Err::Error(HwProtocolError::new()))
|
|
65 |
);
|
|
66 |
}
|
|
67 |
|
|
68 |
#[test]
|
|
69 |
fn parse_server_messages_test() {
|
|
70 |
use crate::messages::HwServerMessage::*;
|
|
71 |
|
|
72 |
assert_eq!(server_message(b"PING\n\n"), Ok((&b""[..], Ping)));
|
|
73 |
|
|
74 |
assert_eq!(
|
|
75 |
server_message(b"JOINING\nnoone\n\n"),
|
|
76 |
Ok((&b""[..], Joining("noone".to_string())))
|
|
77 |
);
|
|
78 |
|
|
79 |
assert_eq!(
|
|
80 |
server_message(b"CLIENT_FLAGS\naaa\nA\nB\n\n"),
|
|
81 |
Ok((
|
|
82 |
&b""[..],
|
|
83 |
ClientFlags("aaa".to_string(), vec!["A".to_string(), "B".to_string()])
|
|
84 |
))
|
|
85 |
)
|
|
86 |
}
|