#[cfg(test)]
mod tests {
use debtmap::context::{detect_file_type, detector::ContextDetector, FileType, FunctionRole};
use std::path::Path;
use syn::visit::Visit;
#[test]
fn test_context_detector_identifies_test_functions() {
let code = r#"
#[test]
fn test_something() {
assert_eq!(1, 1);
}
fn regular_function() {
println!("hello");
}
"#;
let file = syn::parse_file(code).unwrap();
let mut detector = ContextDetector::new(FileType::Test);
detector.visit_file(&file);
let test_ctx = detector.get_context("test_something");
assert!(test_ctx.is_some(), "Test function should be detected");
assert_eq!(test_ctx.unwrap().role, FunctionRole::TestFunction);
let regular_ctx = detector.get_context("regular_function");
assert!(regular_ctx.is_some(), "Regular function should be detected");
assert_ne!(regular_ctx.unwrap().role, FunctionRole::TestFunction);
}
#[test]
fn test_line_based_context_lookup() {
let code = r#"
fn func_at_line_2() {
println!("line 3");
}
#[test]
fn test_at_line_6() {
assert!(true);
}
"#;
let file = syn::parse_file(code).unwrap();
let mut detector = ContextDetector::new(FileType::Test);
detector.visit_file(&file);
let ctx_at_3 = detector.get_context_for_line(3);
let ctx_at_7 = detector.get_context_for_line(7);
println!("Context at line 3: {:?}", ctx_at_3.map(|c| &c.role));
println!("Context at line 7: {:?}", ctx_at_7.map(|c| &c.role));
}
#[test]
fn test_file_type_detection() {
assert_eq!(
detect_file_type(Path::new("project/tests/integration.rs")),
FileType::Test
);
assert_eq!(
detect_file_type(Path::new("src/main.rs")),
FileType::Production
);
assert_eq!(
detect_file_type(Path::new("something_test.rs")),
FileType::Test
);
assert_eq!(
detect_file_type(Path::new("module_tests.rs")),
FileType::Test
);
assert_eq!(
detect_file_type(Path::new("project/benches/bench.rs")),
FileType::Benchmark
);
assert_eq!(
detect_file_type(Path::new("project/examples/demo.rs")),
FileType::Example
);
}
#[test]
fn test_context_aware_environment_variable() {
std::env::set_var("DEBTMAP_CONTEXT_AWARE", "true");
let is_aware = std::env::var("DEBTMAP_CONTEXT_AWARE")
.map(|v| v == "true")
.unwrap_or(false);
assert!(is_aware, "Environment variable should be set");
std::env::remove_var("DEBTMAP_CONTEXT_AWARE");
let is_aware_after = std::env::var("DEBTMAP_CONTEXT_AWARE")
.map(|v| v == "true")
.unwrap_or(false);
assert!(!is_aware_after, "Environment variable should be removed");
}
}