Skip to content

Commit

Permalink
Merge pull request #8 from elegantchaos/issue/3-add-tests-for-url
Browse files Browse the repository at this point in the history
Added encoding and decoding tests for URLs.
  • Loading branch information
samdeane authored Feb 22, 2018
2 parents 5689135 + 4e77665 commit 050c5a5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Tests/DictionaryCodingTests/DictionaryDecodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,37 @@ class DictionaryDecodingTests: XCTestCase {
XCTAssertThrowsError(try decoder.decode(Test.self, from: ["value" : URL(fileURLWithPath: "/test")]))
}

func testDecodingURL() throws {
// if we're expecting a URL, we should be able to cope with getting a string, URL or NSURL
// if we're expecting a UUID, but are given a String or a CFUUID, we should be able to cope
struct Test : Decodable {
let value : URL
}

let decoder = DictionaryDecoder()

let url = URL(string: "http://elegantchaos.com")!
let decoded1 = try decoder.decode(Test.self, from: ["value" : url])
XCTAssertEqual(decoded1.value, url)

let decoded2 = try decoder.decode(Test.self, from: ["value" : url.absoluteString])
XCTAssertEqual(decoded2.value, url)

let decoded3 = try decoder.decode(Test.self, from: ["value" : NSURL(string: url.absoluteString)!])
XCTAssertEqual(decoded3.value, url)
}


static var allTests = [
("testDecodingAllTheTypes", testDecodingAllTheTypes),
("testDecodingNSDictionary", testDecodingNSDictionary),
("testDecodingCFDictionary", testDecodingCFDictionary),
("testFailureWithMissingKeys", testFailureWithMissingKeys),
("testDecodingOptionalValues", testDecodingOptionalValues),
("testDecodingWithDefaults", testDecodingWithDefaults),
("testDecodingStringFromURL", testDecodingStringFromURL),
("testDecodingStringFromUUID", testDecodingStringFromUUID),
("testDecodingUUID", testDecodingUUID),
("testDecodingURL", testDecodingURL),
]
}
15 changes: 15 additions & 0 deletions Tests/DictionaryCodingTests/DictionaryEncodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,27 @@ class DictionaryEncodingTests: XCTestCase {
XCTAssertEqual(encoded.allKeys.count, 1)
}

func testEncodingURL() throws {
struct Test : Encodable {
let value : URL
}

let string = "http://elegantchaos.com"
let test = Test(value: URL(string: string)!)
let encoder = DictionaryEncoder()
let encoded = try encoder.encode(test) as [String:Any]

// currently URLs are encoded as strings
XCTAssertEqual(encoded["value"] as? String, string)
}

static var allTests = [
("testEncodingDataFormats", testEncodingDataFormats),
("testEncodingDateFormats", testEncodingDateFormats),
("testEncodingAllTheTypes", testEncodingAllTheTypes),
("testEncodingAsNSDictionary", testEncodingAsNSDictionary),
("testEncodingAsSwiftDictionary", testEncodingAsSwiftDictionary),
("testEncodingOptionalValues", testEncodingOptionalValues),
("testEncodingURL", testEncodingURL),
]
}

0 comments on commit 050c5a5

Please sign in to comment.