use splice::ingest::python::{extract_python_symbols, PythonSymbolKind};
use std::path::Path;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_simple_function() {
let source = b"def foo():\n pass\n";
let symbols =
extract_python_symbols(Path::new("/tmp/test.py"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 1);
assert_eq!(symbols[0].name, "foo");
assert_eq!(symbols[0].kind, PythonSymbolKind::Function);
assert_eq!(symbols[0].module_path, "module"); assert!(!symbols[0].is_async);
}
#[test]
fn test_extract_class_definition() {
let source = b"class Bar:\n pass\n";
let symbols =
extract_python_symbols(Path::new("/tmp/test.py"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 1);
assert_eq!(symbols[0].name, "Bar");
assert_eq!(symbols[0].kind, PythonSymbolKind::Class);
}
#[test]
fn test_extract_class_with_method() {
let source = b"class Baz:\n def method(self):\n pass\n";
let symbols =
extract_python_symbols(Path::new("/tmp/test.py"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 2);
assert_eq!(symbols[0].name, "Baz");
assert_eq!(symbols[0].kind, PythonSymbolKind::Class);
assert_eq!(symbols[1].name, "method");
assert_eq!(symbols[1].kind, PythonSymbolKind::Function);
assert_eq!(symbols[1].module_path, "module::Baz");
}
#[test]
fn test_extract_async_function() {
let source = b"async def async_func():\n pass\n";
let symbols =
extract_python_symbols(Path::new("/tmp/test.py"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 1);
assert_eq!(symbols[0].name, "async_func");
assert_eq!(symbols[0].kind, PythonSymbolKind::Function);
assert!(symbols[0].is_async);
}
#[test]
fn test_extract_function_with_parameters() {
let source = b"def greet(name: str) -> str:\n return name\n";
let symbols =
extract_python_symbols(Path::new("/tmp/test.py"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 1);
assert_eq!(symbols[0].name, "greet");
assert_eq!(symbols[0].kind, PythonSymbolKind::Function);
assert_eq!(symbols[0].parameters, vec!["name"]);
}
#[test]
fn test_extract_multiple_functions() {
let source = b"def foo():\n pass\n\ndef bar():\n pass\n";
let symbols =
extract_python_symbols(Path::new("/tmp/test.py"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 2);
assert_eq!(symbols[0].name, "foo");
assert_eq!(symbols[1].name, "bar");
}
#[test]
fn test_extract_nested_class_with_method() {
let source =
b"class Outer:\n class Inner:\n def method(self):\n pass\n";
let symbols =
extract_python_symbols(Path::new("/tmp/test.py"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 3);
assert_eq!(symbols[0].name, "Outer");
assert_eq!(symbols[0].kind, PythonSymbolKind::Class);
assert_eq!(symbols[1].name, "Inner");
assert_eq!(symbols[1].kind, PythonSymbolKind::Class);
assert_eq!(symbols[1].module_path, "module::Outer");
assert_eq!(symbols[2].name, "method");
assert_eq!(symbols[2].kind, PythonSymbolKind::Function);
assert_eq!(symbols[2].module_path, "module::Outer::Inner");
}
#[test]
fn test_symbol_has_byte_span() {
let source = b"def foo(): pass\n";
let symbols =
extract_python_symbols(Path::new("/tmp/test.py"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 1);
assert!(symbols[0].byte_start < symbols[0].byte_end);
assert_eq!(symbols[0].byte_start, 0); }
#[test]
fn test_extract_class_with_multiple_methods() {
let source = b"class Calculator:\n def add(self, a, b): return a + b\n def subtract(self, a, b): return a - b\n";
let symbols =
extract_python_symbols(Path::new("/tmp/test.py"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 3);
assert_eq!(symbols[0].name, "Calculator");
assert_eq!(symbols[1].name, "add");
assert_eq!(symbols[2].name, "subtract");
assert_eq!(symbols[1].module_path, "module::Calculator");
assert_eq!(symbols[2].module_path, "module::Calculator");
}
}