Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Commit

Permalink
Cleaned up and added some additional unit tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Jeff Verkoeyen committed Dec 6, 2016
1 parent 0914e5d commit 0a63084
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
12 changes: 6 additions & 6 deletions tests/unit/MemoryLeakTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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()
}
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/ObservableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,37 @@ class ObservableTests: XCTestCase {
waitForExpectations(timeout: 0)
}

func testUnsubscribesOnDeallocation() {
var didUnsubscribe = false

autoreleasepool {
let observable = IndefiniteObservable<CGFloat> { observer in
return {
didUnsubscribe = true
}
}

let _ = observable.subscribe { _ in }
}

XCTAssertTrue(didUnsubscribe)
}

func testUnsubscribesOnUnsubscribe() {
var didUnsubscribe = false

let observable = IndefiniteObservable<CGFloat> { observer in
return {
didUnsubscribe = true
}
}

let subscription = observable.subscribe { _ in }
subscription.unsubscribe()

XCTAssertTrue(didUnsubscribe)
}

func testTwoSubsequentSubscriptions() {
let value = 10

Expand Down
7 changes: 5 additions & 2 deletions tests/unit/SimpleOperators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import IndefiniteObservable
// Simple operators used by the tests.

extension IndefiniteObservable {

// Map from one value type to another.
public func map<U>(_ transform: @escaping (T) -> U) -> IndefiniteObservable<U> {
return IndefiniteObservable<U> { observer in
return self.subscribe {
Expand All @@ -28,10 +30,11 @@ extension IndefiniteObservable {
}
}

public func filter(_ isIncluded: @escaping (T) -> Bool) -> IndefiniteObservable<T> {
// Only emit values downstream for which passesTest returns true
public func filter(_ passesTest: @escaping (T) -> Bool) -> IndefiniteObservable<T> {
return IndefiniteObservable<T> { observer in
return self.subscribe {
if isIncluded($0) {
if passesTest($0) {
observer.next($0)
}
}.unsubscribe
Expand Down

0 comments on commit 0a63084

Please sign in to comment.