-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathJSExceptionTests.swift
More file actions
64 lines (56 loc) · 2.24 KB
/
JSExceptionTests.swift
File metadata and controls
64 lines (56 loc) · 2.24 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
import XCTest
import JavaScriptKit
class JSExceptionTests: XCTestCase {
private func eval(_ code: String) -> JSValue {
return JSObject.global.eval!(code)
}
func testThrowingMethodCalls() {
let context = eval(
"""
(() => ({
func1: () => { throw new Error(); },
func2: () => { throw 'String Error'; },
func3: () => { throw 3.0; },
}))()
"""
).object!
// MARK: Throwing method calls
XCTAssertThrowsError(try context.throwing.func1!()) { error in
XCTAssertTrue(error is JSException)
let errorObject = JSError(from: (error as! JSException).thrownValue)
XCTAssertNotNil(errorObject)
}
XCTAssertThrowsError(try context.throwing.func2!()) { error in
XCTAssertTrue(error is JSException)
let thrownValue = (error as! JSException).thrownValue
XCTAssertEqual(thrownValue.string, "String Error")
}
XCTAssertThrowsError(try context.throwing.func3!()) { error in
XCTAssertTrue(error is JSException)
let thrownValue = (error as! JSException).thrownValue
XCTAssertEqual(thrownValue.number, 3.0)
}
}
func testThrowingUnboundFunctionCalls() {
let jsThrowError = eval("() => { throw new Error(); }")
XCTAssertThrowsError(try jsThrowError.function!.throws()) { error in
XCTAssertTrue(error is JSException)
let errorObject = JSError(from: (error as! JSException).thrownValue)
XCTAssertNotNil(errorObject)
}
}
func testThrowingConstructorCalls() {
let Animal = JSObject.global.Animal.function!
XCTAssertNoThrow(try Animal.throws.new("Tama", 3, true))
XCTAssertThrowsError(try Animal.throws.new("Tama", -3, true)) { error in
XCTAssertTrue(error is JSException)
let errorObject = JSError(from: (error as! JSException).thrownValue)
XCTAssertNotNil(errorObject)
}
}
func testInitWithMessage() {
let message = "THIS IS AN ERROR MESSAGE"
let exception = JSException(message: message)
XCTAssertTrue(exception.description.contains(message))
}
}