-
Notifications
You must be signed in to change notification settings - Fork 104
/
CDTest.swift
67 lines (56 loc) · 1.73 KB
/
CDTest.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// CDTest.swift
// XMLCoderTests
//
// Created by Shawn Moore on 11/15/17.
// Copyright © 2017 Shawn Moore. All rights reserved.
//
import Foundation
import XCTest
@testable import XMLCoder
private struct CDCatalog: Codable, Equatable {
var cds: [CD]
enum CodingKeys: String, CodingKey {
case cds = "CD"
}
}
private struct CD: Codable, Equatable {
var title: String
var artist: String
var country: String
var company: String
var price: Decimal
var year: Int
enum CodingKeys: String, CodingKey {
case title = "TITLE"
case artist = "ARTIST"
case country = "COUNTRY"
case company = "COMPANY"
case price = "PRICE"
case year = "YEAR"
}
}
private let lastCD = CD(title: "Unchain my heart",
artist: "Joe Cocker",
country: "USA",
company: "EMI",
price: 8.20,
year: 1987)
final class CDTest: XCTestCase {
func testXML() throws {
let decoder = XMLDecoder()
let encoder = XMLEncoder()
let cdCatalog1 = try decoder.decode(CDCatalog.self,
from: cdCatalogXML)
XCTAssertEqual(cdCatalog1.cds.count, 26)
XCTAssertEqual(cdCatalog1.cds[25], lastCD)
let data = try encoder.encode(cdCatalog1, withRootKey: "CATALOG",
header: XMLHeader(version: 1.0,
encoding: "UTF-8"))
let cdCatalog2 = try decoder.decode(CDCatalog.self, from: data)
XCTAssertEqual(cdCatalog1, cdCatalog2)
}
static var allTests = [
("testXML", testXML),
]
}