yufmath 0.1.1

A Rust CAS Lib.
Documentation
//! # 增强 CLI 功能测试
//!
//! 测试新增的命令行工具功能。

use std::fs;
use std::process::Command;
use tempfile::NamedTempFile;

/// 测试帮助信息显示
#[test]
fn test_help_display() {
    let output = Command::new("cargo")
        .args(&["run", "--", "--help"])
        .current_dir(".")
        .output()
        .expect("Failed to execute command");
    
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    
    // 帮助信息可能在 stdout 或 stderr 中
    let help_text = if stdout.contains("计算机代数系统") {
        stdout.as_ref()
    } else {
        stderr.as_ref()
    };
    
    // 验证帮助信息包含关键内容
    assert!(help_text.contains("计算机代数系统"));
    assert!(help_text.contains("compute"));
    assert!(help_text.contains("simplify"));
    assert!(help_text.contains("diff"));
    assert!(help_text.contains("integrate"));
    assert!(help_text.contains("solve"));
    assert!(help_text.contains("factor"));
    assert!(help_text.contains("expand"));
    assert!(help_text.contains("batch"));
    assert!(help_text.contains("interactive"));
}

/// 测试版本信息显示
#[test]
fn test_version_display() {
    let output = Command::new("cargo")
        .args(&["run", "--", "--version"])
        .current_dir(".")
        .output()
        .expect("Failed to execute command");
    
    let stdout = String::from_utf8_lossy(&output.stdout);
    
    // 验证版本信息
    assert!(stdout.contains("yufmath"));
    assert!(stdout.contains("0.1.0"));
}

/// 测试新增命令的基本功能
#[test]
fn test_new_commands_basic() {
    // 测试 solve 命令
    let output = Command::new("cargo")
        .args(&["run", "--", "solve", "x^2 - 4 = 0", "x"])
        .current_dir(".")
        .output()
        .expect("Failed to execute solve command");
    
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("求解功能暂未实现") || stdout.contains("错误"));
    
    // 测试 factor 命令
    let output = Command::new("cargo")
        .args(&["run", "--", "factor", "x^2 - 4"])
        .current_dir(".")
        .output()
        .expect("Failed to execute factor command");
    
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("因式分解功能暂未实现") || stdout.contains("错误"));
    
    // 测试 expand 命令
    let output = Command::new("cargo")
        .args(&["run", "--", "expand", "(x+1)^2"])
        .current_dir(".")
        .output()
        .expect("Failed to execute expand command");
    
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("展开功能暂未实现") || stdout.contains("错误"));
}

/// 测试批处理功能
#[test]
fn test_batch_processing() {
    // 创建临时输入文件
    let mut input_file = NamedTempFile::new().expect("Failed to create temp file");
    let input_content = r#"# 这是注释行
// 这也是注释行

2 + 3
5 * 6
10 / 2
"#;
    
    fs::write(input_file.path(), input_content).expect("Failed to write to temp file");
    
    // 创建临时输出文件
    let output_file = NamedTempFile::new().expect("Failed to create output temp file");
    
    // 执行批处理命令
    let output = Command::new("cargo")
        .args(&[
            "run", "--", "batch",
            "-i", input_file.path().to_str().unwrap(),
            "-o", output_file.path().to_str().unwrap()
        ])
        .current_dir(".")
        .output()
        .expect("Failed to execute batch command");
    
    // 检查命令是否成功执行
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        println!("Batch command failed: {}", stderr);
        // 由于解析器可能未完全实现,我们只检查命令是否能正常启动
        assert!(stderr.contains("解析") || stderr.contains("错误") || stderr.contains("未实现"));
    }
    
    // 检查输出文件是否被创建
    assert!(output_file.path().exists());
}

/// 测试详细模式
#[test]
fn test_verbose_mode() {
    let output = Command::new("cargo")
        .args(&["run", "--", "--verbose", "compute", "1 + 1"])
        .current_dir(".")
        .output()
        .expect("Failed to execute verbose command");
    
    let stdout = String::from_utf8_lossy(&output.stdout);
    
    // 验证详细模式输出
    assert!(stdout.contains("Yufmath v") || stdout.contains("详细模式") || stdout.contains("正在计算"));
}

/// 测试静默模式
#[test]
fn test_quiet_mode() {
    let output = Command::new("cargo")
        .args(&["run", "--", "--quiet", "compute", "1 + 1"])
        .current_dir(".")
        .output()
        .expect("Failed to execute quiet command");
    
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    
    // 静默模式下应该减少输出(除非有错误)
    // 由于解析器可能未实现,我们主要检查命令能正常执行
    println!("Quiet mode stdout: {}", stdout);
    println!("Quiet mode stderr: {}", stderr);
}

/// 测试格式选项
#[test]
fn test_format_options() {
    // 测试 LaTeX 格式
    let output = Command::new("cargo")
        .args(&["run", "--", "--format", "latex", "compute", "x^2"])
        .current_dir(".")
        .output()
        .expect("Failed to execute latex format command");
    
    // 由于解析器可能未实现,我们主要检查命令能正常执行
    assert!(output.status.success() || output.status.code() == Some(1));
    
    // 测试 MathML 格式
    let output = Command::new("cargo")
        .args(&["run", "--", "--format", "mathml", "compute", "x^2"])
        .current_dir(".")
        .output()
        .expect("Failed to execute mathml format command");
    
    assert!(output.status.success() || output.status.code() == Some(1));
}

/// 测试精度选项
#[test]
fn test_precision_option() {
    let output = Command::new("cargo")
        .args(&["run", "--", "--precision", "5", "compute", "3.14159265"])
        .current_dir(".")
        .output()
        .expect("Failed to execute precision command");
    
    // 检查命令能正常执行
    assert!(output.status.success() || output.status.code() == Some(1));
}

/// 测试进度条选项
#[test]
fn test_progress_options() {
    // 测试启用进度条
    let output = Command::new("cargo")
        .args(&["run", "--", "--progress", "compute", "1 + 1"])
        .current_dir(".")
        .output()
        .expect("Failed to execute progress command");
    
    assert!(output.status.success() || output.status.code() == Some(1));
    
    // 测试禁用进度条
    let output = Command::new("cargo")
        .args(&["run", "--", "--no-progress", "compute", "1 + 1"])
        .current_dir(".")
        .output()
        .expect("Failed to execute no-progress command");
    
    assert!(output.status.success() || output.status.code() == Some(1));
}

/// 测试超时选项
#[test]
fn test_timeout_option() {
    let output = Command::new("cargo")
        .args(&["run", "--", "--timeout", "10", "compute", "1 + 1"])
        .current_dir(".")
        .output()
        .expect("Failed to execute timeout command");
    
    // 检查命令能正常执行
    assert!(output.status.success() || output.status.code() == Some(1));
}

/// 测试无命令时的帮助显示
#[test]
fn test_no_command_help() {
    let output = Command::new("cargo")
        .args(&["run"])
        .current_dir(".")
        .output()
        .expect("Failed to execute no command");
    
    let stdout = String::from_utf8_lossy(&output.stdout);
    
    // 验证显示帮助信息
    assert!(stdout.contains("Yufmath") && stdout.contains("用法"));
}

#[cfg(test)]
mod integration_tests {
    use super::*;
    
    /// 测试完整的工作流程
    #[test]
    fn test_complete_workflow() {
        // 创建包含多种表达式的批处理文件
        let mut input_file = NamedTempFile::new().expect("Failed to create temp file");
        let input_content = r#"# 数学表达式测试文件
# 基本算术
1 + 2 + 3
4 * 5 * 6

# 注释应该被忽略
// 这行也应该被忽略

# 更复杂的表达式(可能会失败,但不应该崩溃)
x^2 + 2*x + 1
sin(pi/2)
"#;
        
        fs::write(input_file.path(), input_content).expect("Failed to write to temp file");
        
        // 使用详细模式和进度条执行批处理
        let output = Command::new("cargo")
            .args(&[
                "run", "--", 
                "--verbose", 
                "--progress",
                "--format", "standard",
                "batch",
                "-i", input_file.path().to_str().unwrap()
            ])
            .current_dir(".")
            .output()
            .expect("Failed to execute complete workflow");
        
        // 验证命令执行(可能有错误,但不应该崩溃)
        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);
        
        println!("Complete workflow stdout: {}", stdout);
        println!("Complete workflow stderr: {}", stderr);
        
        // 主要验证程序没有崩溃
        assert!(output.status.code().is_some());
    }
}