forked from Quick/Quick
-
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.
Merge pull request Quick#701 from mosamer/master
Introduce `Behavior<Context>`
- Loading branch information
Showing
8 changed files
with
199 additions
and
2 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
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,17 @@ | ||
/** | ||
A `Behavior` encapsulates a set of examples that can be re-used in several locations using the `itBehavesLike` function with a context instance of the generic type. | ||
*/ | ||
|
||
open class Behavior<Context> { | ||
|
||
open static var name: String { return String(describing: self) } | ||
/** | ||
override this method in your behavior to define a set of reusable examples. | ||
|
||
This behaves just like an example group defines using `describe` or `context`--it may contain any number of `beforeEach` | ||
and `afterEach` closures, as well as any number of examples (defined using `it`). | ||
|
||
- parameter aContext: A closure that, when evaluated, returns a `Context` instance that provide the information on the subject. | ||
*/ | ||
open class func spec(_ aContext: @escaping () -> Context) {} | ||
} |
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
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
20 changes: 20 additions & 0 deletions
20
Tests/QuickTests/QuickTests/Fixtures/FunctionalTests_BehaviorTests_Behaviors.swift
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,20 @@ | ||
import Foundation | ||
import Quick | ||
import Nimble | ||
|
||
class FunctionalTests_BehaviorTests_Behavior: Behavior<String> { | ||
override static func spec(_ aContext: @escaping () -> String) { | ||
it("passed the correct parameters via the context") { | ||
let callsite = aContext() | ||
expect(callsite).to(equal("BehaviorSpec")) | ||
} | ||
} | ||
} | ||
|
||
class FunctionalTests_BehaviorTests_Behavior2: Behavior<Void> { | ||
override static func spec(_ aContext: @escaping () -> Void) { | ||
it("passes once") { expect(true).to(beTruthy()) } | ||
it("passes twice") { expect(true).to(beTruthy()) } | ||
it("passes three times") { expect(true).to(beTruthy()) } | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
Tests/QuickTests/QuickTests/FunctionalTests/BehaviorTests.swift
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,59 @@ | ||
import Foundation | ||
import Quick | ||
import Nimble | ||
import XCTest | ||
|
||
class FunctionalTests_BehaviorTests_Spec: QuickSpec { | ||
override func spec() { | ||
itBehavesLike(FunctionalTests_BehaviorTests_Behavior2.self) {_ in ()} | ||
} | ||
} | ||
|
||
class FunctionalTests_BehaviorTests_ContextSpec: QuickSpec { | ||
override func spec() { | ||
itBehavesLike(FunctionalTests_BehaviorTests_Behavior.self) { | ||
"BehaviorSpec" | ||
} | ||
} | ||
} | ||
|
||
#if _runtime(_ObjC) && !SWIFT_PACKAGE | ||
class FunctionalTests_BehaviorTests_ErrorSpec: QuickSpec { | ||
override func spec() { | ||
describe("error handling when misusing ordering") { | ||
it("should throw an exception when including itBehavesLike in it block") { | ||
expect { | ||
itBehavesLike(FunctionalTests_BehaviorTests_Behavior2.self) { () } | ||
} | ||
.to(raiseException {(exception: NSException) in | ||
expect(exception.name).to(equal(NSExceptionName.internalInconsistencyException)) | ||
expect(exception.reason).to(equal("'itBehavesLike' cannot be used inside 'it', 'itBehavesLike' may only be used inside 'context' or 'describe'. ")) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
#endif | ||
|
||
final class BehaviorTests: XCTestCase, XCTestCaseProvider { | ||
|
||
static var allTests: [(String, (BehaviorTests) -> () throws -> Void)] { | ||
return [ | ||
("testBehaviorPassContextToExamples", | ||
testBehaviorPassContextToExamples), | ||
("testBehaviorExecutesThreeExamples", | ||
testBehaviorExecutesThreeExamples) | ||
] | ||
} | ||
|
||
func testBehaviorExecutesThreeExamples() { | ||
let result = qck_runSpec(FunctionalTests_BehaviorTests_Spec.self) | ||
XCTAssert(result!.hasSucceeded) | ||
XCTAssertEqual(result!.executionCount, 3 as UInt) | ||
} | ||
|
||
func testBehaviorPassContextToExamples() { | ||
let result = qck_runSpec(FunctionalTests_BehaviorTests_ContextSpec.self) | ||
XCTAssert(result!.hasSucceeded) | ||
} | ||
} |
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