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
#![allow(missing_docs)]
//! CLI Contract Tests: `ruchy runtime`
//!
//! **Purpose**: Validate user-facing contract (exit codes, stdio, performance analysis)
//! **Layer 4**: CLI expectation testing (black-box validation)
//!
//! **Contract Specification**:
//! - Exit code 0: Runtime analysis completed successfully
//! - Exit code 1: Analysis failed OR file not found OR syntax error
//! - stdout: Performance report (profiling, `BigO`, benchmarks, memory)
//! - stderr: Error messages (analysis errors, missing files)
//! - Options: --profile, --bigo, --bench, --compare, --memory, --verbose, --output
//!
//! **Reference**: docs/specifications/15-tool-improvement-spec.md (v4.0)
//! **TICR**: docs/testing/TICR-ANALYSIS.md (runtime: 0.3 → target 0.5, HIGH RISK)
//!
//! **Note**: Runtime tool is HIGH RISK (complexity 20, minimal test coverage)

use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;

/// Helper: Create ruchy command
fn ruchy_cmd() -> Command {
    assert_cmd::cargo::cargo_bin_cmd!("ruchy")
}

/// Helper: Create temp file with content
fn create_temp_file(dir: &TempDir, name: &str, content: &str) -> std::path::PathBuf {
    let path = dir.path().join(name);
    fs::write(&path, content).expect("Failed to write temp file");
    path
}

// ============================================================================
// CLI CONTRACT TESTS: BASIC BEHAVIOR
// ============================================================================

#[test]
fn cli_runtime_valid_program_exits_zero() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(&temp, "simple.ruchy", "let x = 42\nprintln(x)\n");

    ruchy_cmd().arg("runtime").arg(&file).assert().success(); // Exit code 0
}

#[test]
fn cli_runtime_missing_file_exits_nonzero() {
    ruchy_cmd()
        .arg("runtime")
        .arg("nonexistent_xyz.ruchy")
        .assert()
        .failure(); // Exit code != 0
}

#[test]
fn cli_runtime_syntax_error_exits_nonzero() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(&temp, "invalid.ruchy", "let x = \n");

    ruchy_cmd().arg("runtime").arg(&file).assert().failure(); // Exit code != 0
}

#[test]
fn cli_runtime_outputs_performance_report() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(&temp, "perf_test.ruchy", "let x = 42\n");

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .assert()
        .success()
        .stdout(
            predicate::str::contains("Performance")
                .or(predicate::str::contains("Runtime"))
                .or(predicate::str::contains("Execution")),
        );
}

// ============================================================================
// CLI CONTRACT TESTS: PROFILE OPTION
// ============================================================================

#[test]
fn cli_runtime_profile_flag() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(&temp, "profile_test.ruchy", "let x = 42\n");

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--profile")
        .assert()
        .success()
        .stdout(
            predicate::str::contains("profil")
                .or(predicate::str::contains("Profil"))
                .or(predicate::str::contains("Performance")),
        );
}

#[test]
fn cli_runtime_profile_with_loop() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(
        &temp,
        "profile_loop.ruchy",
        r"
for i in range(10) {
    println(i)
}
",
    );

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--profile")
        .assert()
        .success();
}

// ============================================================================
// CLI CONTRACT TESTS: BIGO OPTION
// ============================================================================

#[test]
fn cli_runtime_bigo_flag() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(&temp, "bigo_test.ruchy", "let x = 42\n");

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--bigo")
        .assert()
        .success()
        .stdout(
            predicate::str::contains("BigO")
                .or(predicate::str::contains("O("))
                .or(predicate::str::contains("complexity")),
        );
}

#[test]
fn cli_runtime_bigo_with_loop() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(
        &temp,
        "bigo_loop.ruchy",
        r"
for i in range(100) {
    let x = i * 2
}
",
    );

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--bigo")
        .assert()
        .success()
        .stdout(predicate::str::contains("O(").or(predicate::str::contains("complexity")));
}

#[test]
fn cli_runtime_bigo_with_nested_loops() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(
        &temp,
        "bigo_nested.ruchy",
        r"
for i in range(10) {
    for j in range(10) {
        let x = i * j
    }
}
",
    );

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--bigo")
        .assert()
        .success();
}

// ============================================================================
// CLI CONTRACT TESTS: BENCH OPTION
// ============================================================================

#[test]
fn cli_runtime_bench_flag() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(&temp, "bench_test.ruchy", "let x = 42\n");

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--bench")
        .assert()
        .success()
        .stdout(
            predicate::str::contains("bench")
                .or(predicate::str::contains("Bench"))
                .or(predicate::str::contains("statistical")),
        );
}

#[test]
fn cli_runtime_bench_with_computation() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(
        &temp,
        "bench_compute.ruchy",
        r"
let sum = 0
for i in range(100) {
    sum = sum + i
}
",
    );

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--bench")
        .assert()
        .success();
}

// ============================================================================
// CLI CONTRACT TESTS: COMPARE OPTION
// ============================================================================

#[test]
fn cli_runtime_compare_two_files() {
    let temp = TempDir::new().unwrap();
    let file1 = create_temp_file(&temp, "file1.ruchy", "let x = 42\n");
    let file2 = create_temp_file(&temp, "file2.ruchy", "let y = 100\n");

    ruchy_cmd()
        .arg("runtime")
        .arg(&file1)
        .arg("--compare")
        .arg(&file2)
        .assert()
        .success()
        .stdout(
            predicate::str::contains("compar")
                .or(predicate::str::contains("Compar"))
                .or(predicate::str::contains("vs")),
        );
}

#[test]
fn cli_runtime_compare_with_nonexistent_baseline() {
    let temp = TempDir::new().unwrap();
    let file1 = create_temp_file(&temp, "file1.ruchy", "let x = 42\n");

    // Note: Compare succeeds even with nonexistent baseline
    // This is by design - allows comparing against a baseline that will be created later
    ruchy_cmd()
        .arg("runtime")
        .arg(&file1)
        .arg("--compare")
        .arg("nonexistent.ruchy")
        .assert()
        .success()
        .stdout(
            predicate::str::contains("Comparison")
                .or(predicate::str::contains("Baseline"))
                .or(predicate::str::contains("faster")),
        );
}

// ============================================================================
// CLI CONTRACT TESTS: MEMORY OPTION
// ============================================================================

#[test]
fn cli_runtime_memory_flag() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(&temp, "memory_test.ruchy", "let x = 42\n");

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--memory")
        .assert()
        .success()
        .stdout(
            predicate::str::contains("memory")
                .or(predicate::str::contains("Memory"))
                .or(predicate::str::contains("allocation")),
        );
}

#[test]
fn cli_runtime_memory_with_allocations() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(
        &temp,
        "memory_alloc.ruchy",
        r"
let arr = [1, 2, 3, 4, 5]
let sum = 0
for i in arr {
    sum = sum + i
}
",
    );

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--memory")
        .assert()
        .success();
}

// ============================================================================
// CLI CONTRACT TESTS: COMBINED OPTIONS
// ============================================================================

#[test]
fn cli_runtime_profile_and_bigo() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(&temp, "combined1.ruchy", "let x = 42\n");

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--profile")
        .arg("--bigo")
        .assert()
        .success();
}

#[test]
fn cli_runtime_all_flags() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(&temp, "all_flags.ruchy", "let x = 42\n");

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--profile")
        .arg("--bigo")
        .arg("--bench")
        .arg("--memory")
        .assert()
        .success();
}

// ============================================================================
// CLI CONTRACT TESTS: OUTPUT FILE
// ============================================================================

#[test]
fn cli_runtime_output_to_file() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(&temp, "output_test.ruchy", "let x = 42\n");
    let output_file = temp.path().join("runtime_report.txt");

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--output")
        .arg(&output_file)
        .assert()
        .success();

    // Verify output file was created
    assert!(output_file.exists(), "Output file should be created");
}

#[test]
fn cli_runtime_output_with_bigo() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(&temp, "output_bigo.ruchy", "let x = 42\n");
    let output_file = temp.path().join("bigo_report.txt");

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--bigo")
        .arg("--output")
        .arg(&output_file)
        .assert()
        .success();

    assert!(output_file.exists(), "Output file should be created");
}

// ============================================================================
// CLI CONTRACT TESTS: VERBOSE MODE
// ============================================================================

#[test]
fn cli_runtime_verbose_flag() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(&temp, "verbose.ruchy", "let x = 42\n");

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--verbose")
        .assert()
        .success();
}

#[test]
fn cli_runtime_verbose_with_profile() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(&temp, "verbose_profile.ruchy", "let x = 42\n");

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--verbose")
        .arg("--profile")
        .assert()
        .success();
}

// ============================================================================
// CLI CONTRACT TESTS: ERROR MESSAGES
// ============================================================================

#[test]
fn cli_runtime_missing_file_writes_stderr() {
    ruchy_cmd()
        .arg("runtime")
        .arg("missing.ruchy")
        .assert()
        .failure()
        .stderr(
            predicate::str::contains("not found")
                .or(predicate::str::contains("No such file"))
                .or(predicate::str::contains("does not exist")),
        );
}

#[test]
fn cli_runtime_syntax_error_writes_stderr() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(&temp, "bad_syntax.ruchy", "fun f( { }\n");

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .assert()
        .failure()
        .stderr(predicate::str::is_empty().not()); // stderr NOT empty
}

// ============================================================================
// CLI CONTRACT TESTS: EDGE CASES
// ============================================================================

#[test]
fn cli_runtime_empty_file_fails() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(&temp, "empty.ruchy", "");

    // Empty files fail with "Parse error: Unexpected end of input"
    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .assert()
        .failure()
        .stderr(
            predicate::str::contains("Unexpected end of input")
                .or(predicate::str::contains("Parse error")),
        );
}

#[test]
fn cli_runtime_complex_program() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(
        &temp,
        "complex.ruchy",
        r"
fun factorial(n) {
    if n <= 1 {
        1
    } else {
        n * factorial(n - 1)
    }
}

let result = factorial(5)
println(result)
",
    );

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--bigo")
        .assert()
        .success();
}

#[test]
fn cli_runtime_recursive_function_bigo() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(
        &temp,
        "recursive.ruchy",
        r"
fun fib(n) {
    if n <= 1 {
        n
    } else {
        fib(n - 1) + fib(n - 2)
    }
}

fib(10)
",
    );

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--bigo")
        .assert()
        .success();
}

// ============================================================================
// CLI CONTRACT TESTS: HELP AND USAGE
// ============================================================================

#[test]
fn cli_runtime_help_flag() {
    ruchy_cmd()
        .arg("runtime")
        .arg("--help")
        .assert()
        .success()
        .stdout(
            predicate::str::contains("runtime")
                .or(predicate::str::contains("Runtime"))
                .or(predicate::str::contains("performance")),
        );
}

// ============================================================================
// CLI CONTRACT TESTS: PERFORMANCE SCENARIOS
// ============================================================================

#[test]
fn cli_runtime_constant_time_program() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(
        &temp,
        "constant.ruchy",
        r"
let x = 42
let y = x * 2
println(y)
",
    );

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--bigo")
        .assert()
        .success()
        .stdout(predicate::str::contains("O(1)").or(predicate::str::contains("constant")));
}

#[test]
fn cli_runtime_linear_time_program() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(
        &temp,
        "linear.ruchy",
        r"
for i in range(100) {
    println(i)
}
",
    );

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--bigo")
        .assert()
        .success()
        .stdout(predicate::str::contains("O(n)").or(predicate::str::contains("linear")));
}

#[test]
fn cli_runtime_quadratic_time_program() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_file(
        &temp,
        "quadratic.ruchy",
        r"
for i in range(10) {
    for j in range(10) {
        println(i * j)
    }
}
",
    );

    ruchy_cmd()
        .arg("runtime")
        .arg(&file)
        .arg("--bigo")
        .assert()
        .success()
        .stdout(predicate::str::contains("O(n").or(predicate::str::contains("quadratic")));
}