gameServer2/src/protocol/test.rs
author alfadur
Thu, 21 Jun 2018 17:23:10 -0400
changeset 13419 81e0ed105f5d
child 13421 d1368c776a4f
permissions -rw-r--r--
implementation of team related messages
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
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    40
#[derive(Debug)]
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    41
struct Ascii(String);
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    42
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    43
struct AsciiValueTree(RegexGeneratorValueTree<String>);
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    44
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    45
impl ValueTree for AsciiValueTree {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    46
    type Value = Ascii;
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    47
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    48
    fn current(&self) -> Self::Value { Ascii(self.0.current()) }
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    49
    fn simplify(&mut self) -> bool { self.0.simplify() }
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    50
    fn complicate(&mut self) -> bool { self.0.complicate() }
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    51
}
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 {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    57
        any_with::<String>(args)
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    58
            .prop_filter("not ascii", |s| {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    59
                s.len() > 0 && s.is_ascii() &&
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    60
                    s.find(|c| {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    61
                        ['\0', '\n', '\x20'].contains(&c)
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    62
                    }).is_none()})
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    63
            .prop_map(Ascii)
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    64
            .boxed()
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    65
    }
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    66
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    67
    type Strategy = BoxedStrategy<Ascii>;
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    68
    type ValueTree = Box<ValueTree<Value = Ascii>>;
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    69
}
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    70
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    71
pub fn gen_proto_msg() -> BoxedStrategy<HWProtocolMessage> where {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    72
    let res = (0..58).no_shrink().prop_flat_map(|i| {
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    73
        proto_msg_match!(i, def = Malformed,
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    74
        0 => Ping(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    75
        1 => Pong(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    76
        2 => Quit(Option<Ascii>),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    77
        //3 => Cmd
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    78
        4 => Global(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    79
        5 => Watch(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    80
        6 => ToggleServerRegisteredOnly(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    81
        7 => SuperPower(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    82
        8 => Info(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    83
        9 => Nick(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    84
        10 => Proto(u32),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    85
        11 => Password(Ascii, Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    86
        12 => Checker(u32, Ascii, Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    87
        13 => List(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    88
        14 => Chat(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    89
        15 => CreateRoom(Ascii, Option<Ascii>),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    90
        16 => JoinRoom(Ascii, Option<Ascii>),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    91
        17 => Follow(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    92
        //18 => Rnd(Vec<String>),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    93
        19 => Kick(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    94
        20 => Ban(Ascii, Ascii, u32),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    95
        21 => BanIP(Ascii, Ascii, u32),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    96
        22 => BanNick(Ascii, Ascii, u32),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    97
        23 => BanList(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    98
        24 => Unban(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
    99
        //25 => SetServerVar(ServerVar),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   100
        26 => GetServerVar(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   101
        27 => RestartServer(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   102
        28 => Stats(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   103
        29 => Part(Option<Ascii>),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   104
        //30 => Cfg(GameCfg),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   105
        //31 => AddTeam(TeamInfo),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   106
        32 => RemoveTeam(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   107
        //33 => SetHedgehogsNumber(String, u8),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   108
        //34 => SetTeamColor(String, u8),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   109
        35 => ToggleReady(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   110
        36 => StartGame(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   111
        37 => EngineMessage(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   112
        38 => RoundFinished(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   113
        39 => ToggleRestrictJoin(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   114
        40 => ToggleRestrictTeams(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   115
        41 => ToggleRegisteredOnly(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   116
        42 => RoomName(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   117
        43 => Delegate(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   118
        44 => TeamChat(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   119
        45 => MaxTeams(u8),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   120
        46 => Fix(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   121
        47 => Unfix(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   122
        48 => Greeting(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   123
        //49 => CallVote(Option<(String, Option<String>)>),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   124
        50 => Vote(String),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   125
        51 => ForceVote(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   126
        //52 => Save(String, String),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   127
        53 => Delete(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   128
        54 => SaveRoom(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   129
        55 => LoadRoom(Ascii),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   130
        56 => Malformed(),
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   131
        57 => Empty()
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   132
    )});
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   133
    res.boxed()
81e0ed105f5d implementation of team related messages
alfadur
parents:
diff changeset
   134
}