Repository navigation
Expand file tree
/
Copy pathToObjectTests.swift
More file actions
93 lines (72 loc) · 2.87 KB
/
Copy pathToObjectTests.swift
File metadata and controls
93 lines (72 loc) · 2.87 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// ReferenceTypesFromJSON.swift
// ObjectMapper
//
// Created by Tristan Himmelman on 2015-11-29.
// Copyright © 2015 hearst. All rights reserved.
//
import Foundation
import XCTest
import ObjectMapper
class ToObjectTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testMappingPersonFromJSON(){
let name = "ASDF"
let spouseName = "HJKL"
let JSONString = "{\"name\" : \"\(name)\", \"spouse\" : {\"name\" : \"\(spouseName)\"}}"
let mappedObject = Mapper<Person>().map(JSONString)
XCTAssertNotNil(mappedObject)
XCTAssertEqual(mappedObject?.name, name)
XCTAssertEqual(mappedObject?.spouse?.name, spouseName)
}
func testUpdatingChildObject(){
let name = "ASDF"
let initialSpouseName = "HJKL"
let updatedSpouseName = "QWERTY"
let initialJSONString = "{\"name\" : \"\(name)\", \"spouse\" : {\"name\" : \"\(initialSpouseName)\"}}"
let updatedJSONString = "{\"name\" : \"\(name)\", \"spouse\" : {\"name\" : \"\(updatedSpouseName)\"}}"
let mappedObject = Mapper<Person>().map(initialJSONString)
let initialSpouse = mappedObject?.spouse
XCTAssertNotNil(mappedObject)
let updatedObject = Mapper<Person>().map(updatedJSONString, toObject: mappedObject!)
XCTAssert(initialSpouse === updatedObject.spouse, "Expected mapping to update the existing object not create a new one")
XCTAssertEqual(updatedObject.spouse?.name, updatedSpouseName)
XCTAssertEqual(initialSpouse?.name, updatedSpouseName)
}
func testUpdatingChildDictionary(){
let childKey = "child_1"
let initialChildName = "HJKL"
let updatedChildName = "QWERTY"
let initialJSONString = "{\"children\" : {\"\(childKey)\" : {\"name\" : \"\(initialChildName)\"}}}"
let updatedJSONString = "{\"children\" : {\"\(childKey)\" : {\"name\" : \"\(updatedChildName)\"}}}"
let mappedObject = Mapper<Person>().map(initialJSONString)
let initialChild = mappedObject?.children?[childKey]
XCTAssertNotNil(mappedObject)
XCTAssertNotNil(initialChild)
XCTAssertEqual(initialChild?.name, initialChildName)
Mapper<Person>().map(updatedJSONString, toObject: mappedObject!)
let updatedChild = mappedObject?.children?[childKey]
XCTAssert(initialChild === updatedChild, "Expected mapping to update the existing object not create a new one")
XCTAssertEqual(updatedChild?.name, updatedChildName)
XCTAssertEqual(initialChild?.name, updatedChildName)
}
class Person: Mappable {
var name: String?
var spouse: Person?
var children: [String : Person]?
required init?(_ map: Map) {
}
func mapping(map: Map) {
name <- map["name"]
spouse <- map["spouse"]
children <- map["children"]
}
}
}