author | alfadur |
Tue, 03 Jul 2018 23:05:09 +0300 | |
changeset 13442 | a0c3431f60ac |
parent 13438 | fb104e150878 |
child 13444 | c4f917c6be51 |
permissions | -rw-r--r-- |
13424 | 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 } } |
|
13438
fb104e150878
Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13437
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:
13437
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:
13437
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:
13437
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:
13437
diff
changeset
|
19 |
} |
13424 | 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 |
||
13437 | 45 |
/// Wrapper type for generating non-empty strings |
13424 | 46 |
#[derive(Debug)] |
47 |
struct Ascii(String); |
|
48 |
||
49 |
impl Arbitrary for Ascii { |
|
50 |
type Parameters = <String as Arbitrary>::Parameters; |
|
51 |
||
52 |
fn arbitrary_with(args: Self::Parameters) -> Self::Strategy { |
|
53 |
any_with::<String>(args) |
|
54 |
.prop_filter("not ascii", |s| { |
|
55 |
s.len() > 0 && s.is_ascii() && |
|
56 |
s.find(|c| { |
|
57 |
['\0', '\n', '\x20'].contains(&c) |
|
58 |
}).is_none()}) |
|
59 |
.prop_map(Ascii) |
|
60 |
.boxed() |
|
61 |
} |
|
62 |
||
63 |
type Strategy = BoxedStrategy<Ascii>; |
|
64 |
} |
|
65 |
||
66 |
pub fn gen_proto_msg() -> BoxedStrategy<HWProtocolMessage> where { |
|
67 |
let res = (0..58).no_shrink().prop_flat_map(|i| { |
|
68 |
proto_msg_match!(i, def = Malformed, |
|
69 |
0 => Ping(), |
|
70 |
1 => Pong(), |
|
71 |
2 => Quit(Option<Ascii>), |
|
72 |
//3 => Cmd |
|
73 |
4 => Global(Ascii), |
|
74 |
5 => Watch(Ascii), |
|
75 |
6 => ToggleServerRegisteredOnly(), |
|
76 |
7 => SuperPower(), |
|
77 |
8 => Info(Ascii), |
|
78 |
9 => Nick(Ascii), |
|
79 |
10 => Proto(u32), |
|
80 |
11 => Password(Ascii, Ascii), |
|
81 |
12 => Checker(u32, Ascii, Ascii), |
|
82 |
13 => List(), |
|
83 |
14 => Chat(Ascii), |
|
84 |
15 => CreateRoom(Ascii, Option<Ascii>), |
|
85 |
16 => JoinRoom(Ascii, Option<Ascii>), |
|
86 |
17 => Follow(Ascii), |
|
13438
fb104e150878
Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13437
diff
changeset
|
87 |
18 => Rnd(Vec<Ascii>), |
13424 | 88 |
19 => Kick(Ascii), |
89 |
20 => Ban(Ascii, Ascii, u32), |
|
90 |
21 => BanIP(Ascii, Ascii, u32), |
|
91 |
22 => BanNick(Ascii, Ascii, u32), |
|
92 |
23 => BanList(), |
|
93 |
24 => Unban(Ascii), |
|
94 |
//25 => SetServerVar(ServerVar), |
|
95 |
26 => GetServerVar(), |
|
96 |
27 => RestartServer(), |
|
97 |
28 => Stats(), |
|
98 |
29 => Part(Option<Ascii>), |
|
99 |
//30 => Cfg(GameCfg), |
|
100 |
//31 => AddTeam(TeamInfo), |
|
101 |
32 => RemoveTeam(Ascii), |
|
102 |
//33 => SetHedgehogsNumber(String, u8), |
|
103 |
//34 => SetTeamColor(String, u8), |
|
104 |
35 => ToggleReady(), |
|
105 |
36 => StartGame(), |
|
106 |
37 => EngineMessage(Ascii), |
|
107 |
38 => RoundFinished(), |
|
108 |
39 => ToggleRestrictJoin(), |
|
109 |
40 => ToggleRestrictTeams(), |
|
110 |
41 => ToggleRegisteredOnly(), |
|
111 |
42 => RoomName(Ascii), |
|
112 |
43 => Delegate(Ascii), |
|
113 |
44 => TeamChat(Ascii), |
|
114 |
45 => MaxTeams(u8), |
|
115 |
46 => Fix(), |
|
116 |
47 => Unfix(), |
|
117 |
48 => Greeting(Ascii), |
|
118 |
//49 => CallVote(Option<(String, Option<String>)>), |
|
13437 | 119 |
50 => Vote(Ascii), |
13424 | 120 |
51 => ForceVote(Ascii), |
121 |
//52 => Save(String, String), |
|
122 |
53 => Delete(Ascii), |
|
123 |
54 => SaveRoom(Ascii), |
|
124 |
55 => LoadRoom(Ascii), |
|
125 |
56 => Malformed(), |
|
126 |
57 => Empty() |
|
127 |
)}); |
|
128 |
res.boxed() |
|
13426
d1368c776a4f
Enable all lints from the rust-2018-idioms suite.
marmistrz
parents:
13424
diff
changeset
|
129 |
} |