use assert_cmd::Command;
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;
#[allow(deprecated)]
pub fn decy_cmd() -> Command {
Command::cargo_bin("decy").expect("Failed to find decy binary")
}
pub fn create_temp_file(dir: &TempDir, name: &str, content: &str) -> PathBuf {
let path = dir.path().join(name);
fs::write(&path, content).expect("Failed to write temp file");
path
}
pub fn create_temp_dir_with_files(files: &[(&str, &str)]) -> TempDir {
let dir = TempDir::new().expect("Failed to create temp dir");
for (name, content) in files {
create_temp_file(&dir, name, content);
}
dir
}
#[allow(dead_code)]
pub const VALID_C_CODE: &str = r#"
int main() {
return 0;
}
"#;
#[allow(dead_code)]
pub const INVALID_C_CODE: &str = r#"
int main( {
return 0;
}
"#;
#[allow(dead_code)]
pub const C_WITH_FUNCTION: &str = r#"
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(2, 3);
return result;
}
"#;
#[allow(dead_code)]
pub const C_WITH_POINTERS: &str = r#"
void increment(int* ptr) {
(*ptr)++;
}
int main() {
int x = 5;
increment(&x);
return x;
}
"#;
#[allow(dead_code)]
pub const C_WITH_MALLOC: &str = r#"
#include <stdlib.h>
int main() {
int* ptr = (int*)malloc(sizeof(int));
*ptr = 42;
free(ptr);
return 0;
}
"#;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_decy_cmd_exists() {
let _cmd = decy_cmd();
}
#[test]
fn test_create_temp_file() {
let dir = TempDir::new().unwrap();
let path = create_temp_file(&dir, "test.c", "int main() { return 0; }");
assert!(path.exists());
let content = fs::read_to_string(&path).unwrap();
assert!(content.contains("int main"));
}
#[test]
fn test_create_temp_dir_with_files() {
let files = vec![("file1.c", "int x = 1;"), ("file2.c", "int y = 2;")];
let dir = create_temp_dir_with_files(&files);
assert!(dir.path().join("file1.c").exists());
assert!(dir.path().join("file2.c").exists());
}
}