mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
* Add Example wrapper in order to display test failures inline when running in Xcode. * Stop using Swift 5.1-only features so we can compile on Xcode 10.2. * Wrap strings in Example. * Add Changelog entry. * Wrap all examples in Example struct. * Better and more complete capturing of line numbers. * Fix broken test. * Better test traceability. * Address or disable linting warnings. * Add documentation comments. * Disable linter for a few cases. * Limit mutability and add copy-and-mutate utility functions. * Limit scope of mutability.
61 lines
2.4 KiB
Swift
61 lines
2.4 KiB
Swift
import SwiftLintFramework
|
|
import XCTest
|
|
|
|
class CyclomaticComplexityRuleTests: XCTestCase {
|
|
private lazy var complexSwitchExample: Example = {
|
|
var example = "func switcheroo() {\n"
|
|
example += " switch foo {\n"
|
|
for index in (0...30) {
|
|
example += " case \(index): print(\"\(index)\")\n"
|
|
}
|
|
example += " }\n"
|
|
example += "}\n"
|
|
return Example(example)
|
|
}()
|
|
|
|
private lazy var complexIfExample: Example = {
|
|
let nest = 22
|
|
var example = "func nestThoseIfs() {\n"
|
|
for index in (0...nest) {
|
|
let indent = String(repeating: " ", count: index + 1)
|
|
example += indent + "if false != true {\n"
|
|
example += indent + " print \"\\(i)\"\n"
|
|
}
|
|
|
|
for index in (0...nest).reversed() {
|
|
let indent = String(repeating: " ", count: index + 1)
|
|
example += indent + "}\n"
|
|
}
|
|
example += "}\n"
|
|
return Example(example)
|
|
}()
|
|
|
|
func testCyclomaticComplexity() {
|
|
verifyRule(CyclomaticComplexityRule.description, commentDoesntViolate: true, stringDoesntViolate: true)
|
|
}
|
|
|
|
func testIgnoresCaseStatementsConfigurationEnabled() {
|
|
let baseDescription = CyclomaticComplexityRule.description
|
|
let triggeringExamples = [complexIfExample]
|
|
let nonTriggeringExamples = baseDescription.nonTriggeringExamples + [complexSwitchExample]
|
|
|
|
let description = baseDescription.with(nonTriggeringExamples: nonTriggeringExamples)
|
|
.with(triggeringExamples: triggeringExamples)
|
|
|
|
verifyRule(description, ruleConfiguration: ["ignores_case_statements": true],
|
|
commentDoesntViolate: true, stringDoesntViolate: true)
|
|
}
|
|
|
|
func testIgnoresCaseStatementsConfigurationDisabled() {
|
|
let baseDescription = CyclomaticComplexityRule.description
|
|
let triggeringExamples = baseDescription.triggeringExamples + [complexSwitchExample]
|
|
let nonTriggeringExamples = baseDescription.nonTriggeringExamples
|
|
|
|
let description = baseDescription.with(nonTriggeringExamples: nonTriggeringExamples)
|
|
.with(triggeringExamples: triggeringExamples)
|
|
|
|
verifyRule(description, ruleConfiguration: ["ignores_case_statements": false],
|
|
commentDoesntViolate: true, stringDoesntViolate: true)
|
|
}
|
|
}
|