ruby-tools 0.0.0

Ruby language tools for Rusty Ruby
Documentation
//! testrb 测试运行器
//! 运行 Ruby 测试文件

use std::env;
use std::fs;
use std::path::Path;

fn main() {
    let args: Vec<String> = env::args().collect();
    let test_files: Vec<&str> = args[1..].iter().map(|s| s.as_str()).collect();

    println!("testrb - Ruby test runner");

    if test_files.is_empty() {
        println!("Usage: testrb <test_file1> <test_file2> ...");
        println!("Example: testrb test/test_example.rb");
        return;
    }

    run_tests(&test_files);
}

fn run_tests(test_files: &[&str]) {
    let mut total_tests = 0;
    let mut passed_tests = 0;

    for test_file in test_files {
        let path = Path::new(test_file);
        if path.exists() && path.is_file() {
            println!("Running tests in: {}", test_file);
            let (tests, passed) = run_test_file(path);
            total_tests += tests;
            passed_tests += passed;
        } else {
            println!("Error: {} does not exist or is not a file", test_file);
        }
    }

    println!("\nTest Summary:");
    println!("Total tests: {}", total_tests);
    println!("Passed: {}", passed_tests);
    println!("Failed: {}", total_tests - passed_tests);

    if passed_tests == total_tests {
        println!("\nAll tests passed!");
    } else {
        println!("\nSome tests failed.");
    }
}

fn run_test_file(test_file: &Path) -> (u32, u32) {
    // 模拟测试运行逻辑
    let file_name = test_file.file_name().unwrap_or_default().to_string_lossy();
    
    // 读取文件内容
    match fs::read_to_string(test_file) {
        Ok(content) => {
            println!("Processing test file: {}", file_name);
            // 简单的测试检测逻辑
            let test_count = content.matches("test_").count() as u32;
            let passed_count = test_count; // 模拟所有测试通过
            
            println!("  Tests found: {}", test_count);
            println!("  Tests passed: {}", passed_count);
            
            (test_count, passed_count)
        }
        Err(e) => {
            println!("Error reading test file: {:?}", e);
            (0, 0)
        }
    }
}