|
| 1 | +import JavaScriptKit |
| 2 | + |
| 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) throws { |
| 9 | + if printTestNames { print(name) } |
| 10 | + do { |
| 11 | + try testBlock() |
| 12 | + } catch { |
| 13 | + print("Error in \(name)") |
| 14 | + print(error) |
| 15 | + throw error |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +func asyncTest(_ name: String, testBlock: () async throws -> Void) async throws -> Void { |
| 20 | + if printTestNames { print(name) } |
| 21 | + do { |
| 22 | + try await testBlock() |
| 23 | + } catch { |
| 24 | + print("Error in \(name)") |
| 25 | + print(error) |
| 26 | + throw error |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +struct MessageError: Error { |
| 31 | + let message: String |
| 32 | + let file: StaticString |
| 33 | + let line: UInt |
| 34 | + let column: UInt |
| 35 | + init(_ message: String, file: StaticString, line: UInt, column: UInt) { |
| 36 | + self.message = message |
| 37 | + self.file = file |
| 38 | + self.line = line |
| 39 | + self.column = column |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func expectEqual<T: Equatable>( |
| 44 | + _ lhs: T, _ rhs: T, |
| 45 | + file: StaticString = #file, line: UInt = #line, column: UInt = #column |
| 46 | +) throws { |
| 47 | + if lhs != rhs { |
| 48 | + throw MessageError("Expect to be equal \"\(lhs)\" and \"\(rhs)\"", file: file, line: line, column: column) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func expectCast<T, U>( |
| 53 | + _ value: T, to type: U.Type = U.self, |
| 54 | + file: StaticString = #file, line: UInt = #line, column: UInt = #column |
| 55 | +) throws -> U { |
| 56 | + guard let value = value as? U else { |
| 57 | + throw MessageError("Expect \"\(value)\" to be \(U.self)", file: file, line: line, column: column) |
| 58 | + } |
| 59 | + return value |
| 60 | +} |
| 61 | + |
| 62 | +func expectObject(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> JSObject { |
| 63 | + switch value { |
| 64 | + case let .object(ref): return ref |
| 65 | + default: |
| 66 | + throw MessageError("Type of \(value) should be \"object\"", file: file, line: line, column: column) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func expectArray(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> JSArray { |
| 71 | + guard let array = value.array else { |
| 72 | + throw MessageError("Type of \(value) should be \"object\"", file: file, line: line, column: column) |
| 73 | + } |
| 74 | + return array |
| 75 | +} |
| 76 | + |
| 77 | +func expectFunction(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> JSFunction { |
| 78 | + switch value { |
| 79 | + case let .function(ref): return ref |
| 80 | + default: |
| 81 | + throw MessageError("Type of \(value) should be \"function\"", file: file, line: line, column: column) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func expectBoolean(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> Bool { |
| 86 | + switch value { |
| 87 | + case let .boolean(bool): return bool |
| 88 | + default: |
| 89 | + throw MessageError("Type of \(value) should be \"boolean\"", file: file, line: line, column: column) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func expectNumber(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> Double { |
| 94 | + switch value { |
| 95 | + case let .number(number): return number |
| 96 | + default: |
| 97 | + throw MessageError("Type of \(value) should be \"number\"", file: file, line: line, column: column) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func expectString(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> String { |
| 102 | + switch value { |
| 103 | + case let .string(string): return String(string) |
| 104 | + default: |
| 105 | + throw MessageError("Type of \(value) should be \"string\"", file: file, line: line, column: column) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func expectAsyncThrow<T>(_ body: @autoclosure () async throws -> T, file: StaticString = #file, line: UInt = #line, column: UInt = #column) async throws -> Error { |
| 110 | + do { |
| 111 | + _ = try await body() |
| 112 | + } catch { |
| 113 | + return error |
| 114 | + } |
| 115 | + throw MessageError("Expect to throw an exception", file: file, line: line, column: column) |
| 116 | +} |
| 117 | + |
| 118 | +func expectNotNil<T>(_ value: T?, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws { |
| 119 | + switch value { |
| 120 | + case .some: return |
| 121 | + case .none: |
| 122 | + throw MessageError("Expect a non-nil value", file: file, line: line, column: column) |
| 123 | + } |
| 124 | +} |
0 commit comments