truecalc-core 4.0.0

Formula engine with exact Google Sheets semantics — stateless, embeddable evaluator
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//! P1.2 reference grammar — parse-level tests (issue #524).
//!
//! Covers sheet-qualified cell refs (`Sheet1!A1`), quoted sheet names
//! (`'Quoted Name'!A1:B2`), cross-sheet ranges, the `Ref` model, canonical
//! display, and the zero-behavior-change guarantee for bare `A1` / `A1:D4` /
//! named identifiers (which stay `Expr::Variable`).
//!
//! Evaluation semantics for these references (e.g. `#REF!` for a missing
//! sheet) are pipeline-verified via the P1.5 workbook fixtures and land with
//! the P1.3 resolver; nothing here self-confirms evaluation values.

use truecalc_core::{CellAddr, Engine, Expr, Ref};

fn parse(formula: &str) -> Expr {
    Engine::sheets()
        .parse(formula)
        .unwrap_or_else(|e| panic!("expected {formula:?} to parse: {e}"))
}

fn parse_err(formula: &str) {
    assert!(
        Engine::sheets().parse(formula).is_err(),
        "expected {formula:?} to fail to parse"
    );
}

fn addr(col: u32, row: u32) -> CellAddr {
    CellAddr::new(col, row)
}

// ── sheet-qualified cells ───────────────────────────────────────────────────

#[test]
fn sheet_qualified_cell() {
    match parse("=Sheet1!A1") {
        Expr::Reference(Ref::Cell { sheet, addr: a }, _) => {
            assert_eq!(sheet.as_deref(), Some("Sheet1"));
            assert_eq!(a, addr(1, 1));
        }
        other => panic!("expected Reference(Cell), got {other:?}"),
    }
}

#[test]
fn sheet_qualified_range() {
    match parse("=Sheet1!A1:B2") {
        Expr::Reference(Ref::Range { sheet, start, end }, _) => {
            assert_eq!(sheet.as_deref(), Some("Sheet1"));
            assert_eq!(start, addr(1, 1));
            assert_eq!(end, addr(2, 2));
        }
        other => panic!("expected Reference(Range), got {other:?}"),
    }
}

#[test]
fn lowercase_sheet_and_cell() {
    match parse("=data!a1") {
        Expr::Reference(Ref::Cell { sheet, addr: a }, _) => {
            // Sheet case is preserved as written; resolution is delegated.
            assert_eq!(sheet.as_deref(), Some("data"));
            assert_eq!(a, addr(1, 1));
        }
        other => panic!("expected Reference(Cell), got {other:?}"),
    }
}

#[test]
fn multi_letter_columns() {
    match parse("=Sheet1!AA10:BC42") {
        Expr::Reference(Ref::Range { start, end, .. }, _) => {
            assert_eq!(start, addr(27, 10));
            assert_eq!(end, addr(55, 42));
        }
        other => panic!("expected Reference(Range), got {other:?}"),
    }
}

#[test]
fn sheet_with_underscore_and_digits() {
    match parse("=My_Sheet1!C3") {
        Expr::Reference(Ref::Cell { sheet, addr: a }, _) => {
            assert_eq!(sheet.as_deref(), Some("My_Sheet1"));
            assert_eq!(a, addr(3, 3));
        }
        other => panic!("expected Reference(Cell), got {other:?}"),
    }
}

#[test]
fn sheet_name_that_looks_like_a_cell() {
    // A sheet named "A1" is unambiguous before `!`.
    match parse("=A1!B2") {
        Expr::Reference(Ref::Cell { sheet, addr: a }, _) => {
            assert_eq!(sheet.as_deref(), Some("A1"));
            assert_eq!(a, addr(2, 2));
        }
        other => panic!("expected Reference(Cell), got {other:?}"),
    }
}

#[test]
fn missing_sheet_still_parses() {
    // Whether the sheet exists is an evaluation concern (#REF! via the
    // resolver, P1.3), not a parse error.
    assert!(matches!(
        parse("=MissingSheet!A1"),
        Expr::Reference(Ref::Cell { .. }, _)
    ));
}

// ── quoted sheet names ──────────────────────────────────────────────────────

#[test]
fn quoted_sheet_cell() {
    match parse("='Quoted Name'!A1") {
        Expr::Reference(Ref::Cell { sheet, addr: a }, _) => {
            assert_eq!(sheet.as_deref(), Some("Quoted Name"));
            assert_eq!(a, addr(1, 1));
        }
        other => panic!("expected Reference(Cell), got {other:?}"),
    }
}

#[test]
fn quoted_sheet_range() {
    match parse("='Quoted Name'!A1:B2") {
        Expr::Reference(Ref::Range { sheet, start, end }, _) => {
            assert_eq!(sheet.as_deref(), Some("Quoted Name"));
            assert_eq!(start, addr(1, 1));
            assert_eq!(end, addr(2, 2));
        }
        other => panic!("expected Reference(Range), got {other:?}"),
    }
}

#[test]
fn quoted_single_word_sheet() {
    // Quoting is optional for bare-identifier names but still valid.
    match parse("='Data'!A1") {
        Expr::Reference(Ref::Cell { sheet, .. }, _) => {
            assert_eq!(sheet.as_deref(), Some("Data"));
        }
        other => panic!("expected Reference(Cell), got {other:?}"),
    }
}

#[test]
fn escaped_quote_in_sheet_name() {
    match parse("='It''s'!A1") {
        Expr::Reference(Ref::Cell { sheet, .. }, _) => {
            assert_eq!(sheet.as_deref(), Some("It's"));
        }
        other => panic!("expected Reference(Cell), got {other:?}"),
    }
}

#[test]
fn unicode_quoted_sheet() {
    match parse("='Q2 Données'!A1") {
        Expr::Reference(Ref::Cell { sheet, .. }, _) => {
            assert_eq!(sheet.as_deref(), Some("Q2 Données"));
        }
        other => panic!("expected Reference(Cell), got {other:?}"),
    }
}

// ── references inside expressions ───────────────────────────────────────────

#[test]
fn refs_in_arithmetic() {
    match parse("=Data!A1+Data!B1") {
        Expr::BinaryOp { left, right, .. } => {
            assert!(matches!(*left, Expr::Reference(Ref::Cell { .. }, _)));
            assert!(matches!(*right, Expr::Reference(Ref::Cell { .. }, _)));
        }
        other => panic!("expected BinaryOp, got {other:?}"),
    }
}

#[test]
fn range_ref_as_function_arg() {
    match parse("=SUM(Data!A1:A3)") {
        Expr::FunctionCall { name, args, .. } => {
            assert_eq!(name, "SUM");
            assert_eq!(args.len(), 1);
            assert!(matches!(args[0], Expr::Reference(Ref::Range { .. }, _)));
        }
        other => panic!("expected FunctionCall, got {other:?}"),
    }
}

#[test]
fn ref_in_if_condition() {
    assert!(Engine::sheets().parse("=IF(Data!A1>5,\"big\",\"small\")").is_ok());
}

#[test]
fn quoted_refs_in_arithmetic() {
    assert!(Engine::sheets()
        .parse("='Quoted Name'!A1+'Quoted Name'!B1")
        .is_ok());
}

#[test]
fn excel_engine_accepts_same_grammar() {
    assert!(Engine::excel().parse("='Quoted Name'!A1:B2").is_ok());
}

// ── spans ───────────────────────────────────────────────────────────────────

#[test]
fn span_covers_whole_sheet_qualified_range() {
    let expr = parse("=Sheet1!A1:B2");
    let span = expr.span();
    assert_eq!(span.offset, 1);
    assert_eq!(span.length, "Sheet1!A1:B2".len());
}

#[test]
fn span_covers_whole_quoted_reference() {
    let expr = parse("='My Sheet'!A1");
    let span = expr.span();
    assert_eq!(span.offset, 1);
    assert_eq!(span.length, "'My Sheet'!A1".len());
}

// ── zero behavior change for bare identifiers ──────────────────────────────

#[test]
fn bare_cell_stays_variable() {
    assert!(matches!(parse("=A1"), Expr::Variable(ref n, _) if n == "A1"));
}

#[test]
fn bare_range_stays_variable() {
    assert!(matches!(parse("=A1:D4"), Expr::Variable(ref n, _) if n == "A1:D4"));
}

#[test]
fn bare_name_stays_variable() {
    assert!(matches!(parse("=TAX_RATE"), Expr::Variable(ref n, _) if n == "TAX_RATE"));
}

#[test]
fn dotted_function_names_still_parse() {
    assert!(matches!(
        parse("=ERROR.TYPE(1)"),
        Expr::FunctionCall { ref name, .. } if name == "ERROR.TYPE"
    ));
}

// ── parse errors ────────────────────────────────────────────────────────────

#[test]
fn error_missing_cell_after_bang() {
    parse_err("=Sheet1!");
}

#[test]
fn error_number_after_bang() {
    parse_err("=Sheet1!123");
}

#[test]
fn error_name_after_bang() {
    // Sheet-scoped named references are not part of the v1 grammar.
    parse_err("=Sheet1!foo");
}

#[test]
fn error_row_zero_after_bang() {
    parse_err("=Sheet1!A0");
}

#[test]
fn error_empty_quoted_sheet() {
    parse_err("=''!A1");
}

#[test]
fn error_unterminated_quoted_sheet() {
    parse_err("='Oops");
}

#[test]
fn error_quoted_sheet_without_bang() {
    parse_err("='Data'");
}

#[test]
fn error_bad_range_second_endpoint() {
    parse_err("=Data!A1:xyz");
}

#[test]
fn error_whitespace_around_bang() {
    parse_err("=Sheet1 !A1");
    parse_err("=Sheet1! A1");
}

// ── CellAddr ───────────────────────────────────────────────────────────────

#[test]
fn celladdr_parse_and_display() {
    for (text, col, row, canonical) in [
        ("A1", 1, 1, "A1"),
        ("z26", 26, 26, "Z26"),
        ("AA1", 27, 1, "AA1"),
        ("BC42", 55, 42, "BC42"),
        ("zz1", 702, 1, "ZZ1"),
    ] {
        let a = CellAddr::parse(text).unwrap_or_else(|| panic!("{text} should parse"));
        assert_eq!(a, addr(col, row), "{text}");
        assert_eq!(a.to_string(), canonical, "{text}");
    }
}

#[test]
fn celladdr_rejects_non_addresses() {
    for text in ["", "A", "1", "A0", "1A", "A1B", "A 1", "A1.5"] {
        assert!(CellAddr::parse(text).is_none(), "{text:?} should not parse");
    }
}

// ── Ref::classify (bare identifiers → named refs) ───────────────────────────

#[test]
fn classify_bare_cell() {
    assert_eq!(
        Ref::classify("A1"),
        Ref::Cell { sheet: None, addr: addr(1, 1) }
    );
}

#[test]
fn classify_bare_range() {
    assert_eq!(
        Ref::classify("A1:D4"),
        Ref::Range { sheet: None, start: addr(1, 1), end: addr(4, 4) }
    );
}

#[test]
fn classify_bare_identifier_is_name() {
    assert_eq!(Ref::classify("TAX_RATE"), Ref::Name("TAX_RATE".into()));
    assert_eq!(Ref::classify("A1:xyz"), Ref::Name("A1:xyz".into()));
}

// ── canonical display ───────────────────────────────────────────────────────

#[test]
fn display_unquoted_when_bare_identifier() {
    let r = Ref::Cell { sheet: Some("Sheet1".into()), addr: addr(1, 1) };
    assert_eq!(r.to_string(), "Sheet1!A1");
}

#[test]
fn display_quotes_when_name_needs_it() {
    let r = Ref::Cell { sheet: Some("Quoted Name".into()), addr: addr(1, 1) };
    assert_eq!(r.to_string(), "'Quoted Name'!A1");
}

#[test]
fn display_doubles_embedded_quotes() {
    let r = Ref::Cell { sheet: Some("It's".into()), addr: addr(1, 1) };
    assert_eq!(r.to_string(), "'It''s'!A1");
}

#[test]
fn display_quotes_a1_like_sheet_name() {
    let r = Ref::Cell { sheet: Some("A1".into()), addr: addr(2, 2) };
    assert_eq!(r.to_string(), "'A1'!B2");
}

#[test]
fn display_quotes_boolean_sheet_names() {
    // TRUE/FALSE parse as boolean literals before identifiers, so a sheet
    // with one of those names must be quoted in canonical form to re-parse.
    let r = Ref::Cell { sheet: Some("TRUE".into()), addr: addr(1, 1) };
    assert_eq!(r.to_string(), "'TRUE'!A1");
    let r = Ref::Cell { sheet: Some("false".into()), addr: addr(1, 1) };
    assert_eq!(r.to_string(), "'false'!A1");
}

#[test]
fn display_quotes_leading_digit_sheet_name() {
    let r = Ref::Range {
        sheet: Some("2024".into()),
        start: addr(1, 1),
        end: addr(2, 2),
    };
    assert_eq!(r.to_string(), "'2024'!A1:B2");
}

#[test]
fn display_range_without_sheet() {
    let r = Ref::Range { sheet: None, start: addr(1, 1), end: addr(4, 4) };
    assert_eq!(r.to_string(), "A1:D4");
}

#[test]
fn display_name() {
    assert_eq!(Ref::Name("TAX_RATE".into()).to_string(), "TAX_RATE");
}

#[test]
fn display_round_trips_through_parser() {
    for formula in ["='It''s'!A1:B2", "=Sheet1!A1", "='Q2 Données'!AA10", "='TRUE'!A1", "='FALSE'!B2:C3"] {
        let r1 = match parse(formula) {
            Expr::Reference(r, _) => r,
            other => panic!("expected Reference, got {other:?}"),
        };
        let printed = format!("={r1}");
        let r2 = match parse(&printed) {
            Expr::Reference(r, _) => r,
            other => panic!("expected Reference on round-trip, got {other:?}"),
        };
        assert_eq!(r1, r2, "round-trip of {formula}");
    }
}

// ── $ absolute/relative references (issue #708) ────────────────────────────

#[test]
fn bare_dollar_cell_stays_variable() {
    for (formula, text) in [("=$A$1", "$A$1"), ("=$A1", "$A1"), ("=A$1", "A$1")] {
        assert!(
            matches!(parse(formula), Expr::Variable(ref n, _) if n == text),
            "{formula} should parse as Variable({text:?})"
        );
    }
}

#[test]
fn sheet_qualified_dollar_cell() {
    for (formula, col_abs, row_abs) in [
        ("=Sheet1!$A$1", true, true),
        ("=Sheet1!$A1", true, false),
        ("=Sheet1!A$1", false, true),
    ] {
        match parse(formula) {
            Expr::Reference(Ref::Cell { sheet, addr: a }, _) => {
                assert_eq!(sheet.as_deref(), Some("Sheet1"));
                assert_eq!(a, addr(1, 1).with_col_abs(col_abs).with_row_abs(row_abs));
            }
            other => panic!("expected Reference(Cell), got {other:?}"),
        }
    }
}

#[test]
fn quoted_sheet_mixed_dollar_range() {
    match parse("='Sheet 1'!$A1:B$4") {
        Expr::Reference(Ref::Range { sheet, start, end }, _) => {
            assert_eq!(sheet.as_deref(), Some("Sheet 1"));
            assert_eq!(start, addr(1, 1).with_col_abs(true));
            assert_eq!(end, addr(2, 4).with_row_abs(true));
        }
        other => panic!("expected Reference(Range), got {other:?}"),
    }
}

#[test]
fn bare_mixed_corner_dollar_ranges_stay_variable() {
    for (formula, text) in [("=$A$1:D4", "$A$1:D4"), ("=A1:$D$4", "A1:$D$4")] {
        assert!(
            matches!(parse(formula), Expr::Variable(ref n, _) if n == text),
            "{formula} should parse as Variable({text:?})"
        );
    }
}

#[test]
fn sheet_qualified_mixed_corner_dollar_range() {
    match parse("=Sheet1!$A$1:D4") {
        Expr::Reference(Ref::Range { start, end, .. }, _) => {
            assert_eq!(start, addr(1, 1).with_col_abs(true).with_row_abs(true));
            assert_eq!(end, addr(4, 4));
        }
        other => panic!("expected Reference(Range), got {other:?}"),
    }
}

#[test]
fn error_malformed_dollar_range_end() {
    // Matches today's error shape for the equivalent non-$ malformed input
    // (error_bad_range_second_endpoint above): a bad end corner degrades to
    // a parse-time error, not a runtime #NAME?.
    parse_err("=$A$1:FOO");
}

#[test]
fn celladdr_parse_and_display_dollar_forms() {
    for (text, col, row, col_abs, row_abs) in [
        ("$A$1", 1, 1, true, true),
        ("$A1", 1, 1, true, false),
        ("A$1", 1, 1, false, true),
    ] {
        let a = CellAddr::parse(text).unwrap_or_else(|| panic!("{text} should parse"));
        assert_eq!(a, addr(col, row).with_col_abs(col_abs).with_row_abs(row_abs), "{text}");
        assert_eq!(a.to_string(), text, "{text}");
    }
}

#[test]
fn celladdr_rejects_malformed_dollar_addresses() {
    for text in ["$", "$$A1", "$1A", "A$$1", "$A$"] {
        assert!(CellAddr::parse(text).is_none(), "{text:?} should not parse");
    }
}