rust/hedgewars-server/src/protocol/parser.rs
changeset 14777 8015a6e4ca3c
parent 14775 09d46ab83361
child 14783 b3adc030104b
equal deleted inserted replaced
14776:1aea457856e2 14777:8015a6e4ca3c
    14     str::{FromStr, Utf8Error},
    14     str::{FromStr, Utf8Error},
    15 };
    15 };
    16 
    16 
    17 use super::messages::{HWProtocolMessage, HWProtocolMessage::*};
    17 use super::messages::{HWProtocolMessage, HWProtocolMessage::*};
    18 use crate::server::coretypes::{GameCfg, HedgehogInfo, TeamInfo, VoteType, MAX_HEDGEHOGS_PER_TEAM};
    18 use crate::server::coretypes::{GameCfg, HedgehogInfo, TeamInfo, VoteType, MAX_HEDGEHOGS_PER_TEAM};
    19 
       
    20 #[cfg(test)]
       
    21 use {
       
    22     super::test::gen_proto_msg,
       
    23     proptest::{proptest, proptest_helper},
       
    24 };
       
    25 
    19 
    26 #[derive(Debug, PartialEq)]
    20 #[derive(Debug, PartialEq)]
    27 pub struct HWProtocolError {}
    21 pub struct HWProtocolError {}
    28 
    22 
    29 impl HWProtocolError {
    23 impl HWProtocolError {
   546 pub fn extract_messages<'a>(input: &'a [u8]) -> HWResult<Vec<HWProtocolMessage>> {
   540 pub fn extract_messages<'a>(input: &'a [u8]) -> HWResult<Vec<HWProtocolMessage>> {
   547     many0(|i: &'a [u8]| complete!(i, message))(input)
   541     many0(|i: &'a [u8]| complete!(i, message))(input)
   548 }
   542 }
   549 
   543 
   550 #[cfg(test)]
   544 #[cfg(test)]
   551 proptest! {
   545 mod test {
       
   546     use super::{extract_messages, message};
       
   547     use crate::protocol::{messages::HWProtocolMessage::*, test::gen_proto_msg};
       
   548     use proptest::{proptest, proptest_helper};
       
   549 
       
   550     #[cfg(test)]
       
   551     proptest! {
       
   552         #[test]
       
   553         fn is_parser_composition_idempotent(ref msg in gen_proto_msg()) {
       
   554             println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes());
       
   555             assert_eq!(message(msg.to_raw_protocol().as_bytes()), Ok((&b""[..], msg.clone())))
       
   556         }
       
   557     }
       
   558 
   552     #[test]
   559     #[test]
   553     fn is_parser_composition_idempotent(ref msg in gen_proto_msg()) {
   560     fn parse_test() {
   554         println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes());
   561         assert_eq!(message(b"PING\n\n"), Ok((&b""[..], Ping)));
   555         assert_eq!(message(msg.to_raw_protocol().as_bytes()), Ok((&b""[..], msg.clone())))
   562         assert_eq!(message(b"START_GAME\n\n"), Ok((&b""[..], StartGame)));
   556     }
   563         assert_eq!(
   557 }
   564             message(b"NICK\nit's me\n\n"),
   558 
   565             Ok((&b""[..], Nick("it's me".to_string())))
   559 #[test]
   566         );
   560 fn parse_test() {
   567         assert_eq!(message(b"PROTO\n51\n\n"), Ok((&b""[..], Proto(51))));
   561     assert_eq!(message(b"PING\n\n"), Ok((&b""[..], Ping)));
   568         assert_eq!(
   562     assert_eq!(message(b"START_GAME\n\n"), Ok((&b""[..], StartGame)));
   569             message(b"QUIT\nbye-bye\n\n"),
   563     assert_eq!(
   570             Ok((&b""[..], Quit(Some("bye-bye".to_string()))))
   564         message(b"NICK\nit's me\n\n"),
   571         );
   565         Ok((&b""[..], Nick("it's me".to_string())))
   572         assert_eq!(message(b"QUIT\n\n"), Ok((&b""[..], Quit(None))));
   566     );
   573         assert_eq!(
   567     assert_eq!(message(b"PROTO\n51\n\n"), Ok((&b""[..], Proto(51))));
   574             message(b"CMD\nwatch demo\n\n"),
   568     assert_eq!(
   575             Ok((&b""[..], Watch("demo".to_string())))
   569         message(b"QUIT\nbye-bye\n\n"),
   576         );
   570         Ok((&b""[..], Quit(Some("bye-bye".to_string()))))
   577         assert_eq!(
   571     );
   578             message(b"BAN\nme\nbad\n77\n\n"),
   572     assert_eq!(message(b"QUIT\n\n"), Ok((&b""[..], Quit(None))));
   579             Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77)))
   573     assert_eq!(
   580         );
   574         message(b"CMD\nwatch demo\n\n"),
   581 
   575         Ok((&b""[..], Watch("demo".to_string())))
   582         assert_eq!(message(b"CMD\nPART\n\n"), Ok((&b""[..], Part(None))));
   576     );
   583         assert_eq!(
   577     assert_eq!(
   584             message(b"CMD\nPART _msg_\n\n"),
   578         message(b"BAN\nme\nbad\n77\n\n"),
   585             Ok((&b""[..], Part(Some("_msg_".to_string()))))
   579         Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77)))
   586         );
   580     );
   587 
   581 
   588         assert_eq!(message(b"CMD\nRND\n\n"), Ok((&b""[..], Rnd(vec![]))));
   582     assert_eq!(message(b"CMD\nPART\n\n"), Ok((&b""[..], Part(None))));
   589         assert_eq!(
   583     assert_eq!(
   590             message(b"CMD\nRND A B\n\n"),
   584         message(b"CMD\nPART _msg_\n\n"),
   591             Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")])))
   585         Ok((&b""[..], Part(Some("_msg_".to_string()))))
   592         );
   586     );
   593 
   587 
   594         assert_eq!(
   588     assert_eq!(message(b"CMD\nRND\n\n"), Ok((&b""[..], Rnd(vec![]))));
   595             extract_messages(b"QUIT\n1\n2\n\n"),
   589     assert_eq!(
   596             Ok((&b""[..], vec![Malformed]))
   590         message(b"CMD\nRND A B\n\n"),
   597         );
   591         Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")])))
   598 
   592     );
   599         assert_eq!(
   593 
   600             extract_messages(b"PING\n\nPING\n\nP"),
   594     assert_eq!(
   601             Ok((&b"P"[..], vec![Ping, Ping]))
   595         extract_messages(b"QUIT\n1\n2\n\n"),
   602         );
   596         Ok((&b""[..], vec![Malformed]))
   603         assert_eq!(
   597     );
   604             extract_messages(b"SING\n\nPING\n\n"),
   598 
   605             Ok((&b""[..], vec![Malformed, Ping]))
   599     assert_eq!(
   606         );
   600         extract_messages(b"PING\n\nPING\n\nP"),
   607         assert_eq!(
   601         Ok((&b"P"[..], vec![Ping, Ping]))
   608             extract_messages(b"\n\n\n\nPING\n\n"),
   602     );
   609             Ok((&b""[..], vec![Empty, Empty, Ping]))
   603     assert_eq!(
   610         );
   604         extract_messages(b"SING\n\nPING\n\n"),
   611         assert_eq!(
   605         Ok((&b""[..], vec![Malformed, Ping]))
   612             extract_messages(b"\n\n\nPING\n\n"),
   606     );
   613             Ok((&b""[..], vec![Empty, Empty, Ping]))
   607     assert_eq!(
   614         );
   608         extract_messages(b"\n\n\n\nPING\n\n"),
   615     }
   609         Ok((&b""[..], vec![Empty, Empty, Ping]))
   616 }
   610     );
       
   611     assert_eq!(
       
   612         extract_messages(b"\n\n\nPING\n\n"),
       
   613         Ok((&b""[..], vec![Empty, Empty, Ping]))
       
   614     );
       
   615 }