-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunit_expression_tests.rs
319 lines (273 loc) · 9.96 KB
/
unit_expression_tests.rs
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#[cfg(test)]
mod unit_tests {
use plox::{expressions::*, rules::TWarningRule, PluginData};
fn init() {
let _ = env_logger::builder().is_test(true).try_init();
}
const A: &str = "a.esp";
const B: &str = "b.esp";
const C: &str = "c.esp";
const D: &str = "d.esp";
const E: &str = "e.esp";
const F: &str = "f.esp";
const X: &str = "x.esp";
const Y: &str = "y.esp";
fn e(str: &str) -> Expression {
Atomic::from(str).into()
}
fn get_mods() -> Vec<PluginData> {
[A, B, C, D, E, F]
.iter()
.map(|e| PluginData::new(e.to_string(), 0))
.collect::<Vec<_>>()
}
#[test]
fn evaluate_all() {
init();
// [ALL] is true if A and B are true
{
let expr = ALL::new(vec![e(A), e(B)]);
assert!(expr.eval(&get_mods()).is_some());
}
// [ALL] is false if A is true and B is not true
{
let expr = ALL::new(vec![e(A), e(X)]);
assert!(expr.eval(&get_mods()).is_none());
}
// [ALL] is false if A is not true and B is true
{
let expr = ALL::new(vec![e(X), e(A)]);
assert!(expr.eval(&get_mods()).is_none());
}
// [ALL] is false if A is not true and B is not true
{
let expr = ALL::new(vec![e(X), e(Y)]);
assert!(expr.eval(&get_mods()).is_none());
}
}
#[test]
fn evaluate_any() {
init();
// [ANY] is true if A and B are true
{
let expr = ANY::new(vec![e(A), e(B)]);
assert!(expr.eval(&get_mods()).is_some());
}
// [ANY] is true if A is true and B is not true
{
let expr = ANY::new(vec![e(A), e(X)]);
assert!(expr.eval(&get_mods()).is_some());
}
// [ANY] is true if A is not true and B is true
{
let expr = ANY::new(vec![e(X), e(A)]);
assert!(expr.eval(&get_mods()).is_some());
}
// [ANY] is false if A is not true and B is not true
{
let expr = ANY::new(vec![e(X), e(Y)]);
assert!(expr.eval(&get_mods()).is_none());
}
}
#[test]
fn evaluate_not() {
init();
// [NOT] is true if A is not true
{
let expr = NOT::new(e(X));
assert!(expr.eval(&get_mods()).is_some());
}
// [NOT] is false if A is true
{
let expr = NOT::new(e(A));
assert!(expr.eval(&get_mods()).is_none());
}
}
#[test]
fn evaluate_size() {
init();
let mods = [A, B, C, D, E, F]
.iter()
.enumerate()
.map(|(i, e)| PluginData::new(e.to_string(), (i + 1) as u64))
.collect::<Vec<_>>();
// [SIZE] is true if the plugin size matches the given size
{
let expr = SIZE::new(Atomic::from(A), 1_u64, false);
assert!(expr.eval(&mods).is_some());
}
// [SIZE] is true if the plugin size does not matches the given size and is negated
{
let expr = SIZE::new(Atomic::from(A), 2_u64, true);
assert!(expr.eval(&mods).is_some());
}
// [SIZE] is false if the plugin size does not match the given size
{
let expr = SIZE::new(Atomic::from(A), 2_u64, false);
assert!(expr.eval(&mods).is_none());
}
}
#[test]
fn evaluate_desc() {
init();
let mods = [A, B, C, D, E, F]
.iter()
.map(|e| PluginData {
name: e.to_string(),
size: 0_u64,
description: Some("description".to_string()),
version: None,
masters: None,
})
.collect::<Vec<_>>();
// [DESC] is true if the plugin description matches the given description
{
let expr = DESC::new(Atomic::from(A), "description".to_string(), false);
assert!(expr.eval(&mods).is_some());
}
// [DESC] is true if the plugin description matches the given description with regex
{
let expr = DESC::new(Atomic::from(A), "des*".to_string(), false);
assert!(expr.eval(&mods).is_some());
}
// [DESC] is false if the plugin description does not match the given description
{
let expr = DESC::new(Atomic::from(A), "another description".to_string(), false);
assert!(expr.eval(&mods).is_none());
}
// [DESC] is true if the plugin description does not matches the given description and is negated is true
{
let expr = DESC::new(Atomic::from(A), "another description".to_string(), true);
assert!(expr.eval(&mods).is_some());
}
// [DESC] is false if the plugin description does match the given description and is negated is true
{
let expr = DESC::new(Atomic::from(A), "description".to_string(), true);
assert!(expr.eval(&mods).is_none());
}
}
#[test]
fn evaluate_ver() {
init();
let mods = [A, B, C, D, E, F]
.iter()
.map(|e| PluginData {
name: e.to_string(),
size: 0_u64,
description: None,
masters: None,
version: Some(lenient_semver::parse("1.0").unwrap()),
})
.collect::<Vec<_>>();
// Check equals
// [VER] equals is true if the plugin version matches the given version
{
let expr = VER::new(Atomic::from(A), EVerOperator::Equal, "1.0.0".to_string());
assert!(expr.eval(&mods).is_some());
}
// [VER] equals is false if the plugin version does not matches the given version
{
let expr = VER::new(Atomic::from(A), EVerOperator::Equal, "1.1.0".to_string());
assert!(expr.eval(&mods).is_none());
}
// Check greater
// [Note this is a newer version, it's broken] [VER > 0.1 foo.esp]
// [VER] greater is true if the plugin version is greater than the rule version
{
let expr = VER::new(Atomic::from(A), EVerOperator::Greater, "0.1.0".to_string());
assert!(expr.eval(&mods).is_some());
}
// [VER] greater is false if the plugin version is less than the given version
{
let expr = VER::new(Atomic::from(A), EVerOperator::Greater, "1.2.0".to_string());
assert!(expr.eval(&mods).is_none());
}
// [VER] greater is false if the plugin version is equal to the given version
{
let expr = VER::new(Atomic::from(A), EVerOperator::Greater, "1.0.0".to_string());
assert!(expr.eval(&mods).is_none());
}
// Check less
// [Note this is an old version, please upgrade] [VER < 1.2 foo.esp]
// [VER] less is true if the plugin version is less than the rule version
{
let expr = VER::new(Atomic::from(A), EVerOperator::Less, "1.2.0".to_string());
assert!(expr.eval(&mods).is_some());
}
// [VER] less is false if the plugin version is greater than the given version
{
let expr = VER::new(Atomic::from(A), EVerOperator::Less, "0.1.0".to_string());
assert!(expr.eval(&mods).is_none());
}
// [VER] less is false if the plugin version is equal to the given version
{
let expr = VER::new(Atomic::from(A), EVerOperator::Less, "1.0.0".to_string());
assert!(expr.eval(&mods).is_none());
}
}
#[test]
fn test_ver_problem() {
// check specific problem
// [Conflict]
// Texture Fix 2.0.esm
// [VER < 2.0 Texture Fix <VER>.esm]
let mods = ["Texture Fix 2.0.esm"]
.iter()
.map(|e| PluginData {
name: e.to_lowercase().to_string(),
size: 0_u64,
description: None,
masters: None,
version: None,
})
.collect::<Vec<_>>();
// {
// let expr = VER::new(
// Atomic::from("Texture Fix <VER>.esm"),
// EVerOperator::Less,
// "3.0.0".to_string(),
// );
// assert!(expr.eval(&mods).is_some());
// }
{
let expr1 = Atomic::from("Texture Fix 2.0.esm");
let expr2 = VER::new(
Atomic::from("Texture Fix <VER>.esm"),
EVerOperator::Less,
"2.0.0".to_string(),
);
let mut rule = plox::rules::Conflict::new("".into(), &[expr1.into(), expr2.into()]);
assert!(!rule.eval(&mods));
}
}
#[test]
fn evaluate_nested() {
init();
// check that (a and x) are not present in the modlist
{
let nested = ALL::new(vec![e(A), e(X)]);
let expr = NOT::new(nested.into());
assert!(expr.eval(&get_mods()).is_some());
}
// check that (a and b) are not present in the modlist
{
let nested = ALL::new(vec![e(A), e(B)]);
let expr = NOT::new(nested.into());
assert!(expr.eval(&get_mods()).is_none()); // should fail
}
// check that (a and b) are present and that either (x and y) are not present
{
let nested1 = ALL::new(vec![e(A), e(B)]);
let nested2 = NOT::new(ANY::new(vec![e(X), e(Y)]).into());
let expr = ALL::new(vec![nested1.into(), nested2.into()]);
assert!(expr.eval(&get_mods()).is_some());
}
// check that (a and b) are present and that either (x and y) are present
{
let nested1 = ALL::new(vec![e(A), e(B)]);
let nested2 = ANY::new(vec![e(A), e(Y)]);
let expr = ALL::new(vec![nested1.into(), nested2.into()]);
assert!(expr.eval(&get_mods()).is_some());
}
}
}