From 23d0bbffd077e24b977bbbbf6e9787068e919d20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Olejni=CC=81k?= Date: Fri, 22 Jan 2016 15:13:21 +0100 Subject: [PATCH 1/3] Added old value to RemoveElement operation --- ReactiveArray/Operation.swift | 18 +++++++++--------- ReactiveArray/ReactiveArray.swift | 6 ++++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/ReactiveArray/Operation.swift b/ReactiveArray/Operation.swift index cca4f56..4effca6 100644 --- a/ReactiveArray/Operation.swift +++ b/ReactiveArray/Operation.swift @@ -13,7 +13,7 @@ public enum Operation: CustomDebugStringConvertible { case Append(value: T) case Insert(value: T, atIndex: Int) case Update(value: T, atIndex: Int) - case RemoveElement(atIndex: Int) + case RemoveElement(atIndex: Int, oldValue : T) public func map(mapper: T -> U) -> Operation { let result: Operation @@ -24,8 +24,8 @@ public enum Operation: CustomDebugStringConvertible { result = Operation.Insert(value: mapper(value), atIndex: index) case .Update(let value, let index): result = Operation.Update(value: mapper(value), atIndex: index) - case .RemoveElement(let index): - result = Operation.RemoveElement(atIndex: index) + case .RemoveElement(let index, let oldValue): + result = Operation.RemoveElement(atIndex: index, oldValue: mapper(oldValue)) } return result } @@ -39,8 +39,8 @@ public enum Operation: CustomDebugStringConvertible { description = ".Insert(value: \(value), atIndex:\(index))" case .Update(let value, let index): description = ".Update(value: \(value), atIndex:\(index))" - case .RemoveElement(let index): - description = ".RemoveElement(atIndex:\(index))" + case .RemoveElement(let index, let oldValue): + description = ".RemoveElement(atIndex:\(index), oldValue:\(oldValue))" } return description } @@ -53,8 +53,8 @@ public enum Operation: CustomDebugStringConvertible { return value case .Update(let value, _): return value - default: - return Optional.None + case .RemoveElement(_, let oldValue): + return oldValue } } @@ -68,8 +68,8 @@ public func ==(lhs: Operation, rhs: Operation) -> Bool { return leftIndex == rightIndex && leftValue == rightValue case (.Update(let leftValue, let leftIndex), .Update(let rightValue, let rightIndex)): return leftIndex == rightIndex && leftValue == rightValue - case (.RemoveElement(let leftIndex), .RemoveElement(let rightIndex)): - return leftIndex == rightIndex + case (.RemoveElement(let leftIndex, let leftValue), .RemoveElement(let rightIndex, let rightValue)): + return leftIndex == rightIndex && leftValue == rightValue default: return false } diff --git a/ReactiveArray/ReactiveArray.swift b/ReactiveArray/ReactiveArray.swift index ef418e2..96ee653 100644 --- a/ReactiveArray/ReactiveArray.swift +++ b/ReactiveArray/ReactiveArray.swift @@ -123,7 +123,8 @@ public final class ReactiveArray: CollectionType, MutableCollectionType, Cust } public func removeAtIndex(index:Int) { - let operation: Operation = .RemoveElement(atIndex: index) + let value = _elements[index] + let operation: Operation = .RemoveElement(atIndex: index, oldValue: value) _sink.sendNext(operation) } @@ -145,10 +146,11 @@ public final class ReactiveArray: CollectionType, MutableCollectionType, Cust _mutableCount.value = _elements.count case .Update(let value, let index): _elements[index] = value - case .RemoveElement(let index): + case .RemoveElement(let index, _): _elements.removeAtIndex(index) _mutableCount.value = _elements.count } } } + From 9e7f0e0e9f9a3b9fce236bf402e507fc42bc989b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Olejni=CC=81k?= Date: Fri, 22 Jan 2016 15:13:39 +0100 Subject: [PATCH 2/3] Updated reactive array tests --- ReactiveArrayTests/ReactiveArraySpec.swift | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/ReactiveArrayTests/ReactiveArraySpec.swift b/ReactiveArrayTests/ReactiveArraySpec.swift index b894667..e157b5f 100644 --- a/ReactiveArrayTests/ReactiveArraySpec.swift +++ b/ReactiveArrayTests/ReactiveArraySpec.swift @@ -16,7 +16,7 @@ private func waitForOperation(fromProducer producer: SignalProducer () = { fail("Invalid operation type: .Append(\($0))") }, onInsert: (T, Int) -> () = { fail("Invalid operation type: .Insert(\($0), \($1))") }, onUpdate: (T, Int) -> () = { fail("Invalid operation type: .Update(\($0), \($1))") }, - onDelete: Int -> () = { fail("Invalid operation type: .Delete(\($0))") }) { + onDelete: (Int, T) -> () = { fail("Invalid operation type: .Delete(\($0))") }) { waitUntil(timeout: 10.0) { done in producer.startWithNext { operation in @@ -27,8 +27,8 @@ private func waitForOperation(fromProducer producer: SignalProducer(fromSignal signal: Signal, NoError onAppend: T -> () = { fail("Invalid operation type: .Append(\($0))") }, onInsert: (T, Int) -> () = { fail("Invalid operation type: .Insert(\($0), \($1))") }, onUpdate: (T, Int) -> () = { fail("Invalid operation type: .Update(\($0), \($1))") }, - onDelete: Int -> () = { fail("Invalid operation type: .Delete(\($0))") }) { + onDelete: (Int, T) -> () = { fail("Invalid operation type: .Delete(\($0))") }) { let producer = SignalProducer, NoError> { (observer, disposable) in signal.observe(observer) } waitForOperation(fromProducer: producer, when: when, onAppend: onAppend, onInsert: onInsert, onUpdate: onUpdate, onDelete: onDelete) @@ -53,7 +53,7 @@ private func waitForOperation(fromArray array: ReactiveArray, onAppend: T -> () = { fail("Invalid operation type: .Append(\($0))") }, onInsert: (T, Int) -> () = { fail("Invalid operation type: .Insert(\($0), \($1))") }, onUpdate: (T, Int) -> () = { fail("Invalid operation type: .Update(\($0), \($1))") }, - onDelete: Int -> () = { fail("Invalid operation type: .Delete(\($0))") }) { + onDelete: (Int, T) -> () = { fail("Invalid operation type: .Delete(\($0))") }) { waitForOperation(fromSignal: array.signal, when: when, onAppend: onAppend, onInsert: onInsert, onUpdate: onUpdate, onDelete: onDelete) } @@ -152,8 +152,9 @@ class ReactiveArraySpec: QuickSpec { when: { array.removeAtIndex(1) }, - onDelete: { index in + onDelete: { index,value in expect(index).to(equal(1)) + expect(value).to(equal(2)) } ) } @@ -250,8 +251,9 @@ class ReactiveArraySpec: QuickSpec { when: { array.removeAtIndex(1) }, - onDelete: { index in + onDelete: { index,value in expect(index).to(equal(1)) + expect(value).to(equal(12)) } ) } @@ -327,8 +329,9 @@ class ReactiveArraySpec: QuickSpec { when: { a.removeAtIndex(0) }, - onDelete: { index in + onDelete: { index,value in expect(index).to(equal(0)) + expect(value).to(equal(1)) } ) } @@ -397,8 +400,9 @@ class ReactiveArraySpec: QuickSpec { when: { array.removeAtIndex(1) }, - onDelete: { index in + onDelete: { index,value in expect(index).to(equal(1)) + expect(value).to(equal(2)) } ) } From 9b5c206c379b788ffde3d372ce2431bbcc09b580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Olejni=CC=81k?= Date: Fri, 22 Jan 2016 15:13:48 +0100 Subject: [PATCH 3/3] Updated operation tests --- ReactiveArrayTests/OperationSpec.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ReactiveArrayTests/OperationSpec.swift b/ReactiveArrayTests/OperationSpec.swift index c4c717c..571d61b 100644 --- a/ReactiveArrayTests/OperationSpec.swift +++ b/ReactiveArrayTests/OperationSpec.swift @@ -67,13 +67,13 @@ class OperationSpec: QuickSpec { context("when the operation is a Delete operation") { beforeEach { - operation = Operation.RemoveElement(atIndex: 5) + operation = Operation.RemoveElement(atIndex: 5, oldValue:10) } - it("does nothing") { + it("maps the old value") { let mappedOperation = operation.map { $0 * 2 } - let areEqual = mappedOperation == operation + let areEqual = mappedOperation == Operation.RemoveElement(atIndex: 5, oldValue: 20) expect(areEqual).to(beTrue()) } @@ -122,11 +122,11 @@ class OperationSpec: QuickSpec { context("when the operation is an Remove operation") { beforeEach { - operation = Operation.RemoveElement(atIndex: 5) + operation = Operation.RemoveElement(atIndex: 5, oldValue:10) } it("returns .None") { - expect(operation.value).to(beNil()) + expect(operation.value).to(equal(10)) } }