use super::{classify_param_type, emit_param_conversion};
use crate::core::ir::TypeRef;
#[test]
fn sync_pyo3_free_fn_releases_gil_around_core_call() {
use crate::codegen::generators::gen_function;
use crate::core::ir::{FunctionDef, ParamDef};
let func = FunctionDef {
name: "count_words".to_owned(),
rust_path: "sample_core::count_words".to_owned(),
params: vec![ParamDef {
name: "text".to_owned(),
ty: TypeRef::String,
optional: false,
default: None,
..ParamDef::default()
}],
return_type: TypeRef::Primitive(crate::core::ir::PrimitiveType::U64),
is_async: false,
error_type: None,
..FunctionDef::default()
};
let mapper = crate::backends::pyo3::type_map::Pyo3Mapper::new();
let cfg = crate::backends::pyo3::gen_bindings::config::binding_config("sample_core", true);
let adapter_bodies = ahash::AHashMap::new();
let opaque_types = ahash::AHashSet::new();
let output = gen_function(&func, &mapper, &cfg, &adapter_bodies, &opaque_types);
assert!(
output.contains("py: Python<'_>"),
"expected injected `py: Python<'_>` handle on sync free function:\n{output}"
);
assert!(
output.contains("py.detach(|| sample_core::count_words("),
"expected core call wrapped in `py.detach(|| ...)`:\n{output}"
);
}
#[test]
fn classify_param_type_returns_plain_for_named() {
let ty = TypeRef::Named("Foo".to_string());
let result = classify_param_type(&ty);
assert!(result.is_some());
let (name, _) = result.unwrap();
assert_eq!(name, "Foo");
}
#[test]
fn classify_param_type_returns_none_for_primitive() {
let ty = TypeRef::Primitive(crate::core::ir::PrimitiveType::Bool);
assert!(classify_param_type(&ty).is_none());
}
#[test]
fn emit_param_conversion_guards_optional() {
let mut out = String::new();
emit_param_conversion(&mut out, "_rust_x", "x", "convert(x)", true);
assert!(out.contains("if x is not None else None"));
}
#[test]
fn emit_param_conversion_direct_when_required() {
let mut out = String::new();
emit_param_conversion(&mut out, "_rust_x", "x", "convert(x)", false);
assert!(!out.contains("if x is not None"));
assert!(out.contains("_rust_x = convert(x)"));
}
#[test]
fn async_pyo3_functions_place_bindings_inside_async_block() {
}