CodableGeoPointTests.swift 1.9 KB

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