gameServer2/src/protocol/test.rs
changeset 13439 c4f917c6be51
parent 13437 a0c3431f60ac
child 13478 d79795acaa73
equal deleted inserted replaced
13438:da71e0d88a1c 13439:c4f917c6be51
     1 use proptest::{
     1 use proptest::{
     2     test_runner::{TestRunner, Reason},
     2     test_runner::{TestRunner, Reason},
     3     arbitrary::{any, any_with, Arbitrary, StrategyFor},
     3     arbitrary::{any, any_with, Arbitrary, StrategyFor},
     4     strategy::{Strategy, BoxedStrategy, Just, Filter, ValueTree},
     4     strategy::{Strategy, BoxedStrategy, Just, Filter, ValueTree},
     5     string::RegexGeneratorValueTree
     5     string::RegexGeneratorValueTree,
     6 };
     6 };
       
     7 
       
     8 use server::coretypes::{GameCfg, TeamInfo, HedgehogInfo};
     7 
     9 
     8 use super::messages::{
    10 use super::messages::{
     9     HWProtocolMessage, HWProtocolMessage::*
    11     HWProtocolMessage, HWProtocolMessage::*
    10 };
    12 };
    11 
    13 
    19 }
    21 }
    20 impl Into2<String> for Ascii { fn into2(self) -> String { self.0 } }
    22 impl Into2<String> for Ascii { fn into2(self) -> String { self.0 } }
    21 impl Into2<Option<String>> for Option<Ascii>{
    23 impl Into2<Option<String>> for Option<Ascii>{
    22     fn into2(self) -> Option<String> { self.map(|x| {x.0}) }
    24     fn into2(self) -> Option<String> { self.map(|x| {x.0}) }
    23 }
    25 }
       
    26 impl Into2<Option<Vec<String>>> for Option<Vec<Ascii>>{
       
    27     fn into2(self) -> Option<Vec<String>> { self.map(|x| {x.into2()}) }
       
    28 }
    24 
    29 
    25 macro_rules! proto_msg_case {
    30 macro_rules! proto_msg_case {
    26     ($val: ident()) =>
    31     ($val: ident()) =>
    27         (Just($val));
    32         (Just($val));
    28     ($val: ident($arg: ty)) =>
    33     ($val: ident($arg: ty)) =>
    32     ($val: ident($arg1: ty, $arg2: ty, $arg3: ty)) =>
    37     ($val: ident($arg1: ty, $arg2: ty, $arg3: ty)) =>
    33         (any::<($arg1, $arg2, $arg3)>().prop_map(|v| {$val(v.0.into2(), v.1.into2(), v.2.into2())}));
    38         (any::<($arg1, $arg2, $arg3)>().prop_map(|v| {$val(v.0.into2(), v.1.into2(), v.2.into2())}));
    34 }
    39 }
    35 
    40 
    36 macro_rules! proto_msg_match {
    41 macro_rules! proto_msg_match {
    37     ($var: expr, def = $default: ident, $($num: expr => $constr: ident $res: tt),*) => (
    42     ($var: expr, def = $default: expr, $($num: expr => $constr: ident $res: tt),*) => (
    38         match $var {
    43         match $var {
    39             $($num => (proto_msg_case!($constr $res)).boxed()),*,
    44             $($num => (proto_msg_case!($constr $res)).boxed()),*,
    40             _ => Just($default).boxed()
    45             _ => Just($default).boxed()
    41         }
    46         }
    42     )
    47     )
    59             .prop_map(Ascii)
    64             .prop_map(Ascii)
    60             .boxed()
    65             .boxed()
    61     }
    66     }
    62 
    67 
    63     type Strategy = BoxedStrategy<Ascii>;
    68     type Strategy = BoxedStrategy<Ascii>;
       
    69 }
       
    70 
       
    71 impl Arbitrary for GameCfg {
       
    72     type Parameters = ();
       
    73 
       
    74     fn arbitrary_with(args: <Self as Arbitrary>::Parameters) -> <Self as Arbitrary>::Strategy {
       
    75         use server::coretypes::GameCfg::*;
       
    76         (0..10).no_shrink().prop_flat_map(|i| {
       
    77             proto_msg_match!(i, def = FeatureSize(0),
       
    78             0 => FeatureSize(u32),
       
    79             1 => MapType(Ascii),
       
    80             2 => MapGenerator(u32),
       
    81             3 => MazeSize(u32),
       
    82             4 => Seed(Ascii),
       
    83             5 => Template(u32),
       
    84             6 => Ammo(Ascii, Option<Ascii>),
       
    85             7 => Scheme(Ascii, Option<Vec<Ascii>>),
       
    86             8 => Script(Ascii),
       
    87             9 => Theme(Ascii),
       
    88             10 => DrawnMap(Ascii))
       
    89         }).boxed()
       
    90     }
       
    91 
       
    92     type Strategy = BoxedStrategy<GameCfg>;
       
    93 }
       
    94 
       
    95 impl Arbitrary for TeamInfo {
       
    96     type Parameters = ();
       
    97 
       
    98     fn arbitrary_with(args: <Self as Arbitrary>::Parameters) -> <Self as Arbitrary>::Strategy {
       
    99         ("[a-z]+", 0u8..127u8, "[a-z]+", "[a-z]+", "[a-z]+", "[a-z]+",  0u8..127u8)
       
   100             .prop_map(|(name, color, grave, fort, voice_pack, flag, difficulty)| {
       
   101                 fn hog(n: u8) -> HedgehogInfo {
       
   102                     HedgehogInfo { name: format!("hog{}", n), hat: format!("hat{}", n)}
       
   103                 }
       
   104                 let hedgehogs = [hog(1), hog(2), hog(3), hog(4), hog(5), hog(6), hog(7), hog(8)];
       
   105                 TeamInfo {
       
   106                     name, color, grave, fort,
       
   107                     voice_pack, flag,difficulty,
       
   108                     hedgehogs, hedgehogs_number: 0
       
   109                 }
       
   110             }).boxed()
       
   111     }
       
   112 
       
   113     type Strategy = BoxedStrategy<TeamInfo>;
    64 }
   114 }
    65 
   115 
    66 pub fn gen_proto_msg() -> BoxedStrategy<HWProtocolMessage> where {
   116 pub fn gen_proto_msg() -> BoxedStrategy<HWProtocolMessage> where {
    67     let res = (0..58).no_shrink().prop_flat_map(|i| {
   117     let res = (0..58).no_shrink().prop_flat_map(|i| {
    68         proto_msg_match!(i, def = Malformed,
   118         proto_msg_match!(i, def = Malformed,
    94         //25 => SetServerVar(ServerVar),
   144         //25 => SetServerVar(ServerVar),
    95         26 => GetServerVar(),
   145         26 => GetServerVar(),
    96         27 => RestartServer(),
   146         27 => RestartServer(),
    97         28 => Stats(),
   147         28 => Stats(),
    98         29 => Part(Option<Ascii>),
   148         29 => Part(Option<Ascii>),
    99         //30 => Cfg(GameCfg),
   149         30 => Cfg(GameCfg),
   100         //31 => AddTeam(TeamInfo),
   150         31 => AddTeam(TeamInfo),
   101         32 => RemoveTeam(Ascii),
   151         32 => RemoveTeam(Ascii),
   102         //33 => SetHedgehogsNumber(String, u8),
   152         33 => SetHedgehogsNumber(Ascii, u8),
   103         //34 => SetTeamColor(String, u8),
   153         34 => SetTeamColor(Ascii, u8),
   104         35 => ToggleReady(),
   154         35 => ToggleReady(),
   105         36 => StartGame(),
   155         36 => StartGame(),
   106         37 => EngineMessage(Ascii),
   156         37 => EngineMessage(Ascii),
   107         38 => RoundFinished(),
   157         38 => RoundFinished(),
   108         39 => ToggleRestrictJoin(),
   158         39 => ToggleRestrictJoin(),