|
| 1 | +// |
| 2 | +// SOAPSample.swift |
| 3 | +// XMLCoderTests |
| 4 | +// |
| 5 | +// Created by Joseph Mattiello on 1/23/19. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import XCTest |
| 10 | +@testable import XMLCoder |
| 11 | + |
| 12 | +let libraryXML = """ |
| 13 | +<?xml version="1.0" encoding="UTF-8"?> |
| 14 | +<library count="2"> |
| 15 | + <book id="123"> |
| 16 | + <id>123</id> |
| 17 | + <title>Cat in the Hat</title> |
| 18 | + <category main="Y">Kids</category> |
| 19 | + <category main="N">Wildlife</category> |
| 20 | + </book> |
| 21 | + <book id="456"> |
| 22 | + <id>789</id> |
| 23 | + <title>1984</title> |
| 24 | + <category main="Y">Classics</category> |
| 25 | + <category main="N">News</category> |
| 26 | + </book> |
| 27 | +</library> |
| 28 | +""".data(using: .utf8)! |
| 29 | + |
| 30 | +private struct Library: Codable, Equatable { |
| 31 | + let count: Int |
| 32 | + let books: [Book] |
| 33 | + |
| 34 | + private enum CodingKeys: String, CodingKey { |
| 35 | + case count |
| 36 | + case books = "book" |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +private struct Book: Codable, Equatable, DynamicNodeEncoding { |
| 41 | + let id: UInt |
| 42 | + let title: String |
| 43 | + let categories: [Category] |
| 44 | + |
| 45 | + private enum CodingKeys: String, CodingKey { |
| 46 | + case id |
| 47 | + case title |
| 48 | + case categories = "category" |
| 49 | + } |
| 50 | + |
| 51 | + static func nodeEncoding(forKey key: CodingKey) -> XMLEncoder.NodeEncoding { |
| 52 | + switch key { |
| 53 | + case Book.CodingKeys.id: return .both |
| 54 | + default: return .element |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +extension Book { |
| 60 | + init(from decoder: Decoder) throws { |
| 61 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 62 | + id = try container.decode(UInt.self, forKey: .id) |
| 63 | + title = try container.decode(String.self, forKey: .title) |
| 64 | + |
| 65 | + var nested = try container.nestedUnkeyedContainer(forKey: .categories) |
| 66 | + |
| 67 | + var decoded = [Category]() |
| 68 | + var finished = false |
| 69 | + |
| 70 | + while !finished { |
| 71 | + do { |
| 72 | + let another = try nested.decode(Category.self) |
| 73 | + decoded.append(another) |
| 74 | + } catch DecodingError.valueNotFound { |
| 75 | + finished = true |
| 76 | + } catch { |
| 77 | + throw error |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + categories = decoded |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +private struct Category: Codable, Equatable, DynamicNodeEncoding { |
| 86 | + let main: Bool |
| 87 | + let value: String |
| 88 | + |
| 89 | + private enum CodingKeys: String, CodingKey { |
| 90 | + case main |
| 91 | + case value = "" |
| 92 | + } |
| 93 | + |
| 94 | + static func nodeEncoding(forKey key: CodingKey) -> XMLEncoder.NodeEncoding { |
| 95 | + switch key { |
| 96 | + case Category.CodingKeys.main: |
| 97 | + return .attribute |
| 98 | + default: |
| 99 | + return .element |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +private func decodeArray<T>(_ decoder: Decoder, decode: (inout UnkeyedDecodingContainer) throws -> T) throws -> [T] { |
| 105 | + let keyedContainer = try decoder.container(keyedBy: CodingKeys.self) |
| 106 | + var container = try keyedContainer.nestedUnkeyedContainer(forKey: .value) |
| 107 | + |
| 108 | + var decoded = [T]() |
| 109 | + var finished = false |
| 110 | + |
| 111 | + while !finished { |
| 112 | + do { |
| 113 | + decoded.append(try decode(&container)) |
| 114 | + } catch DecodingError.valueNotFound { |
| 115 | + finished = true |
| 116 | + } catch { |
| 117 | + throw error |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return decoded |
| 122 | +} |
| 123 | + |
| 124 | +final class IntrinsicTest: XCTestCase { |
| 125 | + |
| 126 | + func testEncode() { |
| 127 | + let book1 = Book(id: 123, |
| 128 | + title: "Cat in the Hat", |
| 129 | + categories: [ |
| 130 | + Category(main: true, value: "Kids"), |
| 131 | + Category(main: false, value: "Wildlife") |
| 132 | + ]) |
| 133 | + |
| 134 | + let book2 = Book(id: 456, |
| 135 | + title: "1984", |
| 136 | + categories: [ |
| 137 | + Category(main: true, value: "Classics"), |
| 138 | + Category(main: false, value: "News") |
| 139 | + ]) |
| 140 | + |
| 141 | + let library = Library(count: 2, books: [book1, book2]) |
| 142 | + let encoder = XMLEncoder() |
| 143 | + encoder.outputFormatting = [.prettyPrinted] |
| 144 | + |
| 145 | + let header = XMLHeader(version: 1.0, encoding: "UTF-8") |
| 146 | + do { |
| 147 | + let encoded = try encoder.encode(library, withRootKey: "library", header: header) |
| 148 | + let xmlString = String(data: encoded, encoding: .utf8) |
| 149 | + XCTAssertNotNil(xmlString) |
| 150 | + print(xmlString!) |
| 151 | + } catch { |
| 152 | + print("Test threw error: " + error.localizedDescription) |
| 153 | + XCTFail(error.localizedDescription) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + static var allTests = [ |
| 158 | + ("testEncode", testEncode), |
| 159 | + ] |
| 160 | +} |
0 commit comments