From 14c7e35c4527c35886e41a1c0db3f24083246535 Mon Sep 17 00:00:00 2001 From: David Hardiman Date: Thu, 26 Sep 2019 12:07:58 +0100 Subject: [PATCH] Add MockRouter library --- .../contents.xcworkspacedata | 7 ++ Package.swift | 3 +- Sources/MockRouter/MockRouter.swift | 104 ++++++++++++++++++ Tests/RouterTests/Info.plist | 22 ---- 4 files changed, 113 insertions(+), 23 deletions(-) create mode 100644 .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata create mode 100644 Sources/MockRouter/MockRouter.swift delete mode 100644 Tests/RouterTests/Info.plist diff --git a/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata b/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Package.swift b/Package.swift index 3ab9213..d001d12 100644 --- a/Package.swift +++ b/Package.swift @@ -7,11 +7,12 @@ let package = Package( name: "Router", products: [ .library(name: "Router", targets: ["Router"]), + .library(name: "MockRouter", targets: ["MockRouter"]) ], dependencies: [], targets: [ .target(name: "Router", dependencies: []), - .testTarget(name: "RouterTests", dependencies: ["Router"]), + .target(name: "MockRouter", dependencies: ["Router"]) ], swiftLanguageVersions: [.v5] ) diff --git a/Sources/MockRouter/MockRouter.swift b/Sources/MockRouter/MockRouter.swift new file mode 100644 index 0000000..8769e98 --- /dev/null +++ b/Sources/MockRouter/MockRouter.swift @@ -0,0 +1,104 @@ +// +// MockRouter.swift +// MockRouter +// +// Created by David Hardiman on 26/09/2019. +// Copyright © 2017 David Hardiman. All rights reserved. +// + +import Foundation +import Hopoate +import Router + +public class MockRouter: Router { + public var routeResponse: UIViewController = UIViewController() + public var receivedDestinations = [Destination]() + public var receivedTargets = [RouteTarget]() + public var vendRealController = false + public private(set) var vendedControllers: [UIViewController] = [] + public var receivedDestination: Destination? { + return receivedDestinations.last + } + + public func typedReceivedDestination(_ type: T.Type) -> T? { + return receivedDestination as? T + } + + public private(set) var receivedDismissMessage = false + + public init() {} + + @discardableResult + public func route(to destination: Destination) -> UIViewController? { + receivedDestinations.append(destination) + guard vendRealController else { + return routeResponse + } + let controller = destination.destination + vendedControllers.append(controller) + return controller + } + + public var receivedURL: URL? { + guard let target = receivedTarget, case let .url(url) = target else { + return nil + } + return url + } + + public var receivedModel: Any? { + guard let target = receivedTarget, case let .model(model) = target else { + return nil + } + return model + } + + private var receivedTarget: RouteTarget? { + return receivedTargets.last + } + + public func route(to target: RouteTarget) -> Bool { + receivedTargets.append(target) + return false + } + + public var receivedCompletion: (() -> Void)? + public func route(to target: RouteTarget, completion: (() -> Void)?) -> Bool { + receivedTargets.append(target) + receivedCompletion = completion + return false + } + + public var receivedExternalURL: URL? + public func routeFromExternalSource(to url: URL) -> Bool { + receivedExternalURL = url + return false + } + + public func dismiss(animated: Bool, completion: (() -> Void)?) { + receivedDismissMessage = true + completion?() + } + + public static func performTests(with testingClosure: (MockRouter) -> Void) { + try? performThrowingTests(with: testingClosure) + } + + public static func performThrowingTests(with testingClosure: (MockRouter) throws -> Void) throws { + let (mockRouter, registration) = registeredMockRouter() + try testingClosure(mockRouter) + DependencyContainer.shared.remove(registration) + } + + public static func registeredMockRouter() -> (MockRouter, ServiceRegistration) { + let mockRouter = MockRouter() + let mockRouterServiceRegistration = DependencyContainer.shared.register(service: Router.self) { + mockRouter + } + return (mockRouter, mockRouterServiceRegistration) + } + + public static func unregister(routerRegistration: ServiceRegistration) { + DependencyContainer.shared.remove(routerRegistration) + } +} diff --git a/Tests/RouterTests/Info.plist b/Tests/RouterTests/Info.plist deleted file mode 100644 index 6c40a6c..0000000 --- a/Tests/RouterTests/Info.plist +++ /dev/null @@ -1,22 +0,0 @@ - - - - - CFBundleDevelopmentRegion - $(DEVELOPMENT_LANGUAGE) - CFBundleExecutable - $(EXECUTABLE_NAME) - CFBundleIdentifier - $(PRODUCT_BUNDLE_IDENTIFIER) - CFBundleInfoDictionaryVersion - 6.0 - CFBundleName - $(PRODUCT_NAME) - CFBundlePackageType - BNDL - CFBundleShortVersionString - 1.0 - CFBundleVersion - 1 - -