mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
# Conflicts: # Tests/BuiltInRulesTests/FileHeaderRuleTests.swift # Tests/BuiltInRulesTests/FileNameNoSpaceRuleTests.swift # Tests/BuiltInRulesTests/IndentationWidthRuleTests.swift # Tests/CoreTests/RegexConfigurationTests.swift # Tests/CoreTests/SwiftLintFileTests.swift # Tests/FileSystemAccessTests/BaselineTests.swift # Tests/FileSystemAccessTests/ConfigurationTests+Mock.swift # Tests/FileSystemAccessTests/ConfigurationTests.swift # Tests/FileSystemAccessTests/GlobTests.swift # Tests/FileSystemAccessTests/MultipleConfigurationsTests.swift # Tests/FileSystemAccessTests/ReporterTests.swift # Tests/FileSystemAccessTests/SourceKitCrashTests.swift # Tests/FrameworkTests/CustomRulesTests.swift # Tests/FrameworkTests/LinterCacheTests.swift # Tests/FrameworkTests/RuleConfigurationTests.swift # Tests/FrameworkTests/SwiftVersionTests.swift # Tests/GeneratedTests/GeneratedTests_02.swift # Tests/GeneratedTests/GeneratedTests_03.swift # Tests/GeneratedTests/GeneratedTests_04.swift # Tests/GeneratedTests/GeneratedTests_05.swift # Tests/GeneratedTests/GeneratedTests_06.swift # Tests/GeneratedTests/GeneratedTests_07.swift # Tests/GeneratedTests/GeneratedTests_08.swift # Tests/GeneratedTests/GeneratedTests_09.swift # Tests/GeneratedTests/GeneratedTests_10.swift # Tests/IntegrationTests/ConfigPathResolutionTests.swift # Tests/IntegrationTests/IntegrationTests.swift # Tests/TestHelpers/TestHelpers.swift
81 lines
2.9 KiB
Swift
81 lines
2.9 KiB
Swift
import TestHelpers
|
|
import Testing
|
|
|
|
@testable import SwiftLintBuiltInRules
|
|
|
|
@Suite(.rulesRegistered)
|
|
struct CyclomaticComplexityRuleTests {
|
|
private static let 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 static let complexSwitchInitExample: Example = {
|
|
var example = "init() {\n"
|
|
example += " switch foo {\n"
|
|
for index in (0...30) {
|
|
example += " case \(index): print(\"\(index)\")\n"
|
|
}
|
|
example += " }\n"
|
|
example += "}\n"
|
|
return Example(example)
|
|
}()
|
|
|
|
private static let 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)
|
|
}()
|
|
|
|
@Test
|
|
func cyclomaticComplexity() {
|
|
verifyRule(CyclomaticComplexityRule.description, commentDoesntViolate: true, stringDoesntViolate: true)
|
|
}
|
|
|
|
@Test
|
|
func ignoresCaseStatementsConfigurationEnabled() {
|
|
let baseDescription = CyclomaticComplexityRule.description
|
|
let triggeringExamples = [Self.complexIfExample]
|
|
let nonTriggeringExamples = baseDescription.nonTriggeringExamples + [Self.complexSwitchExample]
|
|
|
|
let description = baseDescription.with(nonTriggeringExamples: nonTriggeringExamples)
|
|
.with(triggeringExamples: triggeringExamples)
|
|
|
|
verifyRule(description, ruleConfiguration: ["ignores_case_statements": true],
|
|
commentDoesntViolate: true, stringDoesntViolate: true)
|
|
}
|
|
|
|
@Test
|
|
func ignoresCaseStatementsConfigurationDisabled() {
|
|
let baseDescription = CyclomaticComplexityRule.description
|
|
let triggeringExamples = baseDescription.triggeringExamples + [
|
|
Self.complexSwitchExample,
|
|
Self.complexSwitchInitExample,
|
|
]
|
|
let nonTriggeringExamples = baseDescription.nonTriggeringExamples
|
|
|
|
let description = baseDescription.with(nonTriggeringExamples: nonTriggeringExamples)
|
|
.with(triggeringExamples: triggeringExamples)
|
|
|
|
verifyRule(description, ruleConfiguration: ["ignores_case_statements": false],
|
|
commentDoesntViolate: true, stringDoesntViolate: true)
|
|
}
|
|
}
|