Skip to content

Commit

Permalink
100 percent test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
acwright committed Dec 25, 2018
1 parent 937a418 commit d9ffbff
Show file tree
Hide file tree
Showing 25 changed files with 538 additions and 116 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Carthage](https://img.shields.io/badge/Carthage-compatible-brightgreen.svg?style=flat)](https://github.com/Carthage/Carthage)

Token is a unidirectional data flow framework for Swift!
Token is a unidirectional data flow framework for Swift

- [Installation](#installation)
- [License](#license)
Expand Down
98 changes: 51 additions & 47 deletions Token.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions Token.xcodeproj/xcshareddata/xcschemes/Token.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,18 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
codeCoverageEnabled = "YES"
onlyGenerateCoverageForSpecifiedTargets = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
<CodeCoverageTargets>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F039926E1FB3BB1700A76CB7"
BuildableName = "Token.framework"
BlueprintName = "Token"
ReferencedContainer = "container:Token.xcodeproj">
</BuildableReference>
</CodeCoverageTargets>
<Testables>
<TestableReference
skipped = "NO">
Expand Down
11 changes: 11 additions & 0 deletions Token.xcodeproj/xcshareddata/xcschemes/TokenMobile.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,18 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
codeCoverageEnabled = "YES"
onlyGenerateCoverageForSpecifiedTargets = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
<CodeCoverageTargets>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F039928D1FB3BB4700A76CB7"
BuildableName = "Token.framework"
BlueprintName = "TokenMobile"
ReferencedContainer = "container:Token.xcodeproj">
</BuildableReference>
</CodeCoverageTargets>
<Testables>
<TestableReference
skipped = "NO">
Expand Down
11 changes: 11 additions & 0 deletions Token.xcodeproj/xcshareddata/xcschemes/TokenTV.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,18 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
codeCoverageEnabled = "YES"
onlyGenerateCoverageForSpecifiedTargets = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
<CodeCoverageTargets>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F01EA0FA2101CA0F0040FA96"
BuildableName = "Token.framework"
BlueprintName = "TokenTV"
ReferencedContainer = "container:Token.xcodeproj">
</BuildableReference>
</CodeCoverageTargets>
<Testables>
<TestableReference
skipped = "NO">
Expand Down
2 changes: 1 addition & 1 deletion Token/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.3.4</string>
<string>1.4.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSHumanReadableCopyright</key>
Expand Down
2 changes: 1 addition & 1 deletion TokenMobile/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.3.4</string>
<string>1.4.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
2 changes: 2 additions & 0 deletions TokenShared/ActionCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
import Foundation

public protocol ActionCreator {

func create(store: Store, state: State?, result: @escaping (_ action: Action?) -> Void) -> Void

}
33 changes: 15 additions & 18 deletions TokenShared/Array+Cacheable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public extension Array where Element: Cacheable {
// MARK: - Getters

public func uuids() -> [String] { return map { $0.uuid } }
// public func items<T: Element>() -> [T] where T: Cacheable { return compactMap { $0 as? T } }
public func items(for uuids: [String]) -> [Element] { return filter { uuids.contains($0.uuid) } }

public func contains(_ cacheable: Element) -> Bool { return uuids().contains(cacheable.uuid) }
Expand All @@ -26,29 +25,27 @@ public extension Array where Element: Cacheable {
})
}

// public func item(for UUID: String) -> Element? {
// return reduce(nil, { (result, cacheable) -> Element? in
// if let result = result { return result }
// if cacheable.uuid != UUID { return nil }
// return cacheable
// })
// }

// MARK: - Maps

public func merging(_ cacheable: Element) -> [Element] {
var found: Bool = false
// var found: Bool = false
//
// let result: [Element] = map {
// if $0 == cacheable {
// found = true
// return cacheable
// } else {
// return $0
// }
// }
//
// if !found { return result.appending(cacheable) } else { return result }

let result: [Element] = map {
if $0 == cacheable {
found = true
return cacheable
} else {
return $0
}
let result: [Element] = filter { (element) -> Bool in
return element != cacheable
}

if !found { return result.appending(cacheable) } else { return result }
return result.appending(cacheable)
}

public func merging(cacheables: [Element]) -> [Element] {
Expand Down
2 changes: 2 additions & 0 deletions TokenShared/ArrayReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
import Foundation

public protocol ArrayReducer {

func reduce(action: Action, state: [State]) -> [State]

}
4 changes: 3 additions & 1 deletion TokenShared/Middleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ import Foundation
public typealias MiddlewareResult = (store: Store, action: Action, state: State?)

public protocol Middleware {
func execute(store: Store, action: Action, state: State?) -> MiddlewareResult?

func execute(store: Store, action: Action, state: State?) -> MiddlewareResult

}
2 changes: 2 additions & 0 deletions TokenShared/Reducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
import Foundation

public protocol Reducer {

func reduce(action: Action, state: State?) -> State?

}
11 changes: 9 additions & 2 deletions TokenShared/Subscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@
import Foundation

public protocol Subscriber: class {

func onChange(newState: State?, action: Action) -> Void

}

public extension Subscriber {

public func subscribe() { token.subscribe(subscriber: self) }
public func unsubscribe() { token.unsubscribe(subscriber: self) }
public var token: Token { return Token.shared }

public func dispatch(_ action: Action, token: Token = Token.shared) { token.dispatch(action: action) }
public func dispatch(_ actionCreator: ActionCreator, token: Token = Token.shared) { token.dispatch(actionCreator: actionCreator) }

public func subscribe(token: Token = Token.shared) { token.subscribe(subscriber: self) }
public func unsubscribe(token: Token = Token.shared) { token.unsubscribe(subscriber: self) }

}
13 changes: 3 additions & 10 deletions TokenShared/Token.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@

import Foundation

public var token: Token { return Token.shared }

public func dispatch(_ action: Action) { Token.shared.dispatch(action: action) }
public func dispatch(_ actionCreator: ActionCreator) { Token.shared.dispatch(actionCreator: actionCreator) }

public class Token: Store {

public static let shared = Token()
Expand All @@ -36,12 +31,10 @@ public class Token: Store {
public func dispatch(action: Action) {
if self.middleware.count > 0 {
let initial: MiddlewareResult = (self, action, self.state)
if let result = self.middleware.reduce(initial, { (result, middleware) -> MiddlewareResult? in
guard let result = result else { return nil }
let result = self.middleware.reduce(initial, { (result, middleware) -> MiddlewareResult in
return middleware.execute(store: result.store, action: result.action, state: result.state)
}) {
self.state = self.reducer?.reduce(action: result.action, state: result.state)
}
})
self.state = self.reducer?.reduce(action: result.action, state: result.state)
} else {
self.state = self.reducer?.reduce(action: action, state: self.state)
}
Expand Down
2 changes: 1 addition & 1 deletion TokenTV/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.3.4</string>
<string>1.4.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
40 changes: 40 additions & 0 deletions TokenTestsShared/ArrayAppendingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,44 @@ import XCTest

class ArrayAppendingTests: XCTestCase {

func testCanAppendSingleElement() {
let array = [1, 2, 3]

XCTAssertEqual(array.count, 3)

let newArray = array.appending(4)

XCTAssertEqual(newArray.count, 4)
}

func testCanAppendMultipleElements() {
let array = [1, 2, 3]

XCTAssertEqual(array.count, 3)

let newArray = array.appending([4, 5])

XCTAssertEqual(newArray.count, 5)
}

func testCanAppendSingleElementUsingPlus() {
let array = [1, 2, 3]

XCTAssertEqual(array.count, 3)

let newArray = array + 4

XCTAssertEqual(newArray.count, 4)
}

func testCanAppendMultipleElementsUsingPlus() {
let array = [1, 2, 3]

XCTAssertEqual(array.count, 3)

let newArray = array + [4, 5]

XCTAssertEqual(newArray.count, 5)
}

}
47 changes: 47 additions & 0 deletions TokenTestsShared/ArrayCacheableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,51 @@ import XCTest

class ArrayCacheableTests: XCTestCase {

func testItems() {
let array: [TestWidget] = [TestWidget(uuid: "123")]
let items = array.items(for: ["123"])

XCTAssertEqual(items.count, 1)
}

func testRemovingElement() {
let widget = TestWidget(uuid: "123")
var array: [TestWidget] = [widget]

XCTAssertEqual(array.count, 1)

array.remove(widget)

XCTAssertEqual(array.count, 0)
}

func testRemovingElements() {
let widget1 = TestWidget(uuid: "123")
let widget2 = TestWidget(uuid: "234")
var array: [TestWidget] = [widget1, widget2]

XCTAssertEqual(array.count, 2)

array.remove([widget1, widget2])

XCTAssertEqual(array.count, 0)
}

func testMerging() {
let widget1 = TestWidget(uuid: "123")
let widget2 = TestWidget(uuid: "234")
let widget3 = TestWidget(uuid: "345")
var array: [TestWidget] = [widget1, widget2]

XCTAssertEqual(array.count, 2)

array = array.merging(widget2)

XCTAssertEqual(array.count, 2)

array = array.merging(widget3)

XCTAssertEqual(array.count, 3)
}

}
Loading

0 comments on commit d9ffbff

Please sign in to comment.