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