From 7b187aa16993ab0491e5753432a6a719da0f756a Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Fri, 21 Feb 2020 14:38:45 +0300 Subject: [PATCH] Support new assertion methods in framework tests --- backend.native/tests/framework/main.swift | 55 +++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/backend.native/tests/framework/main.swift b/backend.native/tests/framework/main.swift index 15bc900cc4a..c109be69db2 100644 --- a/backend.native/tests/framework/main.swift +++ b/backend.native/tests/framework/main.swift @@ -62,6 +62,53 @@ func assertFalse(_ value: Bool, } } +func assertNil(_ value: Any?, + _ message: String = "Assertion failed:", + file: String = #file, line: Int = #line) throws { + if (value != nil) { + try throwAssertFailed(message: message + " Expected value to be nil, but got: \(value!)", + file: file, line: line) + } +} + +func fail(_ message: String = "Should not reach here", file: String = #file, line: Int = #line) throws -> Never { + try throwAssertFailed(message: message, file: file, line: line) +} + +func assertFailsWith(_ errorType: T.Type, + _ message: String = "Assertion failed:", + file: String = #file, line: Int = #line, + block: () throws -> Void) throws { + do { + try block() + } catch let error { + if error is T { return } + try throwAssertFailed(message: message + " Expected error \(errorType), got \(error)", + file: file, line: line) + } + + try throwAssertFailed(message: message + " Expected error \(errorType), but finished successfully", + file: file, line: line) +} + +func assertFailsWithKotlin(_ exceptionType: T.Type, + _ message: String = "Assertion failed:", + file: String = #file, line: Int = #line, + block: () throws -> Void) throws { + do { + try block() + } catch let error { + let kotlinException = error.kotlinException + if kotlinException is T { return } + let got = kotlinException ?? error + try throwAssertFailed(message: message + " Expected Kotlin exception \(exceptionType), got \(got)", + file: file, line: line) + } + + try throwAssertFailed(message: message + " Expected Kotlin exception \(exceptionType), but finished successfully", + file: file, line: line) +} + // ---------------- Utils -------------------- @@ -71,6 +118,14 @@ func withAutorelease( _ method: @escaping () throws -> Void) -> () throws -> Voi } } +extension Error { + var kotlinException: Any? { + get { + return (self as NSError).userInfo["KotlinException"] + } + } +} + // ---------------- Execution ---------------- private final class Statistics : CustomStringConvertible {