rust/hedgewars-server/src/protocol/test.rs
author unc0rr
Mon, 10 Dec 2018 22:44:46 +0100
changeset 14420 06672690d71b
parent 13810 gameServer2/src/protocol/test.rs@0463a4221327
child 14462 98ef2913ec73
permissions -rw-r--r--
Move rust server into rust folder
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13424
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
     1
use proptest::{
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
     2
    test_runner::{TestRunner, Reason},
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
     3
    arbitrary::{any, any_with, Arbitrary, StrategyFor},
13810
0463a4221327 cleanup crate imports
alfadur
parents: 13806
diff changeset
     4
    strategy::{Strategy, BoxedStrategy, Just, Map}
13424
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
     5
};
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
     6
13671
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13533
diff changeset
     7
use crate::server::coretypes::{GameCfg, TeamInfo, HedgehogInfo};
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
     8
13424
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
     9
use super::messages::{
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    10
    HWProtocolMessage, HWProtocolMessage::*
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    11
};
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    12
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    13
// Due to inability to define From between Options
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    14
trait Into2<T>: Sized { fn into2(self) -> T; }
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    15
impl <T> Into2<T> for T { fn into2(self) -> T { self } }
13438
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13437
diff changeset
    16
impl Into2<Vec<String>> for Vec<Ascii> {
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13437
diff changeset
    17
    fn into2(self) -> Vec<String> {
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13437
diff changeset
    18
        self.into_iter().map(|x| x.0).collect()
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13437
diff changeset
    19
    }
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13437
diff changeset
    20
}
13424
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    21
impl Into2<String> for Ascii { fn into2(self) -> String { self.0 } }
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    22
impl Into2<Option<String>> for Option<Ascii>{
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    23
    fn into2(self) -> Option<String> { self.map(|x| {x.0}) }
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    24
}
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    25
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    26
macro_rules! proto_msg_case {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    27
    ($val: ident()) =>
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    28
        (Just($val));
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    29
    ($val: ident($arg: ty)) =>
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    30
        (any::<$arg>().prop_map(|v| {$val(v.into2())}));
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    31
    ($val: ident($arg1: ty, $arg2: ty)) =>
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    32
        (any::<($arg1, $arg2)>().prop_map(|v| {$val(v.0.into2(), v.1.into2())}));
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    33
    ($val: ident($arg1: ty, $arg2: ty, $arg3: ty)) =>
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    34
        (any::<($arg1, $arg2, $arg3)>().prop_map(|v| {$val(v.0.into2(), v.1.into2(), v.2.into2())}));
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    35
}
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    36
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    37
macro_rules! proto_msg_match {
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    38
    ($var: expr, def = $default: expr, $($num: expr => $constr: ident $res: tt),*) => (
13424
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    39
        match $var {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    40
            $($num => (proto_msg_case!($constr $res)).boxed()),*,
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    41
            _ => Just($default).boxed()
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    42
        }
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    43
    )
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    44
}
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    45
13437
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13426
diff changeset
    46
/// Wrapper type for generating non-empty strings
13424
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    47
#[derive(Debug)]
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    48
struct Ascii(String);
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    49
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    50
impl Arbitrary for Ascii {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    51
    type Parameters = <String as Arbitrary>::Parameters;
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    52
13671
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13533
diff changeset
    53
    fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
13487
7f3289a239dd Optimize test string generation
alfadur
parents: 13483
diff changeset
    54
        "[a-zA-Z0-9]+".prop_map(Ascii).boxed()
13424
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    55
    }
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    56
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    57
    type Strategy = BoxedStrategy<Ascii>;
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    58
}
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    59
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    60
impl Arbitrary for GameCfg {
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    61
    type Parameters = ();
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    62
13671
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13533
diff changeset
    63
    fn arbitrary_with(_args: <Self as Arbitrary>::Parameters) -> <Self as Arbitrary>::Strategy {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13533
diff changeset
    64
        use crate::server::coretypes::GameCfg::*;
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    65
        (0..10).no_shrink().prop_flat_map(|i| {
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    66
            proto_msg_match!(i, def = FeatureSize(0),
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    67
            0 => FeatureSize(u32),
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    68
            1 => MapType(Ascii),
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    69
            2 => MapGenerator(u32),
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    70
            3 => MazeSize(u32),
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    71
            4 => Seed(Ascii),
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    72
            5 => Template(u32),
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    73
            6 => Ammo(Ascii, Option<Ascii>),
13806
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13803
diff changeset
    74
            7 => Scheme(Ascii, Vec<Ascii>),
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    75
            8 => Script(Ascii),
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    76
            9 => Theme(Ascii),
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    77
            10 => DrawnMap(Ascii))
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    78
        }).boxed()
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    79
    }
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    80
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    81
    type Strategy = BoxedStrategy<GameCfg>;
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    82
}
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    83
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    84
impl Arbitrary for TeamInfo {
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    85
    type Parameters = ();
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    86
13671
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13533
diff changeset
    87
    fn arbitrary_with(_args: <Self as Arbitrary>::Parameters) -> <Self as Arbitrary>::Strategy {
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    88
        ("[a-z]+", 0u8..127u8, "[a-z]+", "[a-z]+", "[a-z]+", "[a-z]+",  0u8..127u8)
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    89
            .prop_map(|(name, color, grave, fort, voice_pack, flag, difficulty)| {
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    90
                fn hog(n: u8) -> HedgehogInfo {
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    91
                    HedgehogInfo { name: format!("hog{}", n), hat: format!("hat{}", n)}
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    92
                }
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    93
                let hedgehogs = [hog(1), hog(2), hog(3), hog(4), hog(5), hog(6), hog(7), hog(8)];
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    94
                TeamInfo {
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    95
                    name, color, grave, fort,
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    96
                    voice_pack, flag,difficulty,
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    97
                    hedgehogs, hedgehogs_number: 0
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    98
                }
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
    99
            }).boxed()
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
   100
    }
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
   101
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
   102
    type Strategy = BoxedStrategy<TeamInfo>;
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
   103
}
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
   104
13424
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   105
pub fn gen_proto_msg() -> BoxedStrategy<HWProtocolMessage> where {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   106
    let res = (0..58).no_shrink().prop_flat_map(|i| {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   107
        proto_msg_match!(i, def = Malformed,
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   108
        0 => Ping(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   109
        1 => Pong(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   110
        2 => Quit(Option<Ascii>),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   111
        //3 => Cmd
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   112
        4 => Global(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   113
        5 => Watch(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   114
        6 => ToggleServerRegisteredOnly(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   115
        7 => SuperPower(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   116
        8 => Info(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   117
        9 => Nick(Ascii),
13525
1ee192f13456 Better packing for clients
alfadur
parents: 13487
diff changeset
   118
        10 => Proto(u16),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   119
        11 => Password(Ascii, Ascii),
13803
4664da990556 Add official server feature to cargo
alfadur
parents: 13671
diff changeset
   120
        12 => Checker(u16, Ascii, Ascii),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   121
        13 => List(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   122
        14 => Chat(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   123
        15 => CreateRoom(Ascii, Option<Ascii>),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   124
        16 => JoinRoom(Ascii, Option<Ascii>),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   125
        17 => Follow(Ascii),
13438
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13437
diff changeset
   126
        18 => Rnd(Vec<Ascii>),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   127
        19 => Kick(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   128
        20 => Ban(Ascii, Ascii, u32),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   129
        21 => BanIP(Ascii, Ascii, u32),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   130
        22 => BanNick(Ascii, Ascii, u32),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   131
        23 => BanList(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   132
        24 => Unban(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   133
        //25 => SetServerVar(ServerVar),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   134
        26 => GetServerVar(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   135
        27 => RestartServer(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   136
        28 => Stats(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   137
        29 => Part(Option<Ascii>),
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
   138
        30 => Cfg(GameCfg),
13529
5359ff75da3a indulge clippy
alfadur
parents: 13525
diff changeset
   139
        31 => AddTeam(Box<TeamInfo>),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   140
        32 => RemoveTeam(Ascii),
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
   141
        33 => SetHedgehogsNumber(Ascii, u8),
c4f917c6be51 add missing message tests
alfadur
parents: 13442
diff changeset
   142
        34 => SetTeamColor(Ascii, u8),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   143
        35 => ToggleReady(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   144
        36 => StartGame(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   145
        37 => EngineMessage(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   146
        38 => RoundFinished(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   147
        39 => ToggleRestrictJoin(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   148
        40 => ToggleRestrictTeams(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   149
        41 => ToggleRegisteredOnly(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   150
        42 => RoomName(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   151
        43 => Delegate(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   152
        44 => TeamChat(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   153
        45 => MaxTeams(u8),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   154
        46 => Fix(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   155
        47 => Unfix(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   156
        48 => Greeting(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   157
        //49 => CallVote(Option<(String, Option<String>)>),
13483
d79795acaa73 Mostly implement voting
alfadur
parents: 13444
diff changeset
   158
        50 => Vote(bool),
d79795acaa73 Mostly implement voting
alfadur
parents: 13444
diff changeset
   159
        51 => ForceVote(bool),
13533
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13529
diff changeset
   160
        52 => Save(Ascii, Ascii),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   161
        53 => Delete(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   162
        54 => SaveRoom(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   163
        55 => LoadRoom(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   164
        56 => Malformed(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   165
        57 => Empty()
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   166
    )});
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   167
    res.boxed()
13426
d1368c776a4f Enable all lints from the rust-2018-idioms suite.
marmistrz
parents: 13424
diff changeset
   168
}