TestUtils.mm 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2015 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import "TestUtils.h"
  19. #import <Realm/Realm.h>
  20. #import <Realm/RLMSchema_Private.h>
  21. #import "RLMRealmUtil.hpp"
  22. void RLMAssertThrowsWithReasonMatchingSwift(XCTestCase *self,
  23. __attribute__((noescape)) dispatch_block_t block,
  24. NSString *regexString, NSString *message,
  25. NSString *fileName, NSUInteger lineNumber) {
  26. BOOL didThrow = NO;
  27. @try {
  28. block();
  29. }
  30. @catch (NSException *e) {
  31. didThrow = YES;
  32. NSString *reason = e.reason;
  33. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:(NSRegularExpressionOptions)0 error:nil];
  34. if ([regex numberOfMatchesInString:reason options:(NSMatchingOptions)0 range:NSMakeRange(0, reason.length)] == 0) {
  35. NSString *msg = [NSString stringWithFormat:@"The given expression threw an exception with reason '%@', but expected to match '%@'",
  36. reason, regexString];
  37. [self recordFailureWithDescription:msg inFile:fileName atLine:lineNumber expected:NO];
  38. }
  39. }
  40. if (!didThrow) {
  41. NSString *prefix = @"The given expression failed to throw an exception";
  42. message = message ? [NSString stringWithFormat:@"%@ (%@)", prefix, message] : prefix;
  43. [self recordFailureWithDescription:message inFile:fileName atLine:lineNumber expected:NO];
  44. }
  45. }