rust/hedgewars-network-protocol/tests/parser.rs
author unc0rr
Wed, 30 Jun 2021 00:18:53 +0200
changeset 15832 ee84e417d8d0
parent 15826 747278149393
permissions -rw-r--r--
Add parser and idempotention tests for server messages
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::{
15832
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
     2
    parser::HwProtocolError,
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
     3
    parser::{message, server_message},
15826
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     4
    types::GameCfg,
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() {
15832
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
     9
    use hedgewars_network_protocol::messages::HwProtocolMessage::*;
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    10
15826
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    11
    assert_eq!(message(b"PING\n\n"), Ok((&b""[..], Ping)));
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    12
    assert_eq!(message(b"START_GAME\n\n"), Ok((&b""[..], StartGame)));
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    13
    assert_eq!(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    14
        message(b"NICK\nit's me\n\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    15
        Ok((&b""[..], Nick("it's me".to_string())))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    16
    );
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    17
    assert_eq!(message(b"PROTO\n51\n\n"), Ok((&b""[..], Proto(51))));
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    18
    assert_eq!(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    19
        message(b"QUIT\nbye-bye\n\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    20
        Ok((&b""[..], Quit(Some("bye-bye".to_string()))))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    21
    );
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    22
    assert_eq!(message(b"QUIT\n\n"), Ok((&b""[..], Quit(None))));
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    23
    assert_eq!(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    24
        message(b"CMD\nwatch 49471\n\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    25
        Ok((&b""[..], Watch(49471)))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    26
    );
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    27
    assert_eq!(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    28
        message(b"BAN\nme\nbad\n77\n\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    29
        Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77)))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    30
    );
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    31
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    32
    assert_eq!(message(b"CMD\nPART\n\n"), Ok((&b""[..], Part(None))));
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    33
    assert_eq!(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    34
        message(b"CMD\nPART _msg_\n\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    35
        Ok((&b""[..], Part(Some("_msg_".to_string()))))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    36
    );
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    37
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    38
    assert_eq!(message(b"CMD\nRND\n\n"), Ok((&b""[..], Rnd(vec![]))));
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    39
    assert_eq!(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    40
        message(b"CMD\nRND A B\n\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    41
        Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")])))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    42
    );
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    43
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    44
    assert_eq!(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    45
        message(b"CFG\nSCHEME\na\nA\n\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    46
        Ok((
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    47
            &b""[..],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    48
            Cfg(GameCfg::Scheme("a".to_string(), vec!["A".to_string()]))
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
    );
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    51
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    52
    assert_eq!(
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    53
        message(b"QUIT\n1\n2\n\n"),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    54
        Err(nom::Err::Error(HwProtocolError::new()))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    55
    );
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    56
}
15832
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    57
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    58
#[test]
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    59
fn parse_server_messages_test() {
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    60
    use hedgewars_network_protocol::messages::HwServerMessage::*;
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    61
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    62
    assert_eq!(server_message(b"PING\n\n"), Ok((&b""[..], Ping)));
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    63
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    64
    assert_eq!(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    65
        server_message(b"JOINING\nnoone\n\n"),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    66
        Ok((&b""[..], Joining("noone".to_string())))
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    67
    );
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    68
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    69
    assert_eq!(
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    70
        server_message(b"CLIENT_FLAGS\naaa\nA\nB\n\n"),
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    71
        Ok((
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    72
            &b""[..],
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    73
            ClientFlags("aaa".to_string(), vec!["A".to_string(), "B".to_string()])
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    74
        ))
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    75
    )
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15826
diff changeset
    76
}