Skip to content

Commit

Permalink
Produce detailed log of generated violations from nonTriggeringExamples
Browse files Browse the repository at this point in the history
e.g.
> /Users/norio/github/SwiftLint/Tests/SwiftLintFramework/TestHelpers.swift:136: error: -[SwiftLintFrameworkTests.RulesTests testMissingDocs] : failed - nonTriggeringExample violated:
> ```
> /// docs
> public func a() {}
>        ^ warning: Missing Docs Violation: Public declarations should be documented. (missing_docs)
> ```
  • Loading branch information
norio-nomura committed Dec 1, 2016
1 parent fab2ef9 commit 1beed4b
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion Tests/SwiftLintFrameworkTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ private func cleanedContentsAndMarkerOffsets(from contents: String) -> (String,
return (contents as String, markerOffsets.sorted())
}

func render(violations: [StyleViolation], in contents: String) -> String {
var contents = (contents as NSString).lines().map { $0.content }
for violation in violations.sorted(by: { $0.location > $1.location }) {
guard let line = violation.location.line,
let character = violation.location.character else { continue }

let message = String(repeating: " ", count: character - 1) + "^ " + [
"\(violation.severity.rawValue.lowercased()): ",
"\(violation.ruleDescription.name) Violation: ",
violation.reason,
" (\(violation.ruleDescription.identifier))"].joined()
if line >= contents.count {
contents.append(message)
} else {
contents.insert(message, at: line)
}
}
return (["```"] + contents + ["```"]).joined(separator: "\n")
}

extension Configuration {
fileprivate func assertCorrection(_ before: String, expected: String) {
guard let path = NSURL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
Expand Down Expand Up @@ -105,7 +125,12 @@ extension XCTestCase {
let nonTriggers = ruleDescription.nonTriggeringExamples

// Non-triggering examples don't violate
XCTAssertEqual(nonTriggers.flatMap({ violations($0, config: config) }), [])
for nonTrigger in nonTriggers {
let unexpectedViolations = violations(nonTrigger, config: config)
if unexpectedViolations.isEmpty { continue }
let nonTriggerWithViolations = render(violations: unexpectedViolations, in: nonTrigger)
XCTFail("nonTriggeringExample violated: \n\(nonTriggerWithViolations)")
}

var violationsCount = 0
var expectedViolationsCount = 0
Expand Down

0 comments on commit 1beed4b

Please sign in to comment.