Skip to content

Commit

Permalink
move test to codegen_test
Browse files Browse the repository at this point in the history
  • Loading branch information
NiwakaDev committed Aug 8, 2024
1 parent 889b4cd commit b4dade6
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,115 @@ typedef struct MyType MyType;
.test();
}
}

/// Verify that we generated a Swift class with a failable init method.
mod extern_rust_class_with_failable_init {
use super::*;

fn bridge_module_tokens() -> TokenStream {
quote! {
mod foo {
extern "Rust" {
type Foo;

#[swift_bridge(init)]
fn new() -> Option<Foo>;
}
}
}
}

fn expected_rust_tokens() -> ExpectedRustTokens {
ExpectedRustTokens::Contains(quote! {
# [export_name = "__swift_bridge__$Foo$new"]
pub extern "C" fn __swift_bridge__Foo_new () -> * mut super :: Foo {
if let Some (val) = super :: Foo :: new () {
Box :: into_raw (Box :: new (val))
} else {
std :: ptr :: null_mut ()
}
}
})
}

const EXPECTED_SWIFT: ExpectedSwiftCode = ExpectedSwiftCode::ContainsAfterTrim(
r#"
public class Foo: FooRefMut {
var isOwned: Bool = true
public override init(ptr: UnsafeMutableRawPointer) {
super.init(ptr: ptr)
}
deinit {
if isOwned {
__swift_bridge__$Foo$_free(ptr)
}
}
}
extension Foo {
public convenience init?() {
guard let val = __swift_bridge__$Foo$new() else { return nil }; self.init(ptr: val)
}
}
"#,
);

const EXPECTED_C_HEADER: ExpectedCHeader = ExpectedCHeader::ContainsAfterTrim(
r#"
void* __swift_bridge__$Foo$new(void);
"#,
);

#[test]
fn extern_rust_class_with_failable_init() {
CodegenTest {
bridge_module: bridge_module_tokens().into(),
expected_rust_tokens: expected_rust_tokens(),
expected_swift_code: EXPECTED_SWIFT,
expected_c_header: EXPECTED_C_HEADER,
}
.test();
}
}

/***
#[test]
fn class_with_failable_init() {
let tokens = quote! {
mod foo {
extern "Rust" {
type Foo;
#[swift_bridge(init)]
fn new() -> Option<Foo>;
}
}
};
let module: SwiftBridgeModule = parse_quote!(#tokens);
let generated = module.generate_swift(&CodegenConfig::no_features_enabled());
let expected = r#"
public class Foo: FooRefMut {
var isOwned: Bool = true
public override init(ptr: UnsafeMutableRawPointer) {
super.init(ptr: ptr)
}
deinit {
if isOwned {
__swift_bridge__$Foo$_free(ptr)
}
}
}
extension Foo {
public convenience init?() {
guard let val = __swift_bridge__$Foo$new() else { return nil }; self.init(ptr: val)
}
}
"#;
assert_trimmed_generated_contains_trimmed_expected(&generated, expected);
}
***/
40 changes: 0 additions & 40 deletions crates/swift-bridge-ir/src/codegen/generate_swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,46 +606,6 @@ func __swift_bridge__Foo_new (_ a: UInt8) -> UnsafeMutableRawPointer {
assert_trimmed_generated_contains_trimmed_expected(&generated, &expected);
}

/// Verify that we generated a Swift class with a failable init method.
#[test]
fn class_with_failable_init() {
let tokens = quote! {
mod foo {
extern "Rust" {
type Foo;

#[swift_bridge(init)]
fn new() -> Option<Foo>;
}
}
};
let module: SwiftBridgeModule = parse_quote!(#tokens);
let generated = module.generate_swift(&CodegenConfig::no_features_enabled());

let expected = r#"
public class Foo: FooRefMut {
var isOwned: Bool = true
public override init(ptr: UnsafeMutableRawPointer) {
super.init(ptr: ptr)
}
deinit {
if isOwned {
__swift_bridge__$Foo$_free(ptr)
}
}
}
extension Foo {
public convenience init?() {
guard let val = __swift_bridge__$Foo$new() else { return nil }; self.init(ptr: val)
}
}
"#;

assert_trimmed_generated_contains_trimmed_expected(&generated, expected);
}

/// Verify that we generated a Swift class with an init method.
#[test]
fn class_with_init() {
Expand Down

0 comments on commit b4dade6

Please sign in to comment.