-
Notifications
You must be signed in to change notification settings - Fork 104
/
PropertyWrappersTest.swift
69 lines (57 loc) · 1.78 KB
/
PropertyWrappersTest.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
//
// PropertyWrappersTest.swift
// XMLCoderTests
//
// Created by Max Desiatov on 17/08/2022.
//
import Foundation
import XCTest
import XMLCoder
private struct Book: Codable, Equatable {
@Attribute var id: Int
@Element var name: String
@ElementAndAttribute var authorID: Int
init(id: Int, name: String, authorID: Int) {
_id = Attribute(id)
_name = Element(name)
_authorID = ElementAndAttribute(authorID)
}
}
private let bookAuthorElementAndAttributeXML =
"""
<Book id="42" authorID="24">
<name>The Book</name>
<authorID>24</authorID>
</Book>
"""
private let bookAuthorAttributeXML =
"""
<Book id="42" authorID="24">
<name>The Book</name>
</Book>
"""
private let bookAuthorElementXML =
"""
<Book id="42">
<authorID>24</authorID>
<name>The Book</name>
</Book>
"""
private let book = Book(id: 42, name: "The Book", authorID: 24)
final class PropertyWrappersTest: XCTestCase {
func testEncode() throws {
let encoder = XMLEncoder()
encoder.outputFormatting = .prettyPrinted
let xml = try String(data: encoder.encode(book), encoding: .utf8)
XCTAssertEqual(bookAuthorElementAndAttributeXML, xml)
}
func testDecode() throws {
let decoder = XMLDecoder()
let decodedBookBoth = try decoder.decode(Book.self, from: Data(bookAuthorElementAndAttributeXML.utf8))
let decodedBookElement = try decoder.decode(Book.self, from: Data(bookAuthorElementXML.utf8))
let decodedBookAttribute = try decoder.decode(Book.self, from: Data(bookAuthorAttributeXML.utf8))
XCTAssertEqual(book, decodedBookBoth)
XCTAssertEqual(book, decodedBookElement)
XCTAssertEqual(book, decodedBookAttribute)
}
}