Add more test to cover timer.
This commit is contained in:
parent
d0b9c02501
commit
05dfde1e19
|
@ -8,6 +8,8 @@
|
|||
|
||||
/* Begin PBXBuildFile section */
|
||||
047CD6E523B8128B0551EF93 /* Pods_PainlessInjection_PainlessInjectionTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DED8A06520EB5C31D129C73F /* Pods_PainlessInjection_PainlessInjectionTests.framework */; };
|
||||
C4251EE71D3D4201005C5A57 /* OnDemandDependency.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4251EE61D3D4201005C5A57 /* OnDemandDependency.swift */; };
|
||||
C4251EE91D3D4657005C5A57 /* StandardTimerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4251EE81D3D4657005C5A57 /* StandardTimerTests.swift */; };
|
||||
C4466D151D3D2761008D0A80 /* ModuleLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4466D141D3D2761008D0A80 /* ModuleLoader.swift */; };
|
||||
C4A0CFCB1D3CF88F00BCEC00 /* ArgumentList.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A0CFBC1D3CF88F00BCEC00 /* ArgumentList.swift */; };
|
||||
C4A0CFCC1D3CF88F00BCEC00 /* CacheDependency.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A0CFBD1D3CF88F00BCEC00 /* CacheDependency.swift */; };
|
||||
|
@ -21,7 +23,7 @@
|
|||
C4A0CFD51D3CF88F00BCEC00 /* SingletonDependency.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A0CFC61D3CF88F00BCEC00 /* SingletonDependency.swift */; };
|
||||
C4A0CFD61D3CF88F00BCEC00 /* StandardFatalErrorNotifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A0CFC71D3CF88F00BCEC00 /* StandardFatalErrorNotifier.swift */; };
|
||||
C4A0CFD71D3CF88F00BCEC00 /* StandardTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A0CFC81D3CF88F00BCEC00 /* StandardTimer.swift */; };
|
||||
C4A0CFD81D3CF88F00BCEC00 /* TimeInteval.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A0CFC91D3CF88F00BCEC00 /* TimeInteval.swift */; };
|
||||
C4A0CFD81D3CF88F00BCEC00 /* TimeInterval.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A0CFC91D3CF88F00BCEC00 /* TimeInterval.swift */; };
|
||||
C4A0CFD91D3CF88F00BCEC00 /* Timer.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A0CFCA1D3CF88F00BCEC00 /* Timer.swift */; };
|
||||
C4A0D00B1D3CFB8E00BCEC00 /* ArgumentListTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A0CFDB1D3CF89700BCEC00 /* ArgumentListTests.swift */; };
|
||||
C4A0D00C1D3CFB8E00BCEC00 /* ContainerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A0CFDC1D3CF89700BCEC00 /* ContainerTests.swift */; };
|
||||
|
@ -49,6 +51,8 @@
|
|||
5F6CB7F1DCD19AA2473C6096 /* Pods-PainlessInjection.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PainlessInjection.debug.xcconfig"; path = "Pods/Target Support Files/Pods-PainlessInjection/Pods-PainlessInjection.debug.xcconfig"; sourceTree = "<group>"; };
|
||||
A594AFA22154B33BAD7AB162 /* Pods_PainlessInjection.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_PainlessInjection.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
BE4F60DB130F49109E1B72DD /* Pods-PainlessInjection.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PainlessInjection.release.xcconfig"; path = "Pods/Target Support Files/Pods-PainlessInjection/Pods-PainlessInjection.release.xcconfig"; sourceTree = "<group>"; };
|
||||
C4251EE61D3D4201005C5A57 /* OnDemandDependency.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OnDemandDependency.swift; sourceTree = "<group>"; };
|
||||
C4251EE81D3D4657005C5A57 /* StandardTimerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StandardTimerTests.swift; sourceTree = "<group>"; };
|
||||
C4466D141D3D2761008D0A80 /* ModuleLoader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ModuleLoader.swift; sourceTree = "<group>"; };
|
||||
C4A0CFBC1D3CF88F00BCEC00 /* ArgumentList.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArgumentList.swift; sourceTree = "<group>"; };
|
||||
C4A0CFBD1D3CF88F00BCEC00 /* CacheDependency.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CacheDependency.swift; sourceTree = "<group>"; };
|
||||
|
@ -63,7 +67,7 @@
|
|||
C4A0CFC61D3CF88F00BCEC00 /* SingletonDependency.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SingletonDependency.swift; sourceTree = "<group>"; };
|
||||
C4A0CFC71D3CF88F00BCEC00 /* StandardFatalErrorNotifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StandardFatalErrorNotifier.swift; sourceTree = "<group>"; };
|
||||
C4A0CFC81D3CF88F00BCEC00 /* StandardTimer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StandardTimer.swift; sourceTree = "<group>"; };
|
||||
C4A0CFC91D3CF88F00BCEC00 /* TimeInteval.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TimeInteval.swift; sourceTree = "<group>"; };
|
||||
C4A0CFC91D3CF88F00BCEC00 /* TimeInterval.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TimeInterval.swift; sourceTree = "<group>"; };
|
||||
C4A0CFCA1D3CF88F00BCEC00 /* Timer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Timer.swift; sourceTree = "<group>"; };
|
||||
C4A0CFDB1D3CF89700BCEC00 /* ArgumentListTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArgumentListTests.swift; sourceTree = "<group>"; };
|
||||
C4A0CFDC1D3CF89700BCEC00 /* ContainerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContainerTests.swift; sourceTree = "<group>"; };
|
||||
|
@ -125,9 +129,10 @@
|
|||
C4A0CFC61D3CF88F00BCEC00 /* SingletonDependency.swift */,
|
||||
C4A0CFC71D3CF88F00BCEC00 /* StandardFatalErrorNotifier.swift */,
|
||||
C4A0CFC81D3CF88F00BCEC00 /* StandardTimer.swift */,
|
||||
C4A0CFC91D3CF88F00BCEC00 /* TimeInteval.swift */,
|
||||
C4A0CFC91D3CF88F00BCEC00 /* TimeInterval.swift */,
|
||||
C4A0CFCA1D3CF88F00BCEC00 /* Timer.swift */,
|
||||
C4466D141D3D2761008D0A80 /* ModuleLoader.swift */,
|
||||
C4251EE61D3D4201005C5A57 /* OnDemandDependency.swift */,
|
||||
);
|
||||
path = PainlessInjection;
|
||||
sourceTree = "<group>";
|
||||
|
@ -143,6 +148,7 @@
|
|||
C4A0CFE01D3CF89700BCEC00 /* ModuleLoadingTests.swift */,
|
||||
C4A0CFE11D3CF89700BCEC00 /* TimeIntervalTests.swift */,
|
||||
C4A0CFE21D3CF89700BCEC00 /* TimerTests.swift */,
|
||||
C4251EE81D3D4657005C5A57 /* StandardTimerTests.swift */,
|
||||
);
|
||||
path = PainlessInjectionTests;
|
||||
sourceTree = "<group>";
|
||||
|
@ -373,6 +379,7 @@
|
|||
C4A0CFCB1D3CF88F00BCEC00 /* ArgumentList.swift in Sources */,
|
||||
C4A0CFCF1D3CF88F00BCEC00 /* Dependency.swift in Sources */,
|
||||
C4A0CFCE1D3CF88F00BCEC00 /* DefineConfigurator.swift in Sources */,
|
||||
C4251EE71D3D4201005C5A57 /* OnDemandDependency.swift in Sources */,
|
||||
C4A0CFD91D3CF88F00BCEC00 /* Timer.swift in Sources */,
|
||||
C4A0CFD61D3CF88F00BCEC00 /* StandardFatalErrorNotifier.swift in Sources */,
|
||||
C4466D151D3D2761008D0A80 /* ModuleLoader.swift in Sources */,
|
||||
|
@ -381,7 +388,7 @@
|
|||
C4A0CFD31D3CF88F00BCEC00 /* ModuleLoadingPredicate.swift in Sources */,
|
||||
C4A0CFD21D3CF88F00BCEC00 /* Module.swift in Sources */,
|
||||
C4A0CFCD1D3CF88F00BCEC00 /* Container.swift in Sources */,
|
||||
C4A0CFD81D3CF88F00BCEC00 /* TimeInteval.swift in Sources */,
|
||||
C4A0CFD81D3CF88F00BCEC00 /* TimeInterval.swift in Sources */,
|
||||
C4A0CFCC1D3CF88F00BCEC00 /* CacheDependency.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
|
@ -390,6 +397,7 @@
|
|||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
C4251EE91D3D4657005C5A57 /* StandardTimerTests.swift in Sources */,
|
||||
C4A0D00B1D3CFB8E00BCEC00 /* ArgumentListTests.swift in Sources */,
|
||||
C4A0D0111D3CFB8E00BCEC00 /* TimerTests.swift in Sources */,
|
||||
C4A0D00F1D3CFB8E00BCEC00 /* ModuleLoadingTests.swift in Sources */,
|
||||
|
|
|
@ -13,7 +13,7 @@ class CacheDependency: Dependency, TimerDelelgate {
|
|||
private var _value: Any!
|
||||
private var _dependency: Dependency
|
||||
private let _timer: TimerProtocol
|
||||
init(dependency: Dependency, interval: TimeInteval) {
|
||||
init(dependency: Dependency, interval: TimeInterval) {
|
||||
_dependency = dependency
|
||||
_timer = Timer.factory.newTimerWithInterval(interval)
|
||||
_timer.delegate = self
|
||||
|
@ -27,6 +27,7 @@ class CacheDependency: Dependency, TimerDelelgate {
|
|||
if _value == nil {
|
||||
objc_sync_enter(self)
|
||||
_value = _dependency.create(args)
|
||||
_timer.start()
|
||||
objc_sync_exit(self)
|
||||
}
|
||||
return _value;
|
||||
|
@ -34,5 +35,6 @@ class CacheDependency: Dependency, TimerDelelgate {
|
|||
|
||||
func onTick() {
|
||||
_value = nil
|
||||
_timer.stop()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,14 +17,16 @@ public struct DefineDependencyStatement {
|
|||
_dependency = dependency
|
||||
}
|
||||
|
||||
public func inSingletonScope() {
|
||||
public func inSingletonScope() -> DefineDependencyStatement {
|
||||
let singletonDependency = SingletonDependency(dependency: _dependency)
|
||||
Container.setDependency(singletonDependency, forType: _type)
|
||||
return self
|
||||
}
|
||||
|
||||
public func inCacheScope(interval interval: TimeInteval) {
|
||||
public func inCacheScope(interval interval: TimeInterval) -> DefineDependencyStatement {
|
||||
let singletonDependency = CacheDependency(dependency: _dependency, interval: interval)
|
||||
Container.setDependency(singletonDependency, forType: _type)
|
||||
return self
|
||||
}
|
||||
|
||||
public func decorate(wrapper: (Dependency) -> Dependency) {
|
||||
|
|
|
@ -11,23 +11,4 @@ import Foundation
|
|||
public protocol Dependency {
|
||||
var type: Any.Type { get }
|
||||
func create(args: [Any]) -> Any
|
||||
}
|
||||
|
||||
public class OnDemandDependency: Dependency {
|
||||
|
||||
private var _type: Any.Type
|
||||
public var type: Any.Type {
|
||||
return _type
|
||||
}
|
||||
|
||||
private var _configurator: ([Any]) -> Any
|
||||
public init(type: Any.Type, configurator: ([Any]) -> Any) {
|
||||
_type = type
|
||||
_configurator = configurator
|
||||
}
|
||||
|
||||
public func create(args: [Any]) -> Any {
|
||||
return _configurator(args)
|
||||
}
|
||||
|
||||
}
|
|
@ -8,7 +8,6 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
@objc(PIModule)
|
||||
public class Module: NSObject {
|
||||
|
||||
public required override init() {
|
||||
|
|
|
@ -23,7 +23,6 @@ class ModuleLoader {
|
|||
objc_getClassList(classes, numberOfClasses)
|
||||
for index in 0..<Int(numberOfClasses) {
|
||||
if let cls: AnyClass = classes[index] {
|
||||
if class_isMetaClass(cls) { continue }
|
||||
if class_getSuperclass(cls) != Module.self { continue }
|
||||
if let moduleClass = cls as? Module.Type {
|
||||
let module: Module = moduleClass.init()
|
||||
|
|
|
@ -18,36 +18,36 @@ struct LoadModulePredicate: ModuleLoadingPredicate {
|
|||
}
|
||||
}
|
||||
|
||||
struct ClousureModuleLoadingPredicate: ModuleLoadingPredicate {
|
||||
private var predicate: () -> Bool
|
||||
init(_ predicate: () -> Bool) {
|
||||
self.predicate = predicate
|
||||
}
|
||||
|
||||
func shouldLoadModule() -> Bool {
|
||||
return self.predicate()
|
||||
}
|
||||
}
|
||||
|
||||
public func clousure(predicate: () -> Bool) -> ModuleLoadingPredicate {
|
||||
return ClousureModuleLoadingPredicate(predicate)
|
||||
}
|
||||
|
||||
|
||||
struct CommandLineArgumentModuleLoadingPredicate: ModuleLoadingPredicate {
|
||||
|
||||
private var _args: [String]
|
||||
init(args: [String]) {
|
||||
_args = args
|
||||
}
|
||||
|
||||
func shouldLoadModule() -> Bool {
|
||||
let processArguments = NSProcessInfo.processInfo().arguments
|
||||
return Set(_args).isSubsetOf(Set(processArguments))
|
||||
}
|
||||
}
|
||||
|
||||
public func commandLineArgs(args: String...) -> ModuleLoadingPredicate {
|
||||
return CommandLineArgumentModuleLoadingPredicate(args: args)
|
||||
}
|
||||
|
||||
//struct ClousureModuleLoadingPredicate: ModuleLoadingPredicate {
|
||||
// private var predicate: () -> Bool
|
||||
// init(_ predicate: () -> Bool) {
|
||||
// self.predicate = predicate
|
||||
// }
|
||||
//
|
||||
// func shouldLoadModule() -> Bool {
|
||||
// return self.predicate()
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//public func clousure(predicate: () -> Bool) -> ModuleLoadingPredicate {
|
||||
// return ClousureModuleLoadingPredicate(predicate)
|
||||
//}
|
||||
//
|
||||
//
|
||||
//struct CommandLineArgumentModuleLoadingPredicate: ModuleLoadingPredicate {
|
||||
//
|
||||
// private var _args: [String]
|
||||
// init(args: [String]) {
|
||||
// _args = args
|
||||
// }
|
||||
//
|
||||
// func shouldLoadModule() -> Bool {
|
||||
// let processArguments = NSProcessInfo.processInfo().arguments
|
||||
// return Set(_args).isSubsetOf(Set(processArguments))
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//public func commandLineArgs(args: String...) -> ModuleLoadingPredicate {
|
||||
// return CommandLineArgumentModuleLoadingPredicate(args: args)
|
||||
//}
|
||||
//
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
//
|
||||
// OnDemandDependency.swift
|
||||
// PainlessInjection
|
||||
//
|
||||
// Created by Yaroslav Zhurakovskiy on 7/18/16.
|
||||
// Copyright © 2016 Yaroslav Zhurakovskiy. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class OnDemandDependency: Dependency {
|
||||
|
||||
private var _type: Any.Type
|
||||
var type: Any.Type {
|
||||
return _type
|
||||
}
|
||||
|
||||
private var _configurator: ([Any]) -> Any
|
||||
init(type: Any.Type, configurator: ([Any]) -> Any) {
|
||||
_type = type
|
||||
_configurator = configurator
|
||||
}
|
||||
|
||||
func create(args: [Any]) -> Any {
|
||||
return _configurator(args)
|
||||
}
|
||||
|
||||
}
|
|
@ -11,29 +11,45 @@ import Foundation
|
|||
public class StandardTimer: NSObject, TimerProtocol {
|
||||
public var delegate: TimerDelelgate?
|
||||
|
||||
private let _timeInterval: TimeInteval
|
||||
private var _timer: NSTimer!
|
||||
private let _timeInterval: TimeInterval
|
||||
|
||||
public init(interval: TimeInteval) {
|
||||
public init(interval: TimeInterval) {
|
||||
_timeInterval = interval
|
||||
_timer = nil
|
||||
|
||||
super.init()
|
||||
|
||||
_timer = NSTimer.scheduledTimerWithTimeInterval(_timeInterval.timeInterval, target: self, selector: #selector(onTimer), userInfo: nil, repeats: true)
|
||||
}
|
||||
|
||||
var _nstimer:NSTimer?
|
||||
public var nstimer: NSTimer {
|
||||
get {
|
||||
if let timer = _nstimer {
|
||||
return timer
|
||||
}
|
||||
return NSTimer(timeInterval: _timeInterval.timeInterval, target: self, selector: #selector(onTimer), userInfo: nil, repeats: true)
|
||||
}
|
||||
set {
|
||||
_nstimer = newValue
|
||||
}
|
||||
}
|
||||
|
||||
public func start() {
|
||||
self.nstimer.fire()
|
||||
}
|
||||
|
||||
public func stop() {
|
||||
self.nstimer.invalidate()
|
||||
}
|
||||
|
||||
deinit {
|
||||
_timer.invalidate()
|
||||
self.nstimer.invalidate()
|
||||
}
|
||||
|
||||
func onTimer() {
|
||||
public func onTimer() {
|
||||
delegate?.onTick()
|
||||
}
|
||||
}
|
||||
|
||||
public class StandardTimerFactory: TimerFactoryProtocol {
|
||||
public func newTimerWithInterval(interval: TimeInteval) -> TimerProtocol {
|
||||
public func newTimerWithInterval(interval: TimeInterval) -> TimerProtocol {
|
||||
return StandardTimer(interval: interval)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
//
|
||||
// TimeInteval.swift
|
||||
// TimeInterval.swift
|
||||
// PainlessInjection
|
||||
//
|
||||
// Created by Yaroslav Zhurakovskiy on 7/12/16.
|
||||
|
@ -8,7 +8,7 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
public struct TimeInteval {
|
||||
public struct TimeInterval {
|
||||
private let _timeInterval: Double
|
||||
|
||||
public init(seconds: Int) {
|
|
@ -13,12 +13,15 @@ public protocol TimerDelelgate: class {
|
|||
}
|
||||
|
||||
public protocol TimerProtocol {
|
||||
weak var delegate: TimerDelelgate? { get set }
|
||||
weak var delegate: TimerDelelgate? { get set }
|
||||
|
||||
func start()
|
||||
func stop()
|
||||
}
|
||||
|
||||
|
||||
public protocol TimerFactoryProtocol {
|
||||
func newTimerWithInterval(interval: TimeInteval) -> TimerProtocol
|
||||
func newTimerWithInterval(interval: TimeInterval) -> TimerProtocol
|
||||
}
|
||||
|
||||
public struct Timer {
|
||||
|
|
|
@ -188,7 +188,7 @@ class ContainerTests: XCTestCase {
|
|||
func testShouldCreateDependencyWithCachedScope() {
|
||||
class TestModule: Module {
|
||||
override func load() {
|
||||
define(Service.self) { Service() } . inCacheScope(interval: TimeInteval(seconds: 60))
|
||||
define(Service.self) { Service() } . inCacheScope(interval: TimeInterval(seconds: 60))
|
||||
}
|
||||
}
|
||||
let module = TestModule()
|
||||
|
@ -199,10 +199,13 @@ class ContainerTests: XCTestCase {
|
|||
XCTAssertEqual(s1.id, 1, "Should have the same id until cache is valid")
|
||||
XCTAssertEqual(s2.id, 1, "Should have the same id until cache is valid")
|
||||
|
||||
timer.assertStartWasCalledTimes(1)
|
||||
timer.timeout()
|
||||
timer.assertStopWasCalledOnce()
|
||||
|
||||
let s3: Service = Container.get()
|
||||
XCTAssertEqual(s3.id, 2, "Should have a new service.")
|
||||
timer.assertStartWasCalledTimes(2)
|
||||
}
|
||||
|
||||
func testShouldReturnDefineConfigurator() {
|
||||
|
@ -218,4 +221,49 @@ class ContainerTests: XCTestCase {
|
|||
|
||||
XCTAssertNotNil(module.configurator)
|
||||
}
|
||||
|
||||
func testWhenUseSimpleDependencyShouldReturnCorrectDependencyType() {
|
||||
class TestModule: ModuleWithDependency {
|
||||
override func load() {
|
||||
define(String.self) { "Hello" } . decorate { dependency in
|
||||
self.dependency = dependency
|
||||
return dependency
|
||||
}
|
||||
}
|
||||
}
|
||||
let module = TestModule()
|
||||
module.load()
|
||||
|
||||
module.assertDependencyType(String.self)
|
||||
}
|
||||
|
||||
func testWhenUseSingletoneScopeShouldReturnCorrectDependencyType() {
|
||||
class TestModule: ModuleWithDependency {
|
||||
override func load() {
|
||||
define(String.self) { "Hello" } . inSingletonScope() . decorate { dependency in
|
||||
self.dependency = dependency
|
||||
return dependency
|
||||
}
|
||||
}
|
||||
}
|
||||
let module = TestModule()
|
||||
module.load()
|
||||
|
||||
module.assertDependencyType(String.self)
|
||||
}
|
||||
|
||||
func testWhenUseCacheScopeShouldReturnCorrectDependencyType() {
|
||||
class TestModule: ModuleWithDependency {
|
||||
override func load() {
|
||||
define(String.self) { "Hello" } . inCacheScope(interval: TimeInterval(minutes: 1)) . decorate { dependency in
|
||||
self.dependency = dependency
|
||||
return dependency
|
||||
}
|
||||
}
|
||||
}
|
||||
let module = TestModule()
|
||||
module.load()
|
||||
|
||||
module.assertDependencyType(String.self)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -95,6 +95,24 @@ class FakeTimer: TimerProtocol {
|
|||
func timeout() {
|
||||
delegate?.onTick()
|
||||
}
|
||||
|
||||
var startWasCalledTimes: Int = 0
|
||||
func start() {
|
||||
startWasCalledTimes += 1
|
||||
}
|
||||
|
||||
var stopWasCalledTimes: Int = 0
|
||||
func stop() {
|
||||
stopWasCalledTimes += 1
|
||||
}
|
||||
|
||||
func assertStartWasCalledTimes(times: Int, file: StaticString = #file, line: UInt = #line) {
|
||||
XCTAssertTrue(startWasCalledTimes == times, "Expected start() to be called \(times). But was called \(startWasCalledTimes).", file: file, line: line)
|
||||
}
|
||||
|
||||
func assertStopWasCalledOnce(file: StaticString = #file, line: UInt = #line) {
|
||||
XCTAssertTrue(stopWasCalledTimes == 1, "Expected stop() to be called once. But was called \(startWasCalledTimes).", file: file, line: line)
|
||||
}
|
||||
}
|
||||
|
||||
class FakeTimerFactory: TimerFactoryProtocol {
|
||||
|
@ -102,7 +120,69 @@ class FakeTimerFactory: TimerFactoryProtocol {
|
|||
init(timer: FakeTimer) {
|
||||
_timer = timer
|
||||
}
|
||||
func newTimerWithInterval(interval: TimeInteval) -> TimerProtocol {
|
||||
func newTimerWithInterval(interval: TimeInterval) -> TimerProtocol {
|
||||
return _timer
|
||||
}
|
||||
}
|
||||
|
||||
class ModuleWithDependency : Module {
|
||||
var dependency: Dependency!
|
||||
override func load() {
|
||||
define(String.self) { "Hello" } . decorate { dependency in
|
||||
self.dependency = dependency
|
||||
return dependency
|
||||
}
|
||||
}
|
||||
|
||||
func assertDependencyType(type: Any.Type, file: StaticString = #file, line: UInt = #line) {
|
||||
XCTAssertTrue(dependency.type == type, "Type should be String but got \(dependency.type)", file: file, line: line)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class FakeTimerDelegate: TimerDelelgate {
|
||||
|
||||
private var calls: Int = 0
|
||||
|
||||
func onTick() {
|
||||
calls += 1
|
||||
}
|
||||
|
||||
func assertCallOnTickOnce(file: StaticString = #file, line: UInt = #line) {
|
||||
XCTAssertTrue(calls == 1, "Expected onTick to be called once. But was called \(calls).", file: file, line: line)
|
||||
}
|
||||
}
|
||||
|
||||
class FakeNSTimer: NSTimer {
|
||||
var spyTarget: AnyObject
|
||||
var spySelector: Selector
|
||||
var spyFireDate: NSDate
|
||||
var spyInterval: NSTimeInterval
|
||||
|
||||
override init(fireDate date: NSDate, interval ti: NSTimeInterval, target t: AnyObject, selector s: Selector, userInfo ui: AnyObject?, repeats rep: Bool) {
|
||||
self.spyTarget = t
|
||||
self.spySelector = s
|
||||
self.spyFireDate = date
|
||||
self.spyInterval = ti
|
||||
super.init(fireDate: date, interval: ti, target: t, selector: s, userInfo: ui, repeats: rep)
|
||||
}
|
||||
|
||||
var fireWasCalledTimes: Int = 0
|
||||
override func fire() {
|
||||
fireWasCalledTimes += 1
|
||||
|
||||
// self.spyTarget.performSelector(self.spySelector)
|
||||
|
||||
}
|
||||
func assertFireWasCallTimes(times: Int, file: StaticString = #file, line: UInt = #line) {
|
||||
XCTAssertTrue(fireWasCalledTimes == times, "Expected fire() to be called \(times). But was called \(fireWasCalledTimes).", file: file, line: line)
|
||||
}
|
||||
|
||||
var invalidateWasCalledTimes: Int = 0
|
||||
override func invalidate() {
|
||||
invalidateWasCalledTimes += 1
|
||||
}
|
||||
func assertInvalidateCallTimes(times: Int, file: StaticString = #file, line: UInt = #line) {
|
||||
XCTAssertTrue(invalidateWasCalledTimes == times, "Expected fire() to be called \(times). But was called \(invalidateWasCalledTimes).", file: file, line: line)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
//
|
||||
// StandardTimer.swift
|
||||
// PainlessInjection
|
||||
//
|
||||
// Created by Yaroslav Zhurakovskiy on 7/18/16.
|
||||
// Copyright © 2016 Yaroslav Zhurakovskiy. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
import PainlessInjection
|
||||
|
||||
let TimerValueSeconds: Int = 2
|
||||
|
||||
func dispatchAfter(seconds seconds: Int, block: () -> Void) {
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(UInt64(seconds) * NSEC_PER_SEC)), dispatch_get_main_queue()) {
|
||||
block()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class StandardTimerTests: XCTestCase {
|
||||
|
||||
var timer: StandardTimer!
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
|
||||
timer = StandardTimer(interval: TimeInterval(seconds: TimerValueSeconds))
|
||||
}
|
||||
|
||||
func testSmoke() {
|
||||
XCTAssertNotNil(timer)
|
||||
}
|
||||
|
||||
func testShouldInvalidateOnStop() {
|
||||
let nstimerMock = FakeNSTimer()
|
||||
timer.nstimer = nstimerMock
|
||||
timer.stop()
|
||||
|
||||
nstimerMock.assertInvalidateCallTimes(1)
|
||||
}
|
||||
|
||||
func testShouldInvalidateOnDealloc() {
|
||||
let nstimerMock = FakeNSTimer()
|
||||
timer.nstimer = nstimerMock
|
||||
timer = nil
|
||||
|
||||
defer {
|
||||
nstimerMock.assertInvalidateCallTimes(1)
|
||||
}
|
||||
}
|
||||
|
||||
func testShouldCallDelegateAfterDelay() {
|
||||
let delegateMock = FakeTimerDelegate()
|
||||
timer.delegate = delegateMock
|
||||
timer.start()
|
||||
let expectation = expectationWithDescription("Standard timer")
|
||||
dispatchAfter(seconds: TimerValueSeconds) {
|
||||
expectation.fulfill()
|
||||
}
|
||||
waitForExpectationsWithTimeout(NSTimeInterval(TimerValueSeconds + 1)) { error in
|
||||
XCTAssertNil(error, "Shold not return any error.")
|
||||
delegateMock.assertCallOnTickOnce()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -12,13 +12,13 @@ import PainlessInjection
|
|||
class TimeIntervalTests: XCTestCase {
|
||||
|
||||
func testSeconds() {
|
||||
let interval = TimeInteval(seconds: 1)
|
||||
let interval = TimeInterval(seconds: 1)
|
||||
|
||||
XCTAssertEqual(interval.timeInterval, 1)
|
||||
}
|
||||
|
||||
func testMinutes() {
|
||||
let interval = TimeInteval(minutes: 1)
|
||||
let interval = TimeInterval(minutes: 1)
|
||||
|
||||
XCTAssertEqual(interval.timeInterval, 60)
|
||||
}
|
||||
|
|
|
@ -12,9 +12,9 @@ import PainlessInjection
|
|||
class TimerTests: XCTestCase {
|
||||
|
||||
func testShouldHaveDefaultTimer() {
|
||||
let timer = Timer.factory.newTimerWithInterval(TimeInteval(minutes: 1))
|
||||
let timer = Timer.factory.newTimerWithInterval(TimeInterval(minutes: 1))
|
||||
|
||||
XCTAssertTrue(timer is StandardTimer)
|
||||
XCTAssertTrue(timer is StandardTimer, "Expected StandardTimer but got \(timer.self)")
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue