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);
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() {
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("错误"));
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("错误"));
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() {
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));
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());
}
}