ruchy 4.2.0

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
#![allow(deprecated)]
// LANG-COMP-005: String Interpolation - Validation Tests with Traceability
// Links to: examples/lang_comp/05-string-interpolation/*.ruchy
// Validates: LANG-COMP-005 String Interpolation (basic, expressions, functions, nested)
// EXTREME TDD Protocol: Tests use assert_cmd + mandatory naming convention
#![allow(clippy::ignore_without_reason)] // LANG-COMP tests with known issues use ignore

use assert_cmd::Command;
use predicates::prelude::*;
use std::path::PathBuf;

/// Helper to get ruchy binary command
fn ruchy_cmd() -> Command {
    Command::cargo_bin("ruchy").expect("Failed to find ruchy binary")
}

/// Helper to get example file path
fn example_path(relative_path: &str) -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("examples/lang_comp/05-string-interpolation")
        .join(relative_path)
}

/// 15-TOOL VALIDATION: Run ALL 15 native tools on example file
/// MANDATORY/BLOCKING: Test passes ONLY if all tools succeed
/// Skipped: Tool 3 (repl - interactive), Tool 15 (notebook - needs file arg implementation)
/// Working: 13/15 tools = 87% coverage
fn validate_with_15_tools(example: &PathBuf) {
    // TOOL 1: ruchy check - Syntax validation
    ruchy_cmd().arg("check").arg(example).assert().success();

    // TOOL 2: ruchy transpile - Rust code generation
    ruchy_cmd().arg("transpile").arg(example).assert().success();

    // TOOL 3: ruchy repl - SKIPPED (requires interactive input)

    // TOOL 4: ruchy lint - Static analysis
    ruchy_cmd().arg("lint").arg(example).assert().success();

    // TOOL 5: ruchy compile - Binary compilation
    // DEFECT-RACE-CONDITION FIX: Use unique output path per example file to avoid parallel test collisions
    let compile_output = std::env::temp_dir().join(format!(
        "compile_test_{}_{}",
        example.file_stem().unwrap().to_string_lossy(),
        std::process::id()
    ));
    ruchy_cmd()
        .arg("compile")
        .arg(example)
        .arg("-o")
        .arg(&compile_output)
        .assert()
        .success();
    std::fs::remove_file(&compile_output).ok();

    // TOOL 6: ruchy run - Execution
    ruchy_cmd().arg("run").arg(example).assert().success();

    // TOOL 7: ruchy coverage - Test coverage
    ruchy_cmd().arg("coverage").arg(example).assert().success();

    // TOOL 8: ruchy runtime --bigo - Complexity analysis
    ruchy_cmd()
        .arg("runtime")
        .arg(example)
        .arg("--bigo")
        .assert()
        .success();

    // TOOL 9: ruchy ast - AST verification
    ruchy_cmd().arg("ast").arg(example).assert().success();

    // TOOL 10: ruchy wasm - WASM compilation
    ruchy_cmd().arg("wasm").arg(example).assert().success();

    // TOOL 11: ruchy provability - Formal verification
    ruchy_cmd()
        .arg("provability")
        .arg(example)
        .assert()
        .success();

    // TOOL 12: ruchy property-tests - Property-based testing (100 cases for speed)
    ruchy_cmd()
        .arg("property-tests")
        .arg(example)
        .arg("--cases")
        .arg("100")
        .assert()
        .success();

    // TOOL 13: ruchy mutations - Mutation testing (validates single files correctly)
    ruchy_cmd()
        .arg("mutations")
        .arg(example)
        .arg("--min-coverage")
        .arg("0")
        .arg("--timeout")
        .arg("60")
        .assert()
        .success();

    // TOOL 14: ruchy fuzz - Fuzz testing (10 iterations for speed in tests)
    ruchy_cmd()
        .arg("fuzz")
        .arg(example)
        .arg("--iterations")
        .arg("10")
        .assert()
        .success();

    // TOOL 15: ruchy notebook - SKIPPED (requires server)
}

// ============================================================================
// LANG-COMP-005-01: Basic String Interpolation Tests
// Links to: examples/lang_comp/05-string-interpolation/01_basic_interpolation.ruchy
// ============================================================================

#[test]
fn test_langcomp_005_01_basic_variable_interpolation() {
    // Test: f"Hello, {name}" works with string variables - must use println()
    let temp_file = std::env::temp_dir().join("langcomp_005_01_basic_var.ruchy");
    std::fs::write(
        &temp_file,
        r#"
let name = "World"
println(f"Hello, {name}!")
"#,
    )
    .unwrap();

    ruchy_cmd()
        .arg("run")
        .arg(&temp_file)
        .assert()
        .success()
        .stdout(predicate::str::contains("Hello, World!"));

    std::fs::remove_file(&temp_file).ok();
}

#[test]
fn test_langcomp_005_01_basic_integer_interpolation() {
    // Test: f"Number: {x}" works with integers - must use println()
    let temp_file = std::env::temp_dir().join("langcomp_005_01_basic_int.ruchy");
    std::fs::write(
        &temp_file,
        r#"
let x = 42
println(f"The answer is {x}")
"#,
    )
    .unwrap();

    ruchy_cmd()
        .arg("run")
        .arg(&temp_file)
        .assert()
        .success()
        .stdout(predicate::str::contains("The answer is 42"));

    std::fs::remove_file(&temp_file).ok();
}

#[test]
fn test_langcomp_005_01_multiple_interpolations() {
    // Test: f"{a} and {b}" works with multiple variables - must use println()
    let temp_file = std::env::temp_dir().join("langcomp_005_01_multiple.ruchy");
    std::fs::write(
        &temp_file,
        r#"
let name = "Alice"
let age = 30
println(f"Hello, {name}! You are {age} years old.")
"#,
    )
    .unwrap();

    ruchy_cmd()
        .arg("run")
        .arg(&temp_file)
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Hello, Alice! You are 30 years old.",
        ));

    std::fs::remove_file(&temp_file).ok();
}

#[test]
fn test_langcomp_005_01_basic_interpolation_example_file() {
    // 15-TOOL VALIDATION: examples/lang_comp/05-string-interpolation/01_basic_interpolation.ruchy
    // ACCEPTANCE CRITERIA: ALL 15 tools must succeed
    let example = example_path("01_basic_interpolation.ruchy");
    validate_with_15_tools(&example);

    // Additional validation: Verify output correctness
    ruchy_cmd()
        .arg("run")
        .arg(&example)
        .assert()
        .success()
        .stdout(predicate::str::contains("Name: Alice"))
        .stdout(predicate::str::contains("Age: 30"))
        .stdout(predicate::str::contains(
            "Hello, Alice! You are 30 years old.",
        ));
}

// ============================================================================
// LANG-COMP-005-02: Expression Interpolation Tests
// Links to: examples/lang_comp/05-string-interpolation/02_expressions.ruchy
// ============================================================================

#[test]
fn test_langcomp_005_02_arithmetic_expression_interpolation() {
    // Test: f"{x + y}" evaluates arithmetic expressions - must use println()
    let temp_file = std::env::temp_dir().join("langcomp_005_02_arithmetic.ruchy");
    std::fs::write(
        &temp_file,
        r#"
let x = 10
let y = 20
println(f"x + y = {x + y}")
"#,
    )
    .unwrap();

    ruchy_cmd()
        .arg("run")
        .arg(&temp_file)
        .assert()
        .success()
        .stdout(predicate::str::contains("x + y = 30"));

    std::fs::remove_file(&temp_file).ok();
}

#[test]
fn test_langcomp_005_02_comparison_expression_interpolation() {
    // Test: f"{x > y}" evaluates comparison expressions - must use println()
    let temp_file = std::env::temp_dir().join("langcomp_005_02_comparison.ruchy");
    std::fs::write(
        &temp_file,
        r#"
let x = 10
let y = 20
println(f"x > y is {x > y}")
"#,
    )
    .unwrap();

    ruchy_cmd()
        .arg("run")
        .arg(&temp_file)
        .assert()
        .success()
        .stdout(predicate::str::contains("x > y is false"));

    std::fs::remove_file(&temp_file).ok();
}

#[test]
fn test_langcomp_005_02_complex_expression_interpolation() {
    // Test: f"{x + y * 2}" follows operator precedence - must use println()
    let temp_file = std::env::temp_dir().join("langcomp_005_02_complex.ruchy");
    std::fs::write(
        &temp_file,
        r#"
let x = 10
let y = 20
println(f"Result: {x + y * 2}")
"#,
    )
    .unwrap();

    ruchy_cmd()
        .arg("run")
        .arg(&temp_file)
        .assert()
        .success()
        .stdout(predicate::str::contains("Result: 50"));

    std::fs::remove_file(&temp_file).ok();
}

#[test]
fn test_langcomp_005_02_expression_interpolation_example_file() {
    // Validates: examples/lang_comp/05-string-interpolation/02_expressions.ruchy
    ruchy_cmd()
        .arg("run")
        .arg(example_path("02_expressions.ruchy"))
        .assert()
        .success()
        .stdout(predicate::str::contains("x + y = 30"))
        .stdout(predicate::str::contains("x * y = 200"))
        .stdout(predicate::str::contains("x > y is false"))
        .stdout(predicate::str::contains("Result: 50"));
}

// ============================================================================
// LANG-COMP-005-03: Function Call Interpolation Tests
// Links to: examples/lang_comp/05-string-interpolation/03_function_calls.ruchy
// ============================================================================

#[test]
fn test_langcomp_005_03_function_call_interpolation() {
    // Test: f"{func(x)}" calls function and interpolates result - must use println()
    let temp_file = std::env::temp_dir().join("langcomp_005_03_func_call.ruchy");
    std::fs::write(
        &temp_file,
        r#"
fn double(x) {
    x * 2
}
let num = 21
println(f"double({num}) = {double(num)}")
"#,
    )
    .unwrap();

    ruchy_cmd()
        .arg("run")
        .arg(&temp_file)
        .assert()
        .success()
        .stdout(predicate::str::contains("double(21) = 42"));

    std::fs::remove_file(&temp_file).ok();
}

#[test]
fn test_langcomp_005_03_function_with_interpolated_result() {
    // Test: Interpolating function results works correctly - must use println()
    // NOTE: Functions returning strings need explicit type annotation (LANG-COMP future work)
    let temp_file = std::env::temp_dir().join("langcomp_005_03_func_result.ruchy");
    std::fs::write(
        &temp_file,
        r#"
fn add(a, b) {
    a + b
}
let result = add(10, 20)
println(f"Result: {result}")
"#,
    )
    .unwrap();

    ruchy_cmd()
        .arg("run")
        .arg(&temp_file)
        .assert()
        .success()
        .stdout(predicate::str::contains("Result: 30"));

    std::fs::remove_file(&temp_file).ok();
}

#[test]
fn test_langcomp_005_03_function_call_interpolation_example_file() {
    // Validates: examples/lang_comp/05-string-interpolation/03_function_calls.ruchy
    ruchy_cmd()
        .arg("run")
        .arg(example_path("03_function_calls.ruchy"))
        .assert()
        .success()
        .stdout(predicate::str::contains("double(21) = 42"))
        .stdout(predicate::str::contains("add(10, 20) = 30"));
}

// ============================================================================
// LANG-COMP-005-04: Nested Interpolation Tests
// Links to: examples/lang_comp/05-string-interpolation/04_nested_interpolation.ruchy
// ============================================================================

#[test]
fn test_langcomp_005_04_nested_variable_interpolation() {
    // Test: f-strings can reference other f-string variables - must use println()
    let temp_file = std::env::temp_dir().join("langcomp_005_04_nested_var.ruchy");
    std::fs::write(
        &temp_file,
        r#"
let first = "John"
let last = "Doe"
let full_name = f"{first} {last}"
println(f"Full name: {full_name}")
"#,
    )
    .unwrap();

    ruchy_cmd()
        .arg("run")
        .arg(&temp_file)
        .assert()
        .success()
        .stdout(predicate::str::contains("Full name: John Doe"));

    std::fs::remove_file(&temp_file).ok();
}

#[test]
fn test_langcomp_005_04_interpolated_fstring_variable() {
    // Test: f-string variables can be interpolated into other f-strings - must use println()
    // NOTE: Direct f-string nesting not yet supported (parser limitation)
    let temp_file = std::env::temp_dir().join("langcomp_005_04_fstring_var.ruchy");
    std::fs::write(
        &temp_file,
        r#"
let name = "Alice"
let greeting = f"Hello, {name}!"
println(f"Message: {greeting}")
"#,
    )
    .unwrap();

    ruchy_cmd()
        .arg("run")
        .arg(&temp_file)
        .assert()
        .success()
        .stdout(predicate::str::contains("Message: Hello, Alice!"));

    std::fs::remove_file(&temp_file).ok();
}

#[test]
fn test_langcomp_005_04_nested_interpolation_example_file() {
    // Validates: examples/lang_comp/05-string-interpolation/04_nested_interpolation.ruchy
    ruchy_cmd()
        .arg("run")
        .arg(example_path("04_nested_interpolation.ruchy"))
        .assert()
        .success()
        .stdout(predicate::str::contains("Full name: John Doe"))
        .stdout(predicate::str::contains("Greeting: Hello, John Doe!"));
}

// ============================================================================
// LANG-COMP-005: Property Tests (Mathematical Correctness Proofs)
// ============================================================================

#[cfg(test)]
mod property_tests {
    use super::*;

    #[test]
    #[ignore = "Test disabled - run with --ignored"]
    fn test_langcomp_005_property_interpolation_is_deterministic() {
        // Property: Same input always produces same output - must use println()
        for i in 1..20 {
            let code = format!(
                r#"
let x = {i}
println(f"Value: {{x}}")
"#
            );
            let temp_file =
                std::env::temp_dir().join(format!("langcomp_005_prop_deterministic_{i}.ruchy"));
            std::fs::write(&temp_file, &code).unwrap();

            // Run twice and compare
            let result1 = ruchy_cmd().arg("run").arg(&temp_file).output().unwrap();
            let result2 = ruchy_cmd().arg("run").arg(&temp_file).output().unwrap();

            assert_eq!(result1.stdout, result2.stdout);
            std::fs::remove_file(&temp_file).ok();
        }
    }

    #[test]
    #[ignore = "Test disabled - run with --ignored"]
    fn test_langcomp_005_property_expression_evaluation_in_interpolation() {
        // Property: f"{a + b}" equals println(a + b) for all a, b - must use println()
        for i in 1..10 {
            for j in 1..10 {
                let code = format!(
                    r#"
let a = {i}
let b = {j}
println(f"Result: {{a + b}}")
"#
                );
                let temp_file =
                    std::env::temp_dir().join(format!("langcomp_005_prop_expr_eval_{i}_{j}.ruchy"));
                std::fs::write(&temp_file, &code).unwrap();

                let expected = format!("Result: {}", i + j);
                ruchy_cmd()
                    .arg("run")
                    .arg(&temp_file)
                    .assert()
                    .success()
                    .stdout(predicate::str::contains(expected));

                std::fs::remove_file(&temp_file).ok();
            }
        }
    }

    #[test]
    #[ignore = "Test disabled - run with --ignored"]
    fn test_langcomp_005_property_multiple_interpolations_independent() {
        // Property: f"{a} {b}" equals concatenation of individual interpolations
        for i in 1..10 {
            let code = format!(
                r#"
let a = {}
let b = {}
println(f"{{a}} {{b}}")
"#,
                i,
                i * 2
            );
            let temp_file =
                std::env::temp_dir().join(format!("langcomp_005_prop_multi_interp_{i}.ruchy"));
            std::fs::write(&temp_file, &code).unwrap();

            let expected = format!("{} {}", i, i * 2);
            ruchy_cmd()
                .arg("run")
                .arg(&temp_file)
                .assert()
                .success()
                .stdout(predicate::str::contains(expected));

            std::fs::remove_file(&temp_file).ok();
        }
    }
}