From 992d03e66b378169ce96ea6f78174ab37d9fedbb Mon Sep 17 00:00:00 2001 From: Eric Saxby Date: Thu, 20 Jun 2024 11:44:26 -0700 Subject: [PATCH] Add integration test for returning Result<(), TransparentStruct> to Swift --- .../Result.swift | 3 ++ .../ResultTests.swift | 40 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunner/Result.swift b/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunner/Result.swift index 94ae5aa9..3549fb6f 100644 --- a/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunner/Result.swift +++ b/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunner/Result.swift @@ -35,6 +35,9 @@ extension AsyncResultOpaqueRustType2: Error {} extension ResultTransparentEnum: @unchecked Sendable {} extension ResultTransparentEnum: Error {} +extension ResultTransparentStruct: @unchecked Sendable {} +extension ResultTransparentStruct: Error {} + extension SameEnum: @unchecked Sendable {} extension SameEnum: Error {} diff --git a/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/ResultTests.swift b/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/ResultTests.swift index 003bde5c..17728902 100644 --- a/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/ResultTests.swift +++ b/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/ResultTests.swift @@ -193,6 +193,46 @@ class ResultTests: XCTestCase { } } + /// Verify that we can receive a Result<(), TransparentStruct> from Rust + func testResultNullTransparentStruct() throws { + try! rust_func_return_result_null_transparent_struct(true) + + do { + try rust_func_return_result_null_transparent_struct(false) + XCTFail("The function should have returned an error.") + } catch let error as ResultTransparentStruct { + XCTAssertEqual(error.inner.toString(), "failed") + } + + XCTContext.runActivity(named: "Should return a Unit type") { + _ in + do { + let _ :() = try rust_func_return_result_unit_type_enum_opaque_rust(true) + } catch { + XCTFail() + } + } + + XCTContext.runActivity(named: "Should throw an error") { + _ in + do { + let _ :() = try rust_func_return_result_unit_type_enum_opaque_rust(false) + XCTFail("The function should have returned an error.") + } catch let error as ResultTransparentEnum { + switch error { + case .NamedField(let data): + XCTAssertEqual(data, 123) + case .UnnamedFields(_, _): + XCTFail() + case .NoFields: + XCTFail() + } + } catch { + XCTFail() + } + } + } + /// Verify that we can receive a Result, OpaqueRust> from Rust func testSwiftCallRustResultVecUInt32Rust() throws { let vec = try! rust_func_return_result_of_vec_u32()