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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use errors::Result;
use serde_json::value::{Value};
use context::ValueNumber;
pub type TesterFn = fn(Option<Value>, Vec<Value>) -> Result<bool>;
fn number_args_allowed(tester_name: &str, max: usize, args_len: usize) -> Result<()> {
if max == 0 && args_len > max {
bail!(
"Tester `{}` was called with some args but this test doesn't take args",
tester_name
);
}
if args_len > max {
bail!(
"Tester `{}` was called with {} args, the max number is {}",
tester_name, args_len, max
);
}
Ok(())
}
fn value_defined(tester_name: &str, value: &Option<Value>) -> Result<()> {
if value.is_none() {
bail!("Tester `{}` was called on an undefined variable", tester_name);
}
Ok(())
}
pub fn defined(value: Option<Value>, params: Vec<Value>) -> Result<bool> {
number_args_allowed("defined", 0, params.len())?;
Ok(value.is_some())
}
pub fn undefined(value: Option<Value>, params: Vec<Value>) -> Result<bool> {
number_args_allowed("undefined", 0, params.len())?;
Ok(value.is_none())
}
pub fn string(value: Option<Value>, params: Vec<Value>) -> Result<bool> {
number_args_allowed("string", 0, params.len())?;
value_defined("string", &value)?;
match value {
Some(Value::String(_)) => Ok(true),
_ => Ok(false)
}
}
pub fn number(value: Option<Value>, params: Vec<Value>) -> Result<bool> {
number_args_allowed("number", 0, params.len())?;
value_defined("number", &value)?;
match value {
Some(Value::Number(_)) => Ok(true),
_ => Ok(false)
}
}
pub fn odd(value: Option<Value>, params: Vec<Value>) -> Result<bool> {
number_args_allowed("odd", 0, params.len())?;
value_defined("odd", &value)?;
match value.and_then(|v| v.to_number().ok()) {
Some(f) => Ok(f % 2.0 != 0.0),
_ => bail!("Tester `odd` was called on a variable that isn't a number")
}
}
pub fn even(value: Option<Value>, params: Vec<Value>) -> Result<bool> {
number_args_allowed("even", 0, params.len())?;
value_defined("even", &value)?;
let is_odd = odd(value, params)?;
Ok(!is_odd)
}
pub fn divisible_by(value: Option<Value>, params: Vec<Value>) -> Result<bool> {
number_args_allowed("divisibleby", 1, params.len())?;
value_defined("divisibleby", &value)?;
match value.and_then(|v| v.to_number().ok()) {
Some(val) => match params.first().and_then(|v| v.to_number().ok()) {
Some(p) => Ok(val % p == 0.0),
None => bail!("Tester `divisibleby` was called with a parameter that isn't a number")
},
None => bail!("Tester `divisibleby` was called on a variable that isn't a number")
}
}
pub fn iterable(value: Option<Value>, params: Vec<Value>) -> Result<bool> {
number_args_allowed("iterable", 0, params.len())?;
value_defined("iterable", &value)?;
Ok(value.unwrap().is_array())
}
#[cfg(test)]
mod tests {
use super::{defined, string, divisible_by, iterable};
use serde_json::value::{to_value};
#[test]
fn test_number_args_ok() {
assert!(defined(None, vec![]).is_ok())
}
#[test]
fn test_too_many_args() {
assert!(defined(None, vec![to_value(1).unwrap()]).is_err())
}
#[test]
fn test_value_defined() {
assert!(string(None, vec![]).is_err())
}
#[test]
fn test_divisible_by() {
let tests = vec![
(1.0, 2.0, false),
(4.0, 2.0, true),
(4.0, 2.1, false),
(10.0, 2.0, true),
(10.0, 0.0, false),
];
for (val, divisor, expected) in tests {
assert_eq!(
divisible_by(Some(to_value(val).unwrap()), vec![to_value(divisor).unwrap()]).unwrap(),
expected
);
}
}
#[test]
fn test_iterable() {
assert_eq!(iterable(Some(to_value(vec!["1"]).unwrap()), vec![]).unwrap(), true);
assert_eq!(iterable(Some(to_value(1).unwrap()), vec![]).unwrap(), false);
assert_eq!(iterable(Some(to_value("hello").unwrap()), vec![]).unwrap(), false);
}
}