use super::{CreateSpec, QualityProfile};
use anyhow::Result;
pub struct TestGenerator {
pub(crate) profile: QualityProfile,
}
impl TestGenerator {
#[must_use]
pub fn new(profile: QualityProfile) -> Self {
Self { profile }
}
pub fn generate_for_function(&self, _code: &str, spec: &CreateSpec) -> Result<String> {
let mut tests = String::new();
tests.push_str("#[cfg(test)]\nmod tests {\n use super::*;\n\n");
tests.push_str(&format!(
" #[test]\n fn test_{}_basic() {{\n",
spec.name
));
tests.push_str(" // Test basic functionality\n");
tests.push_str(&format!(" let result = {}(", spec.name));
for (i, param) in spec.inputs.iter().enumerate() {
if i > 0 {
tests.push_str(", ");
}
tests.push_str(&self.generate_test_value(¶m.param_type));
}
tests.push_str(");\n");
tests.push_str(" assert!(result.is_ok());\n");
tests.push_str(" }\n\n");
tests.push_str(&format!(
" #[test]\n fn test_{}_edge_cases() {{\n",
spec.name
));
tests.push_str(" // Test edge cases and error conditions\n");
tests.push_str(" // Add specific edge case tests here\n");
tests.push_str(" }\n\n");
if self.profile.thresholds.require_property_tests {
tests.push_str(" use proptest::prelude::*;\n\n");
tests.push_str(&format!(
" proptest! {{\n #[test]\n fn prop_{}_always_valid(\n",
spec.name
));
for param in &spec.inputs {
tests.push_str(&format!(
" {} in any::<{}>(),\n",
param.name, param.param_type
));
}
tests.push_str(" ) {\n");
tests.push_str(&format!(" let result = {}(", spec.name));
for (i, param) in spec.inputs.iter().enumerate() {
if i > 0 {
tests.push_str(", ");
}
tests.push_str(¶m.name);
}
tests.push_str(");\n");
tests.push_str(
" // Property: function should handle all inputs gracefully\n",
);
tests.push_str(" // Add property assertions here\n");
tests.push_str(" }\n }\n\n");
}
tests.push_str("}\n");
Ok(tests)
}
pub fn generate_test_value(&self, param_type: &str) -> String {
match param_type {
"u32" | "i32" => "42".to_string(),
"f32" | "f64" => "3.14".to_string(),
"String" => "\"test\".to_string()".to_string(),
"&str" => "\"test\"".to_string(),
"bool" => "true".to_string(),
_ => "Default::default()".to_string(),
}
}
pub fn generate_tests(&self, _code: &str) -> Result<String> {
let mut tests = String::new();
tests.push_str("#[cfg(test)]\nmod tests {\n use super::*;\n\n");
tests.push_str(" #[test]\n fn test_generated_code() {\n");
tests.push_str(" // Auto-generated test for code quality\n");
tests.push_str(" assert!(true); // Placeholder\n");
tests.push_str(" }\n");
if self.profile.thresholds.require_property_tests {
tests.push_str("\n #[cfg(feature = \"property-testing\")]\n");
tests.push_str(" mod property_tests {\n");
tests.push_str(" use super::*;\n");
tests.push_str(" use proptest::prelude::*;\n\n");
tests.push_str(" proptest! {\n");
tests.push_str(" #[test]\n");
tests.push_str(" fn property_test_code(input in any::<u32>()) {\n");
tests.push_str(" // Property-based test\n");
tests.push_str(" assert!(input == input);\n");
tests.push_str(" }\n");
tests.push_str(" }\n");
tests.push_str(" }\n");
}
tests.push_str("}\n");
Ok(tests)
}
}