Files
SwiftLint/Tests/SwiftLintFrameworkTests/CyclomaticComplexityRuleTests.swift
T
JP Simard b83e0991b9 Remove all file headers
The MIT license doesn't require that all files be prepended with this
licensing or copyright information. Realm confirmed that they're ok with this
change. This will enable some companies to contribute to SwiftLint and the
date & authorship information will remain accessible via git source control.
2018-05-04 13:42:02 -07:00

63 lines
2.4 KiB
Swift

import Foundation
import SwiftLintFramework
import XCTest
class CyclomaticComplexityRuleTests: XCTestCase {
private lazy var complexSwitchExample: String = {
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
}()
private lazy var complexIfExample: String = {
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
}()
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)
}
}