Skip to content

Commit

Permalink
Merge pull request #143 from jechol/split-async-test
Browse files Browse the repository at this point in the history
Add async test helpers, fix wrong tests.
  • Loading branch information
srdanrasic authored Jan 17, 2017
2 parents d8b9e4d + b6daf34 commit 941b567
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 74 deletions.
57 changes: 45 additions & 12 deletions Tests/ReactiveKitTests/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,64 @@ extension Event {
}

extension SignalProtocol {

func expectNext(_ expectedElements: [Element],
_ message: @autoclosure () -> String = "",
expectation: XCTestExpectation? = nil,
file: StaticString = #file, line: UInt = #line) {
expect(expectedElements.map { .next($0) } + [.completed], message, expectation: expectation, file: file, line: line)

// Synchronous test
func expectComplete(after expectedElements: [Element],
file: StaticString = #file, line: UInt = #line) {
expect(events: expectedElements.map { .next($0) } + [.completed], file: file, line: line)
}

func expect(_ expectedEvents: [Event<Element, Error>],
_ message: @autoclosure () -> String = "",
expectation: XCTestExpectation? = nil,
func expect(events expectedEvents: [Event<Element, Error>],
file: StaticString = #file, line: UInt = #line) {
var eventsToProcess = expectedEvents
var receivedEvents: [Event<Element, Error>] = []
let message = message()
var matchedAll = false
let _ = observe { event in
receivedEvents.append(event)
if eventsToProcess.count == 0 {
XCTFail("Got more events than expected.")
return
}
let expected = eventsToProcess.removeFirst()
XCTAssert(event.isEqualTo(expected), "(Got \(receivedEvents) instead of \(expectedEvents))", file: file, line: line)
if eventsToProcess.count == 0 {
matchedAll = true
}
}
if !matchedAll {
XCTFail("Got only first \(receivedEvents.count) events of expected \(expectedEvents))", file: file, line: line)
}
}

func expectNoEvent(file: StaticString = #file, line: UInt = #line) {
let _ = observe { event in
XCTFail("Got a \(event) when expected empty", file: file, line: line)
}
}

// Asynchronous test
func expectAsyncComplete(after expectedElements: [Element],
expectation: XCTestExpectation,
file: StaticString = #file, line: UInt = #line) {
expectAsync(events: expectedElements.map { .next($0) } + [.completed], expectation: expectation, file: file, line: line)
}

func expectAsync(events expectedEvents: [Event<Element, Error>],
expectation: XCTestExpectation,
file: StaticString = #file, line: UInt = #line) {
XCTAssert(!expectedEvents.isEmpty, "Use expectEmptyAsync for waiting empty signal")
var eventsToProcess = expectedEvents
var receivedEvents: [Event<Element, Error>] = []
let _ = observe { event in
receivedEvents.append(event)
if eventsToProcess.count == 0 {
XCTFail("Got more events than expected.")
return
}
let expected = eventsToProcess.removeFirst()
XCTAssert(event.isEqualTo(expected), message + "(Got \(receivedEvents) instead of \(expectedEvents))", file: file, line: line)
XCTAssert(event.isEqualTo(expected), "(Got \(receivedEvents) instead of \(expectedEvents))", file: file, line: line)
if eventsToProcess.count == 0 {
expectation?.fulfill()
expectation.fulfill()
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions Tests/ReactiveKitTests/PropertyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PropertyTests: XCTestCase {
}

func testEvents() {
property.expect(
property.expectAsync(events:
[
.next(0),
.next(5),
Expand Down Expand Up @@ -52,7 +52,7 @@ class PropertyTests: XCTestCase {
var readOnlyView: AnyProperty<Int>! = property.readOnlyView
XCTAssert(readOnlyView.value == 0)

readOnlyView.expect(
readOnlyView.expectAsync(events:
[
.next(0),
.next(5),
Expand Down Expand Up @@ -84,11 +84,13 @@ class PropertyTests: XCTestCase {
func testBidirectionalBind() {
let target = Property(100)

target.expectNext([100, 0, 50, 60])
property.expectNext([0, 0, 50, 60])
target.ignoreTerminal().expectAsync(events: [.next(100), .next(0), .next(50), .next(60)], expectation: expectation(description: "nexts"))
property.ignoreTerminal().expectAsync(events: [.next(0), .next(0), .next(50), .next(60)], expectation: expectation(description: "nexts"))

property.bidirectionalBind(to: target)
property.value = 50
target.value = 60

waitForExpectations(timeout: 2, handler: nil)
}
}
Loading

0 comments on commit 941b567

Please sign in to comment.