Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: EffectRouterSpec #156

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Mobius.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
025BB529244DA65100E80BD2 /* ConnectableMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 025BB528244DA65100E80BD2 /* ConnectableMap.swift */; };
025BB52C244DA6BF00E80BD2 /* ConnectableMapTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 025BB52A244DA68500E80BD2 /* ConnectableMapTests.swift */; };
025BB52D244DA6C400E80BD2 /* ConnectableMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 025BB528244DA65100E80BD2 /* ConnectableMap.swift */; };
027D4519244DE4D100375BB5 /* EffectRouterSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 027D4518244DE4D100375BB5 /* EffectRouterSpec.swift */; };
027D451C244DE7C600375BB5 /* EffectRouterSpecTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 027D451A244DE7C000375BB5 /* EffectRouterSpecTests.swift */; };
027D451D244DE7CD00375BB5 /* EffectRouterSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 027D4518244DE4D100375BB5 /* EffectRouterSpec.swift */; };
02BED1BB21DD20D20093FB47 /* ConnectableContramap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B9CE80421197FE000DB79A7 /* ConnectableContramap.swift */; };
02C4061D2373078400BD7ED8 /* EffectExecutor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02C4061C2373078400BD7ED8 /* EffectExecutor.swift */; };
02C40621237422EF00BD7ED8 /* EffectRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02C40620237422EF00BD7ED8 /* EffectRouter.swift */; };
Expand Down Expand Up @@ -294,6 +297,8 @@
/* Begin PBXFileReference section */
025BB528244DA65100E80BD2 /* ConnectableMap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectableMap.swift; sourceTree = "<group>"; };
025BB52A244DA68500E80BD2 /* ConnectableMapTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectableMapTests.swift; sourceTree = "<group>"; };
027D4518244DE4D100375BB5 /* EffectRouterSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EffectRouterSpec.swift; sourceTree = "<group>"; };
027D451A244DE7C000375BB5 /* EffectRouterSpecTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EffectRouterSpecTests.swift; sourceTree = "<group>"; };
02C4061C2373078400BD7ED8 /* EffectExecutor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EffectExecutor.swift; sourceTree = "<group>"; };
02C40620237422EF00BD7ED8 /* EffectRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EffectRouter.swift; sourceTree = "<group>"; };
02C40622237425E100BD7ED8 /* EffectRouterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EffectRouterTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -814,6 +819,7 @@
5B5F11B8213805C000911A57 /* MatcherUtils.swift */,
5B19F71A21254CCD00074AD7 /* NextMatchers.swift */,
5BB288A420999B750043B530 /* UpdateSpec.swift */,
027D4518244DE4D100375BB5 /* EffectRouterSpec.swift */,
);
path = Source;
sourceTree = "<group>";
Expand All @@ -835,6 +841,7 @@
5B19F71D21255F9600074AD7 /* NextMatchersTests.swift */,
5B57E8BD2125AD09001AFC71 /* TestUtil.swift */,
5BB288AA20999B750043B530 /* UpdateSpecTests.swift */,
027D451A244DE7C000375BB5 /* EffectRouterSpecTests.swift */,
);
path = Test;
sourceTree = "<group>";
Expand Down Expand Up @@ -1261,6 +1268,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
027D451D244DE7CD00375BB5 /* EffectRouterSpec.swift in Sources */,
2DF4C53A20DBE04B00A4B6DE /* UpdateSpec.swift in Sources */,
5B5F11BA213805C000911A57 /* FirstMatchers.swift in Sources */,
5B5F11BC213805C000911A57 /* MatcherUtils.swift in Sources */,
Expand Down Expand Up @@ -1393,6 +1401,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
027D4519244DE4D100375BB5 /* EffectRouterSpec.swift in Sources */,
DDA64A6820A0AD5000150355 /* UpdateSpec.swift in Sources */,
5B5F11B9213805C000911A57 /* FirstMatchers.swift in Sources */,
5B5F11BB213805C000911A57 /* MatcherUtils.swift in Sources */,
Expand All @@ -1405,6 +1414,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
027D451C244DE7C600375BB5 /* EffectRouterSpecTests.swift in Sources */,
DDA64A6920A0AD5200150355 /* UpdateSpecTests.swift in Sources */,
5B19F71E21255F9600074AD7 /* NextMatchersTests.swift in Sources */,
5B9CE814211C5BCD00DB79A7 /* InitSpecTests.swift in Sources */,
Expand Down
90 changes: 90 additions & 0 deletions MobiusTest/Source/EffectRouterSpec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2020 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.

import MobiusCore
import XCTest

public struct EffectRouterSpec<Effect, Event> {
private let effectRouter: EffectRouter<Effect, Event>

public static func given(_ effectRouter: EffectRouter<Effect, Event>) -> Self {
return EffectRouterSpec(effectRouter: effectRouter)
}

public func when(_ effects: [Effect]) -> When {
When(effectRouter: effectRouter, effects: effects)
}

public func when(_ effects: Effect...) -> When {
When(effectRouter: effectRouter, effects: effects)
}

public struct When {
fileprivate let effectRouter: EffectRouter<Effect, Event>
fileprivate let effects: [Effect]

public func then(
_ matchers: Predicate<[Event]>...,
failFunction: @escaping AssertionFailure = XCTFail
) {
var gotEvents: [Event] = []

let connection = effectRouter.asConnectable
.connect { event in
gotEvents.append(event)
}
defer { connection.dispose() }

for effect in effects {
connection.accept(effect)
}

for matcher in matchers {
if case .failure(let message, let file, let line) = matcher(gotEvents) {
failFunction(message, file, line)
}
}
}
}
}

public func expectEvents<Event: Equatable>(
_ events: Event...,
file: StaticString = #file,
line: UInt = #line
) -> Predicate<[Event]> {
return { gotEvents in
if events.allSatisfy({ gotEvents.contains($0) }) {
return .success
} else {
return .failure(message: "Expected \(gotEvents) to contain: \(events)", file: file, line: line)
}
}
}

public func expectSideEffects<Event: Equatable>(
file: StaticString = #file,
line: UInt = #line,
_ sideEffects: @escaping () -> Void
) -> Predicate<[Event]> {
return { gotEvents in
sideEffects()
return .success
}
}
78 changes: 78 additions & 0 deletions MobiusTest/Test/EffectRouterSpecTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2020 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.

import MobiusCore
import MobiusTest
import Nimble
import Quick

final class EffectRouterSpecTests: QuickSpec {
private enum Effect {
case one
}

override func spec() {
context("Effect Router Spec") {
it("supports matching events") {
let effectRouter = EffectRouter<Effect, String>()
.routeCase(Effect.one).toEvent { _ in
return "test"
}

EffectRouterSpec.given(effectRouter)
.when(.one)
.then(
expectEvents("test")
)
}

it("supports checking for side effects") {
var performedEffect = false
let effectRouter = EffectRouter<Effect, String>()
.routeCase(Effect.one).to { _ in
performedEffect = true
}

EffectRouterSpec.given(effectRouter)
.when(.one)
.then(
expectSideEffects {
expect(performedEffect).to(beTrue())
}
)
}

it("supports multiple matchers") {
let effectRouter = EffectRouter<Effect, String>()
.routeCase(Effect.one).to { _, callback in
callback.end(with: "test1", "test2")
return AnonymousDisposable {}
}

EffectRouterSpec.given(effectRouter)
.when(.one)
.then(
expectEvents("test1"),
expectEvents("test2"),
expectEvents("test1", "test2")
)
}
}
}
}