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