formualizer-eval 0.6.0

High-performance Arrow-backed Excel formula engine with dependency graph and incremental recalculation
Documentation
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! JSON-driven formula test runner.
//!
//! This module loads JSON test files from `tests/formula_tests/` and runs each
//! formula through the evaluator, comparing results. It gathers ALL failures
//! before reporting, rather than failing on the first mismatch.

#[cfg(test)]
use formualizer_common::{ExcelError, ExcelErrorKind, LiteralValue, parse_a1_1based};
#[cfg(test)]
use formualizer_parse::parser::Parser;
#[cfg(test)]
use serde::Deserialize;
#[cfg(test)]
use std::fs;
#[cfg(test)]
use std::path::Path;

/// Represents a single test case from the JSON file.
#[cfg(test)]
#[derive(Debug, Deserialize)]
struct TestCase {
    formula: String,
    result: serde_json::Value,
    #[serde(default)]
    result_type: Option<String>,
    #[serde(default)]
    description: Option<String>,
    #[serde(default)]
    context: Option<serde_json::Value>,
    /// When set, the test is skipped and the value describes why.
    #[serde(default)]
    skip: Option<String>,
}

/// Represents a test file containing multiple test cases.
#[cfg(test)]
#[derive(Debug, Deserialize)]
struct TestFile {
    name: String,
    #[serde(default)]
    generated: Option<String>,
    #[serde(default)]
    generator: Option<String>,
    #[serde(default)]
    context_data: Option<serde_json::Value>,
    tests: Vec<TestCase>,
}

/// A failure record for reporting.
#[cfg(test)]
#[derive(Debug)]
struct TestFailure {
    file: String,
    formula: String,
    description: String,
    expected: String,
    actual: String,
    error: Option<String>,
}

/// Create a workbook with all built-in functions registered.
#[cfg(test)]
fn create_test_workbook() -> crate::test_workbook::TestWorkbook {
    crate::test_workbook::TestWorkbook::new()
}

#[cfg(test)]
fn json_to_literal(value: &serde_json::Value) -> Option<LiteralValue> {
    match value {
        serde_json::Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                Some(LiteralValue::Int(i))
            } else {
                n.as_f64().map(LiteralValue::Number)
            }
        }
        serde_json::Value::Bool(b) => Some(LiteralValue::Boolean(*b)),
        serde_json::Value::String(s) => {
            if s.starts_with('#') {
                Some(LiteralValue::Error(ExcelError::from_error_string(s)))
            } else {
                Some(LiteralValue::Text(s.clone()))
            }
        }
        serde_json::Value::Null => Some(LiteralValue::Empty),
        serde_json::Value::Object(map) => json_typed_value(map),
        _ => None,
    }
}

/// Handle typed context values like {"type": "date", "value": "2023-06-15"}.
/// This allows JSON test files to place native Date/DateTime/Time values into
/// cells, matching what xlsx file loaders produce.
#[cfg(test)]
fn json_typed_value(map: &serde_json::Map<String, serde_json::Value>) -> Option<LiteralValue> {
    let type_str = map.get("type")?.as_str()?;
    let value_str = map.get("value")?.as_str()?;
    match type_str {
        "date" => {
            let date = chrono::NaiveDate::parse_from_str(value_str, "%Y-%m-%d").ok()?;
            Some(LiteralValue::Date(date))
        }
        "datetime" => {
            let dt = chrono::NaiveDateTime::parse_from_str(value_str, "%Y-%m-%dT%H:%M:%S").ok()?;
            Some(LiteralValue::DateTime(dt))
        }
        "time" => {
            let t = chrono::NaiveTime::parse_from_str(value_str, "%H:%M:%S").ok()?;
            Some(LiteralValue::Time(t))
        }
        _ => None,
    }
}

#[cfg(test)]
fn apply_context(
    mut wb: crate::test_workbook::TestWorkbook,
    context: &serde_json::Value,
) -> crate::test_workbook::TestWorkbook {
    let Some(map) = context.as_object() else {
        return wb;
    };

    for (cell_ref, value) in map {
        let Ok((row, col, _, _)) = parse_a1_1based(cell_ref) else {
            continue;
        };
        if let Some(literal) = json_to_literal(value) {
            wb = wb.with_cell("Sheet1", row, col, literal);
        }
    }

    wb
}

/// Evaluate a formula string and return the result.
#[cfg(test)]
fn evaluate_formula(
    formula: &str,
    wb: &crate::test_workbook::TestWorkbook,
) -> Result<LiteralValue, String> {
    let mut parser = Parser::new(formula).map_err(|e| format!("Tokenizer error: {:?}", e))?;
    let ast = parser
        .parse()
        .map_err(|e| format!("Parse error: {}", e.message))?;

    let interpreter = wb.interpreter();
    let cv = interpreter
        .evaluate_ast(&ast)
        .map_err(|e| format!("Eval error: {:?}", e))?;

    Ok(cv.into_literal())
}

/// Compare a LiteralValue to an expected JSON value.
#[cfg(test)]
fn compare_result(
    actual: &LiteralValue,
    expected: &serde_json::Value,
    result_type: Option<&str>,
) -> bool {
    let _ = result_type; // Unused for now, but may be useful later
    compare_literal_json(actual, expected)
}

#[cfg(test)]
fn compare_literal_json(actual: &LiteralValue, expected: &serde_json::Value) -> bool {
    match expected {
        serde_json::Value::Number(n) => {
            let expected_num = n.as_f64().unwrap();
            match actual {
                LiteralValue::Number(actual_num) => {
                    // Use epsilon comparison for floating point
                    (actual_num - expected_num).abs() < 1e-9
                        || (expected_num != 0.0
                            && ((actual_num - expected_num) / expected_num).abs() < 1e-9)
                }
                LiteralValue::Int(actual_int) => (*actual_int as f64 - expected_num).abs() < 1e-9,
                _ => false,
            }
        }
        serde_json::Value::Bool(expected_bool) => {
            matches!(actual, LiteralValue::Boolean(actual_bool) if actual_bool == expected_bool)
        }
        serde_json::Value::String(expected_str) => {
            if expected_str.starts_with('#') {
                if let Some(expected_kind) = parse_error_kind_prefix(expected_str) {
                    matches!(actual, LiteralValue::Error(e) if e.kind == expected_kind)
                } else {
                    matches!(actual, LiteralValue::Error(e) if e.to_string() == *expected_str)
                }
            } else {
                matches!(actual, LiteralValue::Text(actual_str) if actual_str == expected_str)
            }
        }
        serde_json::Value::Null => matches!(actual, LiteralValue::Empty),
        serde_json::Value::Array(_) => compare_array(actual, expected),
        _ => false,
    }
}

#[cfg(test)]
fn compare_array(actual: &LiteralValue, expected: &serde_json::Value) -> bool {
    let actual_rows = match actual {
        LiteralValue::Array(rows) => rows,
        _ => return false,
    };

    let expected_rows = match expected {
        serde_json::Value::Array(rows) => rows,
        _ => return false,
    };

    let expected_matrix: Vec<Vec<&serde_json::Value>> = if expected_rows
        .iter()
        .all(|row| matches!(row, serde_json::Value::Array(_)))
    {
        expected_rows
            .iter()
            .map(|row| row.as_array().unwrap().iter().collect())
            .collect()
    } else {
        vec![expected_rows.iter().collect()]
    };

    if actual_rows.len() != expected_matrix.len() {
        return false;
    }

    for (row_idx, expected_row) in expected_matrix.iter().enumerate() {
        let actual_row = &actual_rows[row_idx];
        if actual_row.len() != expected_row.len() {
            return false;
        }
        for (col_idx, expected_cell) in expected_row.iter().enumerate() {
            if !compare_literal_json(&actual_row[col_idx], expected_cell) {
                return false;
            }
        }
    }

    true
}

#[cfg(test)]
fn parse_error_kind_prefix(value: &str) -> Option<ExcelErrorKind> {
    let trimmed = value.trim();
    if !trimmed.starts_with('#') {
        return None;
    }

    let end = trimmed
        .find(|c: char| [':', ' ', '(', '['].contains(&c))
        .unwrap_or(trimmed.len());
    ExcelErrorKind::try_parse(&trimmed[..end])
}

/// Format a LiteralValue for display.
#[cfg(test)]
fn format_literal(lit: &LiteralValue) -> String {
    match lit {
        LiteralValue::Number(n) => format!("{}", n),
        LiteralValue::Int(i) => format!("{}", i),
        LiteralValue::Boolean(b) => format!("{}", b),
        LiteralValue::Text(s) => format!("\"{}\"", s),
        LiteralValue::Error(e) => format!("{}", e),
        LiteralValue::Empty => "empty".to_string(),
        LiteralValue::Array(arr) => {
            let rows: Vec<String> = arr
                .iter()
                .map(|row| {
                    let cells: Vec<String> = row.iter().map(format_literal).collect();
                    cells.join(",")
                })
                .collect();
            format!("{{{}}}", rows.join(";"))
        }
        LiteralValue::Date(d) => format!("date:{}", d),
        LiteralValue::DateTime(dt) => format!("datetime:{}", dt),
        LiteralValue::Time(t) => format!("time:{}", t),
        LiteralValue::Duration(dur) => format!("duration:{:?}", dur),
        LiteralValue::Pending => "pending".to_string(),
    }
}

/// Run all formula tests from JSON files.
/// Returns (passed_count, skipped_count, failures).
#[cfg(test)]
fn run_formula_tests(test_dir: &Path) -> (usize, usize, Vec<TestFailure>) {
    let mut passed = 0;
    let mut skipped = 0;
    let mut failures = Vec::new();

    // Initialize function registry
    crate::builtins::load_builtins();

    let pattern = test_dir.join("*.json");
    let pattern_str = pattern.to_string_lossy();

    for entry in glob::glob(&pattern_str).expect("Failed to read glob pattern") {
        let path = match entry {
            Ok(p) => p,
            Err(e) => {
                failures.push(TestFailure {
                    file: "unknown".to_string(),
                    formula: "N/A".to_string(),
                    description: "Failed to read file entry".to_string(),
                    expected: "N/A".to_string(),
                    actual: "N/A".to_string(),
                    error: Some(format!("{}", e)),
                });
                continue;
            }
        };

        let file_name = path.file_name().unwrap().to_string_lossy().to_string();
        let content = match fs::read_to_string(&path) {
            Ok(c) => c,
            Err(e) => {
                failures.push(TestFailure {
                    file: file_name,
                    formula: "N/A".to_string(),
                    description: "Failed to read file".to_string(),
                    expected: "N/A".to_string(),
                    actual: "N/A".to_string(),
                    error: Some(format!("{}", e)),
                });
                continue;
            }
        };

        let test_file: TestFile = match serde_json::from_str(&content) {
            Ok(tf) => tf,
            Err(e) => {
                failures.push(TestFailure {
                    file: file_name,
                    formula: "N/A".to_string(),
                    description: "Failed to parse JSON".to_string(),
                    expected: "N/A".to_string(),
                    actual: "N/A".to_string(),
                    error: Some(format!("{}", e)),
                });
                continue;
            }
        };

        for test_case in &test_file.tests {
            if test_case.skip.is_some() {
                skipped += 1;
                continue;
            }
            let mut wb = create_test_workbook();
            if let Some(context) = test_file.context_data.as_ref() {
                wb = apply_context(wb, context);
            }
            if let Some(context) = test_case.context.as_ref() {
                wb = apply_context(wb, context);
            }
            let description = test_case.description.clone().unwrap_or_default();

            match evaluate_formula(&test_case.formula, &wb) {
                Ok(actual) => {
                    let result_type = test_case.result_type.as_deref();
                    if compare_result(&actual, &test_case.result, result_type) {
                        passed += 1;
                    } else {
                        failures.push(TestFailure {
                            file: file_name.clone(),
                            formula: test_case.formula.clone(),
                            description,
                            expected: format!("{:?}", test_case.result),
                            actual: format_literal(&actual),
                            error: None,
                        });
                    }
                }
                Err(e) => {
                    // Check if the expected result is an error
                    if let serde_json::Value::String(expected_str) = &test_case.result {
                        if expected_str.starts_with('#') && expected_str.ends_with('!') {
                            // We expected an error but got a different error - still a failure
                            failures.push(TestFailure {
                                file: file_name.clone(),
                                formula: test_case.formula.clone(),
                                description,
                                expected: expected_str.clone(),
                                actual: "evaluation error".to_string(),
                                error: Some(e),
                            });
                        } else {
                            failures.push(TestFailure {
                                file: file_name.clone(),
                                formula: test_case.formula.clone(),
                                description,
                                expected: format!("{:?}", test_case.result),
                                actual: "evaluation error".to_string(),
                                error: Some(e),
                            });
                        }
                    } else {
                        failures.push(TestFailure {
                            file: file_name.clone(),
                            formula: test_case.formula.clone(),
                            description,
                            expected: format!("{:?}", test_case.result),
                            actual: "evaluation error".to_string(),
                            error: Some(e),
                        });
                    }
                }
            }
        }
    }

    (passed, skipped, failures)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    /// Main test function that runs all JSON formula tests.
    /// This gathers ALL failures before reporting.
    #[test]
    fn run_formula_test_suite() {
        // Find the test directory relative to the crate root
        let manifest_dir = env!("CARGO_MANIFEST_DIR");
        let test_dir = PathBuf::from(manifest_dir)
            .parent()
            .unwrap()
            .parent()
            .unwrap()
            .join("tests")
            .join("formula_tests");

        if !test_dir.exists() {
            eprintln!("Warning: Test directory does not exist: {:?}", test_dir);
            eprintln!("Skipping formula test suite.");
            return;
        }

        let (passed, skipped, failures) = run_formula_tests(&test_dir);

        // Report all failures at the end
        if !failures.is_empty() {
            eprintln!("\n=== {} FORMULA TEST FAILURES ===\n", failures.len());
            for (i, failure) in failures.iter().enumerate() {
                eprintln!(
                    "{}. [{}] {}\n   Formula: {}\n   Expected: {}\n   Actual: {}{}",
                    i + 1,
                    failure.file,
                    failure.description,
                    failure.formula,
                    failure.expected,
                    failure.actual,
                    failure
                        .error
                        .as_ref()
                        .map(|e| format!("\n   Error: {}", e))
                        .unwrap_or_default()
                );
            }
            eprintln!(
                "\n=== SUMMARY: {} passed, {} skipped, {} failed ===\n",
                passed,
                skipped,
                failures.len()
            );
            panic!(
                "Formula test suite: {} passed, {} skipped, {} failed",
                passed,
                skipped,
                failures.len()
            );
        }

        if skipped > 0 {
            println!(
                "\nFormula test suite: {} tests passed, {} skipped",
                passed, skipped
            );
        } else {
            println!("\nFormula test suite: {} tests passed", passed);
        }
    }
}