use cxx_gen::{generate_header_and_cc, Opt};
use std::str;
const BRIDGE0: &str = r#"
#[cxx::bridge]
mod ffi {
unsafe extern "C++" {
pub fn do_cpp_thing(foo: &str);
}
}
"#;
#[test]
fn test_extern_c_function() {
let opt = Opt::default();
let source = BRIDGE0.parse().unwrap();
let generated = generate_header_and_cc(source, &opt).unwrap();
let output = str::from_utf8(&generated.implementation).unwrap();
assert!(output.contains("void cxxbridge1$do_cpp_thing(::rust::Str foo)"));
}
#[test]
fn test_impl_annotation() {
let mut opt = Opt::default();
opt.cxx_impl_annotations = Some("ANNOTATION".to_owned());
let source = BRIDGE0.parse().unwrap();
let generated = generate_header_and_cc(source, &opt).unwrap();
let output = str::from_utf8(&generated.implementation).unwrap();
assert!(output.contains("ANNOTATION void cxxbridge1$do_cpp_thing(::rust::Str foo)"));
}
const BRIDGE1: &str = r#"
#[cxx::bridge]
mod ffi {
extern "C++" {
type CppType;
}
extern "Rust" {
fn rust_method_cpp_receiver(self: Pin<&mut CppType>);
}
}
"#;
#[test]
fn test_extern_rust_method_on_c_type() {
let opt = Opt::default();
let source = BRIDGE1.parse().unwrap();
let generated = generate_header_and_cc(source, &opt).unwrap();
let header = str::from_utf8(&generated.header).unwrap();
let implementation = str::from_utf8(&generated.implementation).unwrap();
assert!(!header.contains("rust_method_cpp_receiver"));
assert!(implementation
.contains("void cxxbridge1$CppType$rust_method_cpp_receiver(::CppType &self) noexcept;"));
assert!(implementation.contains("void CppType::rust_method_cpp_receiver() noexcept {"));
assert!(implementation.contains("cxxbridge1$CppType$rust_method_cpp_receiver(*this);"));
}