ConditionalConformanceTests.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright 2020 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import FirebaseFirestore
  17. import FirebaseFirestoreSwift
  18. import Foundation
  19. import XCTest
  20. class ConditionalConformanceTests: XCTestCase {
  21. func testBaseline() {
  22. struct Model: Codable, Equatable, Hashable {
  23. let x: Int
  24. }
  25. var dict = [Model: String]()
  26. dict[Model(x: 42)] = "foo"
  27. XCTAssertEqual("foo", dict[Model(x: 42)])
  28. }
  29. #if compiler(>=5.1)
  30. func testDocumentIDOfString() {
  31. struct Model: Codable, Equatable, Hashable {
  32. @DocumentID var x: String?
  33. }
  34. XCTAssertTrue(Model(x: "42") == Model(x: "42"))
  35. XCTAssertFalse(Model(x: "42") == Model(x: "1"))
  36. let dict: [Model: String] = [Model(x: "42"): "foo"]
  37. XCTAssertEqual("foo", dict[Model(x: "42")])
  38. }
  39. func testDocumentIDOfDocumentReference() {
  40. // This works because `FIRDocumentReference` implements a `hash` selector
  41. // and that's automatically bridged to conform to Swift `Hashable`.
  42. struct Model: Codable, Equatable, Hashable {
  43. @DocumentID var x: DocumentReference?
  44. }
  45. let doc1 = FSTTestDocRef("abc/xyz")
  46. let doc2 = FSTTestDocRef("abc/xyz")
  47. let doc3 = FSTTestDocRef("abc/def")
  48. XCTAssertTrue(Model(x: doc1) == Model(x: doc2))
  49. XCTAssertTrue(Model(x: doc1) != Model(x: doc3))
  50. XCTAssertFalse(Model(x: doc1) == Model(x: doc3))
  51. let dict: [Model: String] = [Model(x: doc1): "foo"]
  52. XCTAssertEqual("foo", dict[Model(x: doc2)])
  53. }
  54. func testExplicitNull() {
  55. struct Model: Codable, Equatable, Hashable {
  56. @ExplicitNull var x: Int?
  57. }
  58. XCTAssertTrue(Model(x: 42) == Model(x: 42))
  59. XCTAssertFalse(Model(x: 42) == Model(x: 1))
  60. let dict: [Model: String] = [Model(x: 42): "foo"]
  61. XCTAssertEqual("foo", dict[Model(x: 42)])
  62. }
  63. func testServerTimestampOfTimestamp() {
  64. struct Model: Codable, Equatable, Hashable {
  65. @ServerTimestamp var x: Timestamp?
  66. }
  67. let ts1 = Timestamp(seconds: 123, nanoseconds: 456)
  68. let ts2 = ts1.copy() as! Timestamp
  69. let ts3 = Timestamp(seconds: 789, nanoseconds: 0)
  70. XCTAssertTrue(Model(x: ts1) == Model(x: ts2))
  71. XCTAssertTrue(Model(x: ts1) != Model(x: ts3))
  72. XCTAssertFalse(Model(x: ts1) == Model(x: ts3))
  73. let dict: [Model: String] = [Model(x: ts1): "foo"]
  74. XCTAssertEqual("foo", dict[Model(x: ts2)])
  75. }
  76. func testServerTimestampOfDate() {
  77. struct Model: Codable, Equatable, Hashable {
  78. @ServerTimestamp var x: Date?
  79. }
  80. let ts1 = Date(timeIntervalSince1970: 42.0)
  81. let ts2 = Date(timeIntervalSince1970: 42.0)
  82. let ts3 = Date(timeIntervalSince1970: 100.0)
  83. XCTAssertTrue(Model(x: ts1) == Model(x: ts2))
  84. XCTAssertTrue(Model(x: ts1) != Model(x: ts3))
  85. XCTAssertFalse(Model(x: ts1) == Model(x: ts3))
  86. let dict: [Model: String] = [Model(x: ts1): "foo"]
  87. XCTAssertEqual("foo", dict[Model(x: ts2)])
  88. }
  89. #endif // compiler(>=5.1)
  90. }