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
152 lines (127 loc) · 4.41 KB
/
Copy pathImportAPITests.swift
File metadata and controls
152 lines (127 loc) · 4.41 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import XCTest
import JavaScriptKit
@JS enum LightColor {
case red
case yellow
case green
}
@JSFunction func jsRoundTripLightColor(_ value: LightColor) throws(JSException) -> LightColor
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 testRoundTripJSValue() throws {
let symbol = JSSymbol("roundTrip")
let bigInt = JSBigInt(_slowBridge: Int64(123456789))
let values: [JSValue] = [
.boolean(true),
.number(42),
.string(JSString("hello")),
.object(JSObject.global),
.null,
.undefined,
.symbol(symbol),
.bigInt(bigInt),
]
for value in values {
try XCTAssertEqual(jsRoundTripJSValue(value), value)
}
}
func testRoundTripFeatureFlag() throws {
for v in [FeatureFlag.foo, .bar] {
try XCTAssertEqual(jsRoundTripFeatureFlag(v), v)
}
}
func testRoundTripOptionalStruct() throws {
let p = try jsRoundTripOptionalPoint(Point(x: 3, y: 4))
XCTAssertEqual(p?.x, 3)
XCTAssertEqual(p?.y, 4)
XCTAssertNil(try jsRoundTripOptionalPoint(nil))
}
func testRoundTripCaseEnum() throws {
for v in [LightColor.red, .yellow, .green] {
try XCTAssertEqual(jsRoundTripLightColor(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")
}
func testJSNameFunctionAndClass() throws {
XCTAssertEqual(try _jsWeirdFunction(), 42)
let obj = try _WeirdClass()
XCTAssertEqual(try obj.method_with_dashes(), "ok")
}
func testJSClassStaticFunctions() throws {
let created = try StaticBox.create(10)
XCTAssertEqual(try created.value(), 10)
let defaultBox = try StaticBox.makeDefault()
XCTAssertEqual(try defaultBox.value(), 0)
XCTAssertEqual(try StaticBox.value(), 99)
let dashed = try StaticBox.with_dashes()
XCTAssertEqual(try dashed.value(), 7)
}
}