ruchy 4.1.1

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
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
//! Grammar coverage matrix for REPL testing
//!
//! Tracks coverage of all grammar productions to ensure complete testing
use crate::frontend::ast::Expr;
use anyhow::Result;
use std::collections::{HashMap, HashSet};
use std::time::Duration;
/// Statistics for a single grammar production
#[derive(Default, Debug)]
pub struct ProductionStats {
    pub hit_count: usize,
    pub success_count: usize,
    pub avg_latency_ns: u64,
    pub error_patterns: Vec<String>,
}
/// Grammar coverage tracking matrix
#[derive(Default)]
pub struct GrammarCoverageMatrix {
    pub productions: HashMap<&'static str, ProductionStats>,
    pub ast_variants: HashSet<String>,
    pub uncovered: Vec<&'static str>,
}
impl GrammarCoverageMatrix {
    /// Create a new coverage matrix
    pub fn new() -> Self {
        Self::default()
    }
    /// Record a parse attempt
    pub fn record(
        &mut self,
        production: &'static str,
        _input: &str,
        result: Result<Expr>,
        elapsed: Duration,
    ) {
        let stats = self.productions.entry(production).or_default();
        stats.hit_count += 1;
        let elapsed_ns =
            u64::try_from(elapsed.as_nanos().min(u128::from(u64::MAX))).unwrap_or(u64::MAX);
        stats.avg_latency_ns = if stats.hit_count == 1 {
            elapsed_ns
        } else {
            (stats.avg_latency_ns * (stats.hit_count as u64 - 1) + elapsed_ns)
                / stats.hit_count as u64
        };
        match result {
            Ok(ast) => {
                stats.success_count += 1;
                self.record_ast_variants(&ast);
            }
            Err(e) => {
                let error_msg = e.to_string();
                if !stats.error_patterns.contains(&error_msg) {
                    stats.error_patterns.push(error_msg);
                }
            }
        }
    }
    /// Record AST variant usage
    fn record_ast_variants(&mut self, expr: &Expr) {
        use crate::frontend::ast::ExprKind;
        let variant_name = match &expr.kind {
            ExprKind::Literal(_) => "Literal",
            ExprKind::Identifier(_) => "Identifier",
            ExprKind::Binary { .. } => "Binary",
            ExprKind::Unary { .. } => "Unary",
            ExprKind::Call { .. } => "Call",
            ExprKind::MethodCall { .. } => "MethodCall",
            ExprKind::If { .. } => "If",
            ExprKind::Match { .. } => "Match",
            ExprKind::Block(_) => "Block",
            ExprKind::Let { .. } => "Let",
            ExprKind::Function { .. } => "Function",
            ExprKind::Lambda { .. } => "Lambda",
            ExprKind::Throw { .. } => "Throw",
            ExprKind::Ok { .. } => "Ok",
            ExprKind::Err { .. } => "Err",
            ExprKind::While { .. } => "While",
            ExprKind::For { .. } => "For",
            ExprKind::Struct { .. } => "Struct",
            ExprKind::TupleStruct { .. } => "TupleStruct",
            ExprKind::StructLiteral { .. } => "StructLiteral",
            ExprKind::ObjectLiteral { .. } => "ObjectLiteral",
            ExprKind::FieldAccess { .. } => "FieldAccess",
            ExprKind::Trait { .. } => "Trait",
            ExprKind::Impl { .. } => "Impl",
            ExprKind::Extension { .. } => "Extension",
            ExprKind::Await { .. } => "Await",
            ExprKind::List(_) => "List",
            ExprKind::ListComprehension { .. } => "ListComprehension",
            ExprKind::StringInterpolation { .. } => "StringInterpolation",
            ExprKind::QualifiedName { .. } => "QualifiedName",
            ExprKind::Send { .. } => "Send",
            ExprKind::Ask { .. } => "Ask",
            ExprKind::Command { .. } => "Command",
            ExprKind::Macro { .. } => "Macro",
            ExprKind::Actor { .. } => "Actor",
            ExprKind::DataFrame { .. } => "DataFrame",
            ExprKind::DataFrameOperation { .. } => "DataFrameOperation",
            ExprKind::Pipeline { .. } => "Pipeline",
            ExprKind::Import { .. } => "Import",
            ExprKind::Export { .. } => "Export",
            ExprKind::Module { .. } => "Module",
            ExprKind::Range { .. } => "Range",
            ExprKind::Break { .. } => "Break",
            ExprKind::Continue { .. } => "Continue",
            ExprKind::Assign { .. } => "Assign",
            ExprKind::CompoundAssign { .. } => "CompoundAssign",
            _ => "Other",
        };
        self.ast_variants.insert(variant_name.to_string());
    }
    /// Check if coverage is complete
    pub fn is_complete(&self, required_productions: usize) -> bool {
        self.productions.len() >= required_productions && self.uncovered.is_empty()
    }
    /// Assert that coverage is complete
    ///
    /// # Panics
    ///
    /// Panics if there are uncovered productions or if the number of covered
    /// productions is less than the required amount.
    pub fn assert_complete(&self, required_productions: usize) {
        assert!(
            self.uncovered.is_empty(),
            "Uncovered productions: {:?}",
            self.uncovered
        );
        assert!(
            self.productions.len() >= required_productions,
            "Only {} of {} productions covered",
            self.productions.len(),
            required_productions
        );
    }
    /// Get coverage percentage
    pub fn get_coverage_percentage(&self) -> f64 {
        if self.uncovered.is_empty() && self.productions.is_empty() {
            return 0.0;
        }
        // Count uncovered productions that haven't been covered
        let uncovered_count = self
            .uncovered
            .iter()
            .filter(|prod| !self.productions.contains_key(**prod))
            .count();
        let total = self.productions.len() + uncovered_count;
        if total == 0 {
            return 0.0;
        }
        #[allow(clippy::cast_precision_loss)]
        let percentage = (self.productions.len() as f64 / total as f64) * 100.0;
        percentage
    }
    /// Generate a coverage report (alias for `report()`)
    pub fn generate_report(&self) -> String {
        self.report()
    }
    /// Generate a coverage report
    pub fn report(&self) -> String {
        use std::fmt::Write;
        let mut report = String::new();
        report.push_str("Grammar Coverage Report\n");
        report.push_str("=======================\n\n");
        let coverage_percentage = self.get_coverage_percentage();
        let _ = writeln!(&mut report, "Coverage: {coverage_percentage:.1}%");
        let _ = writeln!(
            &mut report,
            "Productions covered: {}",
            self.productions.len()
        );
        let _ = writeln!(
            &mut report,
            "AST variants seen: {}",
            self.ast_variants.len()
        );
        let total_hits: usize = self.productions.values().map(|s| s.hit_count).sum();
        let total_success: usize = self.productions.values().map(|s| s.success_count).sum();
        let _ = writeln!(&mut report, "Total attempts: {total_hits}");
        let success_rate = if total_hits > 0 {
            #[allow(clippy::cast_precision_loss)]
            let rate = (total_success as f64 / total_hits as f64) * 100.0;
            rate
        } else {
            0.0
        };
        let _ = writeln!(&mut report, "Success rate: {success_rate:.2}%");
        // Find slowest productions
        let mut slowest: Vec<_> = self.productions.iter().collect();
        slowest.sort_by_key(|(_, stats)| stats.avg_latency_ns);
        slowest.reverse();
        if !slowest.is_empty() {
            report.push_str("\nSlowest productions:\n");
            for (name, stats) in slowest.iter().take(5) {
                #[allow(clippy::cast_precision_loss)]
                let ms = stats.avg_latency_ns as f64 / 1_000_000.0;
                let _ = writeln!(&mut report, "  {name}: {ms:.2}ms");
            }
        }
        if !self.uncovered.is_empty() {
            report.push_str("\nUncovered productions:\n");
            for prod in &self.uncovered {
                let _ = writeln!(&mut report, "  - {prod}");
            }
        }
        report
    }
}
/// All grammar productions that need coverage
pub const GRAMMAR_PRODUCTIONS: &[(&str, &str)] = &[
    // Core literals (5)
    ("literal_int", "42"),
    ("literal_float", "3.15"),
    ("literal_string", r#""hello""#),
    ("literal_bool", "true"),
    ("literal_unit", "()"),
    // Binary operators (12)
    ("op_assign", "x = 5"),
    ("op_logical_or", "a || b"),
    ("op_logical_and", "a && b"),
    ("op_equality", "x == y"),
    ("op_comparison", "x < y"),
    ("op_bitwise_or", "a | b"),
    ("op_bitwise_xor", "a ^ b"),
    ("op_bitwise_and", "a & b"),
    ("op_shift", "x << 2"),
    ("op_range", "0..10"),
    ("op_add", "x + y"),
    ("op_mul", "x * y"),
    // Unary operators (3)
    ("op_neg", "-x"),
    ("op_not", "!x"),
    ("op_ref", "&value"),
    // Control flow (5)
    ("if_expr", "if x > 0 { 1 } else { -1 }"),
    ("match_expr", "match x { Some(y) => y, None => 0 }"),
    ("for_loop", "for x in 0..10 { print(x) }"),
    ("while_loop", "while x > 0 { x = x - 1 }"),
    ("loop_expr", "loop { break 42 }"),
    // Function calls (5) - CRITICAL: These were missing!
    ("call_simple", "println(42)"),
    ("call_args", "println(\"Hello\", \"World\")"),
    ("call_expr", "add(2 + 3, 4 * 5)"),
    ("call_nested", "println(add(1, 2))"),
    ("call_builtin", "print(\"test\")"),
    // Functions (4)
    ("fun_decl", "fun add(a: Int, b: Int) -> Int { a + b }"),
    ("fun_generic", "fun id<T>(x: T) -> T { x }"),
    ("lambda", "|x| x * 2"),
    ("lambda_typed", "|x: Int| -> Int { x * 2 }"),
    // Pattern matching (6)
    ("pattern_bind", "let x = 5"),
    ("pattern_tuple", "let (x, y) = (1, 2)"),
    ("pattern_struct", "let Point { x, y } = p"),
    ("pattern_enum", "let Some(x) = opt"),
    ("pattern_slice", "let [head, ..tail] = list"),
    ("pattern_guard", "match x { n if n > 0 => n }"),
    // Type system (5)
    ("type_simple", "let x: Int = 5"),
    ("type_generic", "let v: Vec<Int> = vec![1,2,3]"),
    ("type_function", "let f: Fn(Int) -> Int = |x| x"),
    ("type_tuple", "let t: (Int, String) = (1, \"hi\")"),
    ("type_option", "let opt: Option<Int> = Some(5)"),
    // Structs/Traits/Impls (3)
    ("struct_decl", "struct Point { x: Float, y: Float }"),
    ("trait_decl", "trait Show { fun show(self) -> String }"),
    (
        "impl_block",
        "impl Show for Point { fun show(self) -> String { \"...\" } }",
    ),
    // Actor system (4)
    ("actor_decl", "actor Counter { state count: Int = 0 }"),
    ("actor_handler", "on Increment { self.count += 1 }"),
    ("send_op", "counter <- Increment"),
    ("ask_op", "let n = counter <? GetCount"),
    // DataFrame operations (6)
    ("df_literal", "df![a = [1,2,3], b = [4,5,6]]"),
    ("df_filter", "df >> filter(col(\"age\") > 18)"),
    ("df_select", "df >> select([\"name\", \"age\"])"),
    ("df_groupby", "df >> groupby(\"dept\")"),
    ("df_agg", "df >> agg([mean(\"salary\"), count()])"),
    ("df_join", "df1 >> join(df2, on: \"id\")"),
    // Pipeline operators (3)
    ("pipe_simple", "data >> filter(|x| x > 0)"),
    ("pipe_method", "text >> trim() >> uppercase()"),
    ("pipe_nested", "x >> (|y| y >> double() >> square())"),
    // String interpolation (2)
    ("string_interp", r#"f"Hello {name}""#),
    ("string_complex", r#"f"Result: {compute(x):.2f}""#),
    // Import/Export (3)
    ("import_simple", "import std::fs"),
    (
        "import_multi",
        "import std::collections::{HashMap, HashSet}",
    ),
    ("export", "export { Point, distance }"),
    // Macros (2)
    ("macro_println", "println!(\"Hello\", \"World\")"),
    ("macro_vec", "vec![1, 2, 3]"),
];

#[cfg(test)]
mod tests {
    use super::*;
    use crate::frontend::ast::{ExprKind, Literal, Span};
    use crate::BinaryOp;
    use crate::UnaryOp;
    use std::time::Duration;

    // Sprint 14: Grammar coverage tests

    #[test]
    fn test_coverage_matrix_creation() {
        let matrix = GrammarCoverageMatrix::new();
        assert!(matrix.productions.is_empty());
        assert!(matrix.ast_variants.is_empty());
        assert!(matrix.uncovered.is_empty());
    }

    #[test]
    fn test_production_stats_default() {
        let stats = ProductionStats::default();
        assert_eq!(stats.hit_count, 0);
        assert_eq!(stats.success_count, 0);
        assert_eq!(stats.avg_latency_ns, 0);
        assert!(stats.error_patterns.is_empty());
    }

    #[test]
    fn test_record_success() {
        let mut matrix = GrammarCoverageMatrix::new();
        let expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Span { start: 0, end: 1 },
        );

        matrix.record("literal", "42", Ok(expr), Duration::from_millis(10));

        assert_eq!(matrix.productions.len(), 1);
        let stats = &matrix.productions["literal"];
        assert_eq!(stats.hit_count, 1);
        assert_eq!(stats.success_count, 1);
        assert!(stats.avg_latency_ns > 0);
        assert!(stats.error_patterns.is_empty());
    }

    #[test]
    fn test_record_failure() {
        let mut matrix = GrammarCoverageMatrix::new();
        let error = anyhow::anyhow!("Parse error");

        matrix.record("invalid", "bad input", Err(error), Duration::from_millis(5));

        assert_eq!(matrix.productions.len(), 1);
        let stats = &matrix.productions["invalid"];
        assert_eq!(stats.hit_count, 1);
        assert_eq!(stats.success_count, 0);
        assert!(stats.avg_latency_ns > 0);
        assert_eq!(stats.error_patterns.len(), 1);
        assert!(stats.error_patterns[0].contains("Parse error"));
    }

    #[test]
    fn test_record_multiple_attempts() {
        let mut matrix = GrammarCoverageMatrix::new();
        let expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Span { start: 0, end: 1 },
        );

        matrix.record("literal", "42", Ok(expr.clone()), Duration::from_millis(10));
        matrix.record("literal", "43", Ok(expr), Duration::from_millis(20));

        let stats = &matrix.productions["literal"];
        assert_eq!(stats.hit_count, 2);
        assert_eq!(stats.success_count, 2);
        // Average should be around 15ms in nanoseconds
        assert!(stats.avg_latency_ns > 10_000_000 && stats.avg_latency_ns < 20_000_000);
    }

    #[test]
    fn test_ast_variant_recording() {
        let mut matrix = GrammarCoverageMatrix::new();

        let literal_expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Span { start: 0, end: 1 },
        );

        let identifier_expr = Expr::new(
            ExprKind::Identifier("x".to_string()),
            Span { start: 0, end: 1 },
        );

        matrix.record("literal", "42", Ok(literal_expr), Duration::from_millis(1));
        matrix.record(
            "identifier",
            "x",
            Ok(identifier_expr),
            Duration::from_millis(1),
        );

        assert!(matrix.ast_variants.contains("Literal"));
        assert!(matrix.ast_variants.contains("Identifier"));
    }

    #[test]
    fn test_error_pattern_deduplication() {
        let mut matrix = GrammarCoverageMatrix::new();

        let error1 = anyhow::anyhow!("Same error");
        let error2 = anyhow::anyhow!("Same error");
        let error3 = anyhow::anyhow!("Different error");

        matrix.record("test", "input1", Err(error1), Duration::from_millis(1));
        matrix.record("test", "input2", Err(error2), Duration::from_millis(1));
        matrix.record("test", "input3", Err(error3), Duration::from_millis(1));

        let stats = &matrix.productions["test"];
        assert_eq!(stats.hit_count, 3);
        assert_eq!(stats.success_count, 0);
        assert_eq!(stats.error_patterns.len(), 2); // Only unique errors
    }

    /* Commented out - API mismatch
    #[test]
    fn test_calculate_stats() {
        let mut matrix = GrammarCoverageMatrix::new();
        let expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Span { start: 0, end: 1 },
        );

        // Add some successes and failures
        matrix.record("test1", "input", Ok(expr.clone()), Duration::from_millis(1));
        matrix.record("test1", "input", Ok(expr.clone()), Duration::from_millis(1));
        matrix.record("test1", "input", Err(anyhow::anyhow!("error")), Duration::from_millis(1));

        matrix.record("test2", "input", Ok(expr), Duration::from_millis(1));

        let (total, successful, coverage_pct) = matrix.calculate_stats();

        assert_eq!(total, 4);
        assert_eq!(successful, 3);
        assert_eq!(coverage_pct, 75.0);
    }
    */

    #[test]
    fn test_generate_report() {
        let mut matrix = GrammarCoverageMatrix::new();
        let expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Span { start: 0, end: 1 },
        );

        matrix.record("literal", "42", Ok(expr), Duration::from_millis(10));

        let report = matrix.generate_report();

        assert!(report.contains("Grammar Coverage Report"));
        assert!(report.contains("literal"));
        assert!(report.contains("100.00%"));
    }

    /* Commented out - API mismatch
    #[test]
    fn test_check_coverage_threshold_success() {
        let mut matrix = GrammarCoverageMatrix::new();
        let expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Span { start: 0, end: 1 },
        );

        matrix.record("test", "input", Ok(expr), Duration::from_millis(1));

        let result = matrix.check_coverage_threshold(50.0);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 100.0);
    }

    #[test]
    fn test_check_coverage_threshold_failure() {
        let mut matrix = GrammarCoverageMatrix::new();

        matrix.record("test", "input", Err(anyhow::anyhow!("error")), Duration::from_millis(1));

        let result = matrix.check_coverage_threshold(50.0);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("below threshold"));
    }
    */

    #[test]
    fn test_grammar_productions_array() {
        // Test that GRAMMAR_PRODUCTIONS array is properly defined
        // GRAMMAR_PRODUCTIONS is a const array, so this is a compile-time guarantee

        // Check a few samples
        let first = super::GRAMMAR_PRODUCTIONS[0];
        assert!(!first.0.is_empty());
        assert!(!first.1.is_empty());
    }

    // Test 13: is_complete returns true when coverage is sufficient
    #[test]
    fn test_is_complete_true() {
        let mut matrix = GrammarCoverageMatrix::new();
        let expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Span { start: 0, end: 1 },
        );

        matrix.record(
            "prod1",
            "input1",
            Ok(expr.clone()),
            Duration::from_millis(1),
        );
        matrix.record("prod2", "input2", Ok(expr), Duration::from_millis(1));

        assert!(matrix.is_complete(2));
        assert!(!matrix.is_complete(3)); // Not enough productions
    }

    // Test 14: is_complete returns false when there are uncovered productions
    #[test]
    fn test_is_complete_false_uncovered() {
        let mut matrix = GrammarCoverageMatrix::new();
        matrix.uncovered.push("uncovered_prod");

        let expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Span { start: 0, end: 1 },
        );
        matrix.record("prod1", "input1", Ok(expr), Duration::from_millis(1));

        assert!(!matrix.is_complete(1));
    }

    // Test 15: get_coverage_percentage with empty matrix
    #[test]
    fn test_get_coverage_percentage_empty() {
        let matrix = GrammarCoverageMatrix::new();
        assert!((matrix.get_coverage_percentage() - 0.0).abs() < f64::EPSILON);
    }

    // Test 16: get_coverage_percentage with 100% coverage
    #[test]
    fn test_get_coverage_percentage_full() {
        let mut matrix = GrammarCoverageMatrix::new();
        let expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Span { start: 0, end: 1 },
        );

        matrix.record(
            "prod1",
            "input1",
            Ok(expr.clone()),
            Duration::from_millis(1),
        );
        matrix.record("prod2", "input2", Ok(expr), Duration::from_millis(1));

        assert!((matrix.get_coverage_percentage() - 100.0).abs() < 0.01);
    }

    // Test 17: get_coverage_percentage with partial coverage
    #[test]
    fn test_get_coverage_percentage_partial() {
        let mut matrix = GrammarCoverageMatrix::new();
        let expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Span { start: 0, end: 1 },
        );

        matrix.record("prod1", "input1", Ok(expr), Duration::from_millis(1));
        matrix.uncovered.push("uncovered1");
        matrix.uncovered.push("uncovered2");

        // 1 covered, 2 uncovered = 33.33%
        let pct = matrix.get_coverage_percentage();
        assert!(pct > 30.0 && pct < 40.0);
    }

    // Test 18: record_ast_variants Binary
    #[test]
    fn test_ast_variant_binary() {
        let mut matrix = GrammarCoverageMatrix::new();

        let binary_expr = Expr::new(
            ExprKind::Binary {
                op: BinaryOp::Add,
                left: Box::new(Expr::new(
                    ExprKind::Literal(Literal::Integer(1, None)),
                    Span { start: 0, end: 1 },
                )),
                right: Box::new(Expr::new(
                    ExprKind::Literal(Literal::Integer(2, None)),
                    Span { start: 2, end: 3 },
                )),
            },
            Span { start: 0, end: 3 },
        );

        matrix.record("binary", "1 + 2", Ok(binary_expr), Duration::from_millis(1));

        assert!(matrix.ast_variants.contains("Binary"));
    }

    // Test 19: record_ast_variants Unary
    #[test]
    fn test_ast_variant_unary() {
        let mut matrix = GrammarCoverageMatrix::new();

        let unary_expr = Expr::new(
            ExprKind::Unary {
                op: UnaryOp::Negate,
                operand: Box::new(Expr::new(
                    ExprKind::Literal(Literal::Integer(42, None)),
                    Span { start: 1, end: 3 },
                )),
            },
            Span { start: 0, end: 3 },
        );

        matrix.record("unary", "-42", Ok(unary_expr), Duration::from_millis(1));

        assert!(matrix.ast_variants.contains("Unary"));
    }

    // Test 20: record_ast_variants If
    #[test]
    fn test_ast_variant_if() {
        let mut matrix = GrammarCoverageMatrix::new();

        let if_expr = Expr::new(
            ExprKind::If {
                condition: Box::new(Expr::new(
                    ExprKind::Literal(Literal::Bool(true)),
                    Span { start: 0, end: 4 },
                )),
                then_branch: Box::new(Expr::new(
                    ExprKind::Literal(Literal::Integer(1, None)),
                    Span { start: 7, end: 8 },
                )),
                else_branch: None,
            },
            Span { start: 0, end: 10 },
        );

        matrix.record("if", "if true { 1 }", Ok(if_expr), Duration::from_millis(1));

        assert!(matrix.ast_variants.contains("If"));
    }

    // Test 21: record_ast_variants Block
    #[test]
    fn test_ast_variant_block() {
        let mut matrix = GrammarCoverageMatrix::new();

        let block_expr = Expr::new(
            ExprKind::Block(vec![Expr::new(
                ExprKind::Literal(Literal::Integer(42, None)),
                Span { start: 1, end: 3 },
            )]),
            Span { start: 0, end: 4 },
        );

        matrix.record("block", "{ 42 }", Ok(block_expr), Duration::from_millis(1));

        assert!(matrix.ast_variants.contains("Block"));
    }

    // Test 22: record_ast_variants Let
    #[test]
    fn test_ast_variant_let() {
        let mut matrix = GrammarCoverageMatrix::new();

        let let_expr = Expr::new(
            ExprKind::Let {
                name: "x".to_string(),
                type_annotation: None,
                value: Box::new(Expr::new(
                    ExprKind::Literal(Literal::Integer(42, None)),
                    Span { start: 8, end: 10 },
                )),
                body: Box::new(Expr::new(
                    ExprKind::Identifier("x".to_string()),
                    Span { start: 14, end: 15 },
                )),
                is_mutable: false,
                else_block: None,
            },
            Span { start: 0, end: 15 },
        );

        matrix.record(
            "let",
            "let x = 42 in x",
            Ok(let_expr),
            Duration::from_millis(1),
        );

        assert!(matrix.ast_variants.contains("Let"));
    }

    // Test 23: record_ast_variants Call
    #[test]
    fn test_ast_variant_call() {
        let mut matrix = GrammarCoverageMatrix::new();

        let call_expr = Expr::new(
            ExprKind::Call {
                func: Box::new(Expr::new(
                    ExprKind::Identifier("foo".to_string()),
                    Span { start: 0, end: 3 },
                )),
                args: vec![],
            },
            Span { start: 0, end: 5 },
        );

        matrix.record("call", "foo()", Ok(call_expr), Duration::from_millis(1));

        assert!(matrix.ast_variants.contains("Call"));
    }

    // Test 24: record_ast_variants List
    #[test]
    fn test_ast_variant_list() {
        let mut matrix = GrammarCoverageMatrix::new();

        let list_expr = Expr::new(
            ExprKind::List(vec![Expr::new(
                ExprKind::Literal(Literal::Integer(1, None)),
                Span { start: 1, end: 2 },
            )]),
            Span { start: 0, end: 3 },
        );

        matrix.record("list", "[1]", Ok(list_expr), Duration::from_millis(1));

        assert!(matrix.ast_variants.contains("List"));
    }

    // Test 25: report includes all expected sections
    #[test]
    fn test_report_comprehensive() {
        let mut matrix = GrammarCoverageMatrix::new();
        let expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Span { start: 0, end: 1 },
        );

        // Add some successes and failures
        matrix.record("literal", "42", Ok(expr.clone()), Duration::from_millis(10));
        matrix.record("literal", "43", Ok(expr), Duration::from_millis(20));
        matrix.record(
            "other",
            "bad",
            Err(anyhow::anyhow!("error")),
            Duration::from_millis(5),
        );

        let report = matrix.report();

        assert!(report.contains("Grammar Coverage Report"));
        assert!(report.contains("Coverage:"));
        assert!(report.contains("Productions covered:"));
        assert!(report.contains("AST variants seen:"));
        assert!(report.contains("Total attempts:"));
        assert!(report.contains("Success rate:"));
        assert!(report.contains("Slowest productions:"));
    }

    // Test 26: GrammarCoverageMatrix default
    #[test]
    fn test_grammar_coverage_matrix_default() {
        let matrix = GrammarCoverageMatrix::default();
        assert!(matrix.productions.is_empty());
        assert!(matrix.ast_variants.is_empty());
        assert!(matrix.uncovered.is_empty());
    }
}