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