test-better-macros 0.2.1

Procedural macros for the test-better testing library.
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
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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
//! `test-better-macros`: procedural macros.
//!
//! Home of `matches_struct!`, `matches_tuple!`, `matches_variant!`, the
//! `#[test_case]` attribute, and the `#[fixture]` / `#[test_with_fixtures]`
//! attribute pair, with the inline snapshot macros still to come.
//!
//! The structural matchers parse a *pattern* of inner matcher expressions and
//! emit a `Matcher` impl. The matcher holds a projection (a closure that pulls
//! the fields out of the value) plus one inner matcher per field; the
//! projection's type ties the matcher's type parameters to the real field
//! types, so the field types never have to be named in the macro. The
//! projection is threaded through a generated constructor function whose
//! signature carries the `Fn` bound, which is what makes the closure infer as
//! higher-ranked over the borrow.
//!
//! `#[test_case]` is an attribute macro: stacked `#[test_case(..)]` lines on one
//! function each become a generated `#[test]`, all gathered into a module named
//! for the function so the cases share a namespace.
//!
//! `#[fixture]` turns a `fn() -> TestResult<T>` into a fixture: its failures are
//! re-categorized as `ErrorKind::Setup` so a setup problem never reads as an
//! assertion miss. `#[test_with_fixtures]` is the seam that consumes them: each
//! parameter `name: T` is filled by calling the same-named fixture `fn name()`.
//!
//! The generated code refers to the testing library through the `::test_better`
//! facade crate, so these macros are meant to be used via `test-better`, not by
//! depending on `test-better-macros` directly.

use std::collections::HashSet;

use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::{Expr, FnArg, Ident, Index, ItemFn, LitStr, Pat, Path, Token, braced, parenthesized};

/// A named-field pattern: `Path { field: matcher, ..., .. }`.
struct StructPattern {
    path: Path,
    fields: Vec<(Ident, Expr)>,
    rest: bool,
}

impl Parse for StructPattern {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let path: Path = input.parse()?;
        let content;
        braced!(content in input);
        let (fields, rest) = parse_named_fields(&content)?;
        Ok(Self { path, fields, rest })
    }
}

/// A positional pattern: `Path(matcher, ..., ..)`.
struct TuplePattern {
    path: Path,
    elems: Vec<Expr>,
    rest: bool,
}

impl Parse for TuplePattern {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let path: Path = input.parse()?;
        let content;
        parenthesized!(content in input);
        let (elems, rest) = parse_positional_fields(&content)?;
        Ok(Self { path, elems, rest })
    }
}

/// The body of a variant pattern: struct-like, tuple-like, or unit.
enum VariantBody {
    Struct {
        fields: Vec<(Ident, Expr)>,
        rest: bool,
    },
    Tuple {
        elems: Vec<Expr>,
        rest: bool,
    },
    Unit,
}

/// A variant pattern: `Enum::Variant { .. }`, `Enum::Variant( .. )`, or
/// `Enum::Variant`.
struct VariantPattern {
    path: Path,
    body: VariantBody,
}

impl Parse for VariantPattern {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let path: Path = input.parse()?;
        let body = if input.peek(syn::token::Brace) {
            let content;
            braced!(content in input);
            let (fields, rest) = parse_named_fields(&content)?;
            VariantBody::Struct { fields, rest }
        } else if input.peek(syn::token::Paren) {
            let content;
            parenthesized!(content in input);
            let (elems, rest) = parse_positional_fields(&content)?;
            VariantBody::Tuple { elems, rest }
        } else {
            VariantBody::Unit
        };
        Ok(Self { path, body })
    }
}

/// Parses `field: expr` entries, optionally ending with `..`. The `..`, when
/// present, must be the final element.
fn parse_named_fields(content: ParseStream) -> syn::Result<(Vec<(Ident, Expr)>, bool)> {
    let mut fields = Vec::new();
    let mut rest = false;
    while !content.is_empty() {
        if content.peek(Token![..]) {
            content.parse::<Token![..]>()?;
            rest = true;
            break;
        }
        let name: Ident = content.parse()?;
        content.parse::<Token![:]>()?;
        let expr: Expr = content.parse()?;
        fields.push((name, expr));
        if content.is_empty() {
            break;
        }
        content.parse::<Token![,]>()?;
    }
    if !content.is_empty() {
        return Err(content.error("`..` must be the final element of the pattern"));
    }
    Ok((fields, rest))
}

/// Parses positional `expr` entries, optionally ending with `..`. The `..`,
/// when present, must be the final element.
fn parse_positional_fields(content: ParseStream) -> syn::Result<(Vec<Expr>, bool)> {
    let mut elems = Vec::new();
    let mut rest = false;
    while !content.is_empty() {
        if content.peek(Token![..]) {
            content.parse::<Token![..]>()?;
            rest = true;
            break;
        }
        elems.push(content.parse()?);
        if content.is_empty() {
            break;
        }
        content.parse::<Token![,]>()?;
    }
    if !content.is_empty() {
        return Err(content.error("`..` must be the final element of the pattern"));
    }
    Ok((elems, rest))
}

/// Splits `Enum::Variant` into the enum path (`Enum`) and the variant ident
/// (`Variant`).
fn split_variant_path(path: &Path) -> syn::Result<(Path, Ident)> {
    if path.segments.len() < 2 {
        return Err(syn::Error::new_spanned(
            path,
            "expected an enum variant path like `MyEnum::Variant`",
        ));
    }
    let kept = path.segments.len() - 1;
    let segments: Punctuated<syn::PathSegment, Token![::]> =
        path.segments.iter().take(kept).cloned().collect();
    let enum_path = Path {
        leading_colon: path.leading_colon,
        segments,
    };
    let variant_ident = match path.segments.last() {
        Some(seg) => seg.ident.clone(),
        None => return Err(syn::Error::new_spanned(path, "missing variant name")),
    };
    Ok((enum_path, variant_ident))
}

/// The per-field generated idents: the matcher type parameter, the field-type
/// parameter, the struct field holding the matcher, and the binding the
/// projection's output is destructured into.
struct FieldIdents {
    matcher_ty: Vec<Ident>,
    field_ty: Vec<Ident>,
    matcher_field: Vec<Ident>,
    binding: Vec<Ident>,
}

fn field_idents(n: usize) -> FieldIdents {
    FieldIdents {
        matcher_ty: (0..n).map(|i| format_ident!("__TbM{}", i)).collect(),
        field_ty: (0..n).map(|i| format_ident!("__TbF{}", i)).collect(),
        matcher_field: (0..n).map(|i| format_ident!("__tb_m{}", i)).collect(),
        binding: (0..n).map(|i| format_ident!("__tb_f{}", i)).collect(),
    }
}

/// The body of each field's check: run the inner matcher on the projected
/// field, and on failure return a mismatch whose expectation is labeled with
/// the field name.
fn field_check_blocks(
    matcher_field: &[Ident],
    binding: &[Ident],
    labels: &[String],
) -> Vec<TokenStream2> {
    matcher_field
        .iter()
        .zip(binding)
        .zip(labels)
        .map(|((field, bind), label)| {
            let label = label.as_str();
            quote! {
                {
                    let __tb_result = ::test_better::Matcher::check(&self.#field, #bind);
                    if !__tb_result.matched {
                        let __tb_inner = match __tb_result.failure {
                            ::core::option::Option::Some(__tb_mismatch) => __tb_mismatch,
                            ::core::option::Option::None => ::test_better::Mismatch::new(
                                ::test_better::Matcher::description(&self.#field),
                                "the field matcher reported failure without detail",
                            ),
                        };
                        return ::test_better::MatchResult::fail(::test_better::Mismatch {
                            expected: ::test_better::Description::labeled(
                                #label,
                                __tb_inner.expected,
                            ),
                            actual: __tb_inner.actual,
                            diff: __tb_inner.diff,
                        });
                    }
                }
            }
        })
        .collect()
}

/// Folds each field's labeled description together under conjunction.
fn description_fold(matcher_field: &[Ident], labels: &[String]) -> TokenStream2 {
    let mut parts = matcher_field.iter().zip(labels).map(|(field, label)| {
        let label = label.as_str();
        quote! {
            ::test_better::Description::labeled(
                #label,
                ::test_better::Matcher::description(&self.#field),
            )
        }
    });
    match parts.next() {
        Some(first) => {
            let mut acc = first;
            for part in parts {
                acc = quote! { #acc.and(#part) };
            }
            acc
        }
        None => quote! { ::test_better::Description::text("a matching value") },
    }
}

/// Wraps an exhaustiveness-checking statement in a never-called function, so a
/// missing or unknown field is a hard error from rustc's own pattern checking.
fn exhaustiveness_fn(target: &TokenStream2, stmt: Option<TokenStream2>) -> TokenStream2 {
    match stmt {
        Some(stmt) => quote! {
            #[allow(dead_code, unused_variables, irrefutable_let_patterns, clippy::all)]
            fn __tb_assert_exhaustive(__tb_value: &#target) {
                #stmt
            }
        },
        None => quote! {},
    }
}

/// Assembles a plain (struct or tuple struct) structural matcher.
///
/// `projection` is a closure `Fn(&Self) -> (&F0, &F1, ...)`. It is passed to
/// the generated `__tb_make`, whose signature carries the `Fn` bound; that is
/// what lets the closure infer as higher-ranked over the borrow. `__tb_make`'s
/// where-clause then pins the matcher's `Self` and field types.
fn gen_plain(
    target: &TokenStream2,
    labels: &[String],
    field_exprs: &[&Expr],
    projection: TokenStream2,
    exhaustiveness: Option<TokenStream2>,
) -> TokenStream2 {
    let idents = field_idents(labels.len());
    let FieldIdents {
        matcher_ty,
        field_ty,
        matcher_field,
        binding,
    } = &idents;
    let n = labels.len();
    let assertion = exhaustiveness_fn(target, exhaustiveness);

    if n == 0 {
        return quote! {
            {
                #[allow(non_camel_case_types, dead_code, clippy::all)]
                struct __TbStructuralMatcher<__TbP> {
                    __tb_project: __TbP,
                }

                #[allow(clippy::all)]
                impl<__TbS, __TbP> ::test_better::Matcher<__TbS>
                    for __TbStructuralMatcher<__TbP>
                where
                    __TbP: ::core::ops::Fn(&__TbS) -> (),
                {
                    fn check(&self, __tb_actual: &__TbS) -> ::test_better::MatchResult {
                        let () = (self.__tb_project)(__tb_actual);
                        ::test_better::MatchResult::pass()
                    }

                    fn description(&self) -> ::test_better::Description {
                        ::test_better::Description::text("a matching value")
                    }
                }

                #[allow(clippy::all)]
                fn __tb_make<__TbS, __TbP>(
                    __tb_project: __TbP,
                ) -> impl ::test_better::Matcher<__TbS>
                where
                    __TbP: ::core::ops::Fn(&__TbS) -> (),
                {
                    __TbStructuralMatcher { __tb_project }
                }

                #assertion

                __tb_make(#projection)
            }
        };
    }

    let checks = field_check_blocks(matcher_field, binding, labels);
    let desc = description_fold(matcher_field, labels);

    quote! {
        {
            #[allow(non_camel_case_types, dead_code, clippy::all)]
            struct __TbStructuralMatcher<__TbP, #( #matcher_ty, )*> {
                __tb_project: __TbP,
                #( #matcher_field: #matcher_ty, )*
            }

            #[allow(clippy::all)]
            impl<__TbS, #( #field_ty, )* __TbP, #( #matcher_ty, )*>
                ::test_better::Matcher<__TbS>
                for __TbStructuralMatcher<__TbP, #( #matcher_ty, )*>
            where
                __TbP: ::core::ops::Fn(&__TbS) -> ( #( &#field_ty, )* ),
                #( #matcher_ty: ::test_better::Matcher<#field_ty>, )*
            {
                fn check(&self, __tb_actual: &__TbS) -> ::test_better::MatchResult {
                    let ( #( #binding, )* ) = (self.__tb_project)(__tb_actual);
                    #( #checks )*
                    ::test_better::MatchResult::pass()
                }

                fn description(&self) -> ::test_better::Description {
                    #desc
                }
            }

            #[allow(clippy::all)]
            fn __tb_make<__TbS, #( #field_ty, )* __TbP, #( #matcher_ty, )*>(
                __tb_project: __TbP,
                #( #matcher_field: #matcher_ty, )*
            ) -> impl ::test_better::Matcher<__TbS>
            where
                __TbP: ::core::ops::Fn(&__TbS) -> ( #( &#field_ty, )* ),
                #( #matcher_ty: ::test_better::Matcher<#field_ty>, )*
            {
                __TbStructuralMatcher {
                    __tb_project,
                    #( #matcher_field, )*
                }
            }

            #assertion

            __tb_make(#projection, #( #field_exprs, )*)
        }
    }
}

fn gen_struct(path: &Path, fields: &[(Ident, Expr)], rest: bool) -> TokenStream2 {
    let target = quote! { #path };
    let labels: Vec<String> = fields.iter().map(|(name, _)| name.to_string()).collect();
    let field_exprs: Vec<&Expr> = fields.iter().map(|(_, expr)| expr).collect();
    let field_names: Vec<&Ident> = fields.iter().map(|(name, _)| name).collect();

    let projection = if fields.is_empty() {
        quote! { |_: &#path| () }
    } else {
        quote! { |__tb_subject: &#path| ( #( &__tb_subject.#field_names, )* ) }
    };

    let exhaustiveness = if rest {
        None
    } else {
        Some(quote! { let #path { #( #field_names: _, )* } = __tb_value; })
    };

    gen_plain(&target, &labels, &field_exprs, projection, exhaustiveness)
}

fn gen_tuple(path: &Path, elems: &[Expr], rest: bool) -> TokenStream2 {
    let target = quote! { #path };
    let labels: Vec<String> = (0..elems.len()).map(|i| i.to_string()).collect();
    let field_exprs: Vec<&Expr> = elems.iter().collect();
    let indices: Vec<Index> = (0..elems.len()).map(Index::from).collect();

    let projection = if elems.is_empty() {
        quote! { |_: &#path| () }
    } else {
        quote! { |__tb_subject: &#path| ( #( &__tb_subject.#indices, )* ) }
    };

    let exhaustiveness = if rest {
        None
    } else {
        let holes = elems.iter().map(|_| quote!(_));
        Some(quote! { let #path( #( #holes, )* ) = __tb_value; })
    };

    gen_plain(&target, &labels, &field_exprs, projection, exhaustiveness)
}

fn gen_variant(pattern: &VariantPattern) -> syn::Result<TokenStream2> {
    let (enum_path, variant_ident) = split_variant_path(&pattern.path)?;
    let path = &pattern.path;
    let target = quote! { #enum_path };
    let variant_name = variant_ident.to_string();
    let variant_label = format!("the {variant_name} variant");

    // The labels, the inner matcher expressions, the projection closure, and the
    // exhaustiveness assertion all differ by variant shape.
    let (labels, field_exprs, projection, exhaustiveness): (
        Vec<String>,
        Vec<&Expr>,
        TokenStream2,
        Option<TokenStream2>,
    ) = match &pattern.body {
        VariantBody::Struct { fields, rest } => {
            let labels: Vec<String> = fields.iter().map(|(name, _)| name.to_string()).collect();
            let field_exprs: Vec<&Expr> = fields.iter().map(|(_, expr)| expr).collect();
            let field_names: Vec<&Ident> = fields.iter().map(|(name, _)| name).collect();
            let bindings: Vec<Ident> = (0..fields.len())
                .map(|i| format_ident!("__tb_p{}", i))
                .collect();
            let projection = quote! {
                |__tb_subject: &#enum_path| match __tb_subject {
                    #path { #( #field_names: #bindings, )* .. } =>
                        ::core::option::Option::Some(( #( #bindings, )* )),
                    _ => ::core::option::Option::None,
                }
            };
            let exhaustiveness = if *rest {
                None
            } else {
                Some(quote! { if let #path { #( #field_names: _, )* } = __tb_value {} })
            };
            (labels, field_exprs, projection, exhaustiveness)
        }
        VariantBody::Tuple { elems, rest } => {
            let labels: Vec<String> = (0..elems.len()).map(|i| i.to_string()).collect();
            let field_exprs: Vec<&Expr> = elems.iter().collect();
            let bindings: Vec<Ident> = (0..elems.len())
                .map(|i| format_ident!("__tb_p{}", i))
                .collect();
            let projection = quote! {
                |__tb_subject: &#enum_path| match __tb_subject {
                    #path( #( #bindings, )* .. ) =>
                        ::core::option::Option::Some(( #( #bindings, )* )),
                    _ => ::core::option::Option::None,
                }
            };
            let exhaustiveness = if *rest {
                None
            } else {
                let holes = elems.iter().map(|_| quote!(_));
                Some(quote! { if let #path( #( #holes, )* ) = __tb_value {} })
            };
            (labels, field_exprs, projection, exhaustiveness)
        }
        VariantBody::Unit => {
            let projection = quote! {
                |__tb_subject: &#enum_path| match __tb_subject {
                    #path => ::core::option::Option::Some(()),
                    _ => ::core::option::Option::None,
                }
            };
            (Vec::new(), Vec::new(), projection, None)
        }
    };

    let idents = field_idents(labels.len());
    let FieldIdents {
        matcher_ty,
        field_ty,
        matcher_field,
        binding,
    } = &idents;
    let n = labels.len();
    let assertion = exhaustiveness_fn(&target, exhaustiveness);

    let wrong_variant = quote! {
        ::test_better::MatchResult::fail(::test_better::Mismatch::new(
            ::test_better::Description::text(#variant_label),
            ::std::format!("{:?}", __tb_actual),
        ))
    };

    if n == 0 {
        return Ok(quote! {
            {
                #[allow(non_camel_case_types, dead_code, clippy::all)]
                struct __TbVariantMatcher<__TbP> {
                    __tb_project: __TbP,
                }

                #[allow(clippy::all)]
                impl<__TbS, __TbP> ::test_better::Matcher<__TbS>
                    for __TbVariantMatcher<__TbP>
                where
                    __TbP: ::core::ops::Fn(&__TbS) -> ::core::option::Option<()>,
                    __TbS: ::core::fmt::Debug,
                {
                    fn check(&self, __tb_actual: &__TbS) -> ::test_better::MatchResult {
                        match (self.__tb_project)(__tb_actual) {
                            ::core::option::Option::Some(()) => {
                                ::test_better::MatchResult::pass()
                            }
                            ::core::option::Option::None => #wrong_variant,
                        }
                    }

                    fn description(&self) -> ::test_better::Description {
                        ::test_better::Description::text(#variant_label)
                    }
                }

                #[allow(clippy::all)]
                fn __tb_make<__TbS, __TbP>(
                    __tb_project: __TbP,
                ) -> impl ::test_better::Matcher<__TbS>
                where
                    __TbP: ::core::ops::Fn(&__TbS) -> ::core::option::Option<()>,
                    __TbS: ::core::fmt::Debug,
                {
                    __TbVariantMatcher { __tb_project }
                }

                #assertion

                __tb_make(#projection)
            }
        });
    }

    let checks = field_check_blocks(matcher_field, binding, &labels);
    let desc_inner = description_fold(matcher_field, &labels);
    let desc = quote! { ::test_better::Description::labeled(#variant_name, #desc_inner) };

    Ok(quote! {
        {
            #[allow(non_camel_case_types, dead_code, clippy::all)]
            struct __TbVariantMatcher<__TbP, #( #matcher_ty, )*> {
                __tb_project: __TbP,
                #( #matcher_field: #matcher_ty, )*
            }

            #[allow(clippy::all)]
            impl<__TbS, #( #field_ty, )* __TbP, #( #matcher_ty, )*>
                ::test_better::Matcher<__TbS>
                for __TbVariantMatcher<__TbP, #( #matcher_ty, )*>
            where
                __TbP: ::core::ops::Fn(&__TbS)
                    -> ::core::option::Option<( #( &#field_ty, )* )>,
                #( #matcher_ty: ::test_better::Matcher<#field_ty>, )*
                __TbS: ::core::fmt::Debug,
            {
                fn check(&self, __tb_actual: &__TbS) -> ::test_better::MatchResult {
                    match (self.__tb_project)(__tb_actual) {
                        ::core::option::Option::Some(( #( #binding, )* )) => {
                            #( #checks )*
                            ::test_better::MatchResult::pass()
                        }
                        ::core::option::Option::None => #wrong_variant,
                    }
                }

                fn description(&self) -> ::test_better::Description {
                    #desc
                }
            }

            #[allow(clippy::all)]
            fn __tb_make<__TbS, #( #field_ty, )* __TbP, #( #matcher_ty, )*>(
                __tb_project: __TbP,
                #( #matcher_field: #matcher_ty, )*
            ) -> impl ::test_better::Matcher<__TbS>
            where
                __TbP: ::core::ops::Fn(&__TbS)
                    -> ::core::option::Option<( #( &#field_ty, )* )>,
                #( #matcher_ty: ::test_better::Matcher<#field_ty>, )*
                __TbS: ::core::fmt::Debug,
            {
                __TbVariantMatcher {
                    __tb_project,
                    #( #matcher_field, )*
                }
            }

            #assertion

            __tb_make(#projection, #( #field_exprs, )*)
        }
    })
}

/// Matches a struct by applying an inner matcher to each named field.
///
/// Without a trailing `..` every field must be listed, exactly as in a struct
/// pattern; with `..` the unlisted fields are ignored.
///
/// ```ignore
/// use test_better::prelude::*;
/// use test_better::matches_struct;
///
/// #[derive(Debug)]
/// struct User {
///     name: String,
///     age: u32,
///     email: String,
/// }
///
/// fn check(user: User) -> TestResult {
///     check!(user).satisfies(matches_struct!(User {
///         name: eq(String::from("alice")),
///         age: gt(0u32),
///         email: contains_str("@"),
///         .. // remaining fields ignored
///     }))?;
///     Ok(())
/// }
/// ```
#[proc_macro]
pub fn matches_struct(input: TokenStream) -> TokenStream {
    match syn::parse::<StructPattern>(input) {
        Ok(pattern) => gen_struct(&pattern.path, &pattern.fields, pattern.rest).into(),
        Err(error) => error.to_compile_error().into(),
    }
}

/// Matches a tuple struct by applying an inner matcher to each positional
/// field.
///
/// Without a trailing `..` every element must be listed; with `..` the unlisted
/// trailing elements are ignored.
///
/// ```ignore
/// use test_better::prelude::*;
/// use test_better::matches_tuple;
///
/// #[derive(Debug)]
/// struct Point(i32, i32);
///
/// fn check(point: Point) -> TestResult {
///     check!(point).satisfies(matches_tuple!(Point(gt(0), lt(100))))?;
///     Ok(())
/// }
/// ```
#[proc_macro]
pub fn matches_tuple(input: TokenStream) -> TokenStream {
    match syn::parse::<TuplePattern>(input) {
        Ok(pattern) => gen_tuple(&pattern.path, &pattern.elems, pattern.rest).into(),
        Err(error) => error.to_compile_error().into(),
    }
}

/// Matches an enum value against a specific variant, applying an inner matcher
/// to each of that variant's fields.
///
/// A value of a different variant is a match failure, not a compile error. The
/// variant may be struct-like (`Enum::Variant { field: m, .. }`), tuple-like
/// (`Enum::Variant(m, ..)`), or unit (`Enum::Variant`). The enum type must be
/// `Debug` so a wrong-variant failure can render the value.
///
/// ```ignore
/// use test_better::prelude::*;
/// use test_better::matches_variant;
///
/// #[derive(Debug)]
/// enum Shape {
///     Circle { radius: f64 },
///     Rectangle(f64, f64),
/// }
///
/// fn check(shape: Shape) -> TestResult {
///     check!(shape).satisfies(matches_variant!(Shape::Circle { radius: gt(0.0) }))?;
///     Ok(())
/// }
/// ```
#[proc_macro]
pub fn matches_variant(input: TokenStream) -> TokenStream {
    let result = syn::parse::<VariantPattern>(input).and_then(|pattern| gen_variant(&pattern));
    match result {
        Ok(tokens) => tokens.into(),
        Err(error) => error.to_compile_error().into(),
    }
}

/// One `#[test_case(..)]` invocation: the argument expressions and an optional
/// `; "label"`.
struct TestCase {
    /// A span pointing at the invocation, used to place an arg-count error on
    /// the offending attribute rather than on the function.
    span: proc_macro2::Span,
    /// The expressions passed positionally to the annotated function.
    args: Vec<Expr>,
    /// The case label after `;`, if one was given.
    label: Option<LitStr>,
}

impl Parse for TestCase {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let span = input.span();
        let mut args = Vec::new();
        let mut label = None;
        while !input.is_empty() {
            if input.peek(Token![;]) {
                input.parse::<Token![;]>()?;
                label = Some(input.parse::<LitStr>()?);
                if !input.is_empty() {
                    return Err(input.error("unexpected tokens after the test-case label"));
                }
                break;
            }
            args.push(input.parse()?);
            if input.is_empty() || input.peek(Token![;]) {
                continue;
            }
            input.parse::<Token![,]>()?;
        }
        Ok(Self { span, args, label })
    }
}

/// Reads one `#[test_case]` attribute. A bare `#[test_case]` (no parentheses) is
/// a zero-argument, unlabeled case; anything else is parsed as a [`TestCase`].
fn parse_test_case_attr(attribute: &syn::Attribute) -> syn::Result<TestCase> {
    match &attribute.meta {
        syn::Meta::Path(_) => Ok(TestCase {
            span: attribute.span(),
            args: Vec::new(),
            label: None,
        }),
        _ => attribute.parse_args::<TestCase>(),
    }
}

/// Whether an attribute is `#[test_case]` (matched on the final path segment so
/// a fully qualified `#[test_better::test_case]` is recognized too).
fn is_test_case_attr(attribute: &syn::Attribute) -> bool {
    attribute
        .path()
        .segments
        .last()
        .is_some_and(|segment| segment.ident == "test_case")
}

/// Turns a case label into a valid, lowercased identifier fragment: every
/// character that is not ASCII alphanumeric becomes `_`, and a leading digit
/// gets an `_` prefix. An empty result falls back to `case`.
fn sanitize_ident(label: &str) -> String {
    let mut out: String = label
        .chars()
        .map(|ch| {
            if ch.is_ascii_alphanumeric() {
                ch.to_ascii_lowercase()
            } else {
                '_'
            }
        })
        .collect();
    if out.is_empty() {
        out.push_str("case");
    }
    if out.starts_with(|ch: char| ch.is_ascii_digit()) {
        out.insert(0, '_');
    }
    out
}

/// Expands the stacked `#[test_case]` attributes on `func` into one `#[test]`
/// per case, all wrapped in a module named for the function.
fn test_case_impl(first: TestCase, mut func: ItemFn) -> syn::Result<TokenStream2> {
    // The topmost `#[test_case]` is handed to us as `attr`; the rest are still
    // attached to the function. Split the remaining attributes into further
    // cases and everything else (`#[ignore]`, doc comments, ...), which is
    // forwarded onto every generated test.
    let mut cases = vec![first];
    let mut forwarded = Vec::new();
    for attribute in std::mem::take(&mut func.attrs) {
        if is_test_case_attr(&attribute) {
            cases.push(parse_test_case_attr(&attribute)?);
        } else {
            forwarded.push(attribute);
        }
    }

    let fn_name = func.sig.ident.clone();
    let fn_name_str = fn_name.to_string();
    let ret = func.sig.output.clone();
    let expected_arity = func.sig.inputs.len();
    // A `-> ()` (explicit or implicit) test cannot carry failure context; only
    // a value-returning test (the `-> TestResult` shape) is wrapped.
    let returns_value = match &func.sig.output {
        syn::ReturnType::Default => false,
        syn::ReturnType::Type(_, ty) => {
            !matches!(&**ty, syn::Type::Tuple(tuple) if tuple.elems.is_empty())
        }
    };

    let mut used_names: HashSet<String> = HashSet::new();
    let mut tests = Vec::with_capacity(cases.len());
    for (index, case) in cases.iter().enumerate() {
        if case.args.len() != expected_arity {
            return Err(syn::Error::new(
                case.span,
                format!(
                    "this `#[test_case]` passes {} argument(s) but `{fn_name_str}` takes {}",
                    case.args.len(),
                    expected_arity,
                ),
            ));
        }

        let base = match &case.label {
            Some(label) => sanitize_ident(&label.value()),
            None => format!("case_{index}"),
        };
        // Two labels that sanitize to the same identifier (or a label that
        // collides with a `case_N` default) are disambiguated by index.
        let name = if used_names.contains(&base) {
            format!("{base}_{index}")
        } else {
            base
        };
        used_names.insert(name.clone());
        let test_ident = format_ident!("{name}");

        let args = &case.args;
        let args_rendered = args
            .iter()
            .map(|arg| quote!(#arg).to_string())
            .collect::<Vec<_>>()
            .join(", ");
        let label_part = match &case.label {
            Some(label) => format!("{:?}", label.value()),
            None => format!("#{index}"),
        };
        let context_msg = format!("test case {label_part}: {fn_name_str}({args_rendered})");

        let body = if returns_value {
            quote! {
                ::test_better::ContextExt::context(#fn_name(#(#args),*), #context_msg)
            }
        } else {
            quote! { #fn_name(#(#args),*); }
        };

        // `pub(super)` so the generated test stays addressable from the file
        // that wrote the `#[test_case]` (e.g. to drive an `#[ignore]`d failing
        // case directly), without widening visibility any further.
        tests.push(quote! {
            #(#forwarded)*
            #[test]
            pub(super) fn #test_ident() #ret {
                #body
            }
        });
    }

    Ok(quote! {
        mod #fn_name {
            #[allow(unused_imports)]
            use super::*;

            #func

            #(#tests)*
        }
    })
}

/// Generates one `#[test]` per `#[test_case(..)]` line on a function.
///
/// Each attribute lists the positional arguments for one run, optionally
/// followed by `; "label"`. The cases are gathered into a module named for the
/// annotated function, so a labeled case is addressable as
/// `the_fn::the_label`; an unlabeled case becomes `the_fn::case_N`. The
/// original function stays callable inside that module as a helper.
///
/// When the function returns a value (the usual `-> TestResult` shape), each
/// generated test wraps the call in failure context carrying the case label
/// and the rendered arguments, so a failure names the case that produced it.
///
/// ```ignore
/// use test_better::prelude::*;
///
/// #[test_case("alice", 30 ; "common case")]
/// #[test_case("",      0  ; "empty name")]
/// fn validates_user(name: &str, age: u32) -> TestResult {
///     check!(name.len()).satisfies(ge(age as usize))
/// }
/// ```
///
/// A `#[test_case]` whose argument count does not match the function's
/// parameter count is a compile error, as is trailing junk after the label.
/// Other attributes on the function (`#[ignore]`, doc comments) are forwarded
/// onto every generated test.
#[proc_macro_attribute]
pub fn test_case(attr: TokenStream, item: TokenStream) -> TokenStream {
    let first = match syn::parse::<TestCase>(attr) {
        Ok(case) => case,
        Err(error) => return error.to_compile_error().into(),
    };
    let func = match syn::parse::<ItemFn>(item) {
        Ok(func) => func,
        Err(error) => return error.to_compile_error().into(),
    };
    match test_case_impl(first, func) {
        Ok(tokens) => tokens.into(),
        Err(error) => error.to_compile_error().into(),
    }
}

/// How long a fixture's value is kept: rebuilt for every test, or built once
/// per module and shared.
enum FixtureScope {
    /// The default: the fixture body runs afresh for each test that uses it.
    Test,
    /// The fixture body runs once; every test gets a clone of the cached value.
    Module,
}

/// The parsed `#[fixture(..)]` arguments. The only knob is `scope`.
struct FixtureArgs {
    scope: FixtureScope,
}

impl Parse for FixtureArgs {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        // A bare `#[fixture]` is per-test scope.
        if input.is_empty() {
            return Ok(Self {
                scope: FixtureScope::Test,
            });
        }
        let key: Ident = input.parse()?;
        if key != "scope" {
            return Err(syn::Error::new_spanned(
                key,
                "the only `#[fixture]` argument is `scope`",
            ));
        }
        input.parse::<Token![=]>()?;
        let value: LitStr = input.parse()?;
        let scope = match value.value().as_str() {
            "test" => FixtureScope::Test,
            "module" => FixtureScope::Module,
            other => {
                return Err(syn::Error::new_spanned(
                    value,
                    format!("unknown fixture scope {other:?}, expected \"test\" or \"module\""),
                ));
            }
        };
        if !input.is_empty() {
            return Err(input.error("unexpected tokens after the fixture scope"));
        }
        Ok(Self { scope })
    }
}

/// Rewrites a `#[fixture]` function into a setup-aware provider.
///
/// The original body is kept verbatim in a nested `__tb_fixture_impl`; the
/// generated outer function (same name, same signature) calls it and, on the
/// error path, stamps the failure as `ErrorKind::Setup` so it can never read
/// as an assertion miss.
fn fixture_impl(args: FixtureArgs, mut func: ItemFn) -> syn::Result<TokenStream2> {
    if let Some(param) = func.sig.inputs.first() {
        return Err(syn::Error::new_spanned(
            param,
            "a `#[fixture]` function takes no parameters",
        ));
    }
    let return_ty = match &func.sig.output {
        syn::ReturnType::Type(_, ty) => (**ty).clone(),
        syn::ReturnType::Default => {
            return Err(syn::Error::new_spanned(
                &func.sig,
                "a `#[fixture]` function must return a `TestResult<T>`",
            ));
        }
    };

    // Everything attached to the function (`#[ignore]` makes no sense here, but
    // `#[allow(..)]`, `#[cfg(..)]`, doc comments do) is forwarded onto the
    // generated provider; it also covers the nested impl, which sits inside it.
    let forwarded: Vec<syn::Attribute> = std::mem::take(&mut func.attrs);

    let fn_name = func.sig.ident.clone();
    let fn_name_str = fn_name.to_string();
    let vis = func.vis.clone();
    let ret = func.sig.output.clone();
    let body = &func.block;
    let context_msg = format!("setting up fixture `{fn_name_str}`");

    let impl_fn = quote! {
        fn __tb_fixture_impl() #ret #body
    };

    let outer_body = match args.scope {
        // Per-test: run the body, re-categorize any failure as `Setup` and add
        // a frame naming the fixture. The successful value is moved straight
        // out, so the fixture type need not be `Clone`.
        FixtureScope::Test => quote! {
            #impl_fn
            ::core::result::Result::map_err(__tb_fixture_impl(), |__tb_error| {
                ::test_better::TestError::with_context_frame(
                    ::test_better::TestError::with_kind(
                        __tb_error,
                        ::test_better::ErrorKind::Setup,
                    ),
                    ::test_better::ContextFrame::new(#context_msg),
                )
            })
        },
        // Module: build once into a `LazyLock`, then hand every caller a clone.
        // The cached `Err` cannot be moved out, so the error path synthesizes a
        // fresh `Setup` failure carrying the original's rendered text.
        FixtureScope::Module => quote! {
            #impl_fn
            static __TB_FIXTURE_CELL: ::std::sync::LazyLock<#return_ty> =
                ::std::sync::LazyLock::new(__tb_fixture_impl);
            match &*__TB_FIXTURE_CELL {
                ::core::result::Result::Ok(__tb_value) => {
                    ::core::result::Result::Ok(::core::clone::Clone::clone(__tb_value))
                }
                ::core::result::Result::Err(__tb_error) => {
                    ::core::result::Result::Err(
                        ::test_better::TestError::with_message(
                            ::test_better::TestError::new(
                                ::test_better::ErrorKind::Setup,
                            ),
                            ::std::format!(
                                "module-scoped fixture `{}` failed during setup: {}",
                                #fn_name_str,
                                __tb_error,
                            ),
                        ),
                    )
                }
            }
        },
    };

    Ok(quote! {
        #(#forwarded)*
        #vis fn #fn_name() #ret {
            #outer_body
        }
    })
}

/// Marks a `fn() -> TestResult<T>` as a fixture: a reusable piece of test setup
/// whose failures surface as `ErrorKind::Setup`, never as assertion misses.
///
/// A fixture is consumed by [`macro@test_with_fixtures`]: a test parameter
/// `name: T` is filled by the same-named fixture `fn name()`.
///
/// By default a fixture is per-test (`#[fixture]` or `#[fixture(scope =
/// "test")]`): the body runs afresh for every test, and the value is moved
/// straight out, so `T` need not be `Clone`. With `#[fixture(scope = "module")]`
/// the body runs once and every test gets a clone of the cached value, so `T`
/// must be `Clone + Send + Sync + 'static`.
///
/// ```ignore
/// use test_better::prelude::*;
///
/// #[fixture]
/// fn answer() -> TestResult<i32> {
///     Ok(42)
/// }
///
/// #[test_with_fixtures]
/// fn uses_the_answer(answer: i32) -> TestResult {
///     check!(answer).satisfies(eq(42))
/// }
/// ```
#[proc_macro_attribute]
pub fn fixture(attr: TokenStream, item: TokenStream) -> TokenStream {
    let args = match syn::parse::<FixtureArgs>(attr) {
        Ok(args) => args,
        Err(error) => return error.to_compile_error().into(),
    };
    let func = match syn::parse::<ItemFn>(item) {
        Ok(func) => func,
        Err(error) => return error.to_compile_error().into(),
    };
    match fixture_impl(args, func) {
        Ok(tokens) => tokens.into(),
        Err(error) => error.to_compile_error().into(),
    }
}

/// Rewrites a parameterized test into a zero-argument `#[test]` that resolves
/// each parameter from its fixture before calling the original body.
fn test_with_fixtures_impl(mut func: ItemFn) -> syn::Result<TokenStream2> {
    let mut params = Vec::with_capacity(func.sig.inputs.len());
    for input in &func.sig.inputs {
        match input {
            FnArg::Receiver(receiver) => {
                return Err(syn::Error::new_spanned(
                    receiver,
                    "a `#[test_with_fixtures]` function cannot take `self`",
                ));
            }
            FnArg::Typed(pat_type) => match &*pat_type.pat {
                Pat::Ident(pat_ident) => params.push(pat_ident.ident.clone()),
                other => {
                    return Err(syn::Error::new_spanned(
                        other,
                        "each `#[test_with_fixtures]` parameter must be a plain \
                         `name: Type`, where `name` is the fixture function",
                    ));
                }
            },
        }
    }

    // Everything on the function (`#[ignore]`, doc comments, ...) is forwarded
    // onto the generated `#[test]`.
    let forwarded: Vec<syn::Attribute> = std::mem::take(&mut func.attrs);
    let fn_name = func.sig.ident.clone();
    let vis = func.vis.clone();
    let ret = func.sig.output.clone();

    // The original function, renamed, becomes a nested helper the generated
    // test calls once every fixture has been resolved.
    let mut inner = func;
    inner.sig.ident = format_ident!("__tb_inner");
    inner.vis = syn::Visibility::Inherited;

    Ok(quote! {
        #(#forwarded)*
        #[test]
        #vis fn #fn_name() #ret {
            #inner
            #( let #params = #params()?; )*
            __tb_inner(#(#params),*)
        }
    })
}

/// Turns a test whose parameters are fixtures into a runnable `#[test]`.
///
/// Each parameter `name: T` is resolved by calling the same-named [`macro@fixture`]
/// function `fn name() -> TestResult<T>` and `?`-propagating its result, so a
/// fixture failure aborts the test as an `ErrorKind::Setup` error before the
/// body runs. The parameters are resolved left to right.
///
/// Because the resolved fixtures are `?`-propagated, the test must return a
/// type that `?` accepts, the usual `-> TestResult` shape.
///
/// ```ignore
/// use test_better::prelude::*;
///
/// #[fixture]
/// fn name() -> TestResult<String> {
///     Ok(String::from("alice"))
/// }
///
/// #[test_with_fixtures]
/// fn greets_by_name(name: String) -> TestResult {
///     check!(name.as_str()).satisfies(eq("alice"))
/// }
/// ```
#[proc_macro_attribute]
pub fn test_with_fixtures(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let func = match syn::parse::<ItemFn>(item) {
        Ok(func) => func,
        Err(error) => return error.to_compile_error().into(),
    };
    match test_with_fixtures_impl(func) {
        Ok(tokens) => tokens.into(),
        Err(error) => error.to_compile_error().into(),
    }
}