forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportAPITests.swift
More file actions
87 lines (74 loc) · 2.58 KB
/
Copy pathImportAPITests.swift
File metadata and controls
87 lines (74 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import XCTest
import JavaScriptKit
class ImportAPITests: XCTestCase {
func testRoundTripVoid() throws {
try jsRoundTripVoid()
}
func testRoundTripNumber() throws {
for v in [
0, 1, -1,
Double(Int32.max), Double(Int32.min),
Double(Int64.max), Double(Int64.min),
Double(UInt32.max), Double(UInt32.min),
Double(UInt64.max), Double(UInt64.min),
Double.greatestFiniteMagnitude, Double.leastNonzeroMagnitude,
Double.infinity,
Double.pi,
] {
try XCTAssertEqual(jsRoundTripNumber(v), v)
}
try XCTAssert(jsRoundTripNumber(Double.nan).isNaN)
}
func testRoundTripBool() throws {
for v in [true, false] {
try XCTAssertEqual(jsRoundTripBool(v), v)
}
}
func testRoundTripString() throws {
for v in ["", "Hello, world!", "🧑🧑🧒"] {
try XCTAssertEqual(jsRoundTripString(v), v)
}
}
func ensureThrows<T>(_ f: (Bool) throws(JSException) -> T) throws {
do {
_ = try f(true)
XCTFail("Expected exception")
} catch {
XCTAssertTrue(error.description.contains("TestError"))
}
}
func ensureDoesNotThrow<T>(_ f: (Bool) throws(JSException) -> T, _ assertValue: (T) -> Void) throws {
do {
let result = try f(false)
assertValue(result)
} catch {
XCTFail("Expected no exception")
}
}
func testThrowOrVoid() throws {
try ensureThrows(jsThrowOrVoid)
try ensureDoesNotThrow(jsThrowOrVoid, { _ in })
}
func testThrowOrNumber() throws {
try ensureThrows(jsThrowOrNumber)
try ensureDoesNotThrow(jsThrowOrNumber, { XCTAssertEqual($0, 1) })
}
func testThrowOrBool() throws {
try ensureThrows(jsThrowOrBool)
try ensureDoesNotThrow(jsThrowOrBool, { XCTAssertEqual($0, true) })
}
func testThrowOrString() throws {
try ensureThrows(jsThrowOrString)
try ensureDoesNotThrow(jsThrowOrString, { XCTAssertEqual($0, "Hello, world!") })
}
func testClass() throws {
let greeter = try JsGreeter("Alice", "Hello")
XCTAssertEqual(try greeter.greet(), "Hello, Alice!")
try greeter.changeName("Bob")
XCTAssertEqual(try greeter.greet(), "Hello, Bob!")
try greeter.setName("Charlie")
XCTAssertEqual(try greeter.greet(), "Hello, Charlie!")
XCTAssertEqual(try greeter.name, "Charlie")
XCTAssertEqual(try greeter.prefix, "Hello")
}
}