From 55ab89cf307ef40e837950ca797f061e79fa3cee Mon Sep 17 00:00:00 2001 From: NiwakaDev Date: Fri, 9 Aug 2024 00:20:26 +0900 Subject: [PATCH] move test to codegen_test --- .../opaque_rust_type_codegen_tests.rs | 71 +++++++++++++++++++ .../src/codegen/generate_swift.rs | 40 ----------- 2 files changed, 71 insertions(+), 40 deletions(-) diff --git a/crates/swift-bridge-ir/src/codegen/codegen_tests/opaque_rust_type_codegen_tests.rs b/crates/swift-bridge-ir/src/codegen/codegen_tests/opaque_rust_type_codegen_tests.rs index fcb4420c..2d2a6f03 100644 --- a/crates/swift-bridge-ir/src/codegen/codegen_tests/opaque_rust_type_codegen_tests.rs +++ b/crates/swift-bridge-ir/src/codegen/codegen_tests/opaque_rust_type_codegen_tests.rs @@ -456,3 +456,74 @@ 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; + } + } + } + } + + 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(); + } +} diff --git a/crates/swift-bridge-ir/src/codegen/generate_swift.rs b/crates/swift-bridge-ir/src/codegen/generate_swift.rs index c8fbbe39..491cd818 100644 --- a/crates/swift-bridge-ir/src/codegen/generate_swift.rs +++ b/crates/swift-bridge-ir/src/codegen/generate_swift.rs @@ -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; - } - } - }; - 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() {