forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXCTFailMessageRule.swift
66 lines (58 loc) · 1.95 KB
/
XCTFailMessageRule.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import SourceKittenFramework
public struct XCTFailMessageRule: ASTRule, ConfigurationProviderRule, AutomaticTestableRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "xctfail_message",
name: "XCTFail Message",
description: "An XCTFail call should include a description of the assertion.",
kind: .idiomatic,
nonTriggeringExamples: [
Example("""
func testFoo() {
XCTFail("bar")
}
"""),
Example("""
func testFoo() {
XCTFail(bar)
}
""")
],
triggeringExamples: [
Example("""
func testFoo() {
↓XCTFail()
}
"""),
Example("""
func testFoo() {
↓XCTFail("")
}
""")
]
)
public func validate(file: SwiftLintFile,
kind: SwiftExpressionKind,
dictionary: SourceKittenDictionary) -> [StyleViolation] {
guard
kind == .call,
let offset = dictionary.offset,
dictionary.name == "XCTFail",
hasEmptyMessage(dictionary: dictionary, file: file)
else {
return []
}
return [StyleViolation(ruleDescription: Self.description,
severity: configuration.severity,
location: Location(file: file, byteOffset: offset))]
}
private func hasEmptyMessage(dictionary: SourceKittenDictionary, file: SwiftLintFile) -> Bool {
guard let bodyRange = dictionary.bodyByteRange else {
return false
}
guard bodyRange.length > 0 else { return true }
let body = file.stringView.substringWithByteRange(bodyRange)
return body == "\"\""
}
}