diff --git a/Source/SwiftLintFramework/Rules/NestingRule.swift b/Source/SwiftLintFramework/Rules/NestingRule.swift index 5ab3b8e2f..1d09af28d 100644 --- a/Source/SwiftLintFramework/Rules/NestingRule.swift +++ b/Source/SwiftLintFramework/Rules/NestingRule.swift @@ -9,10 +9,12 @@ import SourceKittenFramework import SwiftXPC -struct NestingRule: ASTRule { +public struct NestingRule: ASTRule { + public init() { } + let identifier = "nesting" - func validateFile(file: File) -> [StyleViolation] { + public func validateFile(file: File) -> [StyleViolation] { return self.validateFile(file, dictionary: Structure(file: file).dictionary) } @@ -75,11 +77,20 @@ struct NestingRule: ASTRule { return violations } - let example: RuleExample = RuleExample( + public let example: RuleExample = RuleExample( ruleName: "Nesting Rule", ruleDescription: "Types should be nested at most 1 level deep, and statements should be nested at most 5 levels deep.", - correctExamples: [], - failingExamples: [], - showExamples: false) + correctExamples: ["class", "struct", "enum"].flatMap { kind in + ["\(kind) Class0 { \(kind) Class1 {} }\n", + "func func0() {\nfunc func1() {\nfunc func2() {\nfunc func3() {\nfunc func4() { " + + "func func5() {\n}\n}\n}\n}\n}\n}\n"] + }, + failingExamples: ["class", "struct", "enum"].map { kind in + "\(kind) Class0 { \(kind) Class1 { \(kind) Class2 {} } }\n" + } + [ + "func func0() {\nfunc func1() {\nfunc func2() {\nfunc func3() {\nfunc func4() { " + + "func func5() {\nfunc func6() {\n}\n}\n}\n}\n}\n}\n}\n" + ], + showExamples: true) } diff --git a/Source/SwiftLintFrameworkTests/LinterTests.swift b/Source/SwiftLintFrameworkTests/LinterTests.swift index 27b854185..38cfe54c3 100644 --- a/Source/SwiftLintFrameworkTests/LinterTests.swift +++ b/Source/SwiftLintFrameworkTests/LinterTests.swift @@ -153,28 +153,7 @@ class LinterTests: XCTestCase { } func testNesting() { - for kind in ["class", "struct", "enum"] { - XCTAssertEqual(violations("\(kind) Class0 { \(kind) Class1 {} }\n"), []) - XCTAssertEqual(violations("\(kind) Class0 { \(kind) Class1 { \(kind) Class2 {} } }\n"), - [ - StyleViolation(type: .Nesting, - location: Location(file: nil, line: 1), - reason: "Types should be nested at most 1 level deep") - ]) - } - XCTAssertEqual(violations( - "func func0() {\nfunc func1() {\nfunc func2() {\nfunc func3() {\nfunc func4() { " + - "func func5() {\n}\n}\n}\n}\n}\n}\n" - ), []) - XCTAssertEqual(violations( - "func func0() {\nfunc func1() {\nfunc func2() {\nfunc func3() {\nfunc func4() { " + - "func func5() {\nfunc func6() {\n}\n}\n}\n}\n}\n}\n}\n" - ), - [ - StyleViolation(type: .Nesting, - location: Location(file: nil, line: 6), - reason: "Statements should be nested at most 5 levels deep") - ]) + verifyRule(NestingRule().example, type: StyleViolationType.Nesting, checkCommentsDoesNotViolate: false) } func testControlStatements() {