Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #24 from SwiftDocOrg/fail-on-unexpected-error
Browse files Browse the repository at this point in the history
Fail on unexpected error
  • Loading branch information
mattt authored May 4, 2020
2 parents 18e0005 + ca75220 commit 9972610
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
48 changes: 31 additions & 17 deletions Sources/DocTest/Statement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,42 @@ public class Statement {
}

public func tests(with result: Result<String, REPL.Error>) -> [Test] {
let metadata: [String: Any] = [
var metadata: [String: Any] = [
"file": self.sourceLocation.file as Any?,
"line": self.sourceLocation.line as Any?,
"column": self.sourceLocation.column as Any?
].compactMapValues { $0 }

return expectations.map { expectation in
switch expectation {
case .value(let expected):
switch result {
case .failure(let error):
metadata["actual"] = error.description

if expectations.isEmpty {
return [test { .failure("- `\(self.code)` produced an error", directive: nil, metadata: metadata) }]
} else {
return expectations.map { expectation in
switch expectation {
case .value(let expected):
metadata["expected"] = expected

return test {
.failure("- `\(self.code)` produced an error", directive: nil, metadata: metadata)
}
case .error:
return test {
.success("- `\(self.code)` produced an error, as expected", directive: nil, metadata: metadata)
}
}
}
}
case .success(let actual):
metadata["actual"] = actual

return expectations.map { expectation in
switch expectation {
case .value(let expected):
metadata["expected"] = expected

if case .success(let actual) = result {
if actual == expected {
return test {
.success("- `\(self.code)` produces `\(actual)`", directive: nil, metadata: metadata)
Expand All @@ -36,18 +61,7 @@ public class Statement {
.failure("- `\(self.code)` produces `\(actual)`, expected `\(expected)`", directive: nil, metadata: metadata)
}
}

} else {
return test {
.failure("- `\(self.code)` did not produce `\(expected)`", directive: nil, metadata: metadata)
}
}
case .error:
if case .failure = result {
return test {
.success("- `\(self.code)` produced an error, as expected", directive: nil, metadata: metadata)
}
} else {
case .error:
return test {
.failure("- `\(self.code)` didn't produce an error, which was unexpected", directive: nil, metadata: metadata)
}
Expand Down
7 changes: 6 additions & 1 deletion Tests/DocTestTests/DocTestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ final class DocTestTests: XCTestCase {
1 + 1 // => Int = 2
1 + 1 // => String = "wat"
1 / 0 // !! Error
invalid
1 + 1 // => Int = 2
"""#

let expectation = XCTestExpectation()
Expand All @@ -17,10 +19,13 @@ final class DocTestTests: XCTestCase {
case .failure(let error):
XCTFail("\(error)")
case .success(let report):
XCTAssertEqual(report.results.count, 3)
XCTAssertEqual(report.results.count, 5)
XCTAssertTrue(try! report.results[0].get().ok) // 1 + 1 => 2
XCTAssertFalse(try! report.results[1].get().ok) // 1 + 1 => "wat"
XCTAssertTrue(try! report.results[2].get().ok) // 1 / 0 !! Error
XCTAssertFalse(try! report.results[3].get().ok) // invalid
XCTAssertTrue(try! report.results[4].get().ok) // 1 + 1 => 2

expectation.fulfill()
}
}
Expand Down

0 comments on commit 9972610

Please sign in to comment.