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