ConditionalConformanceTests.swift 3.3 KB

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