rust/hedgewars-network-protocol/tests/parser.rs
author unc0rr
Wed, 23 Jun 2021 23:41:51 +0200
changeset 15826 747278149393
child 15832 ee84e417d8d0
permissions -rw-r--r--
Extract network protocol into a separate crate
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15826
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     1
use hedgewars_network_protocol::{
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     2
    parser::message,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     3
    types::GameCfg,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     4
    {messages::HwProtocolMessage::*, parser::HwProtocolError},
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     5
};
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     6
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     7
#[test]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     8
fn parse_test() {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     9
    assert_eq!(message(b"PING\n\n"), Ok((&b""[..], Ping)));
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    10
    assert_eq!(message(b"START_GAME\n\n"), Ok((&b""[..], StartGame)));
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    11
    assert_eq!(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    12
        message(b"NICK\nit's me\n\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    13
        Ok((&b""[..], Nick("it's me".to_string())))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    14
    );
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    15
    assert_eq!(message(b"PROTO\n51\n\n"), Ok((&b""[..], Proto(51))));
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    16
    assert_eq!(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    17
        message(b"QUIT\nbye-bye\n\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    18
        Ok((&b""[..], Quit(Some("bye-bye".to_string()))))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    19
    );
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    20
    assert_eq!(message(b"QUIT\n\n"), Ok((&b""[..], Quit(None))));
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    21
    assert_eq!(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    22
        message(b"CMD\nwatch 49471\n\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    23
        Ok((&b""[..], Watch(49471)))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    24
    );
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    25
    assert_eq!(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    26
        message(b"BAN\nme\nbad\n77\n\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    27
        Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77)))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    28
    );
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    29
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    30
    assert_eq!(message(b"CMD\nPART\n\n"), Ok((&b""[..], Part(None))));
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    31
    assert_eq!(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    32
        message(b"CMD\nPART _msg_\n\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    33
        Ok((&b""[..], Part(Some("_msg_".to_string()))))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    34
    );
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    35
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    36
    assert_eq!(message(b"CMD\nRND\n\n"), Ok((&b""[..], Rnd(vec![]))));
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    37
    assert_eq!(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    38
        message(b"CMD\nRND A B\n\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    39
        Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")])))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    40
    );
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    41
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    42
    assert_eq!(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    43
        message(b"CFG\nSCHEME\na\nA\n\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    44
        Ok((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    45
            &b""[..],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    46
            Cfg(GameCfg::Scheme("a".to_string(), vec!["A".to_string()]))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    47
        ))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    48
    );
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    49
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    50
    assert_eq!(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    51
        message(b"QUIT\n1\n2\n\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    52
        Err(nom::Err::Error(HwProtocolError::new()))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    53
    );
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    54
}