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