-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for CompositeEventSourceBuilder
Showing
3 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
MobiusCore/Test/EventSources/CompositeEventSourceBuilderTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright (c) 2019 Spotify AB. | ||
// | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
@testable import MobiusCore | ||
import Nimble | ||
import Quick | ||
|
||
class CompositeEventSourceBuilderTest: QuickSpec { | ||
override func spec() { | ||
describe("CompositeEventSourceBuilder") { | ||
context("when configuring the composite event source builder") { | ||
var eventsReceived = [Int]() | ||
var disposable: Disposable? | ||
|
||
context("with no event sources") { | ||
let sut = CompositeEventSourceBuilder<Int>() | ||
let compositeEventSource = sut.build() | ||
|
||
it("should produce an event source") { | ||
// In particular, we want a do-nothing event source rather than an assertion or crash. | ||
let disposable = compositeEventSource.subscribe { | ||
eventsReceived.append($0) | ||
} | ||
disposable.dispose() | ||
|
||
expect(eventsReceived).to(equal([])) | ||
} | ||
} | ||
|
||
context("with one event source") { | ||
let eventSource = TestEventSource() | ||
let sut = CompositeEventSourceBuilder<Int>() | ||
.addEventSource(eventSource) | ||
let compositeEventSource = sut.build() | ||
|
||
beforeEach { | ||
eventsReceived = [] | ||
disposable = compositeEventSource.subscribe { | ||
eventsReceived.append($0) | ||
} | ||
} | ||
|
||
it("should provide an event source equivalent to the input event source") { | ||
eventSource.dispatch(1) | ||
eventSource.dispatch(2) | ||
|
||
let expectedEvents = [1, 2] | ||
expect(eventsReceived).to(equal(expectedEvents)) | ||
} | ||
|
||
it("should return a disposable that disposes the original event source") { | ||
disposable?.dispose() | ||
|
||
expect(eventSource.isDisposed).to(beTrue()) | ||
} | ||
} | ||
|
||
context("with several event sources") { | ||
let eventSources = [TestEventSource(), TestEventSource(), TestEventSource()] | ||
var sut = CompositeEventSourceBuilder<Int>() | ||
eventSources.forEach { | ||
sut = sut.addEventSource($0) | ||
} | ||
let compositeEventSource = sut.build() | ||
|
||
beforeEach { | ||
eventsReceived = [] | ||
disposable = compositeEventSource.subscribe { | ||
eventsReceived.append($0) | ||
} | ||
} | ||
|
||
it("should produce an event source that emits the events from all input sources") { | ||
eventSources.enumerated().forEach { index, source in | ||
source.dispatch(index) | ||
} | ||
|
||
let expectedEvents = [0, 1, 2] | ||
expect(eventsReceived).to(equal(expectedEvents)) | ||
} | ||
|
||
it("should return a disposable that disposes of all the input event sources") { | ||
disposable?.dispose() | ||
|
||
eventSources.forEach { | ||
expect($0.isDisposed).to(beTrue()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private class TestEventSource: EventSource, Disposable { | ||
typealias Event = Int | ||
|
||
var consumer: Consumer<Int>? | ||
func subscribe(consumer: @escaping Consumer<Int>) -> Disposable { | ||
self.consumer = consumer | ||
return self | ||
} | ||
|
||
var isDisposed = false | ||
func dispose() { | ||
isDisposed = true | ||
} | ||
|
||
func dispatch(_ event: Int) { | ||
consumer?(event) | ||
} | ||
} |