2 arbitrary::any, |
2 arbitrary::any, |
3 proptest, |
3 proptest, |
4 strategy::{BoxedStrategy, Just, Strategy}, |
4 strategy::{BoxedStrategy, Just, Strategy}, |
5 }; |
5 }; |
6 |
6 |
7 use hedgewars_network_protocol::messages::{HwProtocolMessage, HwProtocolMessage::*}; |
7 use hedgewars_network_protocol::messages::{HwProtocolMessage, HwServerMessage}; |
8 use hedgewars_network_protocol::parser::message; |
8 use hedgewars_network_protocol::parser::{message, server_message}; |
9 use hedgewars_network_protocol::types::{GameCfg, ServerVar, TeamInfo, VoteType}; |
9 use hedgewars_network_protocol::types::{GameCfg, ServerVar, TeamInfo, VoteType}; |
10 |
10 |
11 use hedgewars_network_protocol::types::testing::*; |
11 use hedgewars_network_protocol::types::testing::*; |
12 use hedgewars_network_protocol::{proto_msg_case, proto_msg_match}; |
12 use hedgewars_network_protocol::{proto_msg_case, proto_msg_match}; |
13 |
13 |
14 pub fn gen_proto_msg() -> BoxedStrategy<HwProtocolMessage> where { |
14 pub fn gen_proto_msg() -> BoxedStrategy<HwProtocolMessage> where { |
|
15 use hedgewars_network_protocol::messages::HwProtocolMessage::*; |
|
16 |
15 let res = (0..=55).no_shrink().prop_flat_map(|i| { |
17 let res = (0..=55).no_shrink().prop_flat_map(|i| { |
16 proto_msg_match!(i, def = Ping, |
18 proto_msg_match!(i, def = Ping, |
17 0 => Ping(), |
19 0 => Ping(), |
18 1 => Pong(), |
20 1 => Pong(), |
19 2 => Quit(Option<Ascii>), |
21 2 => Quit(Option<Ascii>), |
72 ) |
74 ) |
73 }); |
75 }); |
74 res.boxed() |
76 res.boxed() |
75 } |
77 } |
76 |
78 |
|
79 pub fn gen_server_msg() -> BoxedStrategy<HwServerMessage> where { |
|
80 use hedgewars_network_protocol::messages::HwServerMessage::*; |
|
81 |
|
82 let res = (0..=55).no_shrink().prop_flat_map(|i| { |
|
83 proto_msg_match!(i, def = Ping, |
|
84 0 => Connected(Ascii, u32), |
|
85 1 => Redirect(u16), |
|
86 2 => Ping(), |
|
87 3 => Pong(), |
|
88 4 => Bye(Ascii), |
|
89 5 => Nick(Ascii), |
|
90 6 => Proto(u16), |
|
91 7 => AskPassword(Ascii), |
|
92 8 => ServerAuth(Ascii), |
|
93 9 => LogonPassed(), |
|
94 10 => LobbyLeft(Ascii, Ascii), |
|
95 11 => LobbyJoined(Vec<Ascii>), |
|
96 // 12 => ChatMsg { Ascii, Ascii }, |
|
97 13 => ClientFlags(Ascii, Vec<Ascii>), |
|
98 14 => Rooms(Vec<Ascii>), |
|
99 15 => RoomAdd(Vec<Ascii>), |
|
100 16=> RoomJoined(Vec<Ascii>), |
|
101 17 => RoomLeft(Ascii, Ascii), |
|
102 18 => RoomRemove(Ascii), |
|
103 19 => RoomUpdated(Ascii, Vec<Ascii>), |
|
104 20 => Joining(Ascii), |
|
105 21 => TeamAdd(Vec<Ascii>), |
|
106 22 => TeamRemove(Ascii), |
|
107 23 => TeamAccepted(Ascii), |
|
108 24 => TeamColor(Ascii, u8), |
|
109 25 => HedgehogsNumber(Ascii, u8), |
|
110 26 => ConfigEntry(Ascii, Vec<Ascii>), |
|
111 27 => Kicked(), |
|
112 28 => RunGame(), |
|
113 29 => ForwardEngineMessage(Vec<Ascii>), |
|
114 30 => RoundFinished(), |
|
115 31 => ReplayStart(), |
|
116 32 => Info(Vec<Ascii>), |
|
117 33 => ServerMessage(Ascii), |
|
118 34 => ServerVars(Vec<Ascii>), |
|
119 35 => Notice(Ascii), |
|
120 36 => Warning(Ascii), |
|
121 37 => Error(Ascii) |
|
122 ) |
|
123 }); |
|
124 res.boxed() |
|
125 } |
|
126 |
77 proptest! { |
127 proptest! { |
78 #[test] |
128 #[test] |
79 fn is_parser_composition_idempotent(ref msg in gen_proto_msg()) { |
129 fn is_parser_composition_idempotent(ref msg in gen_proto_msg()) { |
80 println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes()); |
130 println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes()); |
81 assert_eq!(message(msg.to_raw_protocol().as_bytes()), Ok((&b""[..], msg.clone()))) |
131 assert_eq!(message(msg.to_raw_protocol().as_bytes()), Ok((&b""[..], msg.clone()))) |
82 } |
132 } |
|
133 |
|
134 #[test] |
|
135 fn is_server_message_parser_composition_idempotent(ref msg in gen_server_msg()) { |
|
136 println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes()); |
|
137 assert_eq!(server_message(msg.to_raw_protocol().as_bytes()), Ok((&b""[..], msg.clone()))) |
|
138 } |
83 } |
139 } |