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