TestCase.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. if !exceptionThrown {
  55. XCTAssertFalse(RLMHasCachedRealmForPath(defaultRealmURL().path))
  56. XCTAssertFalse(RLMHasCachedRealmForPath(testRealmURL().path))
  57. }
  58. resetRealmState()
  59. do {
  60. try FileManager.default.removeItem(atPath: testDir)
  61. } catch {
  62. XCTFail("Unable to delete realm files")
  63. }
  64. // Verify that there are no remaining realm files after the test
  65. let parentDir = (testDir as NSString).deletingLastPathComponent
  66. for url in FileManager.default.enumerator(atPath: parentDir)! {
  67. let url = url as! NSString
  68. XCTAssertNotEqual(url.pathExtension, "realm", "Lingering realm file at \(parentDir)/\(url)")
  69. assert(url.pathExtension != "realm")
  70. }
  71. }
  72. func dispatchSyncNewThread(block: @escaping () -> Void) {
  73. queue.async {
  74. autoreleasepool {
  75. block()
  76. }
  77. }
  78. queue.sync { }
  79. }
  80. /// Check whether two test objects are equal (refer to the same row in the same Realm), even if their models
  81. /// don't define a primary key.
  82. func assertEqual<O: Object>(_ o1: O?, _ o2: O?, fileName: StaticString = #file, lineNumber: UInt = #line) {
  83. if o1 == nil && o2 == nil {
  84. return
  85. }
  86. if let o1 = o1, let o2 = o2, o1.isSameObject(as: o2) {
  87. return
  88. }
  89. XCTFail("Objects expected to be equal, but weren't. First: \(String(describing: o1)), "
  90. + "second: \(String(describing: o2))", file: fileName, line: lineNumber)
  91. }
  92. /// Check whether two collections containing Realm objects are equal.
  93. func assertEqual<C: Collection>(_ c1: C, _ c2: C, fileName: StaticString = #file, lineNumber: UInt = #line)
  94. where C.Iterator.Element: Object {
  95. XCTAssertEqual(c1.count, c2.count, "Collection counts were incorrect", file: fileName, line: lineNumber)
  96. for (o1, o2) in zip(c1, c2) {
  97. assertEqual(o1, o2, fileName: fileName, lineNumber: lineNumber)
  98. }
  99. }
  100. func assertEqual<T: Equatable>(_ expected: [T?], _ actual: [T?], file: StaticString = #file, line: UInt = #line) {
  101. if expected.count != actual.count {
  102. XCTFail("assertEqual failed: (\"\(expected)\") is not equal to (\"\(actual)\")",
  103. file: file, line: line)
  104. return
  105. }
  106. XCTAssertEqual(expected.count, actual.count, "Collection counts were incorrect", file: file, line: line)
  107. for (e, a) in zip(expected, actual) where e != a {
  108. XCTFail("assertEqual failed: (\"\(expected)\") is not equal to (\"\(actual)\")",
  109. file: file, line: line)
  110. return
  111. }
  112. }
  113. func assertThrows<T>(_ block: @autoclosure () -> T, named: String? = RLMExceptionName,
  114. _ message: String? = nil, fileName: String = #file, lineNumber: UInt = #line) {
  115. exceptionThrown = true
  116. RLMAssertThrowsWithName(self, { _ = block() }, named, message, fileName, lineNumber)
  117. }
  118. func assertThrows<T>(_ block: @autoclosure () -> T, reason: String,
  119. _ message: String? = nil, fileName: String = #file, lineNumber: UInt = #line) {
  120. exceptionThrown = true
  121. RLMAssertThrowsWithReason(self, { _ = block() }, reason, message, fileName, lineNumber)
  122. }
  123. func assertThrows<T>(_ block: @autoclosure () -> T, reasonMatching regexString: String,
  124. _ message: String? = nil, fileName: String = #file, lineNumber: UInt = #line) {
  125. exceptionThrown = true
  126. RLMAssertThrowsWithReasonMatching(self, { _ = block() }, regexString, message, fileName, lineNumber)
  127. }
  128. func assertSucceeds(message: String? = nil, fileName: StaticString = #file,
  129. lineNumber: UInt = #line, block: () throws -> Void) {
  130. do {
  131. try block()
  132. } catch {
  133. XCTFail("Expected no error, but instead caught <\(error)>.",
  134. file: fileName, line: lineNumber)
  135. }
  136. }
  137. func assertFails<T>(_ expectedError: Realm.Error.Code, _ message: String? = nil,
  138. fileName: StaticString = #file, lineNumber: UInt = #line,
  139. block: () throws -> T) {
  140. do {
  141. _ = try block()
  142. XCTFail("Expected to catch <\(expectedError)>, but no error was thrown.",
  143. file: fileName, line: lineNumber)
  144. } catch let e as Realm.Error where e.code == expectedError {
  145. // Success!
  146. } catch {
  147. XCTFail("Expected to catch <\(expectedError)>, but instead caught <\(error)>.",
  148. file: fileName, line: lineNumber)
  149. }
  150. }
  151. func assertFails<T>(_ expectedError: Error, _ message: String? = nil,
  152. fileName: StaticString = #file, lineNumber: UInt = #line,
  153. block: () throws -> T) {
  154. do {
  155. _ = try block()
  156. XCTFail("Expected to catch <\(expectedError)>, but no error was thrown.",
  157. file: fileName, line: lineNumber)
  158. } catch let e where e._code == expectedError._code {
  159. // Success!
  160. } catch {
  161. XCTFail("Expected to catch <\(expectedError)>, but instead caught <\(error)>.",
  162. file: fileName, line: lineNumber)
  163. }
  164. }
  165. func assertNil<T>(block: @autoclosure() -> T?, _ message: String? = nil,
  166. fileName: StaticString = #file, lineNumber: UInt = #line) {
  167. XCTAssert(block() == nil, message ?? "", file: fileName, line: lineNumber)
  168. }
  169. func assertMatches(_ block: @autoclosure () -> String, _ regexString: String, _ message: String? = nil,
  170. fileName: String = #file, lineNumber: UInt = #line) {
  171. RLMAssertMatches(self, block, regexString, message, fileName, lineNumber)
  172. }
  173. private func realmFilePrefix() -> String {
  174. let name: String? = self.name
  175. return name!.trimmingCharacters(in: CharacterSet(charactersIn: "-[]"))
  176. }
  177. internal func testRealmURL() -> URL {
  178. return realmURLForFile("test.realm")
  179. }
  180. internal func defaultRealmURL() -> URL {
  181. return realmURLForFile("default.realm")
  182. }
  183. private func realmURLForFile(_ fileName: String) -> URL {
  184. let directory = URL(fileURLWithPath: testDir, isDirectory: true)
  185. return directory.appendingPathComponent(fileName, isDirectory: false)
  186. }
  187. }