rust/hedgewars-network-protocol/tests/parser.rs
changeset 15832 ee84e417d8d0
parent 15826 747278149393
equal deleted inserted replaced
15831:c3971b38bbfa 15832:ee84e417d8d0
     1 use hedgewars_network_protocol::{
     1 use hedgewars_network_protocol::{
     2     parser::message,
     2     parser::HwProtocolError,
       
     3     parser::{message, server_message},
     3     types::GameCfg,
     4     types::GameCfg,
     4     {messages::HwProtocolMessage::*, parser::HwProtocolError},
       
     5 };
     5 };
     6 
     6 
     7 #[test]
     7 #[test]
     8 fn parse_test() {
     8 fn parse_test() {
       
     9     use hedgewars_network_protocol::messages::HwProtocolMessage::*;
       
    10 
     9     assert_eq!(message(b"PING\n\n"), Ok((&b""[..], Ping)));
    11     assert_eq!(message(b"PING\n\n"), Ok((&b""[..], Ping)));
    10     assert_eq!(message(b"START_GAME\n\n"), Ok((&b""[..], StartGame)));
    12     assert_eq!(message(b"START_GAME\n\n"), Ok((&b""[..], StartGame)));
    11     assert_eq!(
    13     assert_eq!(
    12         message(b"NICK\nit's me\n\n"),
    14         message(b"NICK\nit's me\n\n"),
    13         Ok((&b""[..], Nick("it's me".to_string())))
    15         Ok((&b""[..], Nick("it's me".to_string())))
    50     assert_eq!(
    52     assert_eq!(
    51         message(b"QUIT\n1\n2\n\n"),
    53         message(b"QUIT\n1\n2\n\n"),
    52         Err(nom::Err::Error(HwProtocolError::new()))
    54         Err(nom::Err::Error(HwProtocolError::new()))
    53     );
    55     );
    54 }
    56 }
       
    57 
       
    58 #[test]
       
    59 fn parse_server_messages_test() {
       
    60     use hedgewars_network_protocol::messages::HwServerMessage::*;
       
    61 
       
    62     assert_eq!(server_message(b"PING\n\n"), Ok((&b""[..], Ping)));
       
    63 
       
    64     assert_eq!(
       
    65         server_message(b"JOINING\nnoone\n\n"),
       
    66         Ok((&b""[..], Joining("noone".to_string())))
       
    67     );
       
    68 
       
    69     assert_eq!(
       
    70         server_message(b"CLIENT_FLAGS\naaa\nA\nB\n\n"),
       
    71         Ok((
       
    72             &b""[..],
       
    73             ClientFlags("aaa".to_string(), vec!["A".to_string(), "B".to_string()])
       
    74         ))
       
    75     )
       
    76 }