Skip to content

Commit 04dc4ad

Browse files
authored
Add a test("name") { [test code] } helper function (swiftwasm#36)
1 parent 0311620 commit 04dc4ad

File tree

3 files changed

+36
-48
lines changed

3 files changed

+36
-48
lines changed

IntegrationTests/TestSuites/Sources/PrimaryTests/UnitTestUtils.swift

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
import JavaScriptKit
22

3+
var printTestNames = false
4+
// Uncomment the next line to print the name of each test suite before running it.
5+
// This will make it easier to debug any errors that occur on the JS side.
6+
//printTestNames = true
7+
8+
func test(_ name: String, testBlock: () throws -> Void) {
9+
if printTestNames { print(name) }
10+
do {
11+
try testBlock()
12+
} catch {
13+
print("Error in \(name)")
14+
print(error)
15+
}
16+
}
17+
318
struct MessageError: Error {
419
let message: String
520
let file: StaticString

IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift

+21-44
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import JavaScriptKit
22

3-
Literal_Conversion: do {
3+
test("Literal Conversion") {
44
let global = JSObjectRef.global
55
let inputs: [JSValue] = [
66
.boolean(true),
@@ -27,11 +27,9 @@ Literal_Conversion: do {
2727
try expectEqual(got, input)
2828
}
2929
}
30-
} catch {
31-
print(error)
3230
}
3331

34-
Object_Conversion: do {
32+
test("Object Conversion") {
3533
// Notes: globalObject1 is defined in JavaScript environment
3634
//
3735
// ```js
@@ -70,12 +68,9 @@ Object_Conversion: do {
7068
}
7169

7270
try expectEqual(getJSValue(this: globalObject1Ref, name: "undefined_prop"), .undefined)
73-
74-
} catch {
75-
print(error)
7671
}
7772

78-
Value_Construction: do {
73+
test("Value Construction") {
7974
let globalObject1 = getJSValue(this: .global, name: "globalObject1")
8075
let globalObject1Ref = try expectObject(globalObject1)
8176
let prop_2 = getJSValue(this: globalObject1Ref, name: "prop_2")
@@ -85,11 +80,9 @@ Value_Construction: do {
8580
let prop_7 = getJSValue(this: globalObject1Ref, name: "prop_7")
8681
try expectEqual(Double.construct(from: prop_7), 3.14)
8782
try expectEqual(Float.construct(from: prop_7), 3.14)
88-
} catch {
89-
print(error)
9083
}
9184

92-
Array_Iterator: do {
85+
test("Array Iterator") {
9386
let globalObject1 = getJSValue(this: .global, name: "globalObject1")
9487
let globalObject1Ref = try expectObject(globalObject1)
9588
let prop_4 = getJSValue(this: globalObject1Ref, name: "prop_4")
@@ -100,7 +93,7 @@ Array_Iterator: do {
10093
try expectEqual(Array(array), expectedProp_4)
10194
}
10295

103-
Array_RandomAccessCollection: do {
96+
test("Array RandomAccessCollection") {
10497
let globalObject1 = getJSValue(this: .global, name: "globalObject1")
10598
let globalObject1Ref = try expectObject(globalObject1)
10699
let prop_4 = getJSValue(this: globalObject1Ref, name: "prop_4")
@@ -111,7 +104,7 @@ Array_RandomAccessCollection: do {
111104
try expectEqual([array[0], array[1], array[2], array[3]], expectedProp_4)
112105
}
113106

114-
Value_Decoder: do {
107+
test("Value Decoder") {
115108
struct GlobalObject1: Codable {
116109
struct Prop1: Codable {
117110
let nested_prop: Int
@@ -131,7 +124,7 @@ Value_Decoder: do {
131124
try expectEqual(globalObject1.prop_7, 3.14)
132125
}
133126

134-
Function_Call: do {
127+
test("Function Call") {
135128
// Notes: globalObject1 is defined in JavaScript environment
136129
//
137130
// ```js
@@ -173,12 +166,9 @@ Function_Call: do {
173166
try expectEqual(func6(true, 1, 2), .number(1))
174167
try expectEqual(func6(false, 1, 2), .number(2))
175168
try expectEqual(func6(true, "OK", 2), .string("OK"))
176-
177-
} catch {
178-
print(error)
179169
}
180170

181-
Host_Function_Registration: do {
171+
test("Host Function Registration") {
182172
// ```js
183173
// global.globalObject1 = {
184174
// ...
@@ -221,11 +211,9 @@ Host_Function_Registration: do {
221211
try expectEqual(hostFunc2(3), .number(6))
222212
_ = try expectString(hostFunc2(true))
223213
hostFunc2.release()
224-
} catch {
225-
print(error)
226214
}
227215

228-
New_Object_Construction: do {
216+
test("New Object Construction") {
229217
// ```js
230218
// global.Animal = function(name, age, isCat) {
231219
// this.name = name
@@ -247,11 +235,9 @@ New_Object_Construction: do {
247235
let dog1 = objectConstructor.new("Pochi", 3, false)
248236
let dog1Bark = try expectFunction(getJSValue(this: dog1, name: "bark"))
249237
try expectEqual(dog1Bark(), .string("wan"))
250-
} catch {
251-
print(error)
252238
}
253239

254-
Call_Function_With_This: do {
240+
test("Call Function With This") {
255241
// ```js
256242
// global.Animal = function(name, age, isCat) {
257243
// this.name = name
@@ -275,18 +261,15 @@ Call_Function_With_This: do {
275261
// Call with this
276262
let gotIsCat = getIsCat(this: cat1)
277263
try expectEqual(gotIsCat, .boolean(true))
278-
279-
} catch {
280-
print(error)
281264
}
282265

283-
Object_Conversion: do {
284-
let array1 = [1, 2, 3]
285-
let jsArray1 = array1.jsValue().object!
286-
try expectEqual(jsArray1.length, .number(3))
287-
try expectEqual(jsArray1[0], .number(1))
288-
try expectEqual(jsArray1[1], .number(2))
289-
try expectEqual(jsArray1[2], .number(3))
266+
test("Object Conversion") {
267+
let array1 = [1, 2, 3]
268+
let jsArray1 = array1.jsValue().object!
269+
try expectEqual(jsArray1.length, .number(3))
270+
try expectEqual(jsArray1[0], .number(1))
271+
try expectEqual(jsArray1[1], .number(2))
272+
try expectEqual(jsArray1[2], .number(3))
290273

291274
let array2: [JSValueConvertible] = [1, "str", false]
292275
let jsArray2 = array2.jsValue().object!
@@ -296,9 +279,9 @@ Object_Conversion: do {
296279
try expectEqual(jsArray2[2], .boolean(false))
297280
_ = jsArray2.push!(5)
298281
try expectEqual(jsArray2.length, .number(4))
299-
_ = jsArray2.push!(jsArray1)
282+
_ = jsArray2.push!(jsArray1)
300283

301-
try expectEqual(jsArray2[4], .object(jsArray1))
284+
try expectEqual(jsArray2[4], .object(jsArray1))
302285

303286
let dict1: [String: JSValueConvertible] = [
304287
"prop1": 1,
@@ -307,11 +290,9 @@ Object_Conversion: do {
307290
let jsDict1 = dict1.jsValue().object!
308291
try expectEqual(jsDict1.prop1, .number(1))
309292
try expectEqual(jsDict1.prop2, .string("foo"))
310-
} catch {
311-
print(error)
312293
}
313294

314-
ObjectRef_Lifetime: do {
295+
test("ObjectRef Lifetime") {
315296
// ```js
316297
// global.globalObject1 = {
317298
// "prop_1": {
@@ -332,8 +313,6 @@ ObjectRef_Lifetime: do {
332313
try expectEqual(ref1.prop_2, .number(2))
333314
try expectEqual(ref2.prop_2, .number(2))
334315
identity.release()
335-
} catch {
336-
print(error)
337316
}
338317

339318
func closureScope() -> ObjectIdentifier {
@@ -343,10 +322,8 @@ func closureScope() -> ObjectIdentifier {
343322
return result
344323
}
345324

346-
Closure_Identifiers: do {
325+
test("Closure Identifiers") {
347326
let oid1 = closureScope()
348327
let oid2 = closureScope()
349328
try expectEqual(oid1, oid2)
350-
} catch {
351-
print(error)
352329
}

Package.swift

-4
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,5 @@ let package = Package(
3939
),
4040
]
4141
),
42-
.testTarget(
43-
name: "JavaScriptKitTests",
44-
dependencies: ["JavaScriptKit"]
45-
),
4642
]
4743
)

0 commit comments

Comments
 (0)