Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0a8a07a
Bool: Support init from y,n,yes,no any case, false,true added upperca…
JoeMatt Jan 31, 2019
ae4d50a
KeyedEncoding: Add new string formatters, capitalized, lowercased, up…
JoeMatt Jan 31, 2019
e65d30b
SharedBoxProtocol: Generalize for any Box inheritance
JoeMatt Jan 31, 2019
99fde81
Remove junk string in BreakfastTest xml
JoeMatt Jan 31, 2019
38b79a2
Element coding, remove empty brackets if element string value is empt…
JoeMatt Jan 31, 2019
0300dd1
Add DynamicNodeEncoding protocol
JoeMatt Jan 31, 2019
14549a6
XMLEncoder: Add both option to value encoding, refactor encoder
JoeMatt Jan 31, 2019
f8c594a
XMLDecoder.XMLDecodingStorage refactor initial value to inline var
JoeMatt Jan 31, 2019
2ee4b03
Clear up most swiftlint warnings
JoeMatt Jan 31, 2019
f5f6f8d
Rename left over values from different branch
JoeMatt Jan 31, 2019
e86be14
test: Add coding / decoding tests to DynamicNodeEncoding
JoeMatt Jan 31, 2019
5e94557
Convrted BooksTest to DynamicNodeEncoding, tests string equality
JoeMatt Jan 31, 2019
39b5999
Swiftfomat corrections
JoeMatt Jan 31, 2019
f5a8578
Add test coverage for String+Extensions
JoeMatt Jan 31, 2019
a110b83
Fix lowercasingFirstLetter was capitalized
JoeMatt Feb 6, 2019
f60bb38
Apply SwiftFormat fix to UnkeyedDecodingContainer
MaxDesiatov Feb 6, 2019
110a66b
Add isEmpty property to KeyedStorage
MaxDesiatov Feb 6, 2019
2061b34
Merge branch 'master' into feature/dynamicNodeEncoding
MaxDesiatov Feb 6, 2019
d29ce8f
Remove redundant extension of DynamicNodeEncoding
MaxDesiatov Feb 9, 2019
32f58ee
Update DynamicNodeEncoding extensions, fix test
MaxDesiatov Feb 9, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: Add coding / decoding tests to DynamicNodeEncoding
Had removed them since I was testing intrinsics with attributes. Needed to wrap cateogy value in an element tag again.

Also appears the hack for decoding nested arrays is no longer required so removed the complex decoding of Category
  • Loading branch information
JoeMatt committed Feb 6, 2019
commit e86be143c43550ec8b18a10ef89af7ab8edac115
141 changes: 90 additions & 51 deletions Tests/XMLCoderTests/DynamicNodeEncodingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ let libraryXML = """
<book id="123">
<id>123</id>
<title>Cat in the Hat</title>
<category main="Y">Kids</category>
<category main="N">Wildlife</category>
<category main="Y"><value>Kids</value></category>
<category main="N"><value>Wildlife</value></category>
</book>
<book id="456">
<id>789</id>
<title>1984</title>
<category main="Y">Classics</category>
<category main="N">News</category>
<category main="Y"><value>Classics</value></category>
<category main="N"><value>News</value></category>
</book>
</library>
""".data(using: .utf8)!
Expand Down Expand Up @@ -56,39 +56,13 @@ private struct Book: Codable, Equatable, DynamicNodeEncoding {
}
}

extension Book {
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(UInt.self, forKey: .id)
title = try container.decode(String.self, forKey: .title)

var nested = try container.nestedUnkeyedContainer(forKey: .categories)

var decoded = [Category]()
var finished = false

while !finished {
do {
let another = try nested.decode(Category.self)
decoded.append(another)
} catch DecodingError.valueNotFound {
finished = true
} catch {
throw error
}
}

categories = decoded
}
}

private struct Category: Codable, Equatable, DynamicNodeEncoding {
let main: Bool
let value: String

private enum CodingKeys: String, CodingKey {
case main
case value = ""
case value
}

static func nodeEncoding(forKey key: CodingKey) -> XMLEncoder.NodeEncoding {
Expand All @@ -101,26 +75,6 @@ private struct Category: Codable, Equatable, DynamicNodeEncoding {
}
}

private func decodeArray<T>(_ decoder: Decoder, decode: (inout UnkeyedDecodingContainer) throws -> T) throws -> [T] {
let keyedContainer = try decoder.container(keyedBy: CodingKeys.self)
var container = try keyedContainer.nestedUnkeyedContainer(forKey: .value)

var decoded = [T]()
var finished = false

while !finished {
do {
decoded.append(try decode(&container))
} catch DecodingError.valueNotFound {
finished = true
} catch {
throw error
}
}

return decoded
}

final class DynamicNodeEncodingTest: XCTestCase {
func testEncode() {
let book1 = Book(id: 123,
Expand Down Expand Up @@ -153,7 +107,92 @@ final class DynamicNodeEncodingTest: XCTestCase {
}
}

func testDecode() {
do {

let decoder = XMLDecoder()
decoder.errorContextLength = 10

let library = try decoder.decode(Library.self, from: libraryXML)
XCTAssertEqual(library.books.count, 2)
XCTAssertEqual(library.count, 2)

let book1 = library.books[0]
XCTAssertEqual(book1.id, 123)
XCTAssertEqual(book1.title, "Cat in the Hat")

let book1Categories = book1.categories
XCTAssertEqual(book1Categories.count, 2)
XCTAssertEqual(book1Categories[0].value, "Kids")
XCTAssertTrue(book1Categories[0].main)
XCTAssertEqual(book1Categories[1].value, "Wildlife")
XCTAssertFalse(book1Categories[1].main)

let book2 = library.books[1]
// XCTAssertEqual(book2.id, 456)
XCTAssertEqual(book2.title, "1984")

let book2Categories = book2.categories
XCTAssertEqual(book2Categories.count, 2)
XCTAssertEqual(book2Categories[0].value, "Classics")
XCTAssertTrue(book2Categories[0].main)
XCTAssertEqual(book2Categories[1].value, "News")
XCTAssertFalse(book2Categories[1].main)
} catch {
print("Test threw error: " + error.localizedDescription)
XCTFail(error.localizedDescription)
}
}

func testEncodeDecode() {
do {
let decoder = XMLDecoder()
decoder.errorContextLength = 10

let encoder = XMLEncoder()
encoder.outputFormatting = [.prettyPrinted]

let library = try decoder.decode(Library.self, from: libraryXML)
XCTAssertEqual(library.books.count, 2)
XCTAssertEqual(library.count, 2)

let book1 = library.books[0]
XCTAssertEqual(book1.id, 123)
XCTAssertEqual(book1.title, "Cat in the Hat")

let book1Categories = book1.categories
XCTAssertEqual(book1Categories.count, 2)
XCTAssertEqual(book1Categories[0].value, "Kids")
XCTAssertTrue(book1Categories[0].main)
XCTAssertEqual(book1Categories[1].value, "Wildlife")
XCTAssertFalse(book1Categories[1].main)

let book2 = library.books[1]
// XCTAssertEqual(book2.id, 456)
XCTAssertEqual(book2.title, "1984")

let book2Categories = book2.categories
XCTAssertEqual(book2Categories.count, 2)
XCTAssertEqual(book2Categories[0].value, "Classics")
XCTAssertTrue(book2Categories[0].main)
XCTAssertEqual(book2Categories[1].value, "News")
XCTAssertFalse(book2Categories[1].main)

let data = try encoder.encode(library, withRootKey: "library",
header: XMLHeader(version: 1.0,
encoding: "UTF-8"))
print(String(data: data, encoding: .utf8)!)
let library2 = try decoder.decode(Library.self, from: data)
XCTAssertEqual(library, library2)
} catch {
print("Test threw error: " + error.localizedDescription)
XCTFail(error.localizedDescription)
}
}

static var allTests = [
("testEncode", testEncode),
("testDecode", testDecode),
("testEncodeDecode", testEncodeDecode),
]
}