Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to increase test coverage #63

Merged
merged 4 commits into from Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Tests/XMLCoderTests/Minimal/BoolTests.swift
Expand Up @@ -76,6 +76,7 @@ class BoolTests: XCTestCase {
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
]
Expand Down
121 changes: 121 additions & 0 deletions Tests/XMLCoderTests/Minimal/DataTests.swift
Expand Up @@ -76,8 +76,129 @@ class DataTests: XCTestCase {
}
}

func testStrategy() throws {
let decoder = XMLDecoder()
let encoder = XMLEncoder()

encoder.outputFormatting = [.prettyPrinted]

for (value, xmlString) in values {
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)
}
}

func testKeyFormated() throws {
let decoder = XMLDecoder()
let encoder = XMLEncoder()

decoder.dataDecodingStrategy = .keyFormatted { $0.stringValue.data(using: .utf8) }

encoder.outputFormatting = [.prettyPrinted]

for (_, xmlString) in values {
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, Data("value".utf8))
}
}

func testKeyFormatedError() throws {
let decoder = XMLDecoder()
let encoder = XMLEncoder()

decoder.dataDecodingStrategy = .keyFormatted { codingKey in
return Data(base64Encoded: codingKey.stringValue)
}

encoder.outputFormatting = [.prettyPrinted]

for (_, xmlString) in values {
let xmlString =
"""
<container>
<value>\(xmlString)</value>
</container>
"""
let xmlData = xmlString.data(using: .utf8)!

XCTAssertThrowsError(try decoder.decode(Container.self, from: xmlData))
}
}

func testKeyFormatedCouldNotDecodeError() throws {
let decoder = XMLDecoder()
let encoder = XMLEncoder()

decoder.dataDecodingStrategy = .keyFormatted { codingKey in
return Data(base64Encoded: codingKey.stringValue)
}

encoder.outputFormatting = [.prettyPrinted]

for (_, xmlString) in values {
let xmlString =
"""
<container>
<value>\(xmlString)0</value>
<value>\(xmlString)0</value>
</container>
"""
let xmlData = xmlString.data(using: .utf8)!

XCTAssertThrowsError(try decoder.decode(Container.self, from: xmlData))
}
}

func testKeyFormatedNoPathError() throws {
let decoder = XMLDecoder()
let encoder = XMLEncoder()

decoder.dataDecodingStrategy = .keyFormatted { codingKey in
return Data(base64Encoded: codingKey.stringValue)
}

encoder.outputFormatting = [.prettyPrinted]

for (_, _) in values {
let xmlString =
"""
<container>
<value>12</value>
</container>
"""
let xmlData = xmlString.data(using: .utf8)!

XCTAssertThrowsError(try decoder.decode(Container.self, from: xmlData))
}
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
("testKeyFormated", testKeyFormated),
("testKeyFormatedError", testKeyFormatedError),
("testKeyFormatedCouldNotDecodeError", testKeyFormatedCouldNotDecodeError),
("testKeyFormatedNoPathError", testKeyFormatedNoPathError),
]
}
80 changes: 80 additions & 0 deletions Tests/XMLCoderTests/Minimal/DateTests.swift
Expand Up @@ -78,8 +78,88 @@ class DateTests: XCTestCase {
}
}

func testKeyFormatedError() throws {
let decoder = XMLDecoder()
let encoder = XMLEncoder()

decoder.dateDecodingStrategy = .keyFormatted { _ in
let formatter = DateFormatter()
formatter.dateFormat = "value"
return formatter
}

encoder.outputFormatting = [.prettyPrinted]

for (_, xmlString) in values {
let xmlString =
"""
<container>
<value>\(xmlString)</value>
</container>
"""
let xmlData = xmlString.data(using: .utf8)!

XCTAssertThrowsError(try decoder.decode(Container.self, from: xmlData))
}
}

func testKeyFormatedCouldNotDecodeError() throws {
let decoder = XMLDecoder()
let encoder = XMLEncoder()

decoder.dateDecodingStrategy = .keyFormatted { _ in
let formatter = DateFormatter()
formatter.dateFormat = "value"
return formatter
}

encoder.outputFormatting = [.prettyPrinted]

for (_, xmlString) in values {
let xmlString =
"""
<container>
<value>\(xmlString)</value>
<value>\(xmlString)</value>
</container>
"""
let xmlData = xmlString.data(using: .utf8)!

XCTAssertThrowsError(try decoder.decode(Container.self, from: xmlData))
}
}

func testKeyFormatedNoPathError() throws {
let decoder = XMLDecoder()
let encoder = XMLEncoder()

decoder.dateDecodingStrategy = .keyFormatted { _ in
let formatter = DateFormatter()
formatter.dateFormat = "value"
return formatter
}

encoder.outputFormatting = [.prettyPrinted]

for (_, _) in values {
let xmlString =
"""
<container>
<value>12</value>
</container>
"""
let xmlData = xmlString.data(using: .utf8)!

XCTAssertThrowsError(try decoder.decode(Container.self, from: xmlData))
}
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
("testKeyFormatedError", testKeyFormatedError),
("testKeyFormatedCouldNotDecodeError", testKeyFormatedCouldNotDecodeError),
("testKeyFormatedNoPathError", testKeyFormatedNoPathError),
]
}
1 change: 1 addition & 0 deletions Tests/XMLCoderTests/Minimal/DecimalTests.swift
Expand Up @@ -77,6 +77,7 @@ class DecimalTests: XCTestCase {
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
]
Expand Down
1 change: 1 addition & 0 deletions Tests/XMLCoderTests/Minimal/FloatTests.swift
Expand Up @@ -77,6 +77,7 @@ class FloatTests: XCTestCase {
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
]
Expand Down
1 change: 1 addition & 0 deletions Tests/XMLCoderTests/Minimal/IntTests.swift
Expand Up @@ -76,6 +76,7 @@ class IntTests: XCTestCase {
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
]
Expand Down
1 change: 1 addition & 0 deletions Tests/XMLCoderTests/Minimal/StringTests.swift
Expand Up @@ -81,6 +81,7 @@ class StringTests: XCTestCase {
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
]
Expand Down
1 change: 1 addition & 0 deletions Tests/XMLCoderTests/Minimal/UIntTests.swift
Expand Up @@ -76,6 +76,7 @@ class UIntTests: XCTestCase {
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
]
Expand Down
1 change: 1 addition & 0 deletions Tests/XMLCoderTests/Minimal/URLTests.swift
Expand Up @@ -76,6 +76,7 @@ class URLTests: XCTestCase {
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
]
Expand Down