use tempfile::TempDir;
#[test]
fn test_copyright_single_line_not_detected() {
let project = create_cpp_file_with_copyright();
let result = analyze_cpp_file_for_functions(project.path());
assert!(result.is_ok(), "Analysis should succeed");
let functions = result.unwrap();
assert!(
!functions.iter().any(|f| f.contains("Copyright")),
"Copyright should not be detected as function, found: {:?}",
functions
);
}
#[test]
#[ignore = "RED phase test - BUG-009 implementation incomplete"]
fn test_real_functions_still_detected_with_copyright() {
let project = create_cpp_file_with_copyright_and_functions();
let result = analyze_cpp_file_for_functions(project.path());
assert!(result.is_ok(), "Analysis should succeed");
let functions = result.unwrap();
assert!(
functions.iter().any(|f| f.contains("initialize_wmi")),
"Real function 'initialize_wmi' should be detected"
);
assert!(
functions.iter().any(|f| f.contains("process_data")),
"Real function 'process_data' should be detected"
);
assert!(
!functions.iter().any(|f| f.contains("Copyright")),
"Copyright should not be detected as function"
);
}
#[test]
fn test_multiline_copyright_not_detected() {
let project = create_cpp_file_with_multiline_copyright();
let result = analyze_cpp_file_for_functions(project.path());
assert!(result.is_ok(), "Analysis should succeed");
let functions = result.unwrap();
assert!(
!functions.iter().any(|f| f.contains("Copyright")),
"Copyright in multiline comment should not be detected"
);
}
#[test]
fn test_license_headers_not_detected() {
let project = create_cpp_file_with_license_headers();
let result = analyze_cpp_file_for_functions(project.path());
assert!(result.is_ok(), "Analysis should succeed");
let functions = result.unwrap();
let banned_keywords = vec!["Copyright", "License", "Author", "SPDX"];
for keyword in &banned_keywords {
assert!(
!functions.iter().any(|f| f.contains(keyword)),
"{} should not be detected as function",
keyword
);
}
}
#[test]
fn test_c_file_copyright_not_detected() {
let project = create_c_file_with_copyright();
let result = analyze_c_file_for_functions(project.path());
assert!(result.is_ok(), "Analysis should succeed");
let functions = result.unwrap();
assert!(
!functions.iter().any(|f| f.contains("Copyright")),
"Copyright in C file should not be detected"
);
}
fn create_cpp_file_with_copyright() -> TempDir {
use std::fs;
let temp = TempDir::new().unwrap();
let code = r#"
// Copyright (c) 2024 Test Organization
// All rights reserved
#include <iostream>
// This file contains test code
"#;
fs::write(temp.path().join("test.cpp"), code).unwrap();
temp
}
fn create_cpp_file_with_copyright_and_functions() -> TempDir {
use std::fs;
let temp = TempDir::new().unwrap();
let code = r#"
// Copyright (c) 2024 Test Organization
// All rights reserved
#include <iostream>
void initialize_wmi() {
std::cout << "Initializing WMI" << std::endl;
}
int process_data(int input) {
return input * 2;
}
"#;
fs::write(temp.path().join("test.cpp"), code).unwrap();
temp
}
fn create_cpp_file_with_multiline_copyright() -> TempDir {
use std::fs;
let temp = TempDir::new().unwrap();
let code = r#"
/*
* Copyright (c) 2024 Test Organization
* All rights reserved
* Licensed under MIT
*/
#include <iostream>
void real_function() {
// actual code
}
"#;
fs::write(temp.path().join("test.cpp"), code).unwrap();
temp
}
fn create_cpp_file_with_license_headers() -> TempDir {
use std::fs;
let temp = TempDir::new().unwrap();
let code = r#"
// Copyright (c) 2024
// License: MIT
// Author: Test Developer
// SPDX-License-Identifier: MIT
void real_function() {
// actual code
}
"#;
fs::write(temp.path().join("test.cpp"), code).unwrap();
temp
}
fn create_c_file_with_copyright() -> TempDir {
use std::fs;
let temp = TempDir::new().unwrap();
let code = r#"
// Copyright (c) 2024 Test Organization
#include <stdio.h>
void test_function() {
printf("Hello\n");
}
"#;
fs::write(temp.path().join("test.c"), code).unwrap();
temp
}
fn analyze_cpp_file_for_functions(path: &std::path::Path) -> Result<Vec<String>, String> {
#[cfg(feature = "cpp-ast")]
{
use pmat::services::ast::languages::cpp::analyze_cpp_file;
let rt = tokio::runtime::Runtime::new().map_err(|e| e.to_string())?;
let file_path = path.join("test.cpp");
let context = rt
.block_on(analyze_cpp_file(&file_path))
.map_err(|e| e.to_string())?;
let functions: Vec<String> = context
.items
.iter()
.filter_map(|item| {
if let pmat::services::context::AstItem::Function { name, .. } = item {
Some(name.clone())
} else {
None
}
})
.collect();
Ok(functions)
}
#[cfg(not(feature = "cpp-ast"))]
{
Ok(vec![])
}
}
fn analyze_c_file_for_functions(path: &std::path::Path) -> Result<Vec<String>, String> {
#[cfg(feature = "c-ast")]
{
use pmat::services::ast::languages::c::analyze_c_file;
let rt = tokio::runtime::Runtime::new().map_err(|e| e.to_string())?;
let file_path = path.join("test.c");
let context = rt
.block_on(analyze_c_file(&file_path))
.map_err(|e| e.to_string())?;
let functions: Vec<String> = context
.items
.iter()
.filter_map(|item| {
if let pmat::services::context::AstItem::Function { name, .. } = item {
Some(name.clone())
} else {
None
}
})
.collect();
Ok(functions)
}
#[cfg(not(feature = "c-ast"))]
{
Ok(vec![])
}
}