1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
pub fn format_callback(function_name: String, arg: String) -> String {
let formatted_string = &format!("window[\"{}\"]({})", function_name, arg);
formatted_string.to_string()
}
pub fn format_callback_result(
result: Result<String, String>,
callback: String,
error_callback: String,
) -> String {
match result {
Ok(res) => format_callback(callback, res),
Err(err) => format_callback(error_callback, format!("\"{}\"", err)),
}
}
#[cfg(test)]
mod test {
use crate::rpc::*;
use quickcheck_macros::quickcheck;
#[quickcheck]
fn qc_formating(f: String, a: String) -> bool {
if f != "" && a != "" {
let alen = &a.len();
let flen = &f.len();
let fc = format_callback(f, a);
let fclen = fc.len();
fclen == alen + flen + 12
} else {
true
}
}
#[quickcheck]
fn qc_format_res(result: Result<String, String>, c: String, ec: String) -> bool {
match result {
Ok(r) => {
let rlen = r.len();
let clen = c.len();
let resp = format_callback_result(Ok(r), c, ec);
let reslen = resp.len();
reslen == rlen + clen + 12
}
Err(err) => {
let eclen = ec.len();
let errlen = err.len();
let resp = format_callback_result(Err(err), c, ec);
let reslen = resp.len();
reslen == eclen + errlen + 14
}
}
}
}