From 0a630845641602724853ccce7279331adb87368f Mon Sep 17 00:00:00 2001 From: Jeff Verkoeyen Date: Sat, 3 Dec 2016 22:01:48 -0800 Subject: [PATCH] Cleaned up and added some additional unit tests Reviewers: O4 Material Motion Apple platform reviewers, O2 Material Motion, markwei Reviewed By: O4 Material Motion Apple platform reviewers, O2 Material Motion, markwei Tags: #material_motion Differential Revision: http://codereview.cc/D2100 --- tests/unit/MemoryLeakTests.swift | 12 ++++++------ tests/unit/ObservableTests.swift | 31 +++++++++++++++++++++++++++++++ tests/unit/SimpleOperators.swift | 7 +++++-- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/tests/unit/MemoryLeakTests.swift b/tests/unit/MemoryLeakTests.swift index d9ebfcd..13c5df0 100644 --- a/tests/unit/MemoryLeakTests.swift +++ b/tests/unit/MemoryLeakTests.swift @@ -63,9 +63,9 @@ class MemoryLeakTests: XCTestCase { weak var weakObservable = observable autoreleasepool { - let _ = observable!.subscribe(next: { + let _ = observable!.subscribe { let _ = $0 - }) + } // Remove our only strong reference. observable = nil } @@ -85,9 +85,9 @@ class MemoryLeakTests: XCTestCase { autoreleasepool { let _ = observable!.map { value in return value * value - }.subscribe(next: { + }.subscribe { let _ = $0 - }) + } // Remove our only strong reference. observable = nil } @@ -108,9 +108,9 @@ class MemoryLeakTests: XCTestCase { let subscription = observable!.map { value in return value * value - }.subscribe(next: { + }.subscribe { let _ = $0 - }) + } // Remove our only strong reference. subscription.unsubscribe() } diff --git a/tests/unit/ObservableTests.swift b/tests/unit/ObservableTests.swift index a880f78..ca2ce57 100644 --- a/tests/unit/ObservableTests.swift +++ b/tests/unit/ObservableTests.swift @@ -38,6 +38,37 @@ class ObservableTests: XCTestCase { waitForExpectations(timeout: 0) } + func testUnsubscribesOnDeallocation() { + var didUnsubscribe = false + + autoreleasepool { + let observable = IndefiniteObservable { observer in + return { + didUnsubscribe = true + } + } + + let _ = observable.subscribe { _ in } + } + + XCTAssertTrue(didUnsubscribe) + } + + func testUnsubscribesOnUnsubscribe() { + var didUnsubscribe = false + + let observable = IndefiniteObservable { observer in + return { + didUnsubscribe = true + } + } + + let subscription = observable.subscribe { _ in } + subscription.unsubscribe() + + XCTAssertTrue(didUnsubscribe) + } + func testTwoSubsequentSubscriptions() { let value = 10 diff --git a/tests/unit/SimpleOperators.swift b/tests/unit/SimpleOperators.swift index 20ffc59..7e2d70c 100644 --- a/tests/unit/SimpleOperators.swift +++ b/tests/unit/SimpleOperators.swift @@ -20,6 +20,8 @@ import IndefiniteObservable // Simple operators used by the tests. extension IndefiniteObservable { + + // Map from one value type to another. public func map(_ transform: @escaping (T) -> U) -> IndefiniteObservable { return IndefiniteObservable { observer in return self.subscribe { @@ -28,10 +30,11 @@ extension IndefiniteObservable { } } - public func filter(_ isIncluded: @escaping (T) -> Bool) -> IndefiniteObservable { + // Only emit values downstream for which passesTest returns true + public func filter(_ passesTest: @escaping (T) -> Bool) -> IndefiniteObservable { return IndefiniteObservable { observer in return self.subscribe { - if isIncluded($0) { + if passesTest($0) { observer.next($0) } }.unsubscribe