rust/hedgewars-network-protocol/tests/test.rs
changeset 15830 ea459da15b30
parent 15829 d5e6c8c92d87
child 15831 7d0f747afcb8
equal deleted inserted replaced
15829:d5e6c8c92d87 15830:ea459da15b30
     1 use proptest::{
       
     2     arbitrary::any,
       
     3     proptest,
       
     4     strategy::{BoxedStrategy, Just, Strategy},
       
     5 };
       
     6 
       
     7 use hedgewars_network_protocol::messages::{HwProtocolMessage, HwServerMessage};
       
     8 use hedgewars_network_protocol::parser::{message, server_message};
       
     9 use hedgewars_network_protocol::types::{GameCfg, ServerVar, TeamInfo, VoteType};
       
    10 
       
    11 use hedgewars_network_protocol::types::testing::*;
       
    12 use hedgewars_network_protocol::{proto_msg_case, proto_msg_match};
       
    13 
       
    14 pub fn gen_proto_msg() -> BoxedStrategy<HwProtocolMessage> where {
       
    15     use hedgewars_network_protocol::messages::HwProtocolMessage::*;
       
    16 
       
    17     let res = (0..=58).no_shrink().prop_flat_map(|i| {
       
    18         proto_msg_match!(i, def = Ping,
       
    19             0 => Ping(),
       
    20             1 => Pong(),
       
    21             2 => Quit(Option<Ascii>),
       
    22             4 => Global(Ascii),
       
    23             5 => Watch(u32),
       
    24             6 => ToggleServerRegisteredOnly(),
       
    25             7 => SuperPower(),
       
    26             8 => Info(Ascii),
       
    27             9 => Nick(Ascii),
       
    28             10 => Proto(u16),
       
    29             11 => Password(Ascii, Ascii),
       
    30             12 => Checker(u16, Ascii, Ascii),
       
    31             13 => List(),
       
    32             14 => Chat(Ascii),
       
    33             15 => CreateRoom(Ascii, Option<Ascii>),
       
    34             16 => JoinRoom(Ascii, Option<Ascii>),
       
    35             17 => Follow(Ascii),
       
    36             18 => Rnd(Vec<Ascii>),
       
    37             19 => Kick(Ascii),
       
    38             20 => Ban(Ascii, Ascii, u32),
       
    39             21 => BanIp(Ascii, Ascii, u32),
       
    40             22 => BanNick(Ascii, Ascii, u32),
       
    41             23 => BanList(),
       
    42             24 => Unban(Ascii),
       
    43             25 => SetServerVar(ServerVar),
       
    44             26 => GetServerVar(),
       
    45             27 => RestartServer(),
       
    46             28 => Stats(),
       
    47             29 => Part(Option<Ascii>),
       
    48             30 => Cfg(GameCfg),
       
    49             31 => AddTeam(Box<TeamInfo>),
       
    50             32 => RemoveTeam(Ascii),
       
    51             33 => SetHedgehogsNumber(Ascii, u8),
       
    52             34 => SetTeamColor(Ascii, u8),
       
    53             35 => ToggleReady(),
       
    54             36 => StartGame(),
       
    55             37 => EngineMessage(Ascii),
       
    56             38 => RoundFinished(),
       
    57             39 => ToggleRestrictJoin(),
       
    58             40 => ToggleRestrictTeams(),
       
    59             41 => ToggleRegisteredOnly(),
       
    60             42 => RoomName(Ascii),
       
    61             43 => Delegate(Ascii),
       
    62             44 => TeamChat(Ascii),
       
    63             45 => MaxTeams(u8),
       
    64             46 => Fix(),
       
    65             47 => Unfix(),
       
    66             48 => Greeting(Option<Ascii>),
       
    67             49 => CallVote(Option<VoteType>),
       
    68             50 => Vote(bool),
       
    69             51 => ForceVote(bool),
       
    70             52 => Save(Ascii, Ascii),
       
    71             53 => Delete(Ascii),
       
    72             54 => SaveRoom(Ascii),
       
    73             55 => LoadRoom(Ascii),
       
    74             56 => CheckerReady(),
       
    75             57 => CheckedOk(Vec<Ascii>),
       
    76             58 => CheckedFail(Ascii)
       
    77         )
       
    78     });
       
    79     res.boxed()
       
    80 }
       
    81 
       
    82 pub fn gen_server_msg() -> BoxedStrategy<HwServerMessage> where {
       
    83     use hedgewars_network_protocol::messages::HwServerMessage::*;
       
    84 
       
    85     let res = (0..=38).no_shrink().prop_flat_map(|i| {
       
    86         proto_msg_match!(i, def = Ping,
       
    87                     0 => Connected(Ascii, u32),
       
    88                     1 => Redirect(u16),
       
    89                     2 => Ping(),
       
    90                     3 => Pong(),
       
    91                     4 => Bye(Ascii),
       
    92                     5 => Nick(Ascii),
       
    93                     6 => Proto(u16),
       
    94                     7 => AskPassword(Ascii),
       
    95                     8 => ServerAuth(Ascii),
       
    96                     9 => LogonPassed(),
       
    97                     10 => LobbyLeft(Ascii, Ascii),
       
    98                     11 => LobbyJoined(Vec<Ascii>),
       
    99         //            12 => ChatMsg { Ascii, Ascii },
       
   100                     13 => ClientFlags(Ascii, Vec<Ascii>),
       
   101                     14 => Rooms(Vec<Ascii>),
       
   102                     15 => RoomAdd(Vec<Ascii>),
       
   103                     16=> RoomJoined(Vec<Ascii>),
       
   104                     17 => RoomLeft(Ascii, Ascii),
       
   105                     18 => RoomRemove(Ascii),
       
   106                     19 => RoomUpdated(Ascii, Vec<Ascii>),
       
   107                     20 => Joining(Ascii),
       
   108                     21 => TeamAdd(Vec<Ascii>),
       
   109                     22 => TeamRemove(Ascii),
       
   110                     23 => TeamAccepted(Ascii),
       
   111                     24 => TeamColor(Ascii, u8),
       
   112                     25 => HedgehogsNumber(Ascii, u8),
       
   113                     26 => ConfigEntry(Ascii, Vec<Ascii>),
       
   114                     27 => Kicked(),
       
   115                     28 => RunGame(),
       
   116                     29 => ForwardEngineMessage(Vec<Ascii>),
       
   117                     30 => RoundFinished(),
       
   118                     31 => ReplayStart(),
       
   119                     32 => Info(Vec<Ascii>),
       
   120                     33 => ServerMessage(Ascii),
       
   121                     34 => ServerVars(Vec<Ascii>),
       
   122                     35 => Notice(Ascii),
       
   123                     36 => Warning(Ascii),
       
   124                     37 => Error(Ascii),
       
   125                     38 => Replay(Vec<Ascii>)
       
   126                 )
       
   127     });
       
   128     res.boxed()
       
   129 }
       
   130 
       
   131 proptest! {
       
   132     #[test]
       
   133     fn is_parser_composition_idempotent(ref msg in gen_proto_msg()) {
       
   134         println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes());
       
   135         assert_eq!(message(msg.to_raw_protocol().as_bytes()), Ok((&b""[..], msg.clone())))
       
   136     }
       
   137 
       
   138     #[test]
       
   139     fn is_server_message_parser_composition_idempotent(ref msg in gen_server_msg()) {
       
   140         println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes());
       
   141         assert_eq!(server_message(msg.to_raw_protocol().as_bytes()), Ok((&b""[..], msg.clone())))
       
   142     }
       
   143 }