-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
|
||
# Created by https://www.gitignore.io/api/xcode,macos,carthage,cocoapods | ||
# Edit at https://www.gitignore.io/?templates=xcode,macos,carthage,cocoapods | ||
|
||
### Carthage ### | ||
# Carthage | ||
# | ||
# Add this line if you want to avoid checking in source code from Carthage dependencies. | ||
# Carthage/Checkouts | ||
Carthage/Checkouts | ||
Carthage/Build | ||
|
||
### CocoaPods ### | ||
## CocoaPods GitIgnore Template | ||
|
||
# CocoaPods - Only use to conserve bandwidth / Save time on Pushing | ||
# - Also handy if you have a large number of dependant pods | ||
# - AS PER https://guides.cocoapods.org/using/using-cocoapods.html NEVER IGNORE THE LOCK FILE | ||
Pods/ | ||
|
||
### macOS ### | ||
# General | ||
.DS_Store | ||
.AppleDouble | ||
.LSOverride | ||
|
||
# Icon must end with two \r | ||
Icon | ||
|
||
# Thumbnails | ||
._* | ||
|
||
# Files that might appear in the root of a volume | ||
.DocumentRevisions-V100 | ||
.fseventsd | ||
.Spotlight-V100 | ||
.TemporaryItems | ||
.Trashes | ||
.VolumeIcon.icns | ||
.com.apple.timemachine.donotpresent | ||
|
||
# Directories potentially created on remote AFP share | ||
.AppleDB | ||
.AppleDesktop | ||
Network Trash Folder | ||
Temporary Items | ||
.apdisk | ||
|
||
### Xcode ### | ||
# Xcode | ||
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore | ||
|
||
## User settings | ||
xcuserdata/ | ||
|
||
## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) | ||
*.xcscmblueprint | ||
*.xccheckout | ||
|
||
## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) | ||
build/ | ||
DerivedData/ | ||
*.moved-aside | ||
*.pbxuser | ||
!default.pbxuser | ||
*.mode1v3 | ||
!default.mode1v3 | ||
*.mode2v3 | ||
!default.mode2v3 | ||
*.perspectivev3 | ||
!default.perspectivev3 | ||
|
||
## Xcode Patch | ||
*.xcodeproj/* | ||
!*.xcodeproj/project.pbxproj | ||
!*.xcodeproj/xcshareddata/ | ||
!*.xcworkspace/contents.xcworkspacedata | ||
/*.gcno | ||
|
||
### Xcode Patch ### | ||
**/xcshareddata/WorkspaceSettings.xcsettings | ||
|
||
# End of https://www.gitignore.io/api/xcode,macos,carthage,cocoapods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
github "Quick/Nimble" ~> 8.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
github "Quick/Nimble" "v8.0.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import Foundation | ||
import Nimble | ||
|
||
|
||
/// A basic protocol that every state type must conform to in order to be testable. | ||
protocol State { | ||
|
||
associatedtype Event | ||
|
||
/// A basic reduce that accepts current state and event and produces a new state. | ||
/// | ||
/// - Parameters: | ||
/// - state: current state; | ||
/// - event: event that affects the state; | ||
/// - Returns: New state. | ||
static func reduce(state: Self, event: Event) -> Self | ||
} | ||
|
||
|
||
extension State { | ||
|
||
/// Convenience method to mutate a state. | ||
/// | ||
/// - Parameters: | ||
/// - event: event to apply to receiver; | ||
mutating func apply(_ event: Event) { | ||
self = Self.reduce(state: self, event: event) | ||
} | ||
} | ||
|
||
extension Expectation where T: State { | ||
|
||
typealias Query<Q> = (T) -> Q? | ||
|
||
func after(_ events: T.Event...) -> StateChangeExpectation<T> { | ||
return StateChangeExpectation(expectation: self, events: events, change: nil) | ||
} | ||
|
||
func after(_ events: [T.Event]) -> StateChangeExpectation<T> { | ||
return StateChangeExpectation(expectation: self, events: events, change: nil) | ||
} | ||
|
||
func after(_ change: @escaping () -> (), _ events: T.Event...) -> StateChangeExpectation<T> { | ||
return StateChangeExpectation(expectation: self, events: events, change: change) | ||
} | ||
|
||
func to<Q>(_ query: @escaping Query<Q>) { | ||
StateChangeExpectation(expectation: self, events: [], change: nil) | ||
.to(query) | ||
} | ||
|
||
func toHave<Q>(_ query: @escaping Query<Q>) { | ||
to(query) | ||
} | ||
|
||
func toMatch(_ matcher: @escaping (T) -> Bool) { | ||
StateChangeExpectation(expectation: self, events: [], change: nil) | ||
.resolve(matcher) | ||
} | ||
} | ||
|
||
// MARK: - StateChangeExpectation | ||
|
||
struct StateChangeExpectation<S: State> { | ||
|
||
let expectation: Expectation<S> | ||
let events: [S.Event] | ||
let change: (() -> ())? | ||
|
||
var noChanges: Bool { | ||
return events.isEmpty && change == nil | ||
} | ||
|
||
private func afterChanges(to state: S) -> S { | ||
change?() | ||
return events.reduce(into: state) { $0.apply($1) } | ||
} | ||
} | ||
|
||
// MARK: Query | ||
|
||
extension StateChangeExpectation { | ||
|
||
typealias Query<Q> = (S) -> Q? | ||
|
||
func to<Q>(_ query: @escaping Query<Q>) { | ||
resolve(query, toBecomeNonNil: true) | ||
} | ||
|
||
func toHave<Q>(_ query: @escaping Query<Q>) { | ||
resolve(query, toBecomeNonNil: true) | ||
} | ||
|
||
func notTo<Q>(_ query: @escaping Query<Q>) { | ||
resolve(query, toBecomeNonNil: false) | ||
} | ||
|
||
private func resolve<Q>(_ query: @escaping Query<Q>, toBecomeNonNil: Bool) { | ||
resolve({ query($0) == nil }, to: "be a nil query", expectToFail: toBecomeNonNil) | ||
} | ||
} | ||
|
||
// MARK: Match | ||
|
||
extension StateChangeExpectation { | ||
|
||
typealias Match = (S) -> Bool | ||
|
||
|
||
/// An expectation that checks whether a value changes after received events. | ||
/// | ||
/// - Parameters: | ||
/// - matcher: a predicate to match a value against. | ||
func toTurn(_ matcher: @escaping Match) { | ||
resolve(matcher) | ||
} | ||
|
||
/// An expectation that checks whether a value remains the same after received events. | ||
/// | ||
/// - Parameters: | ||
/// - matcher: a predicate to match a value against. | ||
func toStay(_ matcher: @escaping Match) { | ||
resolve(matcher, expectSameResult: true) | ||
} | ||
} | ||
|
||
// MARK: - | ||
|
||
private extension StateChangeExpectation { | ||
|
||
func resolve(_ matcher: @escaping Match, to msg: String = "match", | ||
expectToFail: Bool = false, | ||
expectSameResult: Bool = false) { | ||
|
||
let afterTo = expectToFail ? expectation.notTo : expectation.to | ||
let beforeTo = expectSameResult ? afterTo : expectToFail ? expectation.to : expectation.notTo | ||
|
||
if noChanges { | ||
afterTo(predicate("\(msg) without any events", with: matcher), nil) | ||
} else { | ||
beforeTo(predicate("\(msg) before applying events", with: matcher), nil) | ||
afterTo(predicate("\(msg) after applying events", with: { matcher(self.afterChanges(to: $0)) }), nil) | ||
} | ||
} | ||
|
||
func predicate(_ msg: String, with matcher: @escaping Match) -> Predicate<S> { | ||
return Predicate { s -> PredicateResult in | ||
let result = try s.evaluate().flatMap(matcher) ?? false | ||
return PredicateResult(bool: result, message: .expectedTo(msg)) | ||
} | ||
} | ||
|
||
func apply(_ events: [S.Event], then matcher: @escaping Match) -> Match { | ||
return { matcher(events.reduce(into: $0) { $0.apply($1) }) } | ||
} | ||
} | ||
|
||
|