@@ -13,14 +13,30 @@ class ResultTests: XCTestCase {
13
13
rust_func_takes_result_string ( . Ok( " Success Message " ) )
14
14
rust_func_takes_result_string ( . Err( " Error Message " ) )
15
15
}
16
-
16
+
17
+ /// Verify that we can return a Result<String, String> from Rust -> Swift.
18
+ ///
19
+ /// The Err case evidences Swift’s `Error` protocol is implemented correctly
20
+ /// for `RustStringRef`, i.e. `extension RustStringRef: Error {}`
21
+ func testSwiftCallRustReturnsResultString( ) throws {
22
+ let resultOk = try ! rust_func_returns_result_string ( true )
23
+ XCTAssertEqual ( resultOk. toString ( ) , " Success Message " )
24
+
25
+ do {
26
+ let _ = try rust_func_returns_result_string ( false )
27
+ XCTFail ( " The function should have returned an error. " )
28
+ } catch let error as RustString {
29
+ XCTAssertEqual ( error. toString ( ) , " Error Message " )
30
+ }
31
+ }
32
+
17
33
/// Verify that we can pass a Result<OpaqueRust, OpaqueRust> from Swift -> Rust
18
34
func testSwiftCallRustResultOpaqueRust( ) throws {
19
35
let reflectedOk = try ! rust_func_reflect_result_opaque_rust (
20
36
. Ok( ResultTestOpaqueRustType ( 111 ) )
21
37
)
22
38
XCTAssertEqual ( reflectedOk. val ( ) , 111 )
23
-
39
+
24
40
do {
25
41
let _ = try rust_func_reflect_result_opaque_rust (
26
42
. Err( ResultTestOpaqueRustType ( 222 ) )
@@ -30,7 +46,7 @@ class ResultTests: XCTestCase {
30
46
XCTAssertEqual ( error. val ( ) , 222 )
31
47
}
32
48
}
33
-
49
+
34
50
/// Verify that we can pass a Result<OpaqueSwift, OpaqueSwift> from Swift -> Rust
35
51
func testSwiftCallRustResultOpaqueSwift( ) throws {
36
52
rust_func_takes_result_opaque_swift (
@@ -64,7 +80,7 @@ class ResultTests: XCTestCase {
64
80
XCTAssertEqual ( error. val ( ) , 222 )
65
81
}
66
82
}
67
-
83
+
68
84
/// Verify that we can receive a Result<OpaqueRust, TransparentEnum> from Rust
69
85
func testResultOpaqueRustTransparentEnum( ) throws {
70
86
XCTContext . runActivity ( named: " Should return a ResultTestOpaqueRustType " ) {
@@ -75,7 +91,7 @@ class ResultTests: XCTestCase {
75
91
XCTFail ( )
76
92
}
77
93
}
78
-
94
+
79
95
XCTContext . runActivity ( named: " Should throw an error " ) {
80
96
_ in
81
97
do {
@@ -95,7 +111,7 @@ class ResultTests: XCTestCase {
95
111
}
96
112
}
97
113
}
98
-
114
+
99
115
/// Verify that we can receive a Result<TransparentEnum, OpaqueRust> from Rust
100
116
func testResultTransparentEnumOpaqueRust( ) throws {
101
117
XCTContext . runActivity ( named: " Should return a ResultTestOpaqueRustType " ) {
@@ -114,7 +130,7 @@ class ResultTests: XCTestCase {
114
130
XCTFail ( )
115
131
}
116
132
}
117
-
133
+
118
134
XCTContext . runActivity ( named: " Should throw an error " ) {
119
135
_ in
120
136
do {
@@ -127,7 +143,7 @@ class ResultTests: XCTestCase {
127
143
}
128
144
}
129
145
}
130
-
146
+
131
147
/// Verify that we can receive a Result<(), TransparentEnum> from Rust
132
148
func testResultUnitTypeTransparentEnum( ) throws {
133
149
XCTContext . runActivity ( named: " Should return a Unit type " ) {
@@ -138,7 +154,7 @@ class ResultTests: XCTestCase {
138
154
XCTFail ( )
139
155
}
140
156
}
141
-
157
+
142
158
XCTContext . runActivity ( named: " Should throw an error " ) {
143
159
_ in
144
160
do {
@@ -158,7 +174,7 @@ class ResultTests: XCTestCase {
158
174
}
159
175
}
160
176
}
161
-
177
+
162
178
/// Verify that we can receive a Result<(primitive type, OpaqueRustType, String), TransparentEnum> from Rust
163
179
func testResultTupleTransparentEnum( ) throws {
164
180
XCTContext . runActivity ( named: " Should return a tuple type " ) {
@@ -172,7 +188,7 @@ class ResultTests: XCTestCase {
172
188
XCTFail ( )
173
189
}
174
190
}
175
-
191
+
176
192
XCTContext . runActivity ( named: " Should throw an error " ) {
177
193
_ in
178
194
do {
@@ -249,7 +265,7 @@ class ResultTests: XCTestCase {
249
265
XCTAssertEqual ( UInt32 ( i) , value. val ( ) )
250
266
}
251
267
}
252
-
268
+
253
269
/// Verify that we can use throwing initializers defined on the Rust side.
254
270
func testThrowingInitializers( ) throws {
255
271
XCTContext . runActivity ( named: " Should fail " ) {
0 commit comments