author | unc0rr |
Wed, 30 Jun 2021 00:18:53 +0200 | |
changeset 15832 | ee84e417d8d0 |
parent 15826 | 747278149393 |
permissions | -rw-r--r-- |
15826 | 1 |
use hedgewars_network_protocol::{ |
15832
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
2 |
parser::HwProtocolError, |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
3 |
parser::{message, server_message}, |
15826 | 4 |
types::GameCfg, |
5 |
}; |
|
6 |
||
7 |
#[test] |
|
8 |
fn parse_test() { |
|
15832
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
9 |
use hedgewars_network_protocol::messages::HwProtocolMessage::*; |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
10 |
|
15826 | 11 |
assert_eq!(message(b"PING\n\n"), Ok((&b""[..], Ping))); |
12 |
assert_eq!(message(b"START_GAME\n\n"), Ok((&b""[..], StartGame))); |
|
13 |
assert_eq!( |
|
14 |
message(b"NICK\nit's me\n\n"), |
|
15 |
Ok((&b""[..], Nick("it's me".to_string()))) |
|
16 |
); |
|
17 |
assert_eq!(message(b"PROTO\n51\n\n"), Ok((&b""[..], Proto(51)))); |
|
18 |
assert_eq!( |
|
19 |
message(b"QUIT\nbye-bye\n\n"), |
|
20 |
Ok((&b""[..], Quit(Some("bye-bye".to_string())))) |
|
21 |
); |
|
22 |
assert_eq!(message(b"QUIT\n\n"), Ok((&b""[..], Quit(None)))); |
|
23 |
assert_eq!( |
|
24 |
message(b"CMD\nwatch 49471\n\n"), |
|
25 |
Ok((&b""[..], Watch(49471))) |
|
26 |
); |
|
27 |
assert_eq!( |
|
28 |
message(b"BAN\nme\nbad\n77\n\n"), |
|
29 |
Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77))) |
|
30 |
); |
|
31 |
||
32 |
assert_eq!(message(b"CMD\nPART\n\n"), Ok((&b""[..], Part(None)))); |
|
33 |
assert_eq!( |
|
34 |
message(b"CMD\nPART _msg_\n\n"), |
|
35 |
Ok((&b""[..], Part(Some("_msg_".to_string())))) |
|
36 |
); |
|
37 |
||
38 |
assert_eq!(message(b"CMD\nRND\n\n"), Ok((&b""[..], Rnd(vec![])))); |
|
39 |
assert_eq!( |
|
40 |
message(b"CMD\nRND A B\n\n"), |
|
41 |
Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")]))) |
|
42 |
); |
|
43 |
||
44 |
assert_eq!( |
|
45 |
message(b"CFG\nSCHEME\na\nA\n\n"), |
|
46 |
Ok(( |
|
47 |
&b""[..], |
|
48 |
Cfg(GameCfg::Scheme("a".to_string(), vec!["A".to_string()])) |
|
49 |
)) |
|
50 |
); |
|
51 |
||
52 |
assert_eq!( |
|
53 |
message(b"QUIT\n1\n2\n\n"), |
|
54 |
Err(nom::Err::Error(HwProtocolError::new())) |
|
55 |
); |
|
56 |
} |
|
15832
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
57 |
|
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
58 |
#[test] |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
59 |
fn parse_server_messages_test() { |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
60 |
use hedgewars_network_protocol::messages::HwServerMessage::*; |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
61 |
|
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
62 |
assert_eq!(server_message(b"PING\n\n"), Ok((&b""[..], Ping))); |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
63 |
|
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
64 |
assert_eq!( |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
65 |
server_message(b"JOINING\nnoone\n\n"), |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
66 |
Ok((&b""[..], Joining("noone".to_string()))) |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
67 |
); |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
68 |
|
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
69 |
assert_eq!( |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
70 |
server_message(b"CLIENT_FLAGS\naaa\nA\nB\n\n"), |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
71 |
Ok(( |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
72 |
&b""[..], |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
73 |
ClientFlags("aaa".to_string(), vec!["A".to_string(), "B".to_string()]) |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
74 |
)) |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
75 |
) |
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
76 |
} |