-
-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathJoinJSONTests.swift
More file actions
74 lines (58 loc) · 1.93 KB
/
JoinJSONTests.swift
File metadata and controls
74 lines (58 loc) · 1.93 KB
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
73
74
import Foundation
import XCTest
@testable import JoinJSON
final class JoinJSONTests: XCTestCase {
var sut: JoinJSON!
override func setUp() {
super.setUp()
sut = JoinJSON()
}
override func tearDown() {
sut = nil
super.tearDown()
}
func test_join_two_valid_json_strings() throws {
let json1 = """
{"name": "John"}
"""
let json2 = """
{"age": 30}
"""
let result = sut.join(json1, with: json2)
let dict = try JSONSerialization.jsonObject(with: result) as? [String: Any]
XCTAssertEqual(dict?["name"] as? String, "John")
XCTAssertEqual(dict?["age"] as? Int, 30)
}
func test_join_with_invalid_json_returns_first_data() {
let json1 = """
{"name": "John"}
"""
let invalidJSON = "invalid json"
let result = sut.join(json1, with: invalidJSON)
XCTAssertEqual(result, json1.data(using: .utf8))
}
func test_join_with_overlapping_keys_prefers_second_value() throws {
let json1 = """
{"name": "John", "age": 25}
"""
let json2 = """
{"age": 30}
"""
let result = sut.join(json1, with: json2)
let dict = try JSONSerialization.jsonObject(with: result) as? [String: Any]
XCTAssertEqual(dict?["name"] as? String, "John")
XCTAssertEqual(dict?["age"] as? Int, 30)
}
func test_join_with_data_input() throws {
let data1 = """
{"name": "John"}
""".data(using: .utf8)!
let data2 = """
{"age": 30}
""".data(using: .utf8)!
let result = sut.join(data1, with: data2)
let dict = try JSONSerialization.jsonObject(with: result) as? [String: Any]
XCTAssertEqual(dict?["name"] as? String, "John")
XCTAssertEqual(dict?["age"] as? Int, 30)
}
}