author | alfadur |
Mon, 02 Jul 2018 16:25:49 +0300 | |
changeset 13432 | ee3fa3b8809d |
parent 13421 | d1368c776a4f |
child 13433 | fb104e150878 |
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}, |
|
5 |
string::RegexGeneratorValueTree |
|
6 |
}; |
|
7 |
||
8 |
use super::messages::{ |
|
9 |
HWProtocolMessage, HWProtocolMessage::* |
|
10 |
}; |
|
11 |
||
12 |
// Due to inability to define From between Options |
|
13 |
trait Into2<T>: Sized { fn into2(self) -> T; } |
|
14 |
impl <T> Into2<T> for T { fn into2(self) -> T { self } } |
|
15 |
impl Into2<String> for Ascii { fn into2(self) -> String { self.0 } } |
|
16 |
impl Into2<Option<String>> for Option<Ascii>{ |
|
17 |
fn into2(self) -> Option<String> { self.map(|x| {x.0}) } |
|
18 |
} |
|
19 |
||
20 |
macro_rules! proto_msg_case { |
|
21 |
($val: ident()) => |
|
22 |
(Just($val)); |
|
23 |
($val: ident($arg: ty)) => |
|
24 |
(any::<$arg>().prop_map(|v| {$val(v.into2())})); |
|
25 |
($val: ident($arg1: ty, $arg2: ty)) => |
|
26 |
(any::<($arg1, $arg2)>().prop_map(|v| {$val(v.0.into2(), v.1.into2())})); |
|
27 |
($val: ident($arg1: ty, $arg2: ty, $arg3: ty)) => |
|
28 |
(any::<($arg1, $arg2, $arg3)>().prop_map(|v| {$val(v.0.into2(), v.1.into2(), v.2.into2())})); |
|
29 |
} |
|
30 |
||
31 |
macro_rules! proto_msg_match { |
|
32 |
($var: expr, def = $default: ident, $($num: expr => $constr: ident $res: tt),*) => ( |
|
33 |
match $var { |
|
34 |
$($num => (proto_msg_case!($constr $res)).boxed()),*, |
|
35 |
_ => Just($default).boxed() |
|
36 |
} |
|
37 |
) |
|
38 |
} |
|
39 |
||
13432 | 40 |
/// Wrapper type for generating non-empty strings |
13419 | 41 |
#[derive(Debug)] |
42 |
struct Ascii(String); |
|
43 |
||
44 |
struct AsciiValueTree(RegexGeneratorValueTree<String>); |
|
45 |
||
46 |
impl ValueTree for AsciiValueTree { |
|
47 |
type Value = Ascii; |
|
48 |
||
49 |
fn current(&self) -> Self::Value { Ascii(self.0.current()) } |
|
50 |
fn simplify(&mut self) -> bool { self.0.simplify() } |
|
51 |
fn complicate(&mut self) -> bool { self.0.complicate() } |
|
52 |
} |
|
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>; |
|
13421
d1368c776a4f
Enable all lints from the rust-2018-idioms suite.
marmistrz
parents:
13419
diff
changeset
|
69 |
type ValueTree = Box<dyn ValueTree<Value = Ascii>>; |
13419 | 70 |
} |
71 |
||
72 |
pub fn gen_proto_msg() -> BoxedStrategy<HWProtocolMessage> where { |
|
73 |
let res = (0..58).no_shrink().prop_flat_map(|i| { |
|
74 |
proto_msg_match!(i, def = Malformed, |
|
75 |
0 => Ping(), |
|
76 |
1 => Pong(), |
|
77 |
2 => Quit(Option<Ascii>), |
|
78 |
//3 => Cmd |
|
79 |
4 => Global(Ascii), |
|
80 |
5 => Watch(Ascii), |
|
81 |
6 => ToggleServerRegisteredOnly(), |
|
82 |
7 => SuperPower(), |
|
83 |
8 => Info(Ascii), |
|
84 |
9 => Nick(Ascii), |
|
85 |
10 => Proto(u32), |
|
86 |
11 => Password(Ascii, Ascii), |
|
87 |
12 => Checker(u32, Ascii, Ascii), |
|
88 |
13 => List(), |
|
89 |
14 => Chat(Ascii), |
|
90 |
15 => CreateRoom(Ascii, Option<Ascii>), |
|
91 |
16 => JoinRoom(Ascii, Option<Ascii>), |
|
92 |
17 => Follow(Ascii), |
|
93 |
//18 => Rnd(Vec<String>), |
|
94 |
19 => Kick(Ascii), |
|
95 |
20 => Ban(Ascii, Ascii, u32), |
|
96 |
21 => BanIP(Ascii, Ascii, u32), |
|
97 |
22 => BanNick(Ascii, Ascii, u32), |
|
98 |
23 => BanList(), |
|
99 |
24 => Unban(Ascii), |
|
100 |
//25 => SetServerVar(ServerVar), |
|
101 |
26 => GetServerVar(), |
|
102 |
27 => RestartServer(), |
|
103 |
28 => Stats(), |
|
104 |
29 => Part(Option<Ascii>), |
|
105 |
//30 => Cfg(GameCfg), |
|
106 |
//31 => AddTeam(TeamInfo), |
|
107 |
32 => RemoveTeam(Ascii), |
|
108 |
//33 => SetHedgehogsNumber(String, u8), |
|
109 |
//34 => SetTeamColor(String, u8), |
|
110 |
35 => ToggleReady(), |
|
111 |
36 => StartGame(), |
|
112 |
37 => EngineMessage(Ascii), |
|
113 |
38 => RoundFinished(), |
|
114 |
39 => ToggleRestrictJoin(), |
|
115 |
40 => ToggleRestrictTeams(), |
|
116 |
41 => ToggleRegisteredOnly(), |
|
117 |
42 => RoomName(Ascii), |
|
118 |
43 => Delegate(Ascii), |
|
119 |
44 => TeamChat(Ascii), |
|
120 |
45 => MaxTeams(u8), |
|
121 |
46 => Fix(), |
|
122 |
47 => Unfix(), |
|
123 |
48 => Greeting(Ascii), |
|
124 |
//49 => CallVote(Option<(String, Option<String>)>), |
|
13432 | 125 |
50 => Vote(Ascii), |
13419 | 126 |
51 => ForceVote(Ascii), |
127 |
//52 => Save(String, String), |
|
128 |
53 => Delete(Ascii), |
|
129 |
54 => SaveRoom(Ascii), |
|
130 |
55 => LoadRoom(Ascii), |
|
131 |
56 => Malformed(), |
|
132 |
57 => Empty() |
|
133 |
)}); |
|
134 |
res.boxed() |
|
13421
d1368c776a4f
Enable all lints from the rust-2018-idioms suite.
marmistrz
parents:
13419
diff
changeset
|
135 |
} |