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
// The full JSONLogic suite runner exercises every operator in `tests/suites/`
// — including the gated ones (templating / datetime / try-throw / ext-*). Gate
// behind `templating` because the runner unconditionally builds an engine with
// `Engine::builder().with_templating(true).build()` when a test case
// requests it; in
// practice users running this runner will want `--all-features` to actually
// exercise every suite.
#![cfg(all(feature = "templating", feature = "serde_json"))]
use datalogic_rs::Engine;
use serde_json::{Value, json};
use std::env;
use std::fs;
use std::path::Path;
#[test]
fn test_jsonlogic() {
// Get test file from environment variable, or run all tests from index.json
let test_file = env::var("JSONLOGIC_TEST_FILE");
let mut total_passed = 0;
let mut total_failed = 0;
match test_file {
Ok(file) => {
// Run single test file
println!("Running tests from: {}", file);
let (passed, failed) = run_test_file(&file);
total_passed += passed;
total_failed += failed;
}
Err(_) => {
// Run all tests from index.json
println!("No JSONLOGIC_TEST_FILE specified, running all tests from index.json\n");
let index_path = "tests/suites/index.json";
let index_contents = fs::read_to_string(index_path).expect("Failed to read index.json");
let index: Vec<String> =
serde_json::from_str(&index_contents).expect("Failed to parse index.json");
for test_file in index {
// Suites under `flagd/` exercise operators registered
// only under `--features flagd`. Without the feature
// the operator names parse as `InvalidOperator` and the
// suite would spuriously fail; skip explicitly so the
// index can stay feature-agnostic.
if test_file.starts_with("flagd/") && !cfg!(feature = "flagd") {
println!(
"WARNING: Skipping {} (requires `flagd` feature)\n",
test_file
);
continue;
}
let test_path = format!("tests/suites/{}", test_file);
// Check if file exists
if !Path::new(&test_path).exists() {
println!("WARNING: Skipping {} (file not found)\n", test_file);
continue;
}
println!("\n=== Running tests from: {} ===", test_file);
let (passed, failed) = run_test_file(&test_path);
total_passed += passed;
total_failed += failed;
println!(" Results: {} passed, {} failed", passed, failed);
}
}
}
println!("\n========================================");
println!(
"TOTAL RESULTS: {} passed, {} failed",
total_passed, total_failed
);
println!("========================================");
if total_failed > 0 {
panic!("Some tests failed!");
}
}
fn run_test_file(test_file: &str) -> (usize, usize) {
// Read and parse test file
let contents = fs::read_to_string(test_file)
.unwrap_or_else(|e| panic!("Failed to read test file {test_file}: {e}"));
let test_cases: Value = serde_json::from_str(&contents)
.unwrap_or_else(|e| panic!("Failed to parse JSON from {test_file}: {e}"));
let test_array = test_cases
.as_array()
.expect("Test file should contain an array of test cases");
let mut passed = 0;
let mut failed = 0;
for (index, test_case) in test_array.iter().enumerate() {
// Skip string entries (they're usually section headers)
if test_case.is_string() {
println!("\n{}", test_case.as_str().unwrap());
continue;
}
let test_obj = test_case
.as_object()
.unwrap_or_else(|| panic!("Test case {} should be an object", index));
let description = test_obj
.get("description")
.and_then(|v| v.as_str())
.unwrap_or("No description");
let rule = test_obj
.get("rule")
.unwrap_or_else(|| panic!("Test case {} missing 'rule'", index));
let data = test_obj.get("data").cloned().unwrap_or(json!({}));
// Check for templating flag
let templating = test_obj
.get("templating")
.and_then(|v| v.as_bool())
.unwrap_or(false);
// Create engine with appropriate templating setting
let test_engine = if templating {
Engine::builder().with_templating(true).build()
} else {
Engine::new()
};
// Check if this test expects an error or a result
let expects_error = test_obj.contains_key("error");
let expected_error = test_obj.get("error");
let expected_result = test_obj.get("result");
if !expects_error && expected_result.is_none() {
panic!("Test case {} missing 'result' or 'error'", index);
}
// Compile and evaluate
match test_engine.compile(rule) {
Ok(compiled) => match test_engine
.session()
.eval_into::<serde_json::Value, _>(&compiled, &data)
{
Ok(result) => {
if expects_error {
println!("✗ Test {}: {}", index, description);
println!(" Expected error: {:?}", expected_error);
println!(" Got result: {:?}", result);
failed += 1;
} else if let Some(expected) = expected_result {
if &result == expected {
println!("✓ Test {}: {}", index, description);
passed += 1;
} else {
println!("✗ Test {}: {}", index, description);
println!(" Expected: {:?}", expected);
println!(" Got: {:?}", result);
failed += 1;
}
}
}
Err(e) => {
if expects_error {
// Check if the error matches expected error
if let Some(expected_error_obj) = expected_error {
// Extract the error type from the thrown error
if let Some(thrown_value) = e.thrown_value() {
let thrown_as_serde = serde_json::to_value(thrown_value)
.unwrap_or(serde_json::Value::Null);
if &thrown_as_serde == expected_error_obj {
println!(
"✓ Test {}: {} (error as expected)",
index, description
);
passed += 1;
} else {
println!("✗ Test {}: {}", index, description);
println!(" Expected error: {:?}", expected_error_obj);
println!(" Got error: {:?}", thrown_value);
failed += 1;
}
} else if let datalogic_rs::ErrorKind::InvalidArguments(msg) = &e.kind {
// Check if it's an InvalidArguments error
let error_obj = serde_json::json!({"type": msg});
if &error_obj == expected_error_obj {
println!(
"✓ Test {}: {} (error as expected)",
index, description
);
passed += 1;
} else {
println!("✗ Test {}: {}", index, description);
println!(" Expected error: {:?}", expected_error_obj);
println!(" Got error: {:?}", error_obj);
failed += 1;
}
} else if let datalogic_rs::ErrorKind::InvalidOperator(_msg) = &e.kind {
// Check if it's an InvalidOperator error
let error_obj = serde_json::json!({"type": "Unknown Operator"});
if &error_obj == expected_error_obj {
println!(
"✓ Test {}: {} (error as expected)",
index, description
);
passed += 1;
} else {
println!("✗ Test {}: {}", index, description);
println!(" Expected error: {:?}", expected_error_obj);
println!(" Got error: {:?}", error_obj);
failed += 1;
}
} else {
println!("✗ Test {}: {}", index, description);
println!(" Expected error: {:?}", expected_error_obj);
println!(" Got error: {:?}", e);
failed += 1;
}
} else {
println!("✓ Test {}: {} (error as expected)", index, description);
passed += 1;
}
} else {
println!(
"✗ Test {}: {} - Unexpected evaluation error: {}",
index, description, e
);
failed += 1;
}
}
},
Err(e) => {
if expects_error {
// Check if the compilation error matches expected error
if let Some(expected_error_obj) = expected_error {
if let datalogic_rs::ErrorKind::InvalidOperator(_msg) = &e.kind {
let error_obj = serde_json::json!({"type": "Unknown Operator"});
if &error_obj == expected_error_obj {
println!("✓ Test {}: {} (error as expected)", index, description);
passed += 1;
} else {
println!("✗ Test {}: {}", index, description);
println!(" Expected error: {:?}", expected_error_obj);
println!(" Got error: {:?}", error_obj);
failed += 1;
}
} else {
println!("✗ Test {}: {}", index, description);
println!(" Expected error: {:?}", expected_error_obj);
println!(" Got compilation error: {:?}", e);
failed += 1;
}
} else {
println!("✓ Test {}: {} (error as expected)", index, description);
passed += 1;
}
} else {
println!(
"✗ Test {}: {} - Compilation error: {}",
index, description, e
);
failed += 1;
}
}
}
}
(passed, failed)
}