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