forked from CoreOffice/XMLCoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreakfastTest.swift
72 lines (65 loc) · 2.02 KB
/
BreakfastTest.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
68
69
70
71
72
//
// BreakfastTest.swift
// XMLCoderTests
//
// Created by Max Desiatov on 19/11/2018.
//
import Foundation
import XCTest
@testable import XMLCoder
private let xml = """
<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
""".data(using: .utf8)!
private struct Menu: Codable, Equatable {
var food: [Food]
}
private struct Food: Codable, Equatable {
var name: String
var price: String
var description: String
var calories: Int?
}
final class BreakfastTest: XCTestCase {
func testXML() throws {
let decoder = XMLDecoder()
let encoder = XMLEncoder()
let menu1 = try decoder.decode(Menu.self, from: xml)
XCTAssertEqual(menu1.food.count, 5)
let data = try encoder.encode(menu1, withRootKey: "breakfast_menu",
header: XMLHeader(version: 1.0,
encoding: "UTF-8"))
let menu2 = try decoder.decode(Menu.self, from: data)
XCTAssertEqual(menu1, menu2)
}
}