TestUtils.mm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // This ensures the shared schema is initialized outside of of a test case,
  23. // so if an exception is thrown, it will kill the test process rather than
  24. // allowing hundreds of test cases to fail in strange ways
  25. __attribute((constructor))
  26. static void initializeSharedSchema() {
  27. [RLMSchema sharedSchema];
  28. }
  29. static void assertThrows(XCTestCase *self, dispatch_block_t block, NSString *message,
  30. NSString *fileName, NSUInteger lineNumber,
  31. NSString *(^condition)(NSException *)) {
  32. @try {
  33. block();
  34. NSString *prefix = @"The given expression failed to throw an exception";
  35. message = message ? [NSString stringWithFormat:@"%@ (%@)", prefix, message] : prefix;
  36. [self recordFailureWithDescription:message inFile:fileName atLine:lineNumber expected:NO];
  37. }
  38. @catch (NSException *e) {
  39. if (NSString *failure = condition(e)) {
  40. [self recordFailureWithDescription:failure inFile:fileName atLine:lineNumber expected:NO];
  41. }
  42. }
  43. }
  44. void RLMAssertThrowsWithName(XCTestCase *self, __attribute__((noescape)) dispatch_block_t block,
  45. NSString *name, NSString *message, NSString *fileName, NSUInteger lineNumber) {
  46. assertThrows(self, block, message, fileName, lineNumber, ^NSString *(NSException *e) {
  47. if ([name isEqualToString:e.name]) {
  48. return nil;
  49. }
  50. return [NSString stringWithFormat:@"The given expression threw an exception named '%@', but expected '%@'",
  51. e.name, name];
  52. });
  53. }
  54. void RLMAssertThrowsWithReason(XCTestCase *self, __attribute__((noescape)) dispatch_block_t block,
  55. NSString *expected, NSString *message, NSString *fileName, NSUInteger lineNumber) {
  56. assertThrows(self, block, message, fileName, lineNumber, ^NSString *(NSException *e) {
  57. if ([e.reason rangeOfString:expected].location != NSNotFound) {
  58. return nil;
  59. }
  60. return [NSString stringWithFormat:@"The given expression threw an exception with reason '%@', but expected '%@'",
  61. e.reason, expected];
  62. });
  63. }
  64. void RLMAssertThrowsWithReasonMatching(XCTestCase *self, __attribute__((noescape)) dispatch_block_t block,
  65. NSString *regexString, NSString *message,
  66. NSString *fileName, NSUInteger lineNumber) {
  67. auto regex = [NSRegularExpression regularExpressionWithPattern:regexString
  68. options:(NSRegularExpressionOptions)0 error:nil];
  69. assertThrows(self, block, message, fileName, lineNumber, ^NSString *(NSException *e) {
  70. if ([regex numberOfMatchesInString:e.reason options:(NSMatchingOptions)0 range:{0, e.reason.length}] > 0) {
  71. return nil;
  72. }
  73. return [NSString stringWithFormat:@"The given expression threw an exception with reason '%@', but expected to match '%@'",
  74. e.reason, regexString];
  75. });
  76. }
  77. void RLMAssertMatches(XCTestCase *self, __attribute__((noescape)) NSString *(^block)(),
  78. NSString *regexString, NSString *message, NSString *fileName, NSUInteger lineNumber) {
  79. NSString *result = block();
  80. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:(NSRegularExpressionOptions)0 error:nil];
  81. if ([regex numberOfMatchesInString:result options:(NSMatchingOptions)0 range:NSMakeRange(0, result.length)] == 0) {
  82. NSString *msg = [NSString stringWithFormat:@"The given expression '%@' did not match '%@'%@",
  83. result, regexString, message ? [NSString stringWithFormat:@": %@", message] : @""];
  84. [self recordFailureWithDescription:msg inFile:fileName atLine:lineNumber expected:NO];
  85. }
  86. }
  87. bool RLMHasCachedRealmForPath(NSString *path) {
  88. return RLMGetAnyCachedRealmForPath(path.UTF8String);
  89. }