author | alfadur |
Mon, 09 Jul 2018 20:31:32 +0300 | |
changeset 13461 | 8697b235f236 |
parent 13450 | d79795acaa73 |
child 13462 | 7f3289a239dd |
permissions | -rw-r--r-- |
13419 | 1 |
use proptest::{ |
2 |
test_runner::{TestRunner, Reason}, |
|
3 |
arbitrary::{any, any_with, Arbitrary, StrategyFor}, |
|
4 |
strategy::{Strategy, BoxedStrategy, Just, Filter, ValueTree}, |
|
13439 | 5 |
string::RegexGeneratorValueTree, |
13419 | 6 |
}; |
7 |
||
13439 | 8 |
use server::coretypes::{GameCfg, TeamInfo, HedgehogInfo}; |
9 |
||
13419 | 10 |
use super::messages::{ |
11 |
HWProtocolMessage, HWProtocolMessage::* |
|
12 |
}; |
|
13 |
||
14 |
// Due to inability to define From between Options |
|
15 |
trait Into2<T>: Sized { fn into2(self) -> T; } |
|
16 |
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
|
17 |
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
|
18 |
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
|
19 |
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
|
20 |
} |
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 |
} |
13419 | 22 |
impl Into2<String> for Ascii { fn into2(self) -> String { self.0 } } |
23 |
impl Into2<Option<String>> for Option<Ascii>{ |
|
24 |
fn into2(self) -> Option<String> { self.map(|x| {x.0}) } |
|
25 |
} |
|
13439 | 26 |
impl Into2<Option<Vec<String>>> for Option<Vec<Ascii>>{ |
27 |
fn into2(self) -> Option<Vec<String>> { self.map(|x| {x.into2()}) } |
|
28 |
} |
|
13419 | 29 |
|
30 |
macro_rules! proto_msg_case { |
|
31 |
($val: ident()) => |
|
32 |
(Just($val)); |
|
33 |
($val: ident($arg: ty)) => |
|
34 |
(any::<$arg>().prop_map(|v| {$val(v.into2())})); |
|
35 |
($val: ident($arg1: ty, $arg2: ty)) => |
|
36 |
(any::<($arg1, $arg2)>().prop_map(|v| {$val(v.0.into2(), v.1.into2())})); |
|
37 |
($val: ident($arg1: ty, $arg2: ty, $arg3: ty)) => |
|
38 |
(any::<($arg1, $arg2, $arg3)>().prop_map(|v| {$val(v.0.into2(), v.1.into2(), v.2.into2())})); |
|
39 |
} |
|
40 |
||
41 |
macro_rules! proto_msg_match { |
|
13439 | 42 |
($var: expr, def = $default: expr, $($num: expr => $constr: ident $res: tt),*) => ( |
13419 | 43 |
match $var { |
44 |
$($num => (proto_msg_case!($constr $res)).boxed()),*, |
|
45 |
_ => Just($default).boxed() |
|
46 |
} |
|
47 |
) |
|
48 |
} |
|
49 |
||
13432 | 50 |
/// Wrapper type for generating non-empty strings |
13419 | 51 |
#[derive(Debug)] |
52 |
struct Ascii(String); |
|
53 |
||
54 |
impl Arbitrary for Ascii { |
|
55 |
type Parameters = <String as Arbitrary>::Parameters; |
|
56 |
||
57 |
fn arbitrary_with(args: Self::Parameters) -> Self::Strategy { |
|
58 |
any_with::<String>(args) |
|
59 |
.prop_filter("not ascii", |s| { |
|
60 |
s.len() > 0 && s.is_ascii() && |
|
61 |
s.find(|c| { |
|
62 |
['\0', '\n', '\x20'].contains(&c) |
|
63 |
}).is_none()}) |
|
64 |
.prop_map(Ascii) |
|
65 |
.boxed() |
|
66 |
} |
|
67 |
||
68 |
type Strategy = BoxedStrategy<Ascii>; |
|
69 |
} |
|
70 |
||
13439 | 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>; |
|
114 |
} |
|
115 |
||
13419 | 116 |
pub fn gen_proto_msg() -> BoxedStrategy<HWProtocolMessage> where { |
117 |
let res = (0..58).no_shrink().prop_flat_map(|i| { |
|
118 |
proto_msg_match!(i, def = Malformed, |
|
119 |
0 => Ping(), |
|
120 |
1 => Pong(), |
|
121 |
2 => Quit(Option<Ascii>), |
|
122 |
//3 => Cmd |
|
123 |
4 => Global(Ascii), |
|
124 |
5 => Watch(Ascii), |
|
125 |
6 => ToggleServerRegisteredOnly(), |
|
126 |
7 => SuperPower(), |
|
127 |
8 => Info(Ascii), |
|
128 |
9 => Nick(Ascii), |
|
129 |
10 => Proto(u32), |
|
130 |
11 => Password(Ascii, Ascii), |
|
131 |
12 => Checker(u32, Ascii, Ascii), |
|
132 |
13 => List(), |
|
133 |
14 => Chat(Ascii), |
|
134 |
15 => CreateRoom(Ascii, Option<Ascii>), |
|
135 |
16 => JoinRoom(Ascii, Option<Ascii>), |
|
136 |
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
|
137 |
18 => Rnd(Vec<Ascii>), |
13419 | 138 |
19 => Kick(Ascii), |
139 |
20 => Ban(Ascii, Ascii, u32), |
|
140 |
21 => BanIP(Ascii, Ascii, u32), |
|
141 |
22 => BanNick(Ascii, Ascii, u32), |
|
142 |
23 => BanList(), |
|
143 |
24 => Unban(Ascii), |
|
144 |
//25 => SetServerVar(ServerVar), |
|
145 |
26 => GetServerVar(), |
|
146 |
27 => RestartServer(), |
|
147 |
28 => Stats(), |
|
148 |
29 => Part(Option<Ascii>), |
|
13439 | 149 |
30 => Cfg(GameCfg), |
150 |
31 => AddTeam(TeamInfo), |
|
13419 | 151 |
32 => RemoveTeam(Ascii), |
13439 | 152 |
33 => SetHedgehogsNumber(Ascii, u8), |
153 |
34 => SetTeamColor(Ascii, u8), |
|
13419 | 154 |
35 => ToggleReady(), |
155 |
36 => StartGame(), |
|
156 |
37 => EngineMessage(Ascii), |
|
157 |
38 => RoundFinished(), |
|
158 |
39 => ToggleRestrictJoin(), |
|
159 |
40 => ToggleRestrictTeams(), |
|
160 |
41 => ToggleRegisteredOnly(), |
|
161 |
42 => RoomName(Ascii), |
|
162 |
43 => Delegate(Ascii), |
|
163 |
44 => TeamChat(Ascii), |
|
164 |
45 => MaxTeams(u8), |
|
165 |
46 => Fix(), |
|
166 |
47 => Unfix(), |
|
167 |
48 => Greeting(Ascii), |
|
168 |
//49 => CallVote(Option<(String, Option<String>)>), |
|
13450 | 169 |
50 => Vote(bool), |
170 |
51 => ForceVote(bool), |
|
13419 | 171 |
//52 => Save(String, String), |
172 |
53 => Delete(Ascii), |
|
173 |
54 => SaveRoom(Ascii), |
|
174 |
55 => LoadRoom(Ascii), |
|
175 |
56 => Malformed(), |
|
176 |
57 => Empty() |
|
177 |
)}); |
|
178 |
res.boxed() |
|
13421
d1368c776a4f
Enable all lints from the rust-2018-idioms suite.
marmistrz
parents:
13419
diff
changeset
|
179 |
} |