author | unc0rr |
Mon, 10 Dec 2018 22:44:46 +0100 | |
changeset 14436 | 06672690d71b |
parent 13810 | gameServer2/src/protocol/test.rs@0463a4221327 |
child 14478 | 98ef2913ec73 |
permissions | -rw-r--r-- |
13419 | 1 |
use proptest::{ |
2 |
test_runner::{TestRunner, Reason}, |
|
3 |
arbitrary::{any, any_with, Arbitrary, StrategyFor}, |
|
13810 | 4 |
strategy::{Strategy, BoxedStrategy, Just, Map} |
13419 | 5 |
}; |
6 |
||
13666 | 7 |
use crate::server::coretypes::{GameCfg, TeamInfo, HedgehogInfo}; |
13439 | 8 |
|
13419 | 9 |
use super::messages::{ |
10 |
HWProtocolMessage, HWProtocolMessage::* |
|
11 |
}; |
|
12 |
||
13 |
// Due to inability to define From between Options |
|
14 |
trait Into2<T>: Sized { fn into2(self) -> T; } |
|
15 |
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
|
16 |
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
|
17 |
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
|
18 |
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
|
19 |
} |
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
|
20 |
} |
13419 | 21 |
impl Into2<String> for Ascii { fn into2(self) -> String { self.0 } } |
22 |
impl Into2<Option<String>> for Option<Ascii>{ |
|
23 |
fn into2(self) -> Option<String> { self.map(|x| {x.0}) } |
|
24 |
} |
|
25 |
||
26 |
macro_rules! proto_msg_case { |
|
27 |
($val: ident()) => |
|
28 |
(Just($val)); |
|
29 |
($val: ident($arg: ty)) => |
|
30 |
(any::<$arg>().prop_map(|v| {$val(v.into2())})); |
|
31 |
($val: ident($arg1: ty, $arg2: ty)) => |
|
32 |
(any::<($arg1, $arg2)>().prop_map(|v| {$val(v.0.into2(), v.1.into2())})); |
|
33 |
($val: ident($arg1: ty, $arg2: ty, $arg3: ty)) => |
|
34 |
(any::<($arg1, $arg2, $arg3)>().prop_map(|v| {$val(v.0.into2(), v.1.into2(), v.2.into2())})); |
|
35 |
} |
|
36 |
||
37 |
macro_rules! proto_msg_match { |
|
13439 | 38 |
($var: expr, def = $default: expr, $($num: expr => $constr: ident $res: tt),*) => ( |
13419 | 39 |
match $var { |
40 |
$($num => (proto_msg_case!($constr $res)).boxed()),*, |
|
41 |
_ => Just($default).boxed() |
|
42 |
} |
|
43 |
) |
|
44 |
} |
|
45 |
||
13432 | 46 |
/// Wrapper type for generating non-empty strings |
13419 | 47 |
#[derive(Debug)] |
48 |
struct Ascii(String); |
|
49 |
||
50 |
impl Arbitrary for Ascii { |
|
51 |
type Parameters = <String as Arbitrary>::Parameters; |
|
52 |
||
13666 | 53 |
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy { |
13462 | 54 |
"[a-zA-Z0-9]+".prop_map(Ascii).boxed() |
13419 | 55 |
} |
56 |
||
57 |
type Strategy = BoxedStrategy<Ascii>; |
|
58 |
} |
|
59 |
||
13439 | 60 |
impl Arbitrary for GameCfg { |
61 |
type Parameters = (); |
|
62 |
||
13666 | 63 |
fn arbitrary_with(_args: <Self as Arbitrary>::Parameters) -> <Self as Arbitrary>::Strategy { |
64 |
use crate::server::coretypes::GameCfg::*; |
|
13439 | 65 |
(0..10).no_shrink().prop_flat_map(|i| { |
66 |
proto_msg_match!(i, def = FeatureSize(0), |
|
67 |
0 => FeatureSize(u32), |
|
68 |
1 => MapType(Ascii), |
|
69 |
2 => MapGenerator(u32), |
|
70 |
3 => MazeSize(u32), |
|
71 |
4 => Seed(Ascii), |
|
72 |
5 => Template(u32), |
|
73 |
6 => Ammo(Ascii, Option<Ascii>), |
|
13775 | 74 |
7 => Scheme(Ascii, Vec<Ascii>), |
13439 | 75 |
8 => Script(Ascii), |
76 |
9 => Theme(Ascii), |
|
77 |
10 => DrawnMap(Ascii)) |
|
78 |
}).boxed() |
|
79 |
} |
|
80 |
||
81 |
type Strategy = BoxedStrategy<GameCfg>; |
|
82 |
} |
|
83 |
||
84 |
impl Arbitrary for TeamInfo { |
|
85 |
type Parameters = (); |
|
86 |
||
13666 | 87 |
fn arbitrary_with(_args: <Self as Arbitrary>::Parameters) -> <Self as Arbitrary>::Strategy { |
13439 | 88 |
("[a-z]+", 0u8..127u8, "[a-z]+", "[a-z]+", "[a-z]+", "[a-z]+", 0u8..127u8) |
89 |
.prop_map(|(name, color, grave, fort, voice_pack, flag, difficulty)| { |
|
90 |
fn hog(n: u8) -> HedgehogInfo { |
|
91 |
HedgehogInfo { name: format!("hog{}", n), hat: format!("hat{}", n)} |
|
92 |
} |
|
93 |
let hedgehogs = [hog(1), hog(2), hog(3), hog(4), hog(5), hog(6), hog(7), hog(8)]; |
|
94 |
TeamInfo { |
|
95 |
name, color, grave, fort, |
|
96 |
voice_pack, flag,difficulty, |
|
97 |
hedgehogs, hedgehogs_number: 0 |
|
98 |
} |
|
99 |
}).boxed() |
|
100 |
} |
|
101 |
||
102 |
type Strategy = BoxedStrategy<TeamInfo>; |
|
103 |
} |
|
104 |
||
13419 | 105 |
pub fn gen_proto_msg() -> BoxedStrategy<HWProtocolMessage> where { |
106 |
let res = (0..58).no_shrink().prop_flat_map(|i| { |
|
107 |
proto_msg_match!(i, def = Malformed, |
|
108 |
0 => Ping(), |
|
109 |
1 => Pong(), |
|
110 |
2 => Quit(Option<Ascii>), |
|
111 |
//3 => Cmd |
|
112 |
4 => Global(Ascii), |
|
113 |
5 => Watch(Ascii), |
|
114 |
6 => ToggleServerRegisteredOnly(), |
|
115 |
7 => SuperPower(), |
|
116 |
8 => Info(Ascii), |
|
117 |
9 => Nick(Ascii), |
|
13486 | 118 |
10 => Proto(u16), |
13419 | 119 |
11 => Password(Ascii, Ascii), |
13771 | 120 |
12 => Checker(u16, Ascii, Ascii), |
13419 | 121 |
13 => List(), |
122 |
14 => Chat(Ascii), |
|
123 |
15 => CreateRoom(Ascii, Option<Ascii>), |
|
124 |
16 => JoinRoom(Ascii, Option<Ascii>), |
|
125 |
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
|
126 |
18 => Rnd(Vec<Ascii>), |
13419 | 127 |
19 => Kick(Ascii), |
128 |
20 => Ban(Ascii, Ascii, u32), |
|
129 |
21 => BanIP(Ascii, Ascii, u32), |
|
130 |
22 => BanNick(Ascii, Ascii, u32), |
|
131 |
23 => BanList(), |
|
132 |
24 => Unban(Ascii), |
|
133 |
//25 => SetServerVar(ServerVar), |
|
134 |
26 => GetServerVar(), |
|
135 |
27 => RestartServer(), |
|
136 |
28 => Stats(), |
|
137 |
29 => Part(Option<Ascii>), |
|
13439 | 138 |
30 => Cfg(GameCfg), |
13500 | 139 |
31 => AddTeam(Box<TeamInfo>), |
13419 | 140 |
32 => RemoveTeam(Ascii), |
13439 | 141 |
33 => SetHedgehogsNumber(Ascii, u8), |
142 |
34 => SetTeamColor(Ascii, u8), |
|
13419 | 143 |
35 => ToggleReady(), |
144 |
36 => StartGame(), |
|
145 |
37 => EngineMessage(Ascii), |
|
146 |
38 => RoundFinished(), |
|
147 |
39 => ToggleRestrictJoin(), |
|
148 |
40 => ToggleRestrictTeams(), |
|
149 |
41 => ToggleRegisteredOnly(), |
|
150 |
42 => RoomName(Ascii), |
|
151 |
43 => Delegate(Ascii), |
|
152 |
44 => TeamChat(Ascii), |
|
153 |
45 => MaxTeams(u8), |
|
154 |
46 => Fix(), |
|
155 |
47 => Unfix(), |
|
156 |
48 => Greeting(Ascii), |
|
157 |
//49 => CallVote(Option<(String, Option<String>)>), |
|
13450 | 158 |
50 => Vote(bool), |
159 |
51 => ForceVote(bool), |
|
13528 | 160 |
52 => Save(Ascii, Ascii), |
13419 | 161 |
53 => Delete(Ascii), |
162 |
54 => SaveRoom(Ascii), |
|
163 |
55 => LoadRoom(Ascii), |
|
164 |
56 => Malformed(), |
|
165 |
57 => Empty() |
|
166 |
)}); |
|
167 |
res.boxed() |
|
13421
d1368c776a4f
Enable all lints from the rust-2018-idioms suite.
marmistrz
parents:
13419
diff
changeset
|
168 |
} |