adze-macro 0.8.0

Procedural macros for Rust Sitter
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
#![allow(clippy::needless_range_loop)]

//! Property-based tests for enum variant handling in adze-macro.
//!
//! Uses proptest to generate randomized enum structures and verify that
//! syn correctly parses and preserves variant kinds, field counts,
//! field names, annotations, and other structural properties.

use proptest::prelude::*;
use quote::ToTokens;
use syn::{Attribute, Fields, ItemEnum};

// ── Helpers ─────────────────────────────────────────────────────────────────

fn is_adze_attr(attr: &Attribute, name: &str) -> bool {
    let segs: Vec<_> = attr.path().segments.iter().collect();
    segs.len() == 2 && segs[0].ident == "adze" && segs[1].ident == name
}

fn adze_attr_names(attrs: &[Attribute]) -> Vec<String> {
    attrs
        .iter()
        .filter_map(|a| {
            let segs: Vec<_> = a.path().segments.iter().collect();
            if segs.len() == 2 && segs[0].ident == "adze" {
                Some(segs[1].ident.to_string())
            } else {
                None
            }
        })
        .collect()
}

fn variant_is_unit(v: &syn::Variant) -> bool {
    matches!(v.fields, Fields::Unit)
}

fn variant_is_unnamed(v: &syn::Variant) -> bool {
    matches!(v.fields, Fields::Unnamed(_))
}

fn variant_is_named(v: &syn::Variant) -> bool {
    matches!(v.fields, Fields::Named(_))
}

fn named_field_names(v: &syn::Variant) -> Vec<String> {
    match &v.fields {
        Fields::Named(n) => n
            .named
            .iter()
            .map(|f| f.ident.as_ref().unwrap().to_string())
            .collect(),
        _ => vec![],
    }
}

fn field_type_strings(v: &syn::Variant) -> Vec<String> {
    match &v.fields {
        Fields::Unnamed(u) => u
            .unnamed
            .iter()
            .map(|f| f.ty.to_token_stream().to_string())
            .collect(),
        Fields::Named(n) => n
            .named
            .iter()
            .map(|f| f.ty.to_token_stream().to_string())
            .collect(),
        Fields::Unit => vec![],
    }
}

// ── 1. Named field variants detected for random field counts ────────────────

proptest! {
    #[test]
    fn named_field_variant_detected(field_count in 1usize..=5) {
        // Build a named-field variant with field_count fields
        let fields: Vec<proc_macro2::TokenStream> = (0..field_count)
            .map(|i| {
                let name = syn::Ident::new(&format!("f{i}"), proc_macro2::Span::call_site());
                quote::quote! { #name: String }
            })
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E {
                V { #(#fields),* }
            }
        }).unwrap();
        prop_assert!(variant_is_named(&e.variants[0]));
    }
}

// ── 2. Unnamed (tuple) variants detected for random field counts ────────────

proptest! {
    #[test]
    fn unnamed_variant_detected(field_count in 1usize..=6) {
        let fields: Vec<proc_macro2::TokenStream> = (0..field_count)
            .map(|_| quote::quote! { String })
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E {
                V(#(#fields),*)
            }
        }).unwrap();
        prop_assert!(variant_is_unnamed(&e.variants[0]));
    }
}

// ── 3. Unit variants detected ───────────────────────────────────────────────

proptest! {
    #[test]
    fn unit_variant_detected(count in 1usize..=8) {
        let names: Vec<syn::Ident> = (0..count)
            .map(|i| syn::Ident::new(&format!("V{i}"), proc_macro2::Span::call_site()))
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E {
                #(#names),*
            }
        }).unwrap();
        for v in &e.variants {
            prop_assert!(variant_is_unit(v));
        }
    }
}

// ── 4. Mixed variant types preserved ────────────────────────────────────────

proptest! {
    #[test]
    fn mixed_variant_types_preserved(
        n_unit in 1usize..=3,
        n_tuple in 1usize..=3,
        n_named in 1usize..=3,
    ) {
        let mut variant_tokens: Vec<proc_macro2::TokenStream> = Vec::new();
        for i in 0..n_unit {
            let name = syn::Ident::new(&format!("U{i}"), proc_macro2::Span::call_site());
            variant_tokens.push(quote::quote! { #name });
        }
        for i in 0..n_tuple {
            let name = syn::Ident::new(&format!("T{i}"), proc_macro2::Span::call_site());
            variant_tokens.push(quote::quote! { #name(String) });
        }
        for i in 0..n_named {
            let name = syn::Ident::new(&format!("N{i}"), proc_macro2::Span::call_site());
            variant_tokens.push(quote::quote! { #name { x: i32 } });
        }
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E { #(#variant_tokens),* }
        }).unwrap();
        let total = n_unit + n_tuple + n_named;
        prop_assert_eq!(e.variants.len(), total);
        for i in 0..n_unit {
            prop_assert!(variant_is_unit(&e.variants[i]));
        }
        for i in 0..n_tuple {
            prop_assert!(variant_is_unnamed(&e.variants[n_unit + i]));
        }
        for i in 0..n_named {
            prop_assert!(variant_is_named(&e.variants[n_unit + n_tuple + i]));
        }
    }
}

// ── 5. Variant count preservation ───────────────────────────────────────────

proptest! {
    #[test]
    fn variant_count_preserved(count in 1usize..=10) {
        let names: Vec<syn::Ident> = (0..count)
            .map(|i| syn::Ident::new(&format!("V{i}"), proc_macro2::Span::call_site()))
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E {
                #(#names(String)),*
            }
        }).unwrap();
        prop_assert_eq!(e.variants.len(), count);
    }
}

// ── 6. Field name extraction from named variants ────────────────────────────

proptest! {
    #[test]
    fn field_name_extraction(field_count in 1usize..=6) {
        let expected_names: Vec<String> = (0..field_count)
            .map(|i| format!("field_{i}"))
            .collect();
        let fields: Vec<proc_macro2::TokenStream> = expected_names.iter()
            .map(|name| {
                let ident = syn::Ident::new(name, proc_macro2::Span::call_site());
                quote::quote! { #ident: i32 }
            })
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E {
                V { #(#fields),* }
            }
        }).unwrap();
        let actual_names = named_field_names(&e.variants[0]);
        prop_assert_eq!(actual_names, expected_names);
    }
}

// ── 7. Variant with single leaf annotation ──────────────────────────────────

proptest! {
    #[test]
    fn variant_with_leaf_annotation(idx in 0usize..=4) {
        let texts = ["if", "else", "while", "for", "return"];
        let text = texts[idx];
        let name = syn::Ident::new(&format!("K{idx}"), proc_macro2::Span::call_site());
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum K {
                #[adze::leaf(text = #text)]
                #name
            }
        }).unwrap();
        prop_assert!(e.variants[0].attrs.iter().any(|a| is_adze_attr(a, "leaf")));
        prop_assert!(variant_is_unit(&e.variants[0]));
    }
}

// ── 8. Multiple annotations per variant ─────────────────────────────────────

proptest! {
    #[test]
    fn multiple_annotations_per_variant(prec_val in 1i32..=10) {
        let lit = proc_macro2::Literal::i32_unsuffixed(prec_val);
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum Expr {
                #[adze::prec_left(#lit)]
                #[adze::leaf(text = "+")]
                Add
            }
        }).unwrap();
        let names = adze_attr_names(&e.variants[0].attrs);
        prop_assert!(names.contains(&"prec_left".to_string()));
        prop_assert!(names.contains(&"leaf".to_string()));
        prop_assert_eq!(names.len(), 2);
    }
}

// ── 9. Unnamed field count matches ──────────────────────────────────────────

proptest! {
    #[test]
    fn unnamed_field_count_matches(count in 1usize..=5) {
        let fields: Vec<proc_macro2::TokenStream> = (0..count)
            .map(|_| quote::quote! { i32 })
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E {
                V(#(#fields),*)
            }
        }).unwrap();
        if let Fields::Unnamed(u) = &e.variants[0].fields {
            prop_assert_eq!(u.unnamed.len(), count);
        } else {
            prop_assert!(false, "Expected unnamed fields");
        }
    }
}

// ── 10. Named field count matches ───────────────────────────────────────────

proptest! {
    #[test]
    fn named_field_count_matches(count in 1usize..=5) {
        let fields: Vec<proc_macro2::TokenStream> = (0..count)
            .map(|i| {
                let name = syn::Ident::new(&format!("f{i}"), proc_macro2::Span::call_site());
                quote::quote! { #name: u32 }
            })
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E {
                V { #(#fields),* }
            }
        }).unwrap();
        if let Fields::Named(n) = &e.variants[0].fields {
            prop_assert_eq!(n.named.len(), count);
        } else {
            prop_assert!(false, "Expected named fields");
        }
    }
}

// ── 11. Variant ident names preserved ───────────────────────────────────────

proptest! {
    #[test]
    fn variant_ident_names_preserved(count in 1usize..=8) {
        let expected: Vec<String> = (0..count).map(|i| format!("Var{i}")).collect();
        let idents: Vec<syn::Ident> = expected.iter()
            .map(|n| syn::Ident::new(n, proc_macro2::Span::call_site()))
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E { #(#idents),* }
        }).unwrap();
        let actual: Vec<String> = e.variants.iter().map(|v| v.ident.to_string()).collect();
        prop_assert_eq!(actual, expected);
    }
}

// ── 12. Precedence values parsed correctly ──────────────────────────────────

proptest! {
    #[test]
    fn precedence_values_parsed(prec in 0i32..=20) {
        let lit = proc_macro2::Literal::i32_unsuffixed(prec);
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E {
                #[adze::prec_left(#lit)]
                V(i32, i32)
            }
        }).unwrap();
        let attr = e.variants[0].attrs.iter()
            .find(|a| is_adze_attr(a, "prec_left"))
            .unwrap();
        let expr: syn::Expr = attr.parse_args().unwrap();
        if let syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(i), .. }) = expr {
            prop_assert_eq!(i.base10_parse::<i32>().unwrap(), prec);
        } else {
            prop_assert!(false, "Expected int literal");
        }
    }
}

// ── 13. Leaf text attribute round-trips ─────────────────────────────────────

proptest! {
    #[test]
    fn leaf_text_roundtrips(idx in 0usize..=5) {
        let keywords = ["+", "-", "*", "/", "==", "!="];
        let kw = keywords[idx];
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum Op {
                #[adze::leaf(text = #kw)]
                V
            }
        }).unwrap();
        let attr = e.variants[0].attrs.iter()
            .find(|a| is_adze_attr(a, "leaf"))
            .unwrap();
        let params: syn::punctuated::Punctuated<adze_common::NameValueExpr, syn::Token![,]> =
            attr.parse_args_with(
                syn::punctuated::Punctuated::parse_terminated,
            ).unwrap();
        prop_assert_eq!(params[0].path.to_string(), "text");
        if let syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(s), .. }) = &params[0].expr {
            prop_assert_eq!(s.value(), kw);
        } else {
            prop_assert!(false, "Expected string literal");
        }
    }
}

// ── 14. Unit variants have zero fields ──────────────────────────────────────

proptest! {
    #[test]
    fn unit_variants_have_zero_fields(count in 1usize..=6) {
        let names: Vec<syn::Ident> = (0..count)
            .map(|i| syn::Ident::new(&format!("U{i}"), proc_macro2::Span::call_site()))
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E { #(#names),* }
        }).unwrap();
        for v in &e.variants {
            prop_assert_eq!(field_type_strings(v).len(), 0);
        }
    }
}

// ── 15. Field types preserved for unnamed variants ──────────────────────────

proptest! {
    #[test]
    fn field_types_preserved_unnamed(n_string in 0usize..=3, n_i32 in 0usize..=3) {
        prop_assume!(n_string + n_i32 >= 1);
        let mut fields: Vec<proc_macro2::TokenStream> = Vec::new();
        for _ in 0..n_string {
            fields.push(quote::quote! { String });
        }
        for _ in 0..n_i32 {
            fields.push(quote::quote! { i32 });
        }
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E { V(#(#fields),*) }
        }).unwrap();
        let types = field_type_strings(&e.variants[0]);
        prop_assert_eq!(types.len(), n_string + n_i32);
        for i in 0..n_string {
            prop_assert_eq!(&types[i], "String");
        }
        for i in 0..n_i32 {
            prop_assert_eq!(&types[n_string + i], "i32");
        }
    }
}

// ── 16. prec_right annotation detected ──────────────────────────────────────

proptest! {
    #[test]
    fn prec_right_annotation_detected(prec in 1i32..=15) {
        let lit = proc_macro2::Literal::i32_unsuffixed(prec);
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E {
                #[adze::prec_right(#lit)]
                V(i32, i32)
            }
        }).unwrap();
        let names = adze_attr_names(&e.variants[0].attrs);
        prop_assert!(names.contains(&"prec_right".to_string()));
    }
}

// ── 17. prec annotation (no associativity) detected ─────────────────────────

proptest! {
    #[test]
    fn prec_annotation_detected(prec in 1i32..=15) {
        let lit = proc_macro2::Literal::i32_unsuffixed(prec);
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E {
                #[adze::prec(#lit)]
                V(i32)
            }
        }).unwrap();
        let names = adze_attr_names(&e.variants[0].attrs);
        prop_assert!(names.contains(&"prec".to_string()));
    }
}

// ── 18. Leaf with pattern attribute round-trips ─────────────────────────────

proptest! {
    #[test]
    fn leaf_pattern_roundtrips(idx in 0usize..=3) {
        let patterns = [r"\d+", r"[a-z]+", r"\w+", r"\s+"];
        let pat = patterns[idx];
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E {
                V(#[adze::leaf(pattern = #pat)] String)
            }
        }).unwrap();
        if let Fields::Unnamed(ref u) = e.variants[0].fields {
            let attr = u.unnamed[0].attrs.iter()
                .find(|a| is_adze_attr(a, "leaf"))
                .unwrap();
            let params: syn::punctuated::Punctuated<adze_common::NameValueExpr, syn::Token![,]> =
                attr.parse_args_with(
                    syn::punctuated::Punctuated::parse_terminated,
                ).unwrap();
            prop_assert_eq!(params[0].path.to_string(), "pattern");
        } else {
            prop_assert!(false, "Expected unnamed");
        }
    }
}

// ── 19. Annotations not on variant don't appear ─────────────────────────────

proptest! {
    #[test]
    fn unannotated_variants_have_no_adze_attrs(count in 1usize..=5) {
        let names: Vec<syn::Ident> = (0..count)
            .map(|i| syn::Ident::new(&format!("V{i}"), proc_macro2::Span::call_site()))
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E { #(#names(i32)),* }
        }).unwrap();
        for v in &e.variants {
            prop_assert!(adze_attr_names(&v.attrs).is_empty());
        }
    }
}

// ── 20. Multiple leaf-annotated unit variants ───────────────────────────────

proptest! {
    #[test]
    fn multiple_leaf_unit_variants(count in 2usize..=6) {
        let variant_tokens: Vec<proc_macro2::TokenStream> = (0..count)
            .map(|i| {
                let name = syn::Ident::new(&format!("K{i}"), proc_macro2::Span::call_site());
                let text = format!("kw{i}");
                quote::quote! {
                    #[adze::leaf(text = #text)]
                    #name
                }
            })
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E { #(#variant_tokens),* }
        }).unwrap();
        prop_assert_eq!(e.variants.len(), count);
        for v in &e.variants {
            prop_assert!(variant_is_unit(v));
            prop_assert!(v.attrs.iter().any(|a| is_adze_attr(a, "leaf")));
        }
    }
}

// ── 21. Named variant field types preserved ─────────────────────────────────

proptest! {
    #[test]
    fn named_variant_field_types_preserved(n_fields in 1usize..=4) {
        let fields: Vec<proc_macro2::TokenStream> = (0..n_fields)
            .map(|i| {
                let name = syn::Ident::new(&format!("f{i}"), proc_macro2::Span::call_site());
                if i % 2 == 0 {
                    quote::quote! { #name: String }
                } else {
                    quote::quote! { #name: i32 }
                }
            })
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E { V { #(#fields),* } }
        }).unwrap();
        let types = field_type_strings(&e.variants[0]);
        prop_assert_eq!(types.len(), n_fields);
        for i in 0..n_fields {
            if i % 2 == 0 {
                prop_assert_eq!(&types[i], "String");
            } else {
                prop_assert_eq!(&types[i], "i32");
            }
        }
    }
}

// ── 22. Box<T> field types preserved in unnamed variants ────────────────────

proptest! {
    #[test]
    fn box_field_types_preserved(count in 1usize..=4) {
        let fields: Vec<proc_macro2::TokenStream> = (0..count)
            .map(|_| quote::quote! { Box<Expr> })
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum Expr { V(#(#fields),*) }
        }).unwrap();
        let types = field_type_strings(&e.variants[0]);
        for t in &types {
            prop_assert_eq!(t, "Box < Expr >");
        }
    }
}

// ── 23. Vec<T> field types preserved in named variants ──────────────────────

proptest! {
    #[test]
    fn vec_field_types_preserved(count in 1usize..=3) {
        let fields: Vec<proc_macro2::TokenStream> = (0..count)
            .map(|i| {
                let name = syn::Ident::new(&format!("items{i}"), proc_macro2::Span::call_site());
                quote::quote! { #name: Vec<i32> }
            })
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E { V { #(#fields),* } }
        }).unwrap();
        let types = field_type_strings(&e.variants[0]);
        for t in &types {
            prop_assert_eq!(t, "Vec < i32 >");
        }
    }
}

// ── 24. Variant with prec_left + named fields ───────────────────────────────

proptest! {
    #[test]
    fn prec_left_with_named_fields(prec in 1i32..=10, n_fields in 1usize..=3) {
        let lit = proc_macro2::Literal::i32_unsuffixed(prec);
        let fields: Vec<proc_macro2::TokenStream> = (0..n_fields)
            .map(|i| {
                let name = syn::Ident::new(&format!("f{i}"), proc_macro2::Span::call_site());
                quote::quote! { #name: i32 }
            })
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E {
                #[adze::prec_left(#lit)]
                V { #(#fields),* }
            }
        }).unwrap();
        prop_assert!(variant_is_named(&e.variants[0]));
        let names = adze_attr_names(&e.variants[0].attrs);
        prop_assert!(names.contains(&"prec_left".to_string()));
        if let Fields::Named(n) = &e.variants[0].fields {
            prop_assert_eq!(n.named.len(), n_fields);
        }
    }
}

// ── 25. Enum with language attribute preserved ──────────────────────────────

proptest! {
    #[test]
    fn language_attribute_preserved(count in 1usize..=5) {
        let names: Vec<syn::Ident> = (0..count)
            .map(|i| syn::Ident::new(&format!("V{i}"), proc_macro2::Span::call_site()))
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            #[adze::language]
            pub enum E { #(#names(i32)),* }
        }).unwrap();
        prop_assert!(e.attrs.iter().any(|a| is_adze_attr(a, "language")));
        prop_assert_eq!(e.variants.len(), count);
    }
}

// ── 26. Mixed annotated and unannotated variants ────────────────────────────

proptest! {
    #[test]
    fn mixed_annotated_and_unannotated(n_annotated in 1usize..=3, n_plain in 1usize..=3) {
        let mut variant_tokens: Vec<proc_macro2::TokenStream> = Vec::new();
        for i in 0..n_annotated {
            let name = syn::Ident::new(&format!("A{i}"), proc_macro2::Span::call_site());
            let text = format!("a{i}");
            variant_tokens.push(quote::quote! {
                #[adze::leaf(text = #text)]
                #name
            });
        }
        for i in 0..n_plain {
            let name = syn::Ident::new(&format!("P{i}"), proc_macro2::Span::call_site());
            variant_tokens.push(quote::quote! { #name(i32) });
        }
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E { #(#variant_tokens),* }
        }).unwrap();
        prop_assert_eq!(e.variants.len(), n_annotated + n_plain);
        for i in 0..n_annotated {
            prop_assert!(!adze_attr_names(&e.variants[i].attrs).is_empty());
        }
        for i in 0..n_plain {
            prop_assert!(adze_attr_names(&e.variants[n_annotated + i].attrs).is_empty());
        }
    }
}

// ── 27. Option<T> field types preserved ─────────────────────────────────────

proptest! {
    #[test]
    fn option_field_types_preserved(count in 1usize..=4) {
        let fields: Vec<proc_macro2::TokenStream> = (0..count)
            .map(|_| quote::quote! { Option<String> })
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E { V(#(#fields),*) }
        }).unwrap();
        let types = field_type_strings(&e.variants[0]);
        prop_assert_eq!(types.len(), count);
        for t in &types {
            prop_assert_eq!(t, "Option < String >");
        }
    }
}

// ── 28. Enum name preserved ─────────────────────────────────────────────────

proptest! {
    #[test]
    fn enum_name_preserved(idx in 0usize..=4) {
        let names = ["Expr", "Token", "Stmt", "Type", "Decl"];
        let name = syn::Ident::new(names[idx], proc_macro2::Span::call_site());
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum #name { A, B }
        }).unwrap();
        prop_assert_eq!(e.ident.to_string(), names[idx]);
    }
}

// ── 29. Triple-annotated variant ────────────────────────────────────────────

proptest! {
    #[test]
    fn triple_annotated_variant(prec in 1i32..=10) {
        let lit = proc_macro2::Literal::i32_unsuffixed(prec);
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E {
                #[adze::language]
                #[adze::prec_left(#lit)]
                #[adze::leaf(text = "+")]
                V
            }
        }).unwrap();
        let names = adze_attr_names(&e.variants[0].attrs);
        prop_assert_eq!(names.len(), 3);
        prop_assert!(names.contains(&"language".to_string()));
        prop_assert!(names.contains(&"prec_left".to_string()));
        prop_assert!(names.contains(&"leaf".to_string()));
    }
}

// ── 30. Interleaved unit and named variants ─────────────────────────────────

proptest! {
    #[test]
    fn interleaved_unit_and_named(pairs in 1usize..=4) {
        let mut variant_tokens: Vec<proc_macro2::TokenStream> = Vec::new();
        for i in 0..pairs {
            let uname = syn::Ident::new(&format!("U{i}"), proc_macro2::Span::call_site());
            let nname = syn::Ident::new(&format!("N{i}"), proc_macro2::Span::call_site());
            let fname = syn::Ident::new(&format!("f{i}"), proc_macro2::Span::call_site());
            variant_tokens.push(quote::quote! { #uname });
            variant_tokens.push(quote::quote! { #nname { #fname: i32 } });
        }
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E { #(#variant_tokens),* }
        }).unwrap();
        prop_assert_eq!(e.variants.len(), pairs * 2);
        for i in 0..pairs {
            prop_assert!(variant_is_unit(&e.variants[i * 2]));
            prop_assert!(variant_is_named(&e.variants[i * 2 + 1]));
        }
    }
}

// ── 31. Leaf annotation on tuple field is on the field, not variant ──────────

proptest! {
    #[test]
    fn leaf_on_field_not_variant(field_count in 1usize..=4) {
        let fields: Vec<proc_macro2::TokenStream> = (0..field_count)
            .map(|_| quote::quote! { #[adze::leaf(pattern = r"\d+")] String })
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E { V(#(#fields),*) }
        }).unwrap();
        // Variant itself has no adze attrs
        prop_assert!(adze_attr_names(&e.variants[0].attrs).is_empty());
        // But each field does
        if let Fields::Unnamed(ref u) = e.variants[0].fields {
            for f in &u.unnamed {
                prop_assert!(f.attrs.iter().any(|a| is_adze_attr(a, "leaf")));
            }
        }
    }
}

// ── 32. Variant discriminant not used (no = expr) ───────────────────────────

proptest! {
    #[test]
    fn variants_have_no_discriminant(count in 1usize..=6) {
        let names: Vec<syn::Ident> = (0..count)
            .map(|i| syn::Ident::new(&format!("V{i}"), proc_macro2::Span::call_site()))
            .collect();
        let e: ItemEnum = syn::parse2(quote::quote! {
            pub enum E { #(#names),* }
        }).unwrap();
        for v in &e.variants {
            prop_assert!(v.discriminant.is_none());
        }
    }
}