Skip to content

Commit 3733014

Browse files
committed
BridgeJS: Migrate generic runtime tests to Swift Testing
1 parent b5fe11b commit 3733014

2 files changed

Lines changed: 116 additions & 113 deletions

File tree

Tests/BridgeJSRuntimeTests/ExportGenericAPIs.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import XCTest
1+
import Testing
22
import JavaScriptKit
33

44
@JS public struct ExportGenericPoint {
@@ -157,8 +157,8 @@ public func exportGenericCombineTripleLast<
157157
@_extern(c)
158158
func runExportGenericTests() -> Void
159159

160-
final class ExportGenericAPITests: XCTestCase {
161-
func testExportGenerics() throws {
160+
@Suite struct ExportGenericAPITests {
161+
@Test func exportGenerics() {
162162
runExportGenericTests()
163163
}
164164
}
Lines changed: 113 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import XCTest
1+
import Testing
22
import JavaScriptKit
33

44
@JS struct GenericRTPoint {
@@ -65,166 +65,169 @@ import JavaScriptKit
6565
@JSFunction static func box<T: BridgedSwiftGenericBridgeable>(_ value: T) throws(JSException) -> T
6666
}
6767

68-
class ImportGenericAPITests: XCTestCase {
69-
func testGenericRoundTripScalars() throws {
70-
XCTAssertEqual(try jsGenericRoundTrip(42), 42)
71-
XCTAssertEqual(try jsGenericRoundTrip(-7), -7)
72-
XCTAssertEqual(try jsGenericRoundTrip(3.5), 3.5)
73-
XCTAssertEqual(try jsGenericRoundTrip(Float(1.25)), Float(1.25))
74-
XCTAssertEqual(try jsGenericRoundTrip(true), true)
75-
XCTAssertEqual(try jsGenericRoundTrip(false), false)
76-
XCTAssertEqual(try jsGenericRoundTrip("hello"), "hello")
77-
XCTAssertEqual(try jsGenericRoundTrip(""), "")
78-
}
79-
80-
func testGenericRoundTripNumerics() throws {
81-
XCTAssertEqual(try jsGenericRoundTrip(Int8(-5)), Int8(-5))
82-
XCTAssertEqual(try jsGenericRoundTrip(Int8.min), Int8.min)
83-
XCTAssertEqual(try jsGenericRoundTrip(Int8.max), Int8.max)
84-
XCTAssertEqual(try jsGenericRoundTrip(UInt8(200)), UInt8(200))
85-
XCTAssertEqual(try jsGenericRoundTrip(UInt8.max), UInt8.max)
86-
XCTAssertEqual(try jsGenericRoundTrip(Int16(-1000)), Int16(-1000))
87-
XCTAssertEqual(try jsGenericRoundTrip(UInt16(60000)), UInt16(60000))
88-
XCTAssertEqual(try jsGenericRoundTrip(Int32(-123456)), Int32(-123456))
89-
XCTAssertEqual(try jsGenericRoundTrip(UInt32(3_000_000_000)), UInt32(3_000_000_000))
90-
XCTAssertEqual(try jsGenericRoundTrip(UInt32.max), UInt32.max)
91-
XCTAssertEqual(try jsGenericRoundTrip(UInt(42)), UInt(42))
92-
XCTAssertEqual(try jsGenericRoundTrip(UInt(4_000_000_000)), UInt(4_000_000_000))
93-
XCTAssertEqual(try jsGenericRoundTrip(Int64(-9_000_000_000)), Int64(-9_000_000_000))
94-
XCTAssertEqual(try jsGenericRoundTrip(Int64.min), Int64.min)
95-
XCTAssertEqual(try jsGenericRoundTrip(Int64.max), Int64.max)
96-
XCTAssertEqual(try jsGenericRoundTrip(UInt64(18_000_000_000_000_000_000)), UInt64(18_000_000_000_000_000_000))
97-
XCTAssertEqual(try jsGenericRoundTrip(UInt64.max), UInt64.max)
98-
}
99-
100-
func testGenericRoundTripJSValue() throws {
68+
@Suite struct ImportGenericAPITests {
69+
@Test func genericRoundTripScalars() throws {
70+
#expect(try jsGenericRoundTrip(42) == 42)
71+
#expect(try jsGenericRoundTrip(-7) == -7)
72+
#expect(try jsGenericRoundTrip(3.5) == 3.5)
73+
#expect(try jsGenericRoundTrip(Float(1.25)) == Float(1.25))
74+
#expect(try jsGenericRoundTrip(true) == true)
75+
#expect(try jsGenericRoundTrip(false) == false)
76+
#expect(try jsGenericRoundTrip("hello") == "hello")
77+
#expect(try jsGenericRoundTrip("") == "")
78+
}
79+
80+
@Test func genericRoundTripNumerics() throws {
81+
#expect(try jsGenericRoundTrip(Int8(-5)) == Int8(-5))
82+
#expect(try jsGenericRoundTrip(Int8.min) == Int8.min)
83+
#expect(try jsGenericRoundTrip(Int8.max) == Int8.max)
84+
#expect(try jsGenericRoundTrip(UInt8(200)) == UInt8(200))
85+
#expect(try jsGenericRoundTrip(UInt8.max) == UInt8.max)
86+
#expect(try jsGenericRoundTrip(Int16(-1000)) == Int16(-1000))
87+
#expect(try jsGenericRoundTrip(UInt16(60000)) == UInt16(60000))
88+
#expect(try jsGenericRoundTrip(Int32(-123456)) == Int32(-123456))
89+
#expect(try jsGenericRoundTrip(UInt32(3_000_000_000)) == UInt32(3_000_000_000))
90+
#expect(try jsGenericRoundTrip(UInt32.max) == UInt32.max)
91+
#expect(try jsGenericRoundTrip(UInt(42)) == UInt(42))
92+
#expect(try jsGenericRoundTrip(UInt(4_000_000_000)) == UInt(4_000_000_000))
93+
#expect(try jsGenericRoundTrip(Int64(-9_000_000_000)) == Int64(-9_000_000_000))
94+
#expect(try jsGenericRoundTrip(Int64.min) == Int64.min)
95+
#expect(try jsGenericRoundTrip(Int64.max) == Int64.max)
96+
#expect(try jsGenericRoundTrip(UInt64(18_000_000_000_000_000_000)) == UInt64(18_000_000_000_000_000_000))
97+
#expect(try jsGenericRoundTrip(UInt64.max) == UInt64.max)
98+
}
99+
100+
@Test func genericRoundTripJSValue() throws {
101101
let number = try jsGenericRoundTrip(JSValue.number(3.5))
102-
XCTAssertEqual(number.number, 3.5)
102+
#expect(number.number == 3.5)
103103
let string = try jsGenericRoundTrip(JSValue.string("hi"))
104-
XCTAssertEqual(string.string, "hi")
104+
#expect(string.string == "hi")
105105
let boolean = try jsGenericRoundTrip(JSValue.boolean(true))
106-
XCTAssertEqual(boolean.boolean, true)
107-
XCTAssertTrue(try jsGenericRoundTrip(JSValue.null).isNull)
108-
XCTAssertTrue(try jsGenericRoundTrip(JSValue.undefined).isUndefined)
106+
#expect(boolean.boolean == true)
107+
#expect(try jsGenericRoundTrip(JSValue.null).isNull)
108+
#expect(try jsGenericRoundTrip(JSValue.undefined).isUndefined)
109109
let object = JSObject.global.Object.function!.new()
110110
object.tag = 7
111111
let roundTripped = try jsGenericRoundTrip(JSValue.object(object))
112-
XCTAssertEqual(roundTripped.object?.tag.number, 7)
112+
#expect(roundTripped.object?.tag.number == 7)
113113
}
114114

115-
func testGenericWrappedRoundTrip() throws {
116-
XCTAssertEqual(try jsGenericArrayRoundTrip([1, 2, 3]), [1, 2, 3])
117-
XCTAssertEqual(try jsGenericArrayRoundTrip(["a", "b"]), ["a", "b"])
118-
XCTAssertEqual(try jsGenericArrayRoundTrip([Int]()), [])
119-
XCTAssertEqual(try jsGenericOptionalRoundTrip(Optional<Int>.some(7)), 7)
120-
XCTAssertEqual(try jsGenericOptionalRoundTrip(Optional<Int>.none), nil)
121-
XCTAssertEqual(try jsGenericOptionalRoundTrip(Optional<String>.some("hi")), "hi")
115+
@Test func genericWrappedRoundTrip() throws {
116+
#expect(try jsGenericArrayRoundTrip([1, 2, 3]) == [1, 2, 3])
117+
#expect(try jsGenericArrayRoundTrip(["a", "b"]) == ["a", "b"])
118+
#expect(try jsGenericArrayRoundTrip([Int]()) == [])
119+
#expect(try jsGenericOptionalRoundTrip(Optional<Int>.some(7)) == 7)
120+
#expect(try jsGenericOptionalRoundTrip(Optional<Int>.none) == nil)
121+
#expect(try jsGenericOptionalRoundTrip(Optional<String>.some("hi")) == "hi")
122122
let outcome = try jsGenericOptionalRoundTrip(Optional<GenericRTOutcome>.some(.ok(code: 5)))
123123
guard case .some(.ok(let code)) = outcome else {
124-
return XCTFail("expected .ok")
124+
Issue.record("expected .ok")
125+
return
125126
}
126-
XCTAssertEqual(code, 5)
127-
XCTAssertNil(try jsGenericOptionalRoundTrip(Optional<GenericRTOutcome>.none))
128-
XCTAssertEqual(try jsGenericDictRoundTrip(["x": 1, "y": 2]), ["x": 1, "y": 2])
129-
XCTAssertEqual(try jsGenericDictRoundTrip([String: String]()), [:])
130-
}
131-
132-
func testGenericRoundTripEnums() throws {
133-
XCTAssertEqual(try jsGenericRoundTrip(GenericRTColor.red), .red)
134-
XCTAssertEqual(try jsGenericRoundTrip(GenericRTColor.blue), .blue)
135-
XCTAssertEqual(try jsGenericRoundTrip(GenericRTMode.dark), .dark)
136-
XCTAssertEqual(try jsGenericRoundTrip(GenericRTMode.light).rawValue, "light")
137-
XCTAssertEqual(try jsGenericRoundTrip(GenericRTLevel.high), .high)
127+
#expect(code == 5)
128+
#expect(try jsGenericOptionalRoundTrip(Optional<GenericRTOutcome>.none) == nil)
129+
#expect(try jsGenericDictRoundTrip(["x": 1, "y": 2]) == ["x": 1, "y": 2])
130+
#expect(try jsGenericDictRoundTrip([String: String]()) == [:])
131+
}
132+
133+
@Test func genericRoundTripEnums() throws {
134+
#expect(try jsGenericRoundTrip(GenericRTColor.red) == .red)
135+
#expect(try jsGenericRoundTrip(GenericRTColor.blue) == .blue)
136+
#expect(try jsGenericRoundTrip(GenericRTMode.dark) == .dark)
137+
#expect(try jsGenericRoundTrip(GenericRTMode.light).rawValue == "light")
138+
#expect(try jsGenericRoundTrip(GenericRTLevel.high) == .high)
138139
let outcome = try jsGenericRoundTrip(GenericRTOutcome.ok(code: 42))
139140
guard case .ok(let code) = outcome else {
140-
return XCTFail("expected .ok")
141+
Issue.record("expected .ok")
142+
return
141143
}
142-
XCTAssertEqual(code, 42)
144+
#expect(code == 42)
143145
let failure = try jsGenericRoundTrip(GenericRTOutcome.fail(message: "boom"))
144146
guard case .fail(let message) = failure else {
145-
return XCTFail("expected .fail")
147+
Issue.record("expected .fail")
148+
return
146149
}
147-
XCTAssertEqual(message, "boom")
150+
#expect(message == "boom")
148151
}
149152

150-
func testGenericRoundTripStruct() throws {
153+
@Test func genericRoundTripStruct() throws {
151154
let point = try jsGenericRoundTrip(GenericRTPoint(x: 1, y: 2))
152-
XCTAssertEqual(point.x, 1)
153-
XCTAssertEqual(point.y, 2)
155+
#expect(point.x == 1)
156+
#expect(point.y == 2)
154157
}
155158

156-
func testGenericRoundTripNestedStruct() throws {
159+
@Test func genericRoundTripNestedStruct() throws {
157160
let metadata = try jsGenericRoundTrip(GenericRTNamespace.Metadata(label: "alpha", count: 7))
158-
XCTAssertEqual(metadata.label, "alpha")
159-
XCTAssertEqual(metadata.count, 7)
161+
#expect(metadata.label == "alpha")
162+
#expect(metadata.count == 7)
160163
}
161164

162-
func testGenericParse() throws {
165+
@Test func genericParse() throws {
163166
let point: GenericRTPoint = try jsGenericParsePoint("{\"x\": 10, \"y\": 20}")
164-
XCTAssertEqual(point.x, 10)
165-
XCTAssertEqual(point.y, 20)
167+
#expect(point.x == 10)
168+
#expect(point.y == 20)
166169
let n: Int = try jsGenericParsePoint("42")
167-
XCTAssertEqual(n, 42)
170+
#expect(n == 42)
168171
let string: String = try jsGenericParsePoint("\"hi\"")
169-
XCTAssertEqual(string, "hi")
172+
#expect(string == "hi")
170173
}
171174

172-
func testGenericPickFirstMultiUse() throws {
173-
XCTAssertEqual(try jsImportPickFirst(10, 20) as Int, 10)
174-
XCTAssertEqual(try jsImportPickFirst("a", "b") as String, "a")
175+
@Test func genericPickFirstMultiUse() throws {
176+
#expect(try jsImportPickFirst(10, 20) as Int == 10)
177+
#expect(try jsImportPickFirst("a", "b") as String == "a")
175178
let firstPoint = try jsImportPickFirst(GenericRTPoint(x: 1, y: 2), GenericRTPoint(x: 3, y: 4))
176-
XCTAssertEqual(firstPoint.x, 1)
177-
XCTAssertEqual(firstPoint.y, 2)
179+
#expect(firstPoint.x == 1)
180+
#expect(firstPoint.y == 2)
178181
}
179182

180-
func testGenericMakeReturnOnly() throws {
183+
@Test func genericMakeReturnOnly() throws {
181184
let made: Int = try jsImportMakeInt()
182-
XCTAssertEqual(made, 123)
185+
#expect(made == 123)
183186
}
184187

185-
func testGenericCombineSecondMultiParameter() throws {
186-
XCTAssertEqual(try jsImportCombineSecond(7, "hello") as String, "hello")
187-
XCTAssertEqual(try jsImportCombineSecond("x", 9) as Int, 9)
188+
@Test func genericCombineSecondMultiParameter() throws {
189+
#expect(try jsImportCombineSecond(7, "hello") as String == "hello")
190+
#expect(try jsImportCombineSecond("x", 9) as Int == 9)
188191
let point = try jsImportCombineSecond(42, GenericRTPoint(x: 5, y: 6))
189-
XCTAssertEqual(point.x, 5)
190-
XCTAssertEqual(point.y, 6)
192+
#expect(point.x == 5)
193+
#expect(point.y == 6)
191194
}
192195

193-
func testGenericRoundTripHeapObjectClass() throws {
196+
@Test func genericRoundTripHeapObjectClass() throws {
194197
let box = ImportGenericBox(value: 314)
195-
XCTAssertEqual(box.get(), 314)
198+
#expect(box.get() == 314)
196199
let sameBox = try jsGenericRoundTripClass(box)
197-
XCTAssertEqual(sameBox.get(), 314)
200+
#expect(sameBox.get() == 314)
198201
sameBox.value = 271
199-
XCTAssertEqual(box.get(), 271)
202+
#expect(box.get() == 271)
200203
}
201204

202-
func testGenericMixedConsecutiveCalls() throws {
203-
XCTAssertEqual(try jsGenericRoundTrip(1), 1)
204-
XCTAssertEqual(try jsGenericRoundTrip("two"), "two")
205-
XCTAssertEqual(try jsGenericRoundTrip(3.0), 3.0)
205+
@Test func genericMixedConsecutiveCalls() throws {
206+
#expect(try jsGenericRoundTrip(1) == 1)
207+
#expect(try jsGenericRoundTrip("two") == "two")
208+
#expect(try jsGenericRoundTrip(3.0) == 3.0)
206209
let p = try jsGenericRoundTrip(GenericRTPoint(x: 4, y: 5))
207-
XCTAssertEqual(p.x, 4)
208-
XCTAssertEqual(p.y, 5)
210+
#expect(p.x == 4)
211+
#expect(p.y == 5)
209212
}
210213

211-
func testImportGenericInstanceMethod() throws {
214+
@Test func importGenericInstanceMethod() throws {
212215
let consumer = try ImportGenericConsumer()
213-
XCTAssertEqual(try consumer.identity(42), 42)
214-
XCTAssertEqual(try consumer.identity(-7), -7)
215-
XCTAssertEqual(try consumer.identity("hi"), "hi")
216-
XCTAssertEqual(try consumer.identity(true), true)
217-
XCTAssertEqual(try consumer.identity(false), false)
216+
#expect(try consumer.identity(42) == 42)
217+
#expect(try consumer.identity(-7) == -7)
218+
#expect(try consumer.identity("hi") == "hi")
219+
#expect(try consumer.identity(true) == true)
220+
#expect(try consumer.identity(false) == false)
218221
let point = try consumer.identity(GenericRTPoint(x: 3, y: 4))
219-
XCTAssertEqual(point.x, 3)
220-
XCTAssertEqual(point.y, 4)
222+
#expect(point.x == 3)
223+
#expect(point.y == 4)
221224
}
222225

223-
func testImportGenericStaticMethod() throws {
224-
XCTAssertEqual(try ImportGenericConsumer.box(7), 7)
225-
XCTAssertEqual(try ImportGenericConsumer.box("s"), "s")
226-
XCTAssertEqual(try ImportGenericConsumer.box(true), true)
226+
@Test func importGenericStaticMethod() throws {
227+
#expect(try ImportGenericConsumer.box(7) == 7)
228+
#expect(try ImportGenericConsumer.box("s") == "s")
229+
#expect(try ImportGenericConsumer.box(true) == true)
227230
let color = try ImportGenericConsumer.box(GenericRTColor.green)
228-
XCTAssertEqual(color, .green)
231+
#expect(color == .green)
229232
}
230233
}

0 commit comments

Comments
 (0)