From 46f3020d1bb5b9f2baa1c514df06632d012d4023 Mon Sep 17 00:00:00 2001 From: Zach Stegall Date: Tue, 16 Apr 2019 10:06:33 -0700 Subject: [PATCH] ResetFireCount method. Fix clearLastData documentation. Resetting the fireCount was removed from clearLastData in e29f6ec. The inline documentation to reflect that has been updated. Resetting the fireCount is still useful. --- Signals/Signal.swift | 7 ++++++- SignalsTests/SignalsTests.swift | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Signals/Signal.swift b/Signals/Signal.swift index 9806c90..e48f92c 100644 --- a/Signals/Signal.swift +++ b/Signals/Signal.swift @@ -151,10 +151,15 @@ final public class Signal { signalListeners.removeAll(keepingCapacity: false) } - /// Clears the last fired data from the `Signal` and resets the fire count. + /// Clears the last fired data from the `Signal`. public func clearLastData() { lastDataFired = nil } + + /// Resets the fire count to 0. + public func resetFireCount() { + fireCount = 0 + } // MARK: - Private Interface diff --git a/SignalsTests/SignalsTests.swift b/SignalsTests/SignalsTests.swift index 291bb00..c887916 100644 --- a/SignalsTests/SignalsTests.swift +++ b/SignalsTests/SignalsTests.swift @@ -104,6 +104,39 @@ class SignalsTests: XCTestCase { XCTAssertEqual(dispatchCount, 2, "Dispatched two times") XCTAssertEqual(lastArgument, 2, "Last argument catched with value 2") } + + func test_fire_thenClearLastData() { + emitter.onInt.subscribe(with: self, callback: { _ in }) + emitter.onString.subscribe(with: self, callback: { _ in }) + + emitter.onInt.fire(1) + emitter.onString.fire("test") + + XCTAssertNotNil(emitter.onInt.lastDataFired, "IntSignal lastData was not retained") + XCTAssertEqual(emitter.onInt.lastDataFired, 1, "IntSignal lastData does not equal expected value") + + XCTAssertNotNil(emitter.onString.lastDataFired, "StringSignal lastData was not retained") + XCTAssertEqual(emitter.onString.lastDataFired, "test", "StringSignal lastData does not equal expected value") + + emitter.onInt.clearLastData() + emitter.onString.clearLastData() + + XCTAssertNil(emitter.onInt.lastDataFired, "IntSignal lastData was not cleared") + XCTAssertNil(emitter.onString.lastDataFired, "StringSignal lastData was not cleared") + } + + func test_fire_multipleTimes_thenResetFireCount() { + emitter.onNoParams.subscribe(with: self, callback: { }) + + emitter.onNoParams.fire() + emitter.onNoParams.fire() + + XCTAssertEqual(emitter.onNoParams.fireCount, 2, "NoParamsSignal fireCount was not incremented") + + emitter.onNoParams.resetFireCount() + + XCTAssertEqual(emitter.onNoParams.fireCount, 0, "NoParamsSignal fireCount was not reset") + } func test_subscribeOnce() { let observer1 = TestListener()