withDateFormatString added to XMLDecoder; top array xml test case.
This commit is contained in:
Родитель
da36ef16e6
Коммит
4bc15951a5
|
@ -108,6 +108,13 @@ open class XMLDecoder {
|
|||
let decoder = _XMLDecoder(referencing: topLevel, options: self.options)
|
||||
return try T(from: decoder)
|
||||
}
|
||||
|
||||
open func withDateFormatString(_ dateFormatString: String) -> XMLDecoder {
|
||||
let dateFormatter = DateFormatter()
|
||||
dateFormatter.dateFormat = dateFormatString
|
||||
self.dateDecodingStrategy = .formatted(dateFormatter)
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
//
|
||||
// JustArrayXML.swift
|
||||
// azureSwiftRuntime
|
||||
//
|
||||
// Created by Vladimir Shcherbakov on 1/26/18.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import XCTest
|
||||
import azureSwiftRuntime
|
||||
|
||||
|
||||
class TopLevelXMLTests: XCTestCase {
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
// Put teardown code here. This method is called after the invocation of each test method in the class.
|
||||
super.tearDown()
|
||||
}
|
||||
|
||||
func testTopLevelArray() {
|
||||
do {
|
||||
let res = try XMLDecoder().decode([Int?]?.self, from: xmlTopLevelArray.data(using: .utf8)!)
|
||||
if let array = res {
|
||||
for val in array {
|
||||
print (val ?? "-")
|
||||
}
|
||||
} else {
|
||||
print("Array is empty")
|
||||
}
|
||||
|
||||
} catch {
|
||||
print ("=== Error:", error)
|
||||
XCTFail(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
|
||||
func testTopLevelArrayOfBooks() {
|
||||
do {
|
||||
let res = try XMLDecoder().decode([Book?]?.self, from: xmlTopLevelArrayOfBooks.data(using: .utf8)!)
|
||||
if let books = res {
|
||||
for book in books {
|
||||
print ("id: ", book?.id ?? "-")
|
||||
print ("name: ", book?.name ?? "-")
|
||||
}
|
||||
} else {
|
||||
print("Array is empty")
|
||||
}
|
||||
|
||||
} catch {
|
||||
print ("=== Error:", error)
|
||||
XCTFail(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
//
|
||||
// TopLevelArrayXML.swift
|
||||
// azureSwiftRuntime
|
||||
//
|
||||
// Created by Vladimir Shcherbakov on 1/26/18.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
let xmlTopLevelArray =
|
||||
"""
|
||||
<Array>
|
||||
<val>1</val>
|
||||
<val>2</val>
|
||||
<val>3</val>
|
||||
<val></val>
|
||||
<val>5</val>
|
||||
</Array>
|
||||
"""
|
||||
|
||||
struct Book : Decodable {
|
||||
let name: String?
|
||||
let id: Int?
|
||||
}
|
||||
|
||||
let xmlTopLevelArrayOfBooks =
|
||||
"""
|
||||
<Books>
|
||||
<Book>
|
||||
<name>One</name>
|
||||
<id>1</id>
|
||||
</Book>
|
||||
<Book>
|
||||
<name>Two</name>
|
||||
<id>2</id>
|
||||
</Book>
|
||||
<Book>
|
||||
<name>Three</name>
|
||||
<id>3</id>
|
||||
</Book>
|
||||
<Book>
|
||||
<name>Four</name>
|
||||
<id></id>
|
||||
</Book>
|
||||
<Book/>
|
||||
</Books>
|
||||
"""
|
Загрузка…
Ссылка в новой задаче