Skip to content

Commit

Permalink
Add unit tests for CompositeEventSourceBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
JensAyton committed May 27, 2019
1 parent 6f680f4 commit 7fb96ff
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Mobius.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
2D54D0F121C1167C002AAC19 /* AtomicBool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2D54D0EF21C11362002AAC19 /* AtomicBool.swift */; };
2DDF54C0229BDB4800D05861 /* CompositeEventSourceBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DDF54BF229BDB4700D05861 /* CompositeEventSourceBuilder.swift */; };
2DDF54C1229BDB4800D05861 /* CompositeEventSourceBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DDF54BF229BDB4700D05861 /* CompositeEventSourceBuilder.swift */; };
2DDF54C3229BEEC400D05861 /* CompositeEventSourceBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DDF54C2229BEEC400D05861 /* CompositeEventSourceBuilderTests.swift */; };
2DF4C2FC20DBDD5800A4B6DE /* Next.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BB287B5209995410043B530 /* Next.swift */; };
2DF4C2FD20DBDD5800A4B6DE /* Connection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BB287B6209995410043B530 /* Connection.swift */; };
2DF4C2FE20DBDD5800A4B6DE /* Connectable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B237EB9209C4F3C00764576 /* Connectable.swift */; };
Expand Down Expand Up @@ -261,6 +262,7 @@
2D2FE60F20625E76002DFD69 /* Mobius.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = Mobius.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
2D54D0EF21C11362002AAC19 /* AtomicBool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AtomicBool.swift; sourceTree = "<group>"; };
2DDF54BF229BDB4700D05861 /* CompositeEventSourceBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CompositeEventSourceBuilder.swift; sourceTree = "<group>"; };
2DDF54C2229BEEC400D05861 /* CompositeEventSourceBuilderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompositeEventSourceBuilderTests.swift; sourceTree = "<group>"; };
2DF4C2F520DBDD4700A4B6DE /* libMobiusCore.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libMobiusCore.a; sourceTree = BUILT_PRODUCTS_DIR; };
2DF4C41D20DBDEFA00A4B6DE /* libMobiusExtras.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libMobiusExtras.a; sourceTree = BUILT_PRODUCTS_DIR; };
2DF4C53320DBE03900A4B6DE /* libMobiusTest.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libMobiusTest.a; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -561,6 +563,7 @@
isa = PBXGroup;
children = (
5BB2885320999ACE0043B530 /* AnyEventSourceTests.swift */,
2DDF54C2229BEEC400D05861 /* CompositeEventSourceBuilderTests.swift */,
5B4A369D21107F3200279C7D /* MergedEventSourceTests.swift */,
);
path = EventSources;
Expand Down Expand Up @@ -1290,6 +1293,7 @@
5BB2887820999AD60043B530 /* AnyConnectionTests.swift in Sources */,
5B1F104B211037500067193C /* ConsumerConnectableTests.swift in Sources */,
5BB2887120999AD60043B530 /* LoggingInitTests.swift in Sources */,
2DDF54C3229BEEC400D05861 /* CompositeEventSourceBuilderTests.swift in Sources */,
5B85AD0220AAA8CA00C4FCD5 /* MobiusHooksTests.swift in Sources */,
5BB2886D20999AD60043B530 /* MobiusControllerTests.swift in Sources */,
5BB2887B20999AD60043B530 /* ConnectablePublisherTests.swift in Sources */,
Expand Down Expand Up @@ -1558,6 +1562,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks $(PROJECT_DIR)/Carthage/Build/iOS";
PRODUCT_BUNDLE_IDENTIFIER = com.spotify.MobiusCoreTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_TREAT_WARNINGS_AS_ERRORS = NO;
};
name = Debug;
};
Expand All @@ -1572,6 +1577,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks $(PROJECT_DIR)/Carthage/Build/iOS";
PRODUCT_BUNDLE_IDENTIFIER = com.spotify.MobiusCoreTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_TREAT_WARNINGS_AS_ERRORS = NO;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public struct CompositeEventSourceBuilder<Event> {
/// Returns a new `CompositeEventSourceBuilder` with the specified event source added to it.
public func addEventSource<Source: EventSource>(_ source: Source)
-> CompositeEventSourceBuilder<Event> where Source.Event == Event {
let es = AnyEventSource<Event>(source)
let sources = eventSources + [es]
let sources = eventSources + [AnyEventSource<Event>(source)]
return CompositeEventSourceBuilder(eventSources: sources)
}

Expand Down
139 changes: 139 additions & 0 deletions MobiusCore/Test/EventSources/CompositeEventSourceBuilderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// 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 {
// swiftlint:disable function_body_length
override func spec() {
var eventsReceived: [Int]!
var compositeEventSource: AnyEventSource<Int>!
var disposable: Disposable!

describe("CompositeEventSourceBuilder") {
context("when configuring the composite event source builder") {
context("with no event sources") {
beforeEach {
let sut = CompositeEventSourceBuilder<Int>()
compositeEventSource = sut.build()
eventsReceived = []
}

it("should produce an event source") {
// In particular, we want a do-nothing event source rather than an assertion or crash.
disposable = compositeEventSource.subscribe {
eventsReceived.append($0)
}
disposable.dispose()

expect(eventsReceived).to(equal([]))
}
}

context("with one event source") {
var eventSource: TestEventSource!

beforeEach {
eventSource = TestEventSource()
let sut = CompositeEventSourceBuilder<Int>()
.addEventSource(eventSource)

compositeEventSource = sut.build()
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") {
var eventSources: [TestEventSource]!

beforeEach {
eventSources = [TestEventSource(), TestEventSource(), TestEventSource()]
var sut = CompositeEventSourceBuilder<Int>()
eventSources.forEach {
sut = sut.addEventSource($0)
}

compositeEventSource = sut.build()
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)
}
}

0 comments on commit 7fb96ff

Please sign in to comment.