Created
November 6, 2018 09:35
-
-
Save WeZZard/6576005e0247a3302b56cbdf738b6286 to your computer and use it in GitHub Desktop.
XCTAssertThrows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// XCTestCase+AssertingThrows.h | |
// | |
// | |
// Created by WeZZard on 29/05/2017. | |
// | |
// | |
#import <XCTest/XCTest.h> | |
NS_ASSUME_NONNULL_BEGIN | |
FOUNDATION_EXTERN_INLINE void _XCTSAssertThrows( | |
XCTestCase * self, | |
void (NS_NOESCAPE ^ block)(void), | |
NSString * message, | |
NSString * file, | |
NSUInteger line | |
) NS_REFINED_FOR_SWIFT; | |
FOUNDATION_EXTERN_INLINE void _XCTSAssertThrowsSpecific( | |
XCTestCase * self, | |
void (NS_NOESCAPE ^ block)(void), | |
Class exceptionClass, | |
NSString * message, | |
NSString * file, | |
NSUInteger line | |
) NS_REFINED_FOR_SWIFT; | |
FOUNDATION_EXTERN_INLINE void _XCTSAssertThrowsSpecificNamed( | |
XCTestCase * self, | |
void (NS_NOESCAPE ^ block)(void), | |
NSString * exceptionName, | |
NSString * message, | |
NSString * file, | |
NSUInteger line | |
) NS_REFINED_FOR_SWIFT; | |
NS_ASSUME_NONNULL_END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// XCTestCase+AssertingThrows.m | |
// | |
// | |
// Created by WeZZard on 29/05/2017. | |
// | |
// | |
#import "XCTestCase+AssertingThrows.h" | |
void _XCTSAssertThrows(XCTestCase * self, void (^ block)(void), NSString * message, NSString * file, NSUInteger line) { | |
BOOL didThrow = NO; | |
@try { | |
block(); | |
} @catch (...) { | |
didThrow = YES; | |
} | |
if (!didThrow) { | |
_XCTFailureHandler(self, YES, file.UTF8String, line, _XCTFailureDescription(_XCTAssertion_Fail, 0), @"" @"No throw: %@", message); | |
} | |
} | |
void _XCTSAssertThrowsSpecific(XCTestCase * self, void (^ expression)(void), Class exceptionClass, NSString * message, NSString * file, NSUInteger line) { | |
BOOL didThrow = NO; | |
@try { | |
(void)(expression); | |
} @catch (NSException *exception) { | |
didThrow = YES; | |
if (![exception isKindOfClass: exceptionClass]) { | |
_XCTFailureHandler(self, YES, file.UTF8String, line, _XCTFailureDescription(_XCTAssertion_Fail, 0), @"" @"Unexpected exception get thrown: %@. %@", exception, message); | |
} | |
} @catch (...) { | |
didThrow = YES; | |
_XCTFailureHandler(self, YES, file.UTF8String, line, _XCTFailureDescription(_XCTAssertion_Fail, 0), @"" @"Unkown exception get thrown. %@", message); | |
} | |
if (!didThrow) { | |
_XCTFailureHandler(self, YES, file.UTF8String, line, _XCTFailureDescription(_XCTAssertion_Fail, 0), @"" @"No exception get thrown. %@", message); | |
} | |
} | |
void _XCTSAssertThrowsSpecificNamed(XCTestCase * self, void (^ expression)(void), NSString * exceptionName, NSString * message, NSString * file, NSUInteger line) { | |
BOOL didThrow = NO; | |
@try { | |
(void)(expression); | |
} @catch (NSException *exception) { | |
didThrow = YES; | |
if (![exception.name isEqualToString: exceptionName]) { | |
_XCTFailureHandler(self, YES, file.UTF8String, line, _XCTFailureDescription(_XCTAssertion_Fail, 0), @"" @"Unexpected exception name: %@. %@", exception.name, message); | |
} | |
} @catch (...) { | |
didThrow = YES; | |
_XCTFailureHandler(self, YES, file.UTF8String, line, _XCTFailureDescription(_XCTAssertion_Fail, 0), @"" @"Unkown exception get thrown. %@", message); | |
} | |
if (!didThrow) { | |
_XCTFailureHandler(self, YES, file.UTF8String, line, _XCTFailureDescription(_XCTAssertion_Fail, 0), @"" @"No exception get thrown. %@", message); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// XCTestCase+AssertingThrows.swift | |
// | |
// | |
// Created by WeZZard on 29/05/2017. | |
// | |
// | |
import Foundation | |
import XCTest | |
extension XCTestCase { | |
@inline(__always) | |
public func XCTAssertThrows<T>( | |
_ expression: @autoclosure () -> T, | |
message: String = "", | |
_ file: String = #file, | |
_ line: UInt = #line | |
) | |
{ | |
___XCTSAssertThrows(self, {_ = expression()}, message, file, line) | |
} | |
@inline(__always) | |
public func XCTAssertThrowsSpecific<T, E: NSException>( | |
_ expression: @autoclosure () -> T, | |
exception: E.Type, | |
message: String = "", | |
_ file: String = #file, | |
_ line: UInt = #line | |
) | |
{ | |
___XCTSAssertThrowsSpecific(self, {_ = expression()}, E.self, message, file, line) | |
} | |
@inline(__always) | |
public func XCTAssertThrowsSpecificNamed<T>( | |
_ expression: @autoclosure () -> T, | |
exceptionName: String, | |
message: String = "", | |
_ file: String = #file, | |
_ line: UInt = #line | |
) | |
{ | |
___XCTSAssertThrowsSpecificNamed(self, {_ = expression()}, exceptionName, message, file, line) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment