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
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
//! Property-based tests for verifying compiler invariants
#![allow(clippy::unnecessary_wraps)] // Property tests often need Result for the test framework
use crate::backend::Transpiler;
use crate::frontend::ast::{BinaryOp, Expr, ExprKind, Literal, StringPart, UnaryOp};
use crate::frontend::{Parser, RecoveryParser};
#[allow(unused_imports)]
use crate::testing::generators::{arb_expr, arb_well_typed_expr};
use proptest::prelude::*;
use proptest::test_runner::TestCaseError;
/// Property: Parser should never panic on any input
///
/// # Errors
///
/// Returns an error if the property test fails
/// # Examples
///
/// ```
/// use ruchy::testing::properties::prop_parser_never_panics;
///
/// let result = prop_parser_never_panics("example");
/// assert_eq!(result, Ok(()));
/// ```
pub fn prop_parser_never_panics(input: &str) -> Result<(), TestCaseError> {
    let mut parser = Parser::new(input);
    // Parser should either succeed or return an error, never panic
    let _ = parser.parse();
    Ok(())
}
/// Property: Recovery parser should always produce some AST
///
/// # Errors
///
/// Returns an error if the property test fails
/// # Examples
///
/// ```
/// use ruchy::testing::properties::prop_recovery_parser_always_produces_ast;
///
/// let result = prop_recovery_parser_always_produces_ast("example");
/// assert_eq!(result, Ok(()));
/// ```
pub fn prop_recovery_parser_always_produces_ast(input: &str) -> Result<(), TestCaseError> {
    let mut parser = RecoveryParser::new(input);
    let result = parser.parse_with_recovery();
    // For non-empty input, we should get some AST or errors
    if !input.trim().is_empty() {
        prop_assert!(
            result.ast.is_some() || !result.errors.is_empty(),
            "Recovery parser should produce AST or errors for non-empty input"
        );
    }
    Ok(())
}
/// Property: Transpilation preserves expression structure
///
/// # Errors
///
/// Returns an error if the property test fails
/// # Examples
///
/// ```
/// use ruchy::testing::properties::prop_transpilation_preserves_structure;
///
/// let result = prop_transpilation_preserves_structure(());
/// assert_eq!(result, Ok(()));
/// ```
pub fn prop_transpilation_preserves_structure(expr: &Expr) -> Result<(), TestCaseError> {
    let mut transpiler = Transpiler::new();
    // Transpilation should either succeed or fail cleanly
    if let Ok(rust_code) = transpiler.transpile(expr) {
        // The generated Rust code should not be empty
        let code_str = rust_code.to_string();
        prop_assert!(!code_str.is_empty(), "Transpiled code should not be empty");
    } else {
        // Transpilation errors are acceptable for some ASTs
    }
    Ok(())
}
/// Property: String interpolation transpiles correctly
///
/// # Errors
///
/// Returns an error if the property test fails
/// # Examples
///
/// ```
/// use ruchy::testing::properties::prop_string_interpolation_transpiles;
///
/// let result = prop_string_interpolation_transpiles(());
/// assert_eq!(result, Ok(()));
/// ```
pub fn prop_string_interpolation_transpiles(parts: &[StringPart]) -> Result<(), TestCaseError> {
    let transpiler = Transpiler::new();
    let result = transpiler.transpile_string_interpolation(parts);
    // Should either succeed or fail cleanly, never panic
    if let Ok(tokens) = result {
        let code = tokens.to_string();
        // Should either be a format! call or a simple string literal
        prop_assert!(
            code.contains("format!")
                || code.contains("format !")
                || code.starts_with('"')
                || code.is_empty(),
            "String interpolation should produce format! call or string literal, got: {}",
            code
        );
    }
    // Transpilation errors are acceptable for malformed parts
    Ok(())
}
/// Property: Parse-print roundtrip
///
/// # Errors
///
/// Returns an error if the property test fails
/// # Examples
///
/// ```
/// use ruchy::testing::properties::prop_parse_print_roundtrip;
///
/// let result = prop_parse_print_roundtrip(());
/// assert_eq!(result, Ok(()));
/// ```
pub fn prop_parse_print_roundtrip(expr: &Expr) -> Result<(), TestCaseError> {
    // Property: Any valid AST can be transpiled to Rust without panicking
    // This tests the robustness of the transpiler, not the exact output format
    let mut transpiler = Transpiler::new();

    // The key property is that transpilation doesn't panic
    // We don't check the exact output format since that's implementation-dependent
    // (e.g., constant folding, type suffixes, formatting, etc.)
    let _ = transpiler.transpile(expr);

    // If we got here without panicking, the property holds
    Ok(())
}
/// Property: Well-typed expressions should always transpile successfully
///
/// # Errors
///
/// Returns an error if the property test fails
/// # Examples
///
/// ```
/// use ruchy::testing::properties::prop_well_typed_always_transpiles;
///
/// let result = prop_well_typed_always_transpiles(());
/// assert_eq!(result, Ok(()));
/// ```
pub fn prop_well_typed_always_transpiles(expr: &Expr) -> Result<(), TestCaseError> {
    let mut transpiler = Transpiler::new();
    // Check if this is a simple, well-typed expression
    if is_well_typed(expr) {
        match transpiler.transpile(expr) {
            Ok(_) => Ok(()),
            Err(e) => {
                prop_assert!(
                    false,
                    "Well-typed expression failed to transpile: {:?}\nError: {}",
                    expr,
                    e
                );
                Ok(())
            }
        }
    } else {
        // Complex expressions may fail, which is acceptable
        Ok(())
    }
}
/// Property: Error recovery should handle truncated input gracefully
///
/// # Errors
///
/// Returns an error if the property test fails
/// # Examples
///
/// ```
/// use ruchy::testing::properties::prop_recovery_handles_truncation;
///
/// let result = prop_recovery_handles_truncation("example");
/// assert_eq!(result, Ok(()));
/// ```
pub fn prop_recovery_handles_truncation(input: &str) -> Result<(), TestCaseError> {
    if input.is_empty() {
        return Ok(());
    }
    // Limit input length to prevent O(n²) performance in property tests
    let max_test_length = 50; // Reasonable limit for testing
    let test_input = if input.len() > max_test_length {
        &input[..max_test_length]
    } else {
        input
    };

    // Try parsing truncated versions of the input
    for i in 0..test_input.len() {
        let truncated = &test_input[..i];
        let mut parser = RecoveryParser::new(truncated);
        let result = parser.parse_with_recovery();
        // Should not panic, and should produce something
        if !truncated.trim().is_empty() {
            prop_assert!(
                result.ast.is_some() || !result.errors.is_empty(),
                "Recovery parser should handle truncated input at position {i}"
            );
        }
    }
    Ok(())
}
/// Helper to check if an expression is well-typed (simplified)
fn is_well_typed(expr: &Expr) -> bool {
    match &expr.kind {
        ExprKind::Literal(_) | ExprKind::Identifier(_) => true,
        ExprKind::Binary { left, right, op } => {
            match op {
                BinaryOp::Add | BinaryOp::Subtract | BinaryOp::Multiply | BinaryOp::Divide => {
                    is_numeric(left) && is_numeric(right)
                }
                BinaryOp::And | BinaryOp::Or => is_boolean(left) && is_boolean(right),
                BinaryOp::Equal | BinaryOp::NotEqual => {
                    // Equality can work on many types
                    is_well_typed(left) && is_well_typed(right)
                }
                _ => is_well_typed(left) && is_well_typed(right),
            }
        }
        ExprKind::Unary { operand, op } => match op {
            UnaryOp::Not => is_boolean(operand),
            UnaryOp::Negate | UnaryOp::BitwiseNot => is_numeric(operand),
            UnaryOp::Reference | UnaryOp::MutableReference | UnaryOp::Deref => true, // Reference/Deref can be applied to any type (PARSER-085: Issue #71)
        },
        ExprKind::If {
            condition,
            then_branch,
            else_branch,
        } => {
            is_boolean(condition)
                && is_well_typed(then_branch)
                && else_branch.as_ref().is_none_or(|e| is_well_typed(e))
        }
        _ => false, // Conservative for complex expressions
    }
}
fn is_numeric(expr: &Expr) -> bool {
    matches!(
        &expr.kind,
        ExprKind::Literal(Literal::Integer(_, _) | Literal::Float(_))
    )
}
fn is_boolean(expr: &Expr) -> bool {
    matches!(&expr.kind, ExprKind::Literal(Literal::Bool(_)))
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::panic)]
mod tests {
    use super::*;
    proptest! {
        #![proptest_config(proptest::test_runner::Config::with_cases(100))] // Limit test cases to prevent hanging

        #[test]
        fn test_parser_never_panics(input in prop::string::string_regex("[a-zA-Z0-9 (){}\n]{0,50}").unwrap()) {
            // Limited to reasonable inputs - no infinite loops
            prop_parser_never_panics(&input)?;
        }
        #[test]
        fn test_recovery_parser_always_produces_ast(input in prop::string::string_regex("[a-zA-Z0-9 (){}\n]{0,50}").unwrap()) {
            // Limited to reasonable inputs to prevent hanging
            prop_recovery_parser_always_produces_ast(&input)?;
        }
        #[test]
        fn test_transpilation_preserves_structure(expr in arb_expr()) {
            prop_transpilation_preserves_structure(&expr)?;
        }
        #[test]
        fn test_well_typed_always_transpiles(expr in arb_well_typed_expr()) {
            prop_well_typed_always_transpiles(&expr)?;
        }
        #[test]
        fn test_recovery_handles_truncation(input in "[a-zA-Z0-9 +\\-*/()]{0,30}") {
            prop_recovery_handles_truncation(&input)?;
        }
        #[test]
        fn test_parse_print_roundtrip(expr in arb_well_typed_expr()) {
            prop_parse_print_roundtrip(&expr)?;
        }
    }
    #[test]
    #[ignore = "Parser has infinite loop on certain inputs"]
    fn test_specific_recovery_cases() {
        // Test each case individually to identify which one hangs

        // Test case 1
        let mut parser = RecoveryParser::new("let x =");
        let result = parser.parse_with_recovery();
        assert!(
            result.ast.is_some() || !result.errors.is_empty(),
            "Failed: let x ="
        );

        // Test case 2
        let mut parser = RecoveryParser::new("if x >");
        let result = parser.parse_with_recovery();
        assert!(
            result.ast.is_some() || !result.errors.is_empty(),
            "Failed: if x >"
        );

        // Test case 3
        let mut parser = RecoveryParser::new("fun foo(");
        let result = parser.parse_with_recovery();
        assert!(
            result.ast.is_some() || !result.errors.is_empty(),
            "Failed: fun foo("
        );

        // Test case 4
        let mut parser = RecoveryParser::new("[1, 2,");
        let result = parser.parse_with_recovery();
        assert!(
            result.ast.is_some() || !result.errors.is_empty(),
            "Failed: [1, 2,"
        );

        // Test case 5
        let mut parser = RecoveryParser::new("1 + + 2");
        let result = parser.parse_with_recovery();
        assert!(
            result.ast.is_some() || !result.errors.is_empty(),
            "Failed: 1 + + 2"
        );
    }

    #[test]
    fn test_prop_parser_never_panics_unit() {
        let test_cases = vec![
            "",
            "42",
            "hello",
            "1 + 2",
            "invalid syntax !@#$",
            "let x = 42 in x",
            "fun test() { }",
            "if true { 1 } else { 2 }",
        ];

        for input in test_cases {
            let result = prop_parser_never_panics(input);
            assert!(result.is_ok(), "Parser panicked on input: {input}");
        }
    }

    #[test]
    fn test_prop_recovery_parser_unit() {
        let test_cases = vec![
            ("", false),       // Empty input shouldn't require recovery
            ("42", true),      // Valid input should produce AST
            ("1 + 2", true),   // Valid binary expression
            ("invalid", true), // Invalid but non-empty should produce something
        ];

        for (input, expect_output) in test_cases {
            let result = prop_recovery_parser_always_produces_ast(input);
            if expect_output {
                assert!(result.is_ok(), "Recovery parser failed on: {input}");
            } else {
                // Empty input case is handled differently
                assert!(result.is_ok(), "Recovery parser failed on empty input");
            }
        }
    }

    #[test]
    fn test_prop_transpilation_unit() {
        let test_exprs = vec![
            Expr::new(
                ExprKind::Literal(Literal::Integer(42, None)),
                Default::default(),
            ),
            Expr::new(ExprKind::Literal(Literal::Bool(true)), Default::default()),
            Expr::new(ExprKind::Identifier("x".to_string()), Default::default()),
        ];

        for expr in test_exprs {
            let result = prop_transpilation_preserves_structure(&expr);
            assert!(result.is_ok(), "Transpilation property failed");
        }
    }

    #[test]
    fn test_prop_string_interpolation_unit() {
        let test_cases = [
            vec![],                                      // Empty parts
            vec![StringPart::Text("hello".to_string())], // Text only
            vec![StringPart::Expr(Box::new(Expr::new(
                ExprKind::Literal(Literal::Integer(42, None)),
                Default::default(),
            )))], // Expression only
        ];

        for (i, parts) in test_cases.iter().enumerate() {
            println!("Testing case {i}: {parts:?}");
            let transpiler = Transpiler::new();
            let result = transpiler.transpile_string_interpolation(parts);
            match result {
                Ok(tokens) => {
                    let code = tokens.to_string();
                    println!("Generated code: {code}");
                    assert!(
                        code.contains("format!")
                            || code.contains("format !")
                            || code.starts_with('"')
                            || code.is_empty(),
                        "String interpolation should produce format! call or string literal, got: {code}"
                    );
                }
                Err(e) => {
                    println!("Transpilation error (acceptable): {e:?}");
                }
            }
        }
    }

    #[test]
    fn test_is_well_typed_function() {
        // Test numeric literals
        let int_expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Default::default(),
        );
        assert!(is_well_typed(&int_expr));

        let float_expr = Expr::new(ExprKind::Literal(Literal::Float(3.15)), Default::default());
        assert!(is_well_typed(&float_expr));

        // Test boolean literals
        let bool_expr = Expr::new(ExprKind::Literal(Literal::Bool(true)), Default::default());
        assert!(is_well_typed(&bool_expr));

        // Test identifiers
        let id_expr = Expr::new(ExprKind::Identifier("x".to_string()), Default::default());
        assert!(is_well_typed(&id_expr));
    }

    #[test]
    fn test_is_numeric_function() {
        let int_expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Default::default(),
        );
        assert!(is_numeric(&int_expr));

        let float_expr = Expr::new(ExprKind::Literal(Literal::Float(3.15)), Default::default());
        assert!(is_numeric(&float_expr));

        let bool_expr = Expr::new(ExprKind::Literal(Literal::Bool(true)), Default::default());
        assert!(!is_numeric(&bool_expr));

        let string_expr = Expr::new(
            ExprKind::Literal(Literal::String("hello".to_string())),
            Default::default(),
        );
        assert!(!is_numeric(&string_expr));
    }

    #[test]
    fn test_is_boolean_function() {
        let bool_expr = Expr::new(ExprKind::Literal(Literal::Bool(true)), Default::default());
        assert!(is_boolean(&bool_expr));

        let bool_false_expr =
            Expr::new(ExprKind::Literal(Literal::Bool(false)), Default::default());
        assert!(is_boolean(&bool_false_expr));

        let int_expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Default::default(),
        );
        assert!(!is_boolean(&int_expr));

        let string_expr = Expr::new(
            ExprKind::Literal(Literal::String("hello".to_string())),
            Default::default(),
        );
        assert!(!is_boolean(&string_expr));
    }

    #[test]
    fn test_well_typed_binary_expressions() {
        let left = Expr::new(
            ExprKind::Literal(Literal::Integer(1, None)),
            Default::default(),
        );
        let right = Expr::new(
            ExprKind::Literal(Literal::Integer(2, None)),
            Default::default(),
        );

        // Test arithmetic operations
        let add_expr = Expr::new(
            ExprKind::Binary {
                left: Box::new(left),
                op: BinaryOp::Add,
                right: Box::new(right),
            },
            Default::default(),
        );
        assert!(is_well_typed(&add_expr));

        // Test boolean operations
        let bool_left = Expr::new(ExprKind::Literal(Literal::Bool(true)), Default::default());
        let bool_right = Expr::new(ExprKind::Literal(Literal::Bool(false)), Default::default());
        let boolean_and_expr = Expr::new(
            ExprKind::Binary {
                left: Box::new(bool_left),
                op: BinaryOp::And,
                right: Box::new(bool_right),
            },
            Default::default(),
        );
        assert!(is_well_typed(&boolean_and_expr));
    }

    #[test]
    fn test_well_typed_unary_expressions() {
        // Test negation on numeric
        let int_expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Default::default(),
        );
        let neg_expr = Expr::new(
            ExprKind::Unary {
                operand: Box::new(int_expr),
                op: UnaryOp::Negate,
            },
            Default::default(),
        );
        assert!(is_well_typed(&neg_expr));

        // Test not on boolean
        let bool_expr = Expr::new(ExprKind::Literal(Literal::Bool(true)), Default::default());
        let not_expr = Expr::new(
            ExprKind::Unary {
                operand: Box::new(bool_expr),
                op: UnaryOp::Not,
            },
            Default::default(),
        );
        assert!(is_well_typed(&not_expr));
    }

    #[test]
    fn test_well_typed_if_expressions() {
        let condition = Expr::new(ExprKind::Literal(Literal::Bool(true)), Default::default());
        let then_branch = Expr::new(
            ExprKind::Literal(Literal::Integer(1, None)),
            Default::default(),
        );
        let else_branch = Expr::new(
            ExprKind::Literal(Literal::Integer(2, None)),
            Default::default(),
        );

        let if_expr = Expr::new(
            ExprKind::If {
                condition: Box::new(condition),
                then_branch: Box::new(then_branch),
                else_branch: Some(Box::new(else_branch)),
            },
            Default::default(),
        );
        assert!(is_well_typed(&if_expr));
    }

    #[test]
    fn test_ill_typed_expressions() {
        // Test adding boolean to integer (ill-typed)
        let bool_expr = Expr::new(ExprKind::Literal(Literal::Bool(true)), Default::default());
        let int_expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Default::default(),
        );

        let bad_add = Expr::new(
            ExprKind::Binary {
                left: Box::new(bool_expr),
                op: BinaryOp::Add,
                right: Box::new(int_expr),
            },
            Default::default(),
        );
        assert!(!is_well_typed(&bad_add));
    }

    #[test]
    fn test_parser_with_edge_cases() {
        let edge_cases = vec![
            "\0",     // Null character
            "\n\n\n", // Multiple newlines
            "   ",    // Whitespace only
            "\t\t\t", // Tabs only
            "\"",     // Unterminated string
            "(",      // Unmatched paren
            "}",      // Unmatched brace
        ];

        for case in edge_cases {
            let result = prop_parser_never_panics(case);
            assert!(result.is_ok(), "Parser should not panic on: {case:?}");
        }
    }

    #[test]
    fn test_transpilation_with_complex_expressions() {
        // Test more complex expressions that might stress the transpiler
        let complex_exprs = vec![
            // Nested binary operations
            Expr::new(
                ExprKind::Binary {
                    left: Box::new(Expr::new(
                        ExprKind::Binary {
                            left: Box::new(Expr::new(
                                ExprKind::Literal(Literal::Integer(1, None)),
                                Default::default(),
                            )),
                            op: BinaryOp::Add,
                            right: Box::new(Expr::new(
                                ExprKind::Literal(Literal::Integer(2, None)),
                                Default::default(),
                            )),
                        },
                        Default::default(),
                    )),
                    op: BinaryOp::Multiply,
                    right: Box::new(Expr::new(
                        ExprKind::Literal(Literal::Integer(3, None)),
                        Default::default(),
                    )),
                },
                Default::default(),
            ),
        ];

        for expr in complex_exprs {
            let result = prop_transpilation_preserves_structure(&expr);
            assert!(
                result.is_ok(),
                "Transpilation should handle complex expressions"
            );
        }
    }

    #[test]
    fn test_string_interpolation_edge_cases() {
        let edge_cases = vec![
            // Empty string parts
            vec![StringPart::Text(String::new())],
            // Mixed text and expressions
            vec![
                StringPart::Text("Hello ".to_string()),
                StringPart::Expr(Box::new(Expr::new(
                    ExprKind::Identifier("name".to_string()),
                    Default::default(),
                ))),
                StringPart::Text("!".to_string()),
            ],
        ];

        for parts in edge_cases {
            let result = prop_string_interpolation_transpiles(&parts);
            assert!(
                result.is_ok(),
                "String interpolation should handle edge cases"
            );
        }
    }

    #[test]
    fn test_property_functions_return_ok() {
        // Test that all property functions return Ok for simple cases
        assert!(prop_parser_never_panics("42").is_ok());
        assert!(prop_recovery_parser_always_produces_ast("42").is_ok());

        let simple_expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Default::default(),
        );
        assert!(prop_transpilation_preserves_structure(&simple_expr).is_ok());

        let simple_parts = vec![StringPart::Text("hello".to_string())];
        assert!(prop_string_interpolation_transpiles(&simple_parts).is_ok());
    }

    // === EXTREME TDD Round 21 - Coverage Push Tests ===

    #[test]
    fn test_prop_recovery_handles_truncation_empty() {
        let result = prop_recovery_handles_truncation("");
        assert!(result.is_ok());
    }

    #[test]
    fn test_prop_recovery_handles_truncation_short() {
        let result = prop_recovery_handles_truncation("1+2");
        assert!(result.is_ok());
    }

    #[test]
    fn test_prop_recovery_handles_truncation_long() {
        // Input longer than 50 chars to hit truncation logic
        let long_input = "a".repeat(100);
        let result = prop_recovery_handles_truncation(&long_input);
        assert!(result.is_ok());
    }

    #[test]
    fn test_well_typed_with_subtract() {
        let left = Expr::new(
            ExprKind::Literal(Literal::Integer(10, None)),
            Default::default(),
        );
        let right = Expr::new(
            ExprKind::Literal(Literal::Integer(3, None)),
            Default::default(),
        );
        let sub_expr = Expr::new(
            ExprKind::Binary {
                left: Box::new(left),
                op: BinaryOp::Subtract,
                right: Box::new(right),
            },
            Default::default(),
        );
        assert!(is_well_typed(&sub_expr));
    }

    #[test]
    fn test_well_typed_with_divide() {
        let left = Expr::new(
            ExprKind::Literal(Literal::Integer(20, None)),
            Default::default(),
        );
        let right = Expr::new(
            ExprKind::Literal(Literal::Integer(4, None)),
            Default::default(),
        );
        let div_expr = Expr::new(
            ExprKind::Binary {
                left: Box::new(left),
                op: BinaryOp::Divide,
                right: Box::new(right),
            },
            Default::default(),
        );
        assert!(is_well_typed(&div_expr));
    }

    #[test]
    fn test_well_typed_with_or() {
        let left = Expr::new(ExprKind::Literal(Literal::Bool(true)), Default::default());
        let right = Expr::new(ExprKind::Literal(Literal::Bool(false)), Default::default());
        let or_expr = Expr::new(
            ExprKind::Binary {
                left: Box::new(left),
                op: BinaryOp::Or,
                right: Box::new(right),
            },
            Default::default(),
        );
        assert!(is_well_typed(&or_expr));
    }

    #[test]
    fn test_well_typed_with_equal() {
        let left = Expr::new(
            ExprKind::Literal(Literal::Integer(5, None)),
            Default::default(),
        );
        let right = Expr::new(
            ExprKind::Literal(Literal::Integer(5, None)),
            Default::default(),
        );
        let eq_expr = Expr::new(
            ExprKind::Binary {
                left: Box::new(left),
                op: BinaryOp::Equal,
                right: Box::new(right),
            },
            Default::default(),
        );
        assert!(is_well_typed(&eq_expr));
    }

    #[test]
    fn test_well_typed_with_not_equal() {
        let left = Expr::new(
            ExprKind::Literal(Literal::Integer(5, None)),
            Default::default(),
        );
        let right = Expr::new(
            ExprKind::Literal(Literal::Integer(10, None)),
            Default::default(),
        );
        let neq_expr = Expr::new(
            ExprKind::Binary {
                left: Box::new(left),
                op: BinaryOp::NotEqual,
                right: Box::new(right),
            },
            Default::default(),
        );
        assert!(is_well_typed(&neq_expr));
    }

    #[test]
    fn test_well_typed_with_bitwise_not() {
        let int_expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Default::default(),
        );
        let bnot_expr = Expr::new(
            ExprKind::Unary {
                operand: Box::new(int_expr),
                op: UnaryOp::BitwiseNot,
            },
            Default::default(),
        );
        assert!(is_well_typed(&bnot_expr));
    }

    #[test]
    fn test_well_typed_with_reference() {
        let expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Default::default(),
        );
        let ref_expr = Expr::new(
            ExprKind::Unary {
                operand: Box::new(expr),
                op: UnaryOp::Reference,
            },
            Default::default(),
        );
        assert!(is_well_typed(&ref_expr));
    }

    #[test]
    fn test_well_typed_with_mutable_reference() {
        let expr = Expr::new(ExprKind::Identifier("x".to_string()), Default::default());
        let mut_ref_expr = Expr::new(
            ExprKind::Unary {
                operand: Box::new(expr),
                op: UnaryOp::MutableReference,
            },
            Default::default(),
        );
        assert!(is_well_typed(&mut_ref_expr));
    }

    #[test]
    fn test_well_typed_with_deref() {
        let expr = Expr::new(ExprKind::Identifier("ptr".to_string()), Default::default());
        let deref_expr = Expr::new(
            ExprKind::Unary {
                operand: Box::new(expr),
                op: UnaryOp::Deref,
            },
            Default::default(),
        );
        assert!(is_well_typed(&deref_expr));
    }

    #[test]
    fn test_well_typed_if_no_else() {
        let condition = Expr::new(ExprKind::Literal(Literal::Bool(true)), Default::default());
        let then_branch = Expr::new(
            ExprKind::Literal(Literal::Integer(1, None)),
            Default::default(),
        );
        let if_no_else = Expr::new(
            ExprKind::If {
                condition: Box::new(condition),
                then_branch: Box::new(then_branch),
                else_branch: None,
            },
            Default::default(),
        );
        assert!(is_well_typed(&if_no_else));
    }

    #[test]
    fn test_is_well_typed_string_literal() {
        let str_expr = Expr::new(
            ExprKind::Literal(Literal::String("hello".to_string())),
            Default::default(),
        );
        assert!(is_well_typed(&str_expr));
    }

    #[test]
    fn test_is_well_typed_char_literal() {
        let char_expr = Expr::new(ExprKind::Literal(Literal::Char('a')), Default::default());
        assert!(is_well_typed(&char_expr));
    }

    #[test]
    fn test_is_well_typed_unit_literal() {
        // Unit literal (closest to nil in Ruchy)
        let unit_expr = Expr::new(ExprKind::Literal(Literal::Unit), Default::default());
        assert!(is_well_typed(&unit_expr));
    }

    #[test]
    fn test_ill_typed_and_with_int() {
        let left = Expr::new(
            ExprKind::Literal(Literal::Integer(1, None)),
            Default::default(),
        );
        let right = Expr::new(
            ExprKind::Literal(Literal::Integer(2, None)),
            Default::default(),
        );
        let bad_and = Expr::new(
            ExprKind::Binary {
                left: Box::new(left),
                op: BinaryOp::And,
                right: Box::new(right),
            },
            Default::default(),
        );
        assert!(!is_well_typed(&bad_and));
    }

    #[test]
    fn test_ill_typed_or_with_int() {
        let left = Expr::new(
            ExprKind::Literal(Literal::Integer(1, None)),
            Default::default(),
        );
        let right = Expr::new(
            ExprKind::Literal(Literal::Integer(2, None)),
            Default::default(),
        );
        let bad_or = Expr::new(
            ExprKind::Binary {
                left: Box::new(left),
                op: BinaryOp::Or,
                right: Box::new(right),
            },
            Default::default(),
        );
        assert!(!is_well_typed(&bad_or));
    }

    #[test]
    fn test_ill_typed_not_with_int() {
        let int_expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Default::default(),
        );
        let bad_not = Expr::new(
            ExprKind::Unary {
                operand: Box::new(int_expr),
                op: UnaryOp::Not,
            },
            Default::default(),
        );
        assert!(!is_well_typed(&bad_not));
    }

    #[test]
    fn test_ill_typed_negate_with_bool() {
        let bool_expr = Expr::new(ExprKind::Literal(Literal::Bool(true)), Default::default());
        let bad_neg = Expr::new(
            ExprKind::Unary {
                operand: Box::new(bool_expr),
                op: UnaryOp::Negate,
            },
            Default::default(),
        );
        assert!(!is_well_typed(&bad_neg));
    }

    #[test]
    fn test_ill_typed_if_non_bool_condition() {
        let condition = Expr::new(
            ExprKind::Literal(Literal::Integer(1, None)),
            Default::default(),
        );
        let then_branch = Expr::new(
            ExprKind::Literal(Literal::Integer(2, None)),
            Default::default(),
        );
        let bad_if = Expr::new(
            ExprKind::If {
                condition: Box::new(condition),
                then_branch: Box::new(then_branch),
                else_branch: None,
            },
            Default::default(),
        );
        assert!(!is_well_typed(&bad_if));
    }

    #[test]
    fn test_prop_parse_print_roundtrip_direct() {
        let expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Default::default(),
        );
        let result = prop_parse_print_roundtrip(&expr);
        assert!(result.is_ok());
    }

    #[test]
    fn test_prop_well_typed_always_transpiles_simple() {
        let expr = Expr::new(
            ExprKind::Literal(Literal::Integer(42, None)),
            Default::default(),
        );
        let result = prop_well_typed_always_transpiles(&expr);
        assert!(result.is_ok());
    }

    #[test]
    fn test_prop_well_typed_always_transpiles_complex() {
        // A well-typed but more complex expression
        let left = Expr::new(
            ExprKind::Literal(Literal::Integer(1, None)),
            Default::default(),
        );
        let right = Expr::new(
            ExprKind::Literal(Literal::Integer(2, None)),
            Default::default(),
        );
        let expr = Expr::new(
            ExprKind::Binary {
                left: Box::new(left),
                op: BinaryOp::Add,
                right: Box::new(right),
            },
            Default::default(),
        );
        let result = prop_well_typed_always_transpiles(&expr);
        assert!(result.is_ok());
    }
}