rust/hedgewars-network-protocol/src/tests/parser.rs
author unC0Rr
Sat, 14 Dec 2024 18:07:29 +0100
branchtransitional_engine
changeset 16077 67469603872a
parent 16026 7c8697fa019f
permissions -rw-r--r--
Add templates for maps with 3 to 8 vertical structures
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15852
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
     1
use crate::{
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
     2
    parser::HwProtocolError,
16026
7c8697fa019f fix protocol recovery
alfadur
parents: 15852
diff changeset
     3
    parser::{malformed_message, message, server_message},
15852
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
     4
    types::GameCfg,
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
     5
};
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
     6
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
     7
#[test]
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
     8
fn parse_test() {
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
     9
    use crate::messages::HwProtocolMessage::*;
16026
7c8697fa019f fix protocol recovery
alfadur
parents: 15852
diff changeset
    10
    use nom::Err::Incomplete;
7c8697fa019f fix protocol recovery
alfadur
parents: 15852
diff changeset
    11
7c8697fa019f fix protocol recovery
alfadur
parents: 15852
diff changeset
    12
    assert!(matches!(
7c8697fa019f fix protocol recovery
alfadur
parents: 15852
diff changeset
    13
        dbg!(message(b"CHAT\nWhat the")),
7c8697fa019f fix protocol recovery
alfadur
parents: 15852
diff changeset
    14
        Err(Incomplete(_))
7c8697fa019f fix protocol recovery
alfadur
parents: 15852
diff changeset
    15
    ));
7c8697fa019f fix protocol recovery
alfadur
parents: 15852
diff changeset
    16
    assert!(matches!(
7c8697fa019f fix protocol recovery
alfadur
parents: 15852
diff changeset
    17
        dbg!(malformed_message(b"CHAT\nWhat the \xF0\x9F\xA6\x94\n\nBYE")),
7c8697fa019f fix protocol recovery
alfadur
parents: 15852
diff changeset
    18
        Ok((b"BYE", _))
7c8697fa019f fix protocol recovery
alfadur
parents: 15852
diff changeset
    19
    ));
15852
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    20
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    21
    assert_eq!(message(b"PING\n\n"), Ok((&b""[..], Ping)));
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    22
    assert_eq!(message(b"START_GAME\n\n"), Ok((&b""[..], StartGame)));
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    23
    assert_eq!(
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    24
        message(b"NICK\nit's me\n\n"),
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    25
        Ok((&b""[..], Nick("it's me".to_string())))
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    26
    );
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    27
    assert_eq!(message(b"PROTO\n51\n\n"), Ok((&b""[..], Proto(51))));
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    28
    assert_eq!(
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    29
        message(b"QUIT\nbye-bye\n\n"),
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    30
        Ok((&b""[..], Quit(Some("bye-bye".to_string()))))
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    31
    );
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    32
    assert_eq!(message(b"QUIT\n\n"), Ok((&b""[..], Quit(None))));
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    33
    assert_eq!(
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    34
        message(b"CMD\nwatch 49471\n\n"),
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    35
        Ok((&b""[..], Watch(49471)))
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    36
    );
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    37
    assert_eq!(
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    38
        message(b"BAN\nme\nbad\n77\n\n"),
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    39
        Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77)))
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    40
    );
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    41
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    42
    assert_eq!(message(b"CMD\nPART\n\n"), Ok((&b""[..], Part(None))));
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    43
    assert_eq!(
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    44
        message(b"CMD\nPART _msg_\n\n"),
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    45
        Ok((&b""[..], Part(Some("_msg_".to_string()))))
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    46
    );
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    47
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    48
    assert_eq!(message(b"CMD\nRND\n\n"), Ok((&b""[..], Rnd(vec![]))));
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    49
    assert_eq!(
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    50
        message(b"CMD\nRND A B\n\n"),
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    51
        Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")])))
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    52
    );
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    53
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    54
    assert_eq!(
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    55
        message(b"CFG\nSCHEME\na\nA\n\n"),
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    56
        Ok((
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    57
            &b""[..],
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    58
            Cfg(GameCfg::Scheme("a".to_string(), vec!["A".to_string()]))
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    59
        ))
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    60
    );
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    61
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    62
    assert_eq!(
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    63
        message(b"QUIT\n1\n2\n\n"),
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    64
        Err(nom::Err::Error(HwProtocolError::new()))
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    65
    );
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    66
}
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    67
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    68
#[test]
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    69
fn parse_server_messages_test() {
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    70
    use crate::messages::HwServerMessage::*;
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    71
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    72
    assert_eq!(server_message(b"PING\n\n"), Ok((&b""[..], Ping)));
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    73
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    74
    assert_eq!(
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    75
        server_message(b"JOINING\nnoone\n\n"),
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    76
        Ok((&b""[..], Joining("noone".to_string())))
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    77
    );
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    78
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    79
    assert_eq!(
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    80
        server_message(b"CLIENT_FLAGS\naaa\nA\nB\n\n"),
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    81
        Ok((
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    82
            &b""[..],
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    83
            ClientFlags("aaa".to_string(), vec!["A".to_string(), "B".to_string()])
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    84
        ))
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    85
    )
ea459da15b30 update protocol 📦
alfadur
parents:
diff changeset
    86
}