gameServer2/src/protocol/test.rs
author alfadur
Tue, 03 Jul 2018 23:05:09 +0300
changeset 13437 a0c3431f60ac
parent 13433 fb104e150878
child 13439 c4f917c6be51
permissions -rw-r--r--
update proptest
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},
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
     4
    strategy::{Strategy, BoxedStrategy, Just, Filter, ValueTree},
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
     5
    string::RegexGeneratorValueTree
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
     6
};
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
     7
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
     8
use super::messages::{
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
     9
    HWProtocolMessage, HWProtocolMessage::*
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    10
};
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    11
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    12
// Due to inability to define From between Options
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    13
trait Into2<T>: Sized { fn into2(self) -> T; }
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    14
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
    15
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
    16
    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
    17
        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
    18
    }
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
}
13419
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    20
impl Into2<String> for Ascii { fn into2(self) -> String { self.0 } }
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    21
impl Into2<Option<String>> for Option<Ascii>{
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    22
    fn into2(self) -> Option<String> { self.map(|x| {x.0}) }
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    23
}
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    24
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    25
macro_rules! proto_msg_case {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    26
    ($val: ident()) =>
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    27
        (Just($val));
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    28
    ($val: ident($arg: ty)) =>
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    29
        (any::<$arg>().prop_map(|v| {$val(v.into2())}));
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    30
    ($val: ident($arg1: ty, $arg2: ty)) =>
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    31
        (any::<($arg1, $arg2)>().prop_map(|v| {$val(v.0.into2(), v.1.into2())}));
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    32
    ($val: ident($arg1: ty, $arg2: ty, $arg3: ty)) =>
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    33
        (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
    34
}
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    35
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    36
macro_rules! proto_msg_match {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    37
    ($var: expr, def = $default: ident, $($num: expr => $constr: ident $res: tt),*) => (
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    38
        match $var {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    39
            $($num => (proto_msg_case!($constr $res)).boxed()),*,
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    40
            _ => Just($default).boxed()
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    41
        }
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
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13421
diff changeset
    45
/// Wrapper type for generating non-empty strings
13419
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    46
#[derive(Debug)]
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    47
struct Ascii(String);
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    48
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    49
impl Arbitrary for Ascii {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    50
    type Parameters = <String as Arbitrary>::Parameters;
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    51
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    52
    fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    53
        any_with::<String>(args)
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    54
            .prop_filter("not ascii", |s| {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    55
                s.len() > 0 && s.is_ascii() &&
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    56
                    s.find(|c| {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    57
                        ['\0', '\n', '\x20'].contains(&c)
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    58
                    }).is_none()})
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    59
            .prop_map(Ascii)
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    60
            .boxed()
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    61
    }
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    62
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    63
    type Strategy = BoxedStrategy<Ascii>;
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    64
}
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    65
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    66
pub fn gen_proto_msg() -> BoxedStrategy<HWProtocolMessage> where {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    67
    let res = (0..58).no_shrink().prop_flat_map(|i| {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    68
        proto_msg_match!(i, def = Malformed,
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    69
        0 => Ping(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    70
        1 => Pong(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    71
        2 => Quit(Option<Ascii>),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    72
        //3 => Cmd
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    73
        4 => Global(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    74
        5 => Watch(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    75
        6 => ToggleServerRegisteredOnly(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    76
        7 => SuperPower(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    77
        8 => Info(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    78
        9 => Nick(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    79
        10 => Proto(u32),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    80
        11 => Password(Ascii, Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    81
        12 => Checker(u32, Ascii, Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    82
        13 => List(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    83
        14 => Chat(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    84
        15 => CreateRoom(Ascii, Option<Ascii>),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    85
        16 => JoinRoom(Ascii, Option<Ascii>),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    86
        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
    87
        18 => Rnd(Vec<Ascii>),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    88
        19 => Kick(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    89
        20 => Ban(Ascii, Ascii, u32),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    90
        21 => BanIP(Ascii, Ascii, u32),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    91
        22 => BanNick(Ascii, Ascii, u32),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    92
        23 => BanList(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    93
        24 => Unban(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    94
        //25 => SetServerVar(ServerVar),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    95
        26 => GetServerVar(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    96
        27 => RestartServer(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    97
        28 => Stats(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    98
        29 => Part(Option<Ascii>),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    99
        //30 => Cfg(GameCfg),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   100
        //31 => AddTeam(TeamInfo),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   101
        32 => RemoveTeam(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   102
        //33 => SetHedgehogsNumber(String, u8),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   103
        //34 => SetTeamColor(String, u8),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   104
        35 => ToggleReady(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   105
        36 => StartGame(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   106
        37 => EngineMessage(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   107
        38 => RoundFinished(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   108
        39 => ToggleRestrictJoin(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   109
        40 => ToggleRestrictTeams(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   110
        41 => ToggleRegisteredOnly(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   111
        42 => RoomName(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   112
        43 => Delegate(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   113
        44 => TeamChat(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   114
        45 => MaxTeams(u8),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   115
        46 => Fix(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   116
        47 => Unfix(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   117
        48 => Greeting(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   118
        //49 => CallVote(Option<(String, Option<String>)>),
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13421
diff changeset
   119
        50 => Vote(Ascii),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   120
        51 => ForceVote(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   121
        //52 => Save(String, String),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   122
        53 => Delete(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   123
        54 => SaveRoom(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   124
        55 => LoadRoom(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   125
        56 => Malformed(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   126
        57 => Empty()
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   127
    )});
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   128
    res.boxed()
13421
d1368c776a4f Enable all lints from the rust-2018-idioms suite.
marmistrz
parents: 13419
diff changeset
   129
}