ConditionalConformanceTests.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. func testDocumentIDOfString() {
  30. struct Model: Codable, Equatable, Hashable {
  31. @DocumentID var x: String?
  32. }
  33. XCTAssertTrue(Model(x: "42") == Model(x: "42"))
  34. XCTAssertFalse(Model(x: "42") == Model(x: "1"))
  35. let dict: [Model: String] = [Model(x: "42"): "foo"]
  36. XCTAssertEqual("foo", dict[Model(x: "42")])
  37. }
  38. func testDocumentIDOfDocumentReference() {
  39. // This works because `FIRDocumentReference` implements a `hash` selector
  40. // and that's automatically bridged to conform to Swift `Hashable`.
  41. struct Model: Codable, Equatable, Hashable {
  42. @DocumentID var x: DocumentReference?
  43. }
  44. let doc1 = FSTTestDocRef("abc/xyz")
  45. let doc2 = FSTTestDocRef("abc/xyz")
  46. let doc3 = FSTTestDocRef("abc/def")
  47. XCTAssertTrue(Model(x: doc1) == Model(x: doc2))
  48. XCTAssertTrue(Model(x: doc1) != Model(x: doc3))
  49. XCTAssertFalse(Model(x: doc1) == Model(x: doc3))
  50. let dict: [Model: String] = [Model(x: doc1): "foo"]
  51. XCTAssertEqual("foo", dict[Model(x: doc2)])
  52. }
  53. func testExplicitNull() {
  54. struct Model: Codable, Equatable, Hashable {
  55. @ExplicitNull var x: Int?
  56. }
  57. XCTAssertTrue(Model(x: 42) == Model(x: 42))
  58. XCTAssertFalse(Model(x: 42) == Model(x: 1))
  59. let dict: [Model: String] = [Model(x: 42): "foo"]
  60. XCTAssertEqual("foo", dict[Model(x: 42)])
  61. }
  62. func testServerTimestampOfTimestamp() {
  63. struct Model: Codable, Equatable, Hashable {
  64. @ServerTimestamp var x: Timestamp?
  65. }
  66. let ts1 = Timestamp(seconds: 123, nanoseconds: 456)
  67. let ts2 = ts1.copy() as! Timestamp
  68. let ts3 = Timestamp(seconds: 789, nanoseconds: 0)
  69. XCTAssertTrue(Model(x: ts1) == Model(x: ts2))
  70. XCTAssertTrue(Model(x: ts1) != Model(x: ts3))
  71. XCTAssertFalse(Model(x: ts1) == Model(x: ts3))
  72. let dict: [Model: String] = [Model(x: ts1): "foo"]
  73. XCTAssertEqual("foo", dict[Model(x: ts2)])
  74. }
  75. func testServerTimestampOfDate() {
  76. struct Model: Codable, Equatable, Hashable {
  77. @ServerTimestamp var x: Date?
  78. }
  79. let ts1 = Date(timeIntervalSince1970: 42.0)
  80. let ts2 = Date(timeIntervalSince1970: 42.0)
  81. let ts3 = Date(timeIntervalSince1970: 100.0)
  82. XCTAssertTrue(Model(x: ts1) == Model(x: ts2))
  83. XCTAssertTrue(Model(x: ts1) != Model(x: ts3))
  84. XCTAssertFalse(Model(x: ts1) == Model(x: ts3))
  85. let dict: [Model: String] = [Model(x: ts1): "foo"]
  86. XCTAssertEqual("foo", dict[Model(x: ts2)])
  87. }
  88. }