Skip to content

Commit

Permalink
Test round-tripping GeoJSON-T
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 committed Dec 15, 2021
1 parent 50908c9 commit 8659740
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions Tests/TurfTests/GeoJSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,39 @@ class GeoJSONTests: XCTestCase {
}

func testForeignMemberCoding(in object: GeoJSONObject) throws {
let today = ISO8601DateFormatter().string(from: Date())

let data = try JSONEncoder().encode(object)
guard var json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any?] else {
return
}
json["title"] = "Example Feature"

// Convert the GeoJSON object to valid GeoJSON-T <https://github.com/kgeographer/geojson-t/>.
XCTAssert(json["when"] == nil)
json["when"] = [
"timespans": [
[
// Starts and ends sometime today.
"start": [
"in": today,
],
"end": [
"in": today,
],
],
],
"duration": "PT1M", // 1 minute long
"label": "Today",
]

let modifiedData = try JSONSerialization.data(withJSONObject: json, options: [])
let modifiedObject = try JSONDecoder().decode(GeoJSONObject.self, from: modifiedData)

let roundTrippedData = try JSONEncoder().encode(modifiedObject)
let roundTrippedJSON = try JSONSerialization.jsonObject(with: roundTrippedData, options: []) as? [String: Any?]
XCTAssertEqual(roundTrippedJSON?["title"] as? String, "Example Feature")

let when = try XCTUnwrap(roundTrippedJSON?["when"] as? [String: Any?])
XCTAssertEqual(when as NSDictionary, json["when"] as? NSDictionary)
}

func testForeignMemberCoding() throws {
Expand Down

0 comments on commit 8659740

Please sign in to comment.