UtilTests.mm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "RLMTestCase.h"
  19. #import "RLMConstants.h"
  20. #import "RLMUtil.hpp"
  21. #ifndef REALM_COCOA_VERSION
  22. #import "RLMVersion.h"
  23. #endif
  24. #import "shared_realm.hpp"
  25. @interface UtilTests : RLMTestCase
  26. @end
  27. static BOOL RLMEqualExceptions(NSException *actual, NSException *expected) {
  28. return [actual.name isEqualToString:expected.name]
  29. && [actual.reason isEqualToString:expected.reason]
  30. && [actual.userInfo isEqual:expected.userInfo];
  31. }
  32. @implementation UtilTests
  33. - (void)testRLMExceptionWithReasonAndUserInfo {
  34. NSString *const reason = @"Reason";
  35. NSDictionary *expectedUserInfo = @{
  36. RLMRealmVersionKey : REALM_COCOA_VERSION,
  37. RLMRealmCoreVersionKey : @REALM_VERSION,
  38. };
  39. XCTAssertTrue(RLMEqualExceptions(RLMException(reason),
  40. [NSException exceptionWithName:RLMExceptionName reason:reason userInfo:expectedUserInfo]));
  41. }
  42. - (void)testRLMExceptionWithCPlusPlusException {
  43. std::runtime_error exception("Reason");
  44. NSDictionary *expectedUserInfo = @{
  45. RLMRealmVersionKey : REALM_COCOA_VERSION,
  46. RLMRealmCoreVersionKey : @REALM_VERSION,
  47. };
  48. XCTAssertTrue(RLMEqualExceptions(RLMException(exception),
  49. [NSException exceptionWithName:RLMExceptionName reason:@"Reason" userInfo:expectedUserInfo]));
  50. }
  51. - (void)testSystemExceptionWithPOSIXSystemException {
  52. int code = ENOENT;
  53. NSString *description = @"No such file or directory";
  54. std::system_error exception(code, std::generic_category());
  55. NSDictionary *expectedUserInfo = @{
  56. NSLocalizedDescriptionKey : description,
  57. @"Error Code" : @(code),
  58. @"Category": [NSString stringWithUTF8String:std::generic_category().name()]
  59. };
  60. XCTAssertEqualObjects(RLMMakeError(exception),
  61. [NSError errorWithDomain:NSPOSIXErrorDomain code:code userInfo:expectedUserInfo]);
  62. }
  63. - (void)testSystemExceptionWithNonPOSIXSystemException {
  64. int code = 999;
  65. NSString *description = @"unspecified system_category error";
  66. std::system_error exception(code, std::system_category());
  67. NSDictionary *expectedUserInfo = @{
  68. NSLocalizedDescriptionKey : description,
  69. @"Error Code" : @(code),
  70. @"Category": [NSString stringWithUTF8String:std::system_category().name()]
  71. };
  72. XCTAssertEqualObjects(RLMMakeError(exception),
  73. [NSError errorWithDomain:RLMUnknownSystemErrorDomain code:code userInfo:expectedUserInfo]);
  74. }
  75. - (void)testRealmFileException {
  76. realm::RealmFileException exception(realm::RealmFileException::Kind::NotFound,
  77. "/some/path",
  78. "don't do that to your files",
  79. "lp0 on fire");
  80. RLMError dummyCode = RLMErrorFail;
  81. NSDictionary *expectedUserInfo = @{NSLocalizedDescriptionKey: @"don't do that to your files",
  82. NSFilePathErrorKey: @"/some/path",
  83. @"Error Code": @(dummyCode),
  84. @"Underlying": @"lp0 on fire"};
  85. XCTAssertEqualObjects(RLMMakeError(dummyCode, exception),
  86. [NSError errorWithDomain:RLMErrorDomain code:dummyCode userInfo:expectedUserInfo]);
  87. }
  88. - (void)testRLMMakeError {
  89. std::runtime_error exception("Reason");
  90. RLMError code = RLMErrorFail;
  91. NSDictionary *expectedUserInfo = @{
  92. NSLocalizedDescriptionKey : @"Reason",
  93. @"Error Code" : @(code),
  94. };
  95. XCTAssertEqualObjects(RLMMakeError(code, exception),
  96. [NSError errorWithDomain:RLMErrorDomain code:code userInfo:expectedUserInfo]);
  97. }
  98. - (void)testRLMSetErrorOrThrowWithNilErrorPointer {
  99. NSError *error = [NSError errorWithDomain:RLMErrorDomain code:RLMErrorFail userInfo:nil];
  100. XCTAssertThrows(RLMSetErrorOrThrow(error, nil));
  101. }
  102. - (void)testRLMSetErrorOrThrowWithErrorPointer {
  103. NSError *error = [NSError errorWithDomain:RLMErrorDomain code:RLMErrorFail userInfo:nil];
  104. NSError *outError = nil;
  105. XCTAssertNoThrow(RLMSetErrorOrThrow(error, &outError));
  106. XCTAssertEqualObjects(error, outError);
  107. }
  108. @end