|
| 1 | +import XCTest |
| 2 | +import JavaScriptKit |
| 3 | + |
| 4 | +class JSExceptionTests: XCTestCase { |
| 5 | + private func eval(_ code: String) -> JSValue { |
| 6 | + return JSObject.global.eval!(code) |
| 7 | + } |
| 8 | + |
| 9 | + func testThrowingMethodCalls() { |
| 10 | + let context = eval( |
| 11 | + """ |
| 12 | + (() => ({ |
| 13 | + func1: () => { throw new Error(); }, |
| 14 | + func2: () => { throw 'String Error'; }, |
| 15 | + func3: () => { throw 3.0; }, |
| 16 | + }))() |
| 17 | + """ |
| 18 | + ).object! |
| 19 | + |
| 20 | + // MARK: Throwing method calls |
| 21 | + XCTAssertThrowsError(try context.throwing.func1!()) { error in |
| 22 | + XCTAssertTrue(error is JSException) |
| 23 | + let errorObject = JSError(from: (error as! JSException).thrownValue) |
| 24 | + XCTAssertNotNil(errorObject) |
| 25 | + } |
| 26 | + |
| 27 | + XCTAssertThrowsError(try context.throwing.func2!()) { error in |
| 28 | + XCTAssertTrue(error is JSException) |
| 29 | + let thrownValue = (error as! JSException).thrownValue |
| 30 | + XCTAssertEqual(thrownValue.string, "String Error") |
| 31 | + } |
| 32 | + |
| 33 | + XCTAssertThrowsError(try context.throwing.func3!()) { error in |
| 34 | + XCTAssertTrue(error is JSException) |
| 35 | + let thrownValue = (error as! JSException).thrownValue |
| 36 | + XCTAssertEqual(thrownValue.number, 3.0) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + func testThrowingUnboundFunctionCalls() { |
| 41 | + let jsThrowError = eval("() => { throw new Error(); }") |
| 42 | + XCTAssertThrowsError(try jsThrowError.function!.throws()) { error in |
| 43 | + XCTAssertTrue(error is JSException) |
| 44 | + let errorObject = JSError(from: (error as! JSException).thrownValue) |
| 45 | + XCTAssertNotNil(errorObject) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + func testThrowingConstructorCalls() { |
| 50 | + let Animal = JSObject.global.Animal.function! |
| 51 | + XCTAssertNoThrow(try Animal.throws.new("Tama", 3, true)) |
| 52 | + XCTAssertThrowsError(try Animal.throws.new("Tama", -3, true)) { error in |
| 53 | + XCTAssertTrue(error is JSException) |
| 54 | + let errorObject = JSError(from: (error as! JSException).thrownValue) |
| 55 | + XCTAssertNotNil(errorObject) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + func testInitWithMessage() { |
| 60 | + let message = "THIS IS AN ERROR MESSAGE" |
| 61 | + let exception = JSException(message: message) |
| 62 | + XCTAssertTrue(exception.description.contains(message)) |
| 63 | + } |
| 64 | +} |
0 commit comments