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

Commit

Permalink
Fail on error when a statement has no expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
mattt committed May 4, 2020
1 parent f72a1a7 commit ca75220
Showing 1 changed file with 31 additions and 17 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

0 comments on commit ca75220

Please sign in to comment.