CodableGeoPointTests.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright 2018 Google
  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 CodableGeoPointTests: XCTestCase {
  21. func testGeoPointEncodes() {
  22. let geoPoint = GeoPoint(latitude: 37.77493, longitude: -122.41942)
  23. let jsonData = try! JSONEncoder().encode(geoPoint)
  24. let json = String(data: jsonData, encoding: .utf8)!
  25. // The ordering of attributes in the JSON output is not guaranteed, nor is the rounding of
  26. // the values so just verify that each required property is present and that the value
  27. // starts as expected.
  28. XCTAssert(json.contains("\"latitude\":37."))
  29. XCTAssert(json.contains("\"longitude\":-122."))
  30. }
  31. func testGeoPointDecodes() {
  32. let json = """
  33. {
  34. "latitude": 37.77493,
  35. "longitude": -122.41942
  36. }
  37. """
  38. let jsonData: Data = json.data(using: .utf8)!
  39. let geoPoint = try! JSONDecoder().decode(GeoPoint.self, from: jsonData)
  40. XCTAssertEqual(37.77493, geoPoint.latitude, accuracy: 0.0001)
  41. XCTAssertEqual(-122.41942, geoPoint.longitude, accuracy: 0.0001)
  42. }
  43. func testGeoPointIsHashable() {
  44. let geoPoint = GeoPoint(latitude: 37.77493, longitude: -122.41942)
  45. let geoPoint2 = GeoPoint(latitude: 37.77493, longitude: -122.41942)
  46. let dictionary: [GeoPoint: String] = [geoPoint: "foo"]
  47. XCTAssertEqual("foo", dictionary[geoPoint2])
  48. }
  49. }