Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted URL coding logic into
URLBox
with corresponding unit tests
- Loading branch information
1 parent
a6a7233
commit d21bb2e
Showing
6 changed files
with
234 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// URLBox.swift | ||
// XMLCoder | ||
// | ||
// Created by Vincent Esche on 12/21/18. | ||
// | ||
|
||
import Foundation | ||
|
||
struct URLBox: Equatable { | ||
typealias Unboxed = URL | ||
|
||
let unboxed: Unboxed | ||
|
||
init(_ unboxed: Unboxed) { | ||
self.unboxed = unboxed | ||
} | ||
|
||
init?(xmlString: String) { | ||
guard let unboxed = Unboxed(string: xmlString) else { | ||
return nil | ||
} | ||
self.init(unboxed) | ||
} | ||
|
||
func unbox() -> Unboxed { | ||
return self.unboxed | ||
} | ||
} | ||
|
||
extension URLBox: Box { | ||
var isNull: Bool { | ||
return false | ||
} | ||
|
||
var isFragment: Bool { | ||
return true | ||
} | ||
|
||
func xmlString() -> String? { | ||
return self.unboxed.absoluteString | ||
} | ||
} | ||
|
||
extension URLBox: SimpleBox { | ||
|
||
} | ||
|
||
extension URLBox: CustomStringConvertible { | ||
var description: String { | ||
return self.unboxed.description | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// URLBoxTests.swift | ||
// XMLCoderTests | ||
// | ||
// Created by Vincent Esche on 12/21/18. | ||
// | ||
|
||
import XCTest | ||
@testable import XMLCoder | ||
|
||
class URLBoxTests: XCTestCase { | ||
typealias Boxed = URLBox | ||
|
||
func testUnbox() { | ||
let values: [Boxed.Unboxed] = [ | ||
URL(string: "file:///")!, | ||
URL(string: "http://example.com")!, | ||
] | ||
|
||
for unboxed in values { | ||
let box = Boxed(unboxed) | ||
XCTAssertEqual(box.unbox(), unboxed) | ||
} | ||
} | ||
|
||
func testXMLString() { | ||
let values: [(Boxed.Unboxed, String)] = [ | ||
(URL(string: "file:///")!, "file:///"), | ||
(URL(string: "http://example.com")!, "http://example.com"), | ||
] | ||
|
||
for (bool, string) in values { | ||
let box = Boxed(bool) | ||
XCTAssertEqual(box.xmlString(), string) | ||
} | ||
} | ||
|
||
func testValidValues() { | ||
let values: [String] = [ | ||
"file:///", | ||
"http://example.com", | ||
] | ||
|
||
for string in values { | ||
let box = Boxed(xmlString: string) | ||
XCTAssertNotNil(box) | ||
} | ||
} | ||
|
||
func testInvalidValues() { | ||
let values: [String] = [ | ||
"foo\nbar", | ||
"", | ||
] | ||
|
||
for string in values { | ||
let box = Boxed(xmlString: string) | ||
XCTAssertNil(box) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// URLTests.swift | ||
// XMLCoderTests | ||
// | ||
// Created by Vincent Esche on 12/19/18. | ||
// | ||
|
||
import XCTest | ||
@testable import XMLCoder | ||
|
||
class URLTests: XCTestCase { | ||
typealias Value = URL | ||
|
||
struct Container: Codable, Equatable { | ||
let value: Value | ||
} | ||
|
||
let values: [(Value, String)] = [ | ||
(URL(string: "file:///")!, "file:///"), | ||
(URL(string: "http://example.com")!, "http://example.com"), | ||
] | ||
|
||
func testAttribute() { | ||
let decoder = XMLDecoder() | ||
let encoder = XMLEncoder() | ||
|
||
encoder.nodeEncodingStrategy = .custom { codableType, _ in | ||
return { _ in .attribute } | ||
} | ||
|
||
for (value, xmlString) in values { | ||
do { | ||
let xmlString = | ||
""" | ||
<container value="\(xmlString)" /> | ||
""" | ||
let xmlData = xmlString.data(using: .utf8)! | ||
|
||
let decoded = try decoder.decode(Container.self, from: xmlData) | ||
XCTAssertEqual(decoded.value, value) | ||
|
||
let encoded = try encoder.encode(decoded, withRootKey: "container") | ||
XCTAssertEqual(String(data: encoded, encoding: .utf8)!, xmlString) | ||
} catch { | ||
XCTAssert(false, "failed to decode test xml: \(error)") | ||
} | ||
} | ||
} | ||
|
||
func testElement() { | ||
let decoder = XMLDecoder() | ||
let encoder = XMLEncoder() | ||
|
||
encoder.outputFormatting = [.prettyPrinted] | ||
|
||
for (value, xmlString) in values { | ||
do { | ||
let xmlString = | ||
""" | ||
<container> | ||
<value>\(xmlString)</value> | ||
</container> | ||
""" | ||
let xmlData = xmlString.data(using: .utf8)! | ||
|
||
let decoded = try decoder.decode(Container.self, from: xmlData) | ||
XCTAssertEqual(decoded.value, value) | ||
|
||
let encoded = try encoder.encode(decoded, withRootKey: "container") | ||
XCTAssertEqual(String(data: encoded, encoding: .utf8)!, xmlString) | ||
} catch { | ||
XCTAssert(false, "failed to decode test xml: \(error)") | ||
} | ||
} | ||
} | ||
|
||
static var allTests = [ | ||
("testAttribute", testAttribute), | ||
("testElement", testElement), | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters