tishlang_test 3.7.1

Native tish test runner, expect, and Node-compatible assert
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
//! Jest/Bun-shaped `expect` matchers.

use tishlang_core::{
    has_pending_throw, set_pending_throw, take_pending_throw, value_call, Arc, ObjectMap, Value,
};

use crate::assert::assertion_error;
use crate::deep_equal::deep_strict_equal;

fn fail(msg: String, actual: Value, expected: Option<Value>, op: &str) -> Value {
    set_pending_throw(assertion_error(msg, Some(actual), expected, op, true));
    Value::Null
}

fn is_truthy(v: &Value) -> bool {
    match v {
        Value::Null => false,
        Value::Bool(b) => *b,
        Value::Number(n) => *n != 0.0 && !n.is_nan(),
        Value::String(s) => !s.is_empty(),
        _ => true,
    }
}

/// Can `value_call` actually invoke this? Objects are callable only via a `__call` member.
pub(crate) fn is_callable(v: &Value) -> bool {
    match v {
        Value::Function(_) => true,
        Value::Object(o) => o.borrow().strings.contains_key("__call"),
        _ => false,
    }
}

/// Human-readable text for a thrown value: `Error`-shaped objects report their `message`.
fn error_text(err: &Value) -> String {
    if let Value::Object(o) = err {
        if let Some(Value::String(m)) = o.borrow().strings.get("message") {
            return m.to_string();
        }
    }
    err.to_display_string()
}

/// Does a thrown value satisfy `expected` (string substring, regex, `Error`-shaped object,
/// or a deep-equal value)? Node/Jest semantics — the previous version accepted *any* throw.
pub(crate) fn error_matches(err: &Value, expected: &Value) -> bool {
    match expected {
        Value::String(pat) => error_text(err).contains(pat.as_str()),
        #[cfg(feature = "regex")]
        Value::RegExp(re) => re.borrow_mut().test(&error_text(err)),
        // `{ message, name, … }` — every listed field must match.
        Value::Object(o) if !o.borrow().strings.contains_key(ASYMMETRIC_KEY) => {
            let want = o.borrow();
            if want.strings.is_empty() {
                return deep_strict_equal(err, expected);
            }
            let Value::Object(got) = err else {
                return false;
            };
            let got = got.borrow();
            want.strings.iter().all(|(k, wv)| match got.strings.get(k) {
                Some(gv) => deep_strict_equal(gv, wv),
                None => false,
            })
        }
        _ => deep_strict_equal(err, expected),
    }
}

fn describe_error_matcher(expected: &Value) -> String {
    match expected {
        Value::String(s) => format!("a message containing {:?}", s.as_str()),
        _ => expected.to_display_string(),
    }
}

fn type_of_name(v: &Value) -> &'static str {
    match v {
        Value::Null => "null",
        Value::Bool(_) => "boolean",
        Value::Number(_) => "number",
        Value::String(_) => "string",
        Value::Symbol(_) => "symbol",
        Value::Function(_) => "function",
        _ => "object",
    }
}

/// Resolve a `toHaveProperty` path (`"a.b.0"` or `["a", "b", 0]`) to its value.
fn property_at(root: &Value, path: &Value) -> Option<Value> {
    let segments: Vec<String> = match path {
        Value::Array(a) => a.borrow().iter().map(|v| v.to_display_string()).collect(),
        other => other
            .to_display_string()
            .split('.')
            .map(|s| s.to_string())
            .collect(),
    };
    let mut cur = root.clone();
    for seg in segments {
        cur = match &cur {
            Value::Object(o) => o.borrow().strings.get(seg.as_str())?.clone(),
            Value::Array(a) => {
                let idx: usize = seg.parse().ok()?;
                a.borrow().get(idx)?.clone()
            }
            Value::NumberArray(a) => {
                let idx: usize = seg.parse().ok()?;
                a.borrow().to_values().get(idx)?.clone()
            }
            _ => return None,
        };
    }
    Some(cur)
}

fn length_of(v: &Value) -> Option<f64> {
    match v {
        Value::Array(a) => Some(a.borrow().len() as f64),
        Value::NumberArray(a) => Some(a.borrow().len() as f64),
        Value::String(s) => Some(s.chars().count() as f64),
        Value::Object(o) => o.borrow().strings.get("length").and_then(|x| x.as_number()),
        _ => None,
    }
}

/// Resolve Jest/Bun mock metadata from `expect(fn)` / `expect(fn.mock)`.
fn mock_meta(v: &Value) -> Option<Value> {
    let Value::Object(o) = v else {
        return None;
    };
    let b = o.borrow();
    if b.strings.contains_key("calls") && b.strings.contains_key("callCount") {
        return Some(v.clone());
    }
    b.strings.get("mock").cloned().and_then(|m| {
        if let Value::Object(mo) = &m {
            let mb = mo.borrow();
            if mb.strings.contains_key("calls") {
                return Some(m.clone());
            }
        }
        None
    })
}

fn mock_call_count(meta: &Value) -> f64 {
    let Value::Object(o) = meta else {
        return 0.0;
    };
    o.borrow()
        .strings
        .get("callCount")
        .and_then(|v| v.as_number())
        .unwrap_or(0.0)
}

fn mock_calls(meta: &Value) -> Vec<Value> {
    let Value::Object(o) = meta else {
        return Vec::new();
    };
    match o.borrow().strings.get("calls") {
        Some(Value::Array(a)) => a.borrow().clone(),
        _ => Vec::new(),
    }
}

// `!(a > b)` and friends are deliberate: they are TRUE when either side is NaN, which is what
// these matchers must report. `partial_cmp` would silently pass an incomparable pair.
#[allow(clippy::neg_cmp_op_on_partial_ord)]
fn matcher_object(actual: Value) -> Value {
    let actual_be = actual.clone();
    let actual_eq = actual.clone();
    let actual_strict = actual.clone();
    let actual_throw = actual.clone();
    let actual_match = actual.clone();
    let actual_contain = actual.clone();
    let actual_null = actual.clone();
    let actual_truthy = actual.clone();
    let actual_falsy = actual.clone();
    let actual_defined = actual.clone();
    let actual_len = actual.clone();
    let actual_gt = actual.clone();
    let actual_gte = actual.clone();
    let actual_lt = actual.clone();
    let actual_lte = actual.clone();
    let actual_snap = actual.clone();
    let actual_called = actual.clone();
    let actual_called_times = actual.clone();
    let actual_called_with = actual.clone();
    let actual_last_with = actual.clone();

    let mut m = ObjectMap::default();
    m.insert(
        Arc::from("toBe"),
        Value::native(move |args: &[Value]| {
            let expected = args.first().cloned().unwrap_or(Value::Null);
            if !actual_be.strict_eq(&expected) {
                return fail(
                    format!(
                        "expect(received).toBe(expected)\n\nExpected: {}\nReceived: {}",
                        expected.to_display_string(),
                        actual_be.to_display_string()
                    ),
                    actual_be.clone(),
                    Some(expected),
                    "toBe",
                );
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toEqual"),
        Value::native(move |args: &[Value]| {
            let expected = args.first().cloned().unwrap_or(Value::Null);
            if !deep_strict_equal(&actual_eq, &expected) {
                return fail(
                    format!(
                        "expect(received).toEqual(expected)\n\nExpected: {}\nReceived: {}",
                        expected.to_display_string(),
                        actual_eq.to_display_string()
                    ),
                    actual_eq.clone(),
                    Some(expected),
                    "toEqual",
                );
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toStrictEqual"),
        Value::native(move |args: &[Value]| {
            let expected = args.first().cloned().unwrap_or(Value::Null);
            if !deep_strict_equal(&actual_strict, &expected) {
                return fail(
                    format!(
                        "expect(received).toStrictEqual(expected)\n\nExpected: {}\nReceived: {}",
                        expected.to_display_string(),
                        actual_strict.to_display_string()
                    ),
                    actual_strict.clone(),
                    Some(expected),
                    "toStrictEqual",
                );
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toThrow"),
        Value::native(move |args: &[Value]| {
            // A non-callable would make `value_call` set a pending throw, which used to read
            // as "the code threw" and pass. Reject it up front instead.
            if !is_callable(&actual_throw) {
                return fail(
                    format!(
                        "expect(received).toThrow()\n\nReceived value is not a function: {}",
                        actual_throw.to_display_string()
                    ),
                    actual_throw.clone(),
                    None,
                    "toThrow",
                );
            }
            let _ = take_pending_throw();
            let _ = value_call(&actual_throw, &[]);
            if !has_pending_throw() {
                return fail(
                    "expect(received).toThrow()\n\nReceived function did not throw".into(),
                    actual_throw.clone(),
                    None,
                    "toThrow",
                );
            }
            let err = take_pending_throw().unwrap_or(Value::Null);
            if let Some(expected) = args.first() {
                if !error_matches(&err, expected) {
                    return fail(
                        format!(
                            "expect(received).toThrow(expected)\n\nExpected: {}\nReceived: {}",
                            describe_error_matcher(expected),
                            error_text(&err)
                        ),
                        err,
                        Some(expected.clone()),
                        "toThrow",
                    );
                }
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toMatch"),
        Value::native(move |args: &[Value]| {
            let s = actual_match.to_display_string();
            let pattern = args.first().cloned().unwrap_or(Value::Null);
            let ok = match &pattern {
                #[cfg(feature = "regex")]
                Value::RegExp(r) => r.borrow_mut().test(&s),
                Value::String(p) => s.contains(p.as_str()),
                other => s.contains(&other.to_display_string()),
            };
            if !ok {
                return fail(
                    format!(
                        "expect(received).toMatch(expected)\n\nExpected pattern: {}\nReceived: {}",
                        pattern.to_display_string(),
                        s
                    ),
                    actual_match.clone(),
                    Some(pattern),
                    "toMatch",
                );
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toContain"),
        Value::native(move |args: &[Value]| {
            let item = args.first().cloned().unwrap_or(Value::Null);
            let ok = match &actual_contain {
                Value::String(s) => s.as_str().contains(&item.to_display_string()),
                Value::Array(a) => a.borrow().iter().any(|v| v.strict_eq(&item)),
                Value::NumberArray(a) => a
                    .borrow()
                    .to_values()
                    .iter()
                    .any(|v| v.strict_eq(&item)),
                _ => false,
            };
            if !ok {
                return fail(
                    format!(
                        "expect(received).toContain(expected)\n\nExpected to contain: {}\nReceived: {}",
                        item.to_display_string(),
                        actual_contain.to_display_string()
                    ),
                    actual_contain.clone(),
                    Some(item),
                    "toContain",
                );
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toBeNull"),
        Value::native(move |_args: &[Value]| {
            if !matches!(actual_null, Value::Null) {
                return fail(
                    format!(
                        "expect(received).toBeNull()\n\nReceived: {}",
                        actual_null.to_display_string()
                    ),
                    actual_null.clone(),
                    Some(Value::Null),
                    "toBeNull",
                );
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toBeTruthy"),
        Value::native(move |_args: &[Value]| {
            if !is_truthy(&actual_truthy) {
                return fail(
                    format!(
                        "expect(received).toBeTruthy()\n\nReceived: {}",
                        actual_truthy.to_display_string()
                    ),
                    actual_truthy.clone(),
                    None,
                    "toBeTruthy",
                );
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toBeFalsy"),
        Value::native(move |_args: &[Value]| {
            if is_truthy(&actual_falsy) {
                return fail(
                    format!(
                        "expect(received).toBeFalsy()\n\nReceived: {}",
                        actual_falsy.to_display_string()
                    ),
                    actual_falsy.clone(),
                    None,
                    "toBeFalsy",
                );
            }
            Value::Null
        }),
    );
    // Tish has no `undefined`; `null` is the absence value, so these mirror `toBeNull`.
    // Always-passing / always-failing versions were traps: ported Node suites got no signal.
    m.insert(
        Arc::from("toBeDefined"),
        Value::native(move |_args: &[Value]| {
            if matches!(actual_defined, Value::Null) {
                return fail(
                    "expect(received).toBeDefined()\n\nReceived: null".into(),
                    actual_defined.clone(),
                    None,
                    "toBeDefined",
                );
            }
            Value::Null
        }),
    );
    let actual_undefined = actual.clone();
    m.insert(
        Arc::from("toBeUndefined"),
        Value::native(move |_args: &[Value]| {
            if !matches!(actual_undefined, Value::Null) {
                return fail(
                    format!(
                        "expect(received).toBeUndefined()\n\nTish has no undefined; null is the absence value.\nReceived: {}",
                        actual_undefined.to_display_string()
                    ),
                    actual_undefined.clone(),
                    None,
                    "toBeUndefined",
                );
            }
            Value::Null
        }),
    );
    let actual_close = actual.clone();
    m.insert(
        Arc::from("toBeCloseTo"),
        Value::native(move |args: &[Value]| {
            let expected = args.first().and_then(|v| v.as_number()).unwrap_or(0.0);
            let digits = args.get(1).and_then(|v| v.as_number()).unwrap_or(2.0);
            let act = actual_close.as_number().unwrap_or(f64::NAN);
            // Jest/Bun: |a - b| < 10^-digits / 2. The un-halved form accepted values Jest rejects.
            let prec = 10f64.powf(-digits.max(0.0)) / 2.0;
            if !((act - expected).abs() < prec) {
                return fail(
                    format!(
                        "expect(received).toBeCloseTo({expected}, {digits})\n\nReceived: {act}"
                    ),
                    actual_close.clone(),
                    Some(Value::Number(expected)),
                    "toBeCloseTo",
                );
            }
            Value::Null
        }),
    );
    let actual_match_obj = actual.clone();
    m.insert(
        Arc::from("toMatchObject"),
        Value::native(move |args: &[Value]| {
            let expected = args.first().cloned().unwrap_or(Value::Null);
            if !crate::deep_equal::partial_deep_strict_equal(&actual_match_obj, &expected) {
                return fail(
                    format!(
                        "expect(received).toMatchObject(expected)\n\nExpected: {}\nReceived: {}",
                        expected.to_display_string(),
                        actual_match_obj.to_display_string()
                    ),
                    actual_match_obj.clone(),
                    Some(expected),
                    "toMatchObject",
                );
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toHaveLength"),
        Value::native(move |args: &[Value]| {
            let expected = args.first().and_then(|v| v.as_number()).unwrap_or(0.0);
            match length_of(&actual_len) {
                Some(n) if (n - expected).abs() < f64::EPSILON => Value::Null,
                Some(n) => fail(
                    format!(
                        "expect(received).toHaveLength({})\n\nReceived length: {}",
                        expected, n
                    ),
                    actual_len.clone(),
                    Some(Value::Number(expected)),
                    "toHaveLength",
                ),
                None => fail(
                    format!(
                        "expect(received).toHaveLength()\n\nReceived value has no length: {}",
                        actual_len.to_display_string()
                    ),
                    actual_len.clone(),
                    Some(Value::Number(expected)),
                    "toHaveLength",
                ),
            }
        }),
    );
    m.insert(
        Arc::from("toBeGreaterThan"),
        Value::native(move |args: &[Value]| {
            let exp = args.first().and_then(|v| v.as_number()).unwrap_or(0.0);
            let act = actual_gt.as_number().unwrap_or(f64::NAN);
            if !(act > exp) {
                return fail(
                    format!("expect({act}).toBeGreaterThan({exp})"),
                    actual_gt.clone(),
                    Some(Value::Number(exp)),
                    "toBeGreaterThan",
                );
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toBeGreaterThanOrEqual"),
        Value::native(move |args: &[Value]| {
            let exp = args.first().and_then(|v| v.as_number()).unwrap_or(0.0);
            let act = actual_gte.as_number().unwrap_or(f64::NAN);
            if !(act >= exp) {
                return fail(
                    format!("expect({act}).toBeGreaterThanOrEqual({exp})"),
                    actual_gte.clone(),
                    Some(Value::Number(exp)),
                    "toBeGreaterThanOrEqual",
                );
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toBeLessThan"),
        Value::native(move |args: &[Value]| {
            let exp = args.first().and_then(|v| v.as_number()).unwrap_or(0.0);
            let act = actual_lt.as_number().unwrap_or(f64::NAN);
            if !(act < exp) {
                return fail(
                    format!("expect({act}).toBeLessThan({exp})"),
                    actual_lt.clone(),
                    Some(Value::Number(exp)),
                    "toBeLessThan",
                );
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toBeLessThanOrEqual"),
        Value::native(move |args: &[Value]| {
            let exp = args.first().and_then(|v| v.as_number()).unwrap_or(0.0);
            let act = actual_lte.as_number().unwrap_or(f64::NAN);
            if !(act <= exp) {
                return fail(
                    format!("expect({act}).toBeLessThanOrEqual({exp})"),
                    actual_lte.clone(),
                    Some(Value::Number(exp)),
                    "toBeLessThanOrEqual",
                );
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toHaveBeenCalled"),
        Value::native(move |_args: &[Value]| {
            let Some(meta) = mock_meta(&actual_called) else {
                return fail(
                    "expect(...).toHaveBeenCalled() requires a mock or spy".into(),
                    actual_called.clone(),
                    None,
                    "toHaveBeenCalled",
                );
            };
            if mock_call_count(&meta) < 1.0 {
                return fail(
                    "expect(mock).toHaveBeenCalled()".into(),
                    actual_called.clone(),
                    None,
                    "toHaveBeenCalled",
                );
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toHaveBeenCalledTimes"),
        Value::native(move |args: &[Value]| {
            let Some(meta) = mock_meta(&actual_called_times) else {
                return fail(
                    "expect(...).toHaveBeenCalledTimes() requires a mock or spy".into(),
                    actual_called_times.clone(),
                    None,
                    "toHaveBeenCalledTimes",
                );
            };
            let expected = args.first().and_then(|v| v.as_number()).unwrap_or(0.0);
            let got = mock_call_count(&meta);
            if got != expected {
                return fail(
                    format!("expect(mock).toHaveBeenCalledTimes({expected})\n\nReceived: {got}"),
                    actual_called_times.clone(),
                    Some(Value::Number(expected)),
                    "toHaveBeenCalledTimes",
                );
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toHaveBeenCalledWith"),
        Value::native(move |args: &[Value]| {
            let Some(meta) = mock_meta(&actual_called_with) else {
                return fail(
                    "expect(...).toHaveBeenCalledWith() requires a mock or spy".into(),
                    actual_called_with.clone(),
                    None,
                    "toHaveBeenCalledWith",
                );
            };
            let expected = args.to_vec();
            let ok = mock_calls(&meta).iter().any(|call| match call {
                Value::Array(a) => {
                    let got = a.borrow();
                    got.len() == expected.len()
                        && got
                            .iter()
                            .zip(expected.iter())
                            .all(|(g, e)| deep_strict_equal(g, e))
                }
                _ => false,
            });
            if !ok {
                return fail(
                    format!(
                        "expect(mock).toHaveBeenCalledWith({})\n\nNumber of calls: {}",
                        expected
                            .iter()
                            .map(|v| v.to_display_string())
                            .collect::<Vec<_>>()
                            .join(", "),
                        mock_call_count(&meta)
                    ),
                    actual_called_with.clone(),
                    None,
                    "toHaveBeenCalledWith",
                );
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toHaveBeenLastCalledWith"),
        Value::native(move |args: &[Value]| {
            let Some(meta) = mock_meta(&actual_last_with) else {
                return fail(
                    "expect(...).toHaveBeenLastCalledWith() requires a mock or spy".into(),
                    actual_last_with.clone(),
                    None,
                    "toHaveBeenLastCalledWith",
                );
            };
            let expected = args.to_vec();
            let calls = mock_calls(&meta);
            let Some(last) = calls.last() else {
                return fail(
                    "expect(mock).toHaveBeenLastCalledWith(...)\n\nNumber of calls: 0".into(),
                    actual_last_with.clone(),
                    None,
                    "toHaveBeenLastCalledWith",
                );
            };
            let ok = match last {
                Value::Array(a) => {
                    let got = a.borrow();
                    got.len() == expected.len()
                        && got
                            .iter()
                            .zip(expected.iter())
                            .all(|(g, e)| deep_strict_equal(g, e))
                }
                _ => false,
            };
            if !ok {
                return fail(
                    "expect(mock).toHaveBeenLastCalledWith(...)".into(),
                    actual_last_with.clone(),
                    None,
                    "toHaveBeenLastCalledWith",
                );
            }
            Value::Null
        }),
    );
    m.insert(
        Arc::from("toMatchSnapshot"),
        Value::native(move |args: &[Value]| {
            let hint = args.first().map(|v| v.to_display_string());
            crate::snapshots::match_snapshot(&actual_snap, hint.as_deref())
        }),
    );
    let actual_nan = actual.clone();
    m.insert(
        Arc::from("toBeNaN"),
        Value::native(move |_args: &[Value]| {
            if !matches!(actual_nan, Value::Number(n) if n.is_nan()) {
                return fail(
                    format!(
                        "expect(received).toBeNaN()\n\nReceived: {}",
                        actual_nan.to_display_string()
                    ),
                    actual_nan.clone(),
                    None,
                    "toBeNaN",
                );
            }
            Value::Null
        }),
    );
    let actual_typeof = actual.clone();
    m.insert(
        Arc::from("toBeTypeOf"),
        Value::native(move |args: &[Value]| {
            let want = args
                .first()
                .map(|v| v.to_display_string())
                .unwrap_or_default();
            let got = type_of_name(&actual_typeof);
            if got != want {
                return fail(
                    format!("expect(received).toBeTypeOf({want:?})\n\nReceived type: {got}"),
                    actual_typeof.clone(),
                    None,
                    "toBeTypeOf",
                );
            }
            Value::Null
        }),
    );
    let actual_prop = actual.clone();
    m.insert(
        Arc::from("toHaveProperty"),
        Value::native(move |args: &[Value]| {
            let path = args.first().cloned().unwrap_or(Value::Null);
            let Some(found) = property_at(&actual_prop, &path) else {
                return fail(
                    format!(
                        "expect(received).toHaveProperty({})\n\nProperty not found on: {}",
                        path.to_display_string(),
                        actual_prop.to_display_string()
                    ),
                    actual_prop.clone(),
                    None,
                    "toHaveProperty",
                );
            };
            if let Some(expected) = args.get(1) {
                if !deep_strict_equal(&found, expected) {
                    return fail(
                        format!(
                            "expect(received).toHaveProperty({}, expected)\n\nExpected: {}\nReceived: {}",
                            path.to_display_string(),
                            expected.to_display_string(),
                            found.to_display_string()
                        ),
                        found,
                        Some(expected.clone()),
                        "toHaveProperty",
                    );
                }
            }
            Value::Null
        }),
    );
    let actual_contain_eq = actual.clone();
    m.insert(
        Arc::from("toContainEqual"),
        Value::native(move |args: &[Value]| {
            let item = args.first().cloned().unwrap_or(Value::Null);
            let items = match actual_contain_eq.clone().coerce_number_array() {
                Value::Array(a) => a.borrow().clone(),
                _ => Vec::new(),
            };
            if !items.iter().any(|v| deep_strict_equal(v, &item)) {
                return fail(
                    format!(
                        "expect(received).toContainEqual(expected)\n\nExpected to contain: {}\nReceived: {}",
                        item.to_display_string(),
                        actual_contain_eq.to_display_string()
                    ),
                    actual_contain_eq.clone(),
                    Some(item),
                    "toContainEqual",
                );
            }
            Value::Null
        }),
    );

    // `toThrowError` is Jest's alias for `toThrow`.
    if let Some(t) = m.get("toThrow").cloned() {
        m.insert(Arc::from("toThrowError"), t);
    }

    attach_not(&mut m, actual);
    Value::object(m)
}

/// Build `expect(x).not.*` by inverting each matcher via pending-throw polarity.
fn attach_not(m: &mut ObjectMap, actual: Value) {
    let mut not_map = ObjectMap::default();
    let keys: Vec<Arc<str>> = m.keys().cloned().collect();
    for key in keys {
        // `.not.toMatchSnapshot()` would write (or diff-invert) a snapshot; Jest has no such
        // matcher either.
        if key.as_ref() == "not" || key.as_ref() == "toMatchSnapshot" {
            continue;
        }
        let Some(matcher) = m.get(key.as_ref()).cloned() else {
            continue;
        };
        let act = actual.clone();
        let name = key.to_string();
        not_map.insert(
            Arc::clone(&key),
            Value::native(move |args: &[Value]| {
                let _ = take_pending_throw();
                let _ = value_call(&matcher, args);
                if has_pending_throw() {
                    let _ = take_pending_throw();
                    return Value::Null;
                }
                fail(
                    format!("expect(received).not.{name}() expected matcher to fail"),
                    act.clone(),
                    None,
                    &format!("not.{name}"),
                )
            }),
        );
    }
    m.insert(Arc::from("not"), Value::object(not_map));
}

thread_local! {
    static ASSERTIONS_MADE: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
    /// From `expect.assertions(n)`.
    static ASSERTIONS_EXPECTED: std::cell::Cell<Option<usize>> =
        const { std::cell::Cell::new(None) };
    /// From `expect.hasAssertions()` — "at least one".
    static ASSERTIONS_AT_LEAST: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}

/// Reset the per-test assertion counters. Called by the runner before each test body.
pub fn begin_test_assertions() {
    ASSERTIONS_MADE.with(|c| c.set(0));
    ASSERTIONS_EXPECTED.with(|c| c.set(None));
    ASSERTIONS_AT_LEAST.with(|c| c.set(false));
}

/// Verify an `expect.assertions(n)` / `expect.hasAssertions()` contract for the test that just ran.
pub fn check_test_assertions() -> Result<(), String> {
    let made = ASSERTIONS_MADE.with(|c| c.get());
    if ASSERTIONS_AT_LEAST.with(|c| c.get()) && made == 0 {
        return Err("expect.hasAssertions(): no assertions were made".to_string());
    }
    if let Some(want) = ASSERTIONS_EXPECTED.with(|c| c.get()) {
        if made != want {
            return Err(format!(
                "expect.assertions({want}): {made} assertion(s) were made"
            ));
        }
    }
    Ok(())
}

fn expect_call(args: &[Value]) -> Value {
    ASSERTIONS_MADE.with(|c| c.set(c.get() + 1));
    let actual = args.first().cloned().unwrap_or(Value::Null);
    matcher_object(actual)
}

fn asymmetric_any(args: &[Value]) -> Value {
    let type_name = args
        .first()
        .map(|v| v.to_display_string())
        .unwrap_or_else(|| "Object".into());
    let mut m = ObjectMap::default();
    m.insert(Arc::from(ASYMMETRIC_KEY), Value::String("any".into()));
    m.insert(Arc::from("typeName"), Value::String(type_name.into()));
    Value::object(m)
}

fn asymmetric_object_containing(args: &[Value]) -> Value {
    let sample = args.first().cloned().unwrap_or(Value::Null);
    let mut m = ObjectMap::default();
    m.insert(
        Arc::from(ASYMMETRIC_KEY),
        Value::String("objectContaining".into()),
    );
    m.insert(Arc::from("sample"), sample);
    Value::object(m)
}

fn asymmetric_array_containing(args: &[Value]) -> Value {
    let sample = args.first().cloned().unwrap_or(Value::Null);
    let mut m = ObjectMap::default();
    m.insert(
        Arc::from(ASYMMETRIC_KEY),
        Value::String("arrayContaining".into()),
    );
    m.insert(Arc::from("sample"), sample);
    Value::object(m)
}

fn asymmetric_string_matching(args: &[Value]) -> Value {
    let sample = args.first().cloned().unwrap_or(Value::Null);
    let mut m = ObjectMap::default();
    m.insert(
        Arc::from(ASYMMETRIC_KEY),
        Value::String("stringMatching".into()),
    );
    m.insert(Arc::from("sample"), sample);
    Value::object(m)
}

/// Marker key for `expect.any` / `objectContaining` / … Deliberately *not* React's `$$typeof`,
/// which a plain user object may legitimately carry — treating those as matchers made them
/// compare unequal to everything.
pub(crate) const ASYMMETRIC_KEY: &str = "__tishAsymmetricMatcher";

/// Build the callable `expect` object.
pub fn expect_object() -> Value {
    let mut m = ObjectMap::default();
    m.insert(Arc::from("__call"), Value::native(expect_call));
    m.insert(
        Arc::from("assertions"),
        Value::native(|args: &[Value]| {
            let n = args.first().and_then(|v| v.as_number()).unwrap_or(0.0);
            ASSERTIONS_EXPECTED.with(|c| c.set(Some(n.max(0.0) as usize)));
            Value::Null
        }),
    );
    m.insert(
        Arc::from("hasAssertions"),
        Value::native(|_args: &[Value]| {
            ASSERTIONS_AT_LEAST.with(|c| c.set(true));
            Value::Null
        }),
    );
    m.insert(Arc::from("any"), Value::native(asymmetric_any));
    m.insert(
        Arc::from("anything"),
        Value::native(|_args: &[Value]| {
            let mut o = ObjectMap::default();
            o.insert(Arc::from(ASYMMETRIC_KEY), Value::String("anything".into()));
            Value::object(o)
        }),
    );
    m.insert(
        Arc::from("objectContaining"),
        Value::native(asymmetric_object_containing),
    );
    m.insert(
        Arc::from("arrayContaining"),
        Value::native(asymmetric_array_containing),
    );
    m.insert(
        Arc::from("stringMatching"),
        Value::native(asymmetric_string_matching),
    );
    Value::object(m)
}