From f5f8336afa4bc22949e7f55f6110b53cbf18df92 Mon Sep 17 00:00:00 2001 From: haritowa Date: Sun, 11 Dec 2016 01:54:25 +0300 Subject: [PATCH 1/3] Add ImmutableMappable & Array Context mapping tests --- Tests/ObjectMapperTests/MapContextTests.swift | 156 +++++++++++++++++- 1 file changed, 154 insertions(+), 2 deletions(-) diff --git a/Tests/ObjectMapperTests/MapContextTests.swift b/Tests/ObjectMapperTests/MapContextTests.swift index 7df559c4..637211b4 100644 --- a/Tests/ObjectMapperTests/MapContextTests.swift +++ b/Tests/ObjectMapperTests/MapContextTests.swift @@ -41,7 +41,9 @@ class MapContextTests: XCTestCase { super.tearDown() } - func testMappingWithContext() { + // MARK: - BaseMappable + // MARK: Single + func testMappingWithContext() { let JSON = ["name": "Tristan"] let context = Context(shouldMap: true) @@ -49,7 +51,7 @@ class MapContextTests: XCTestCase { XCTAssertNotNil(person) XCTAssertNotNil(person?.name) - } + } func testMappingWithContextViaMappableExtension() { let JSON = ["name": "Tristan"] @@ -70,6 +72,111 @@ class MapContextTests: XCTestCase { XCTAssertNil(person?.name) } + // MARK: Array + func testArrayMappingWithContext() { + let JSON = ["persons": [["name": "Tristan"], ["name": "Anton"]]] + let context = Context(shouldMap: true) + + let person = Mapper(context: context).map(JSON: JSON) + + XCTAssertNotNil(person) + XCTAssertNotNil(person?.persons) + } + + func testArrayMappingWithContextViaMappableExtension() { + let JSON = ["persons": [["name": "Tristan"], ["name": "Anton"]]] + let context = Context(shouldMap: true) + + let person = PersonList(JSON: JSON, context: context) + + XCTAssertNotNil(person) + XCTAssertNotNil(person?.persons) + } + + func testArrayMappingWithoutContext() { + let JSON = ["persons": [["name": "Tristan"], ["name": "Anton"]]] + + let person = Mapper().map(JSON: JSON) + + XCTAssertNotNil(person) + XCTAssertNil(person?.persons) + } + + // MARK: ImmutableMappable + // MARK: Single + func testImmatableMappingWithContext() { + let JSON = ["name": "Anton"] + let context = ImmutableContext(isDeveloper: true) + + let person = try? Mapper(context: context).map(JSON: JSON) + + XCTAssertNotNil(person) + XCTAssertTrue(person!.isDeveloper) + } + + func testImmatableMappingWithContextViaMappableExtension() { + let JSON = ["name": "Anton"] + let context = ImmutableContext(isDeveloper: true) + + let person = try? ImmutablePerson(JSON: JSON, context: context) + + XCTAssertNotNil(person) + XCTAssertTrue(person!.isDeveloper) + } + + func testImmatableMappingWithoutContext() { + let JSON = ["name": "Anton"] + + do { + let _ = try Mapper().map(JSON: JSON) + } catch ImmutablePersonMappingError.contextAbsense { + // Empty + } catch { + XCTFail() + } + } + + // MARK: Array + func testArrayImmutableMappingWithContext() { + let JSON = ["persons": [["name": "Tristan"], ["name": "Anton"]]] + let context = ImmutableContext(isDeveloper: true) + + let personList = try? Mapper(context: context).map(JSON: JSON) + + XCTAssertNotNil(personList) + + personList?.persons.forEach { person in + XCTAssertTrue(person.isDeveloper) + } + } + + func testArrayImmutableMappingWithContextViaMappableExtension() { + let JSON = ["persons": [["name": "Tristan"], ["name": "Anton"]]] + let context = ImmutableContext(isDeveloper: true) + + let personList = try? ImmutablePersonList(JSON: JSON, context: context) + + XCTAssertNotNil(personList) + + personList?.persons.forEach { person in + XCTAssertTrue(person.isDeveloper) + } + } + + func testArrayImmutableMappingWithoutContext() { + let JSON = ["persons": [["name": "Tristan"], ["name": "Anton"]]] + + do { + let _ = try Mapper().map(JSON: JSON) + } catch ImmutablePersonMappingError.contextAbsense { + // Empty + } catch { + XCTFail() + } + } + + // MARK: - Nested Types + // MARK: BaseMappable struct Context: MapContext { var shouldMap = false @@ -91,4 +198,49 @@ class MapContextTests: XCTestCase { } } } + + class PersonList: Mappable { + var persons: [Person]? + + required init?(map: Map){ + + } + + func mapping(map: Map) { + if (map.context as? Context)?.shouldMap == true { + persons <- map["persons"] + } + } + } + + // MARK: ImmutableMappable + struct ImmutableContext: MapContext { + let isDeveloper: Bool + } + + enum ImmutablePersonMappingError: Error { + case contextAbsense + } + + struct ImmutablePerson: ImmutableMappable { + let name: String + let isDeveloper: Bool + + init(map: Map) throws { + guard let context = map.context as? ImmutableContext else { + throw ImmutablePersonMappingError.contextAbsense + } + + name = try map.value("name") + isDeveloper = context.isDeveloper + } + } + + struct ImmutablePersonList: ImmutableMappable { + let persons: [ImmutablePerson] + + init(map: Map) throws { + persons = try map.value("persons") + } + } } From 9381a484dc263646e51187d14b09f24445d37442 Mon Sep 17 00:00:00 2001 From: haritowa Date: Sun, 11 Dec 2016 02:00:27 +0300 Subject: [PATCH 2/3] Forward mapping context for ImmutableMappable --- Sources/ImmutableMappable.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/ImmutableMappable.swift b/Sources/ImmutableMappable.swift index e187d801..4e2ba540 100644 --- a/Sources/ImmutableMappable.swift +++ b/Sources/ImmutableMappable.swift @@ -92,7 +92,7 @@ public extension Map { guard let JSONObject = currentValue else { throw MapError(key: key, currentValue: currentValue, reason: "Found unexpected nil value", file: file, function: function, line: line) } - return try Mapper().mapOrFail(JSONObject: JSONObject) + return try Mapper(context: context).mapOrFail(JSONObject: JSONObject) } // MARK: [BaseMappable] @@ -104,7 +104,7 @@ public extension Map { throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[Any]'", file: file, function: function, line: line) } return try jsonArray.enumerated().map { i, JSONObject -> T in - return try Mapper().mapOrFail(JSONObject: JSONObject) + return try Mapper(context: context).mapOrFail(JSONObject: JSONObject) } } @@ -132,7 +132,7 @@ public extension Map { } var value: [String: T] = [:] for (key, json) in jsonDictionary { - value[key] = try Mapper().mapOrFail(JSONObject: json) + value[key] = try Mapper(context: context).mapOrFail(JSONObject: json) } return value } From 77004729c964db9de35b987a327da5b502fa684a Mon Sep 17 00:00:00 2001 From: haritowa Date: Sun, 11 Dec 2016 02:15:22 +0300 Subject: [PATCH 3/3] Add tests for context forwarding in nested props --- Tests/ObjectMapperTests/MapContextTests.swift | 100 +++++++++++++++++- 1 file changed, 95 insertions(+), 5 deletions(-) diff --git a/Tests/ObjectMapperTests/MapContextTests.swift b/Tests/ObjectMapperTests/MapContextTests.swift index 637211b4..28eb14ec 100644 --- a/Tests/ObjectMapperTests/MapContextTests.swift +++ b/Tests/ObjectMapperTests/MapContextTests.swift @@ -72,6 +72,36 @@ class MapContextTests: XCTestCase { XCTAssertNil(person?.name) } + // MARK: Nested + func testNestedMappingWithContext() { + let JSON = ["person": ["name": "Tristan"]] + let context = Context(shouldMap: true) + + let nestedPerson = Mapper(context: context).map(JSON: JSON) + + XCTAssertNotNil(nestedPerson) + XCTAssertNotNil(nestedPerson?.person?.name) + } + + func testNestedMappingWithContextViaMappableExtension() { + let JSON = ["person": ["name": "Tristan"]] + let context = Context(shouldMap: true) + + let nestedPerson = NestedPerson(JSON: JSON, context: context) + + XCTAssertNotNil(nestedPerson) + XCTAssertNotNil(nestedPerson?.person?.name) + } + + func testNestedMappingWithoutContext() { + let JSON = ["person": ["name": "Tristan"]] + + let nestedPerson = Mapper().map(JSON: JSON) + + XCTAssertNotNil(nestedPerson) + XCTAssertNil(nestedPerson?.person?.name) + } + // MARK: Array func testArrayMappingWithContext() { let JSON = ["persons": [["name": "Tristan"], ["name": "Anton"]]] @@ -111,7 +141,8 @@ class MapContextTests: XCTestCase { let person = try? Mapper(context: context).map(JSON: JSON) XCTAssertNotNil(person) - XCTAssertTrue(person!.isDeveloper) + + XCTAssertEqual(person?.isDeveloper ?? !context.isDeveloper, context.isDeveloper) } func testImmatableMappingWithContextViaMappableExtension() { @@ -121,7 +152,7 @@ class MapContextTests: XCTestCase { let person = try? ImmutablePerson(JSON: JSON, context: context) XCTAssertNotNil(person) - XCTAssertTrue(person!.isDeveloper) + XCTAssertEqual(person?.isDeveloper ?? !context.isDeveloper, context.isDeveloper) } func testImmatableMappingWithoutContext() { @@ -136,6 +167,41 @@ class MapContextTests: XCTestCase { } } + // MARK: Nested + func testNestedImmutableMappingWithContext() { + let JSON = ["person": ["name": "Anton"]] + let context = ImmutableContext(isDeveloper: true) + + let nestedPerson = try? Mapper(context: context).map(JSON: JSON) + + XCTAssertNotNil(nestedPerson) + XCTAssertEqual(nestedPerson?.person.isDeveloper ?? !context.isDeveloper, context.isDeveloper) + } + + func testNestedImmutableMappingWithContextViaMappableExtension() { + let JSON = ["person": ["name": "Anton"]] + let context = ImmutableContext(isDeveloper: true) + + let nestedPerson = try? ImmutableNestedPerson(JSON: JSON, context: context) + + XCTAssertNotNil(nestedPerson) + XCTAssertEqual(nestedPerson?.person.isDeveloper ?? !context.isDeveloper, context.isDeveloper) + } + + func testNestedImmutableMappingWithoutContext() { + let JSON = ["person": ["name": "Anton"]] + + do { + let _ = try Mapper().map(JSON: JSON) + } catch ImmutablePersonMappingError.contextAbsense { + return + } catch { + XCTFail() + } + + XCTFail() + } + // MARK: Array func testArrayImmutableMappingWithContext() { let JSON = ["persons": [["name": "Tristan"], ["name": "Anton"]]] @@ -146,7 +212,7 @@ class MapContextTests: XCTestCase { XCTAssertNotNil(personList) personList?.persons.forEach { person in - XCTAssertTrue(person.isDeveloper) + XCTAssertEqual(person.isDeveloper, context.isDeveloper) } } @@ -159,7 +225,7 @@ class MapContextTests: XCTestCase { XCTAssertNotNil(personList) personList?.persons.forEach { person in - XCTAssertTrue(person.isDeveloper) + XCTAssertEqual(person.isDeveloper, context.isDeveloper) } } @@ -169,10 +235,12 @@ class MapContextTests: XCTestCase { do { let _ = try Mapper().map(JSON: JSON) } catch ImmutablePersonMappingError.contextAbsense { - // Empty + return } catch { XCTFail() } + + XCTFail() } // MARK: - Nested Types @@ -199,6 +267,20 @@ class MapContextTests: XCTestCase { } } + class NestedPerson: Mappable { + var person: Person? + + required init?(map: Map){ + + } + + func mapping(map: Map) { + if (map.context as? Context)?.shouldMap == true { + person <- map["person"] + } + } + } + class PersonList: Mappable { var persons: [Person]? @@ -236,6 +318,14 @@ class MapContextTests: XCTestCase { } } + struct ImmutableNestedPerson: ImmutableMappable { + let person: ImmutablePerson + + init(map: Map) throws { + person = try map.value("person") + } + } + struct ImmutablePersonList: ImmutableMappable { let persons: [ImmutablePerson]