objective c - Can XCTAssertThrows catch c++ exception? -


I am using Exstate to test some C ++ / OC mixed code units. I think it seems that XCTAssertThrows can not take exceptions c ++?

My use is very simple

Say, as an expression c ++ test ()

    < p> Any suggestions?  

>

Wrap


Long answer: You can wrap NSE expansion for any std :: exception, like << p>

  #import & lt; XCTest / XCTest.h & gt; #import & lt; Exceptions & gt; @interface NSException (ForCppException) @end @implementation NSException (ForCppException) - (id) initWithCppException: (std :: exception) cppException {NSString * Description = [NSString stringWithUTF8String: cppException.what ()]; Return [self-initWithName: @ "cppException" Reason: Description userInfo: zero]; } @end @interface XCTestCase (ForCppException) @end @implementation XCTestCase (ForCppException) - (zero) rethowNSExceptionForCppException: (zero (^) ()) ACTION {ACTION (try {); } Hold (const std :: exception & e) {@throw [[NSException alloc] initWithCppException: e]; }} @end #define XCTAssertCppThrows (Expression, Format ...) \ XCTAssertThrows ([self rethowNSExceptionForCppException: ^ {expression;}], ## format)   

Use this like this:

  #pragma mark - test void foo () {std :: exception (); } Zero Times () {} @ Interface Test Test: XctestCase @ @ @ Implementation Test Test - Examples (empty) Examples {XCTAssertCppThrows (foo (), @ "Exception should be teaser"); // Successful XCTAssertCppThrows (bar (), @ "should be refracted"); // failed} @end    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -