TestCase.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2014 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 Foundation
  19. import Realm
  20. import Realm.Dynamic
  21. import RealmTestSupport
  22. import RealmSwift
  23. import XCTest
  24. func inMemoryRealm(_ inMememoryIdentifier: String) -> Realm {
  25. return try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: inMememoryIdentifier))
  26. }
  27. class TestCase: RLMTestCaseBase {
  28. var exceptionThrown = false
  29. var testDir: String! = nil
  30. let queue = DispatchQueue(label: "background")
  31. @discardableResult
  32. func realmWithTestPath(configuration: Realm.Configuration = Realm.Configuration()) -> Realm {
  33. var configuration = configuration
  34. configuration.fileURL = testRealmURL()
  35. return try! Realm(configuration: configuration)
  36. }
  37. override class func tearDown() {
  38. RLMRealm.resetRealmState()
  39. super.tearDown()
  40. }
  41. override func invokeTest() {
  42. testDir = RLMRealmPathForFile(realmFilePrefix())
  43. do {
  44. try FileManager.default.removeItem(atPath: testDir)
  45. } catch {
  46. // The directory shouldn't actually already exist, so not an error
  47. }
  48. try! FileManager.default.createDirectory(at: URL(fileURLWithPath: testDir, isDirectory: true),
  49. withIntermediateDirectories: true, attributes: nil)
  50. let config = Realm.Configuration(fileURL: defaultRealmURL())
  51. Realm.Configuration.defaultConfiguration = config
  52. exceptionThrown = false
  53. autoreleasepool { super.invokeTest() }
  54. queue.sync { }
  55. if !exceptionThrown {
  56. XCTAssertFalse(RLMHasCachedRealmForPath(defaultRealmURL().path))
  57. XCTAssertFalse(RLMHasCachedRealmForPath(testRealmURL().path))
  58. }
  59. resetRealmState()
  60. do {
  61. try FileManager.default.removeItem(atPath: testDir)
  62. } catch {
  63. XCTFail("Unable to delete realm files")
  64. }
  65. // Verify that there are no remaining realm files after the test
  66. let parentDir = (testDir as NSString).deletingLastPathComponent
  67. for url in FileManager.default.enumerator(atPath: parentDir)! {
  68. let url = url as! NSString
  69. XCTAssertNotEqual(url.pathExtension, "realm", "Lingering realm file at \(parentDir)/\(url)")
  70. assert(url.pathExtension != "realm")
  71. }
  72. }
  73. func dispatchSyncNewThread(block: @escaping () -> Void) {
  74. queue.async {
  75. autoreleasepool {
  76. block()
  77. }
  78. }
  79. queue.sync { }
  80. }
  81. /// Check whether two test objects are equal (refer to the same row in the same Realm), even if their models
  82. /// don't define a primary key.
  83. func assertEqual<O: Object>(_ o1: O?, _ o2: O?, fileName: StaticString = #file, lineNumber: UInt = #line) {
  84. if o1 == nil && o2 == nil {
  85. return
  86. }
  87. if let o1 = o1, let o2 = o2, o1.isSameObject(as: o2) {
  88. return
  89. }
  90. XCTFail("Objects expected to be equal, but weren't. First: \(String(describing: o1)), "
  91. + "second: \(String(describing: o2))", file: (fileName), line: lineNumber)
  92. }
  93. /// Check whether two collections containing Realm objects are equal.
  94. func assertEqual<C: Collection>(_ c1: C, _ c2: C, fileName: StaticString = #file, lineNumber: UInt = #line)
  95. where C.Iterator.Element: Object {
  96. XCTAssertEqual(c1.count, c2.count, "Collection counts were incorrect", file: (fileName), line: lineNumber)
  97. for (o1, o2) in zip(c1, c2) {
  98. assertEqual(o1, o2, fileName: fileName, lineNumber: lineNumber)
  99. }
  100. }
  101. func assertEqual<T: Equatable>(_ expected: [T?], _ actual: [T?], file: StaticString = #file, line: UInt = #line) {
  102. if expected.count != actual.count {
  103. XCTFail("assertEqual failed: (\"\(expected)\") is not equal to (\"\(actual)\")",
  104. file: (file), line: line)
  105. return
  106. }
  107. XCTAssertEqual(expected.count, actual.count, "Collection counts were incorrect", file: (file), line: line)
  108. for (e, a) in zip(expected, actual) where e != a {
  109. XCTFail("assertEqual failed: (\"\(expected)\") is not equal to (\"\(actual)\")",
  110. file: (file), line: line)
  111. return
  112. }
  113. }
  114. func assertThrows<T>(_ block: @autoclosure () -> T, named: String? = RLMExceptionName,
  115. _ message: String? = nil, fileName: String = #file, lineNumber: UInt = #line) {
  116. exceptionThrown = true
  117. RLMAssertThrowsWithName(self, { _ = block() }, named, message, fileName, lineNumber)
  118. }
  119. func assertThrows<T>(_ block: @autoclosure () -> T, reason: String,
  120. _ message: String? = nil, fileName: String = #file, lineNumber: UInt = #line) {
  121. exceptionThrown = true
  122. RLMAssertThrowsWithReason(self, { _ = block() }, reason, message, fileName, lineNumber)
  123. }
  124. func assertThrows<T>(_ block: @autoclosure () -> T, reasonMatching regexString: String,
  125. _ message: String? = nil, fileName: String = #file, lineNumber: UInt = #line) {
  126. exceptionThrown = true
  127. RLMAssertThrowsWithReasonMatching(self, { _ = block() }, regexString, message, fileName, lineNumber)
  128. }
  129. func assertSucceeds(message: String? = nil, fileName: StaticString = #file,
  130. lineNumber: UInt = #line, block: () throws -> Void) {
  131. do {
  132. try block()
  133. } catch {
  134. XCTFail("Expected no error, but instead caught <\(error)>.",
  135. file: (fileName), line: lineNumber)
  136. }
  137. }
  138. func assertFails<T>(_ expectedError: Realm.Error.Code, _ message: String? = nil,
  139. fileName: StaticString = #file, lineNumber: UInt = #line,
  140. block: () throws -> T) {
  141. do {
  142. _ = try block()
  143. XCTFail("Expected to catch <\(expectedError)>, but no error was thrown.",
  144. file: (fileName), line: lineNumber)
  145. } catch let e as Realm.Error where e.code == expectedError {
  146. // Success!
  147. } catch {
  148. XCTFail("Expected to catch <\(expectedError)>, but instead caught <\(error)>.",
  149. file: (fileName), line: lineNumber)
  150. }
  151. }
  152. func assertFails<T>(_ expectedError: Error, _ message: String? = nil,
  153. fileName: StaticString = #file, lineNumber: UInt = #line,
  154. block: () throws -> T) {
  155. do {
  156. _ = try block()
  157. XCTFail("Expected to catch <\(expectedError)>, but no error was thrown.",
  158. file: (fileName), line: lineNumber)
  159. } catch let e where e._code == expectedError._code {
  160. // Success!
  161. } catch {
  162. XCTFail("Expected to catch <\(expectedError)>, but instead caught <\(error)>.",
  163. file: (fileName), line: lineNumber)
  164. }
  165. }
  166. func assertNil<T>(block: @autoclosure() -> T?, _ message: String? = nil,
  167. fileName: StaticString = #file, lineNumber: UInt = #line) {
  168. XCTAssert(block() == nil, message ?? "", file: (fileName), line: lineNumber)
  169. }
  170. func assertMatches(_ block: @autoclosure () -> String, _ regexString: String, _ message: String? = nil,
  171. fileName: String = #file, lineNumber: UInt = #line) {
  172. RLMAssertMatches(self, block, regexString, message, fileName, lineNumber)
  173. }
  174. private func realmFilePrefix() -> String {
  175. let name: String? = self.name
  176. return name!.trimmingCharacters(in: CharacterSet(charactersIn: "-[]"))
  177. }
  178. internal func testRealmURL() -> URL {
  179. return realmURLForFile("test.realm")
  180. }
  181. internal func defaultRealmURL() -> URL {
  182. return realmURLForFile("default.realm")
  183. }
  184. private func realmURLForFile(_ fileName: String) -> URL {
  185. let directory = URL(fileURLWithPath: testDir, isDirectory: true)
  186. return directory.appendingPathComponent(fileName, isDirectory: false)
  187. }
  188. }