-
Notifications
You must be signed in to change notification settings - Fork 0
/
UtilsExampleTests.swift
65 lines (54 loc) · 1.92 KB
/
UtilsExampleTests.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
//
// UtilsExampleTests.swift
// UtilsExampleTests
//
// Created by vitalii on 17.08.2022.
//
import XCTest
import Utils
struct Person: Codable, Equatable {
let name: String
let age: Int
}
class UtilsExampleTests: XCTestCase {
private enum Constnts {
static let jsonFileName: String = "jsonArrayForBundleFileReaderTest"
}
func testDataEncoding() {
let testObject = 434.33
do {
let encodedData = try JSONConverter.encode(object: testObject)
let decodedData = try JSONConverter.decode(Double.self, data: encodedData)
XCTAssertEqual(decodedData, testObject)
} catch {
XCTFail("Data encoding fall")
}
}
func testDictionaryEncoding() {
let dictionary: [String: Any] = ["One": 1, "Two": "Два", "Three": 0.47, "Four": true]
do {
let encodedData = try JSONConverter.encode(dictionary: dictionary)
let decodedData = try JSONConverter.decode(data: encodedData) as [String: Any]
XCTAssertEqual(
NSDictionary(dictionary: dictionary, copyItems: false),
NSDictionary(dictionary: decodedData, copyItems: false)
)
} catch {
XCTFail("Dictionary encoding fall")
}
}
func testJsonArrayEncoding() {
do {
let initialDecodedData: [Person] = try BundleFileReader.readObject(
name: Constnts.jsonFileName,
bundle: Bundle.main.self
)
let encodedData = try JSONConverter.encode(object: initialDecodedData)
let finalDecodedData = try JSONConverter.decode([Person].self, data: encodedData)
XCTAssertNotNil(initialDecodedData)
XCTAssertEqual(initialDecodedData, finalDecodedData)
} catch {
XCTFail("Array encoding fall")
}
}
}