ObjectiveCSupportTests.swift 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Foundation
  19. import Realm
  20. import RealmSwift
  21. import XCTest
  22. class ObjectiveCSupportTests: TestCase {
  23. func testSupport() {
  24. let realm = try! Realm()
  25. try! realm.write {
  26. realm.add(SwiftObject())
  27. return
  28. }
  29. let results = realm.objects(SwiftObject.self)
  30. let rlmResults = ObjectiveCSupport.convert(object: results)
  31. XCTAssert(rlmResults.isKind(of: RLMResults<AnyObject>.self))
  32. XCTAssertEqual(rlmResults.count, 1)
  33. XCTAssertEqual(unsafeBitCast(rlmResults.firstObject(), to: SwiftObject.self).intCol, 123)
  34. let list = List<SwiftObject>()
  35. list.append(SwiftObject())
  36. let rlmArray = ObjectiveCSupport.convert(object: list)
  37. XCTAssert(rlmArray.isKind(of: RLMArray<AnyObject>.self))
  38. XCTAssertEqual(unsafeBitCast(rlmArray.firstObject(), to: SwiftObject.self).floatCol, 1.23)
  39. XCTAssertEqual(rlmArray.count, 1)
  40. let rlmRealm = ObjectiveCSupport.convert(object: realm)
  41. XCTAssert(rlmRealm.isKind(of: RLMRealm.self))
  42. XCTAssertEqual(rlmRealm.allObjects("SwiftObject").count, 1)
  43. let sortDescriptor: RealmSwift.SortDescriptor = "property"
  44. XCTAssertEqual(sortDescriptor.keyPath,
  45. ObjectiveCSupport.convert(object: sortDescriptor).keyPath,
  46. "SortDescriptor.keyPath must be equal to RLMSortDescriptor.keyPath")
  47. XCTAssertEqual(sortDescriptor.ascending,
  48. ObjectiveCSupport.convert(object: sortDescriptor).ascending,
  49. "SortDescriptor.ascending must be equal to RLMSortDescriptor.ascending")
  50. }
  51. func testConfigurationSupport() {
  52. let realm = try! Realm()
  53. try! realm.write {
  54. realm.add(SwiftObject())
  55. }
  56. XCTAssertEqual(realm.configuration.fileURL,
  57. ObjectiveCSupport.convert(object: realm.configuration).fileURL,
  58. "Configuration.fileURL must be equal to RLMConfiguration.fileURL")
  59. XCTAssertEqual(realm.configuration.inMemoryIdentifier,
  60. ObjectiveCSupport.convert(object: realm.configuration).inMemoryIdentifier,
  61. "Configuration.inMemoryIdentifier must be equal to RLMConfiguration.inMemoryIdentifier")
  62. #if !SWIFT_PACKAGE
  63. XCTAssertEqual(realm.configuration.syncConfiguration?.realmURL,
  64. ObjectiveCSupport.convert(object: realm.configuration).syncConfiguration?.realmURL,
  65. "Configuration.syncConfiguration must be equal to RLMConfiguration.syncConfiguration")
  66. #endif
  67. XCTAssertEqual(realm.configuration.encryptionKey,
  68. ObjectiveCSupport.convert(object: realm.configuration).encryptionKey,
  69. "Configuration.encryptionKey must be equal to RLMConfiguration.encryptionKey")
  70. XCTAssertEqual(realm.configuration.readOnly,
  71. ObjectiveCSupport.convert(object: realm.configuration).readOnly,
  72. "Configuration.readOnly must be equal to RLMConfiguration.readOnly")
  73. XCTAssertEqual(realm.configuration.schemaVersion,
  74. ObjectiveCSupport.convert(object: realm.configuration).schemaVersion,
  75. "Configuration.schemaVersion must be equal to RLMConfiguration.schemaVersion")
  76. XCTAssertEqual(realm.configuration.deleteRealmIfMigrationNeeded,
  77. ObjectiveCSupport.convert(object: realm.configuration).deleteRealmIfMigrationNeeded,
  78. "Configuration.deleteRealmIfMigrationNeeded must be equal to RLMConfiguration.deleteRealmIfMigrationNeeded")
  79. }
  80. }