-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Report describe/it inside XCTestCases using XCTest (#32)
Closes #2
- Loading branch information
Showing
7 changed files
with
74 additions
and
23 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
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,38 @@ | ||
#if os(macOS) | ||
import XCTest | ||
|
||
|
||
extension XCTestCase { | ||
public func describe(_ name: String, _ closure: (ContextType) -> Void) { | ||
let context = Context(name: name) | ||
closure(context) | ||
context.run(reporter: XcodeReporter(testCase: self)) | ||
} | ||
|
||
public func it(_ name: String, closure: @escaping () throws -> Void) { | ||
let `case` = Case(name: name, closure: closure) | ||
`case`.run(reporter: XcodeReporter(testCase: self)) | ||
} | ||
} | ||
|
||
|
||
class XcodeReporter: ContextReporter { | ||
let testCase: XCTestCase | ||
|
||
init(testCase: XCTestCase) { | ||
self.testCase = testCase | ||
} | ||
|
||
func report(_ name: String, closure: (ContextReporter) -> Void) { | ||
closure(self) | ||
} | ||
|
||
func addSuccess(_ name: String) {} | ||
|
||
func addDisabled(_ name: String) {} | ||
|
||
func addFailure(_ name: String, failure: FailureType) { | ||
testCase.recordFailure(withDescription: "\(name): \(failure.reason)", inFile: failure.file, atLine: failure.line, expected: false) | ||
} | ||
} | ||
#endif |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import Spectre | ||
import SpectreTests | ||
|
||
testExpectation() | ||
testFailure() | ||
describe("Expectation", testExpectation) | ||
describe("Failure", testFailure) |
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 |
---|---|---|
@@ -1,20 +1,19 @@ | ||
import Spectre | ||
|
||
public func testFailure() { | ||
describe("Failure") { | ||
$0.it("throws an error") { | ||
var didFail = false | ||
public let testFailure: ((ContextType) -> Void) = { | ||
$0.it("throws an error") { | ||
|
||
do { | ||
throw failure("it's broken") | ||
} catch { | ||
didFail = true | ||
} | ||
var didFail = false | ||
|
||
if !didFail { | ||
// We cannot trust fail inside fails tests. | ||
fatalError("Test failed") | ||
} | ||
do { | ||
throw failure("it's broken") | ||
} catch { | ||
didFail = true | ||
} | ||
|
||
if !didFail { | ||
// We cannot trust fail inside fails tests. | ||
fatalError("Test failed") | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import XCTest | ||
import Spectre | ||
|
||
class SpectreTests: XCTestCase { | ||
func testSpectre() { | ||
testFailure() | ||
testExpectation() | ||
describe("Failure", testFailure) | ||
describe("Expectation", testExpectation) | ||
} | ||
} |