mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
83 lines
3.4 KiB
Swift
83 lines
3.4 KiB
Swift
//
|
|
// FunctionBodyLengthRuleTests.swift
|
|
// SwiftLint
|
|
//
|
|
// Created by Marcelo Fabri on 12/01/16.
|
|
// Copyright © 2016 Realm. All rights reserved.
|
|
//
|
|
|
|
import SwiftLintFramework
|
|
import XCTest
|
|
|
|
private func funcWithBody(body: String) -> String {
|
|
return "func abc() {\nvar x = 0\n\(body)}\n"
|
|
}
|
|
|
|
class FunctionBodyLengthRuleTests: XCTestCase {
|
|
|
|
// protocol XCTestCaseProvider
|
|
lazy var allTests: [(String, () throws -> Void)] = [
|
|
("testFunctionBodyLengths", self.testFunctionBodyLengths),
|
|
("testFunctionBodyLengthsWithComments", self.testFunctionBodyLengthsWithComments),
|
|
("testFunctionBodyLengthsWithMultilineComments",
|
|
self.testFunctionBodyLengthsWithMultilineComments),
|
|
]
|
|
|
|
func testFunctionBodyLengths() {
|
|
let longFunctionBody = funcWithBody(
|
|
Repeat(count: 39, repeatedValue: "x = 0\n").joinWithSeparator("")
|
|
)
|
|
XCTAssertEqual(violations(longFunctionBody), [])
|
|
|
|
let longerFunctionBody = funcWithBody(
|
|
Repeat(count: 40, repeatedValue: "x = 0\n").joinWithSeparator("")
|
|
)
|
|
XCTAssertEqual(violations(longerFunctionBody), [StyleViolation(
|
|
ruleDescription: FunctionBodyLengthRule.description,
|
|
location: Location(file: nil, line: 1, character: 1),
|
|
reason: "Function body should span 40 lines or less excluding comments and " +
|
|
"whitespace: currently spans 41 lines")])
|
|
|
|
let longerFunctionBodyWithEmptyLines = funcWithBody(
|
|
Repeat(count: 100, repeatedValue: "\n").joinWithSeparator("")
|
|
)
|
|
XCTAssertEqual(violations(longerFunctionBodyWithEmptyLines), [])
|
|
}
|
|
|
|
func testFunctionBodyLengthsWithComments() {
|
|
let longFunctionBodyWithComments = funcWithBody(
|
|
Repeat(count: 39, repeatedValue: "x = 0\n").joinWithSeparator("") +
|
|
"// comment only line should be ignored.\n"
|
|
)
|
|
XCTAssertEqual(violations(longFunctionBodyWithComments), [])
|
|
|
|
let longerFunctionBodyWithComments = funcWithBody(
|
|
Repeat(count: 40, repeatedValue: "x = 0\n").joinWithSeparator("") +
|
|
"// comment only line should be ignored.\n"
|
|
)
|
|
XCTAssertEqual(violations(longerFunctionBodyWithComments), [StyleViolation(
|
|
ruleDescription: FunctionBodyLengthRule.description,
|
|
location: Location(file: nil, line: 1, character: 1),
|
|
reason: "Function body should span 40 lines or less excluding comments and " +
|
|
"whitespace: currently spans 41 lines")])
|
|
}
|
|
|
|
func testFunctionBodyLengthsWithMultilineComments() {
|
|
let longFunctionBodyWithMultilineComments = funcWithBody(
|
|
Repeat(count: 39, repeatedValue: "x = 0\n").joinWithSeparator("") +
|
|
"/* multi line comment only line should be ignored.\n*/\n"
|
|
)
|
|
XCTAssertEqual(violations(longFunctionBodyWithMultilineComments), [])
|
|
|
|
let longerFunctionBodyWithMultilineComments = funcWithBody(
|
|
Repeat(count: 40, repeatedValue: "x = 0\n").joinWithSeparator("") +
|
|
"/* multi line comment only line should be ignored.\n*/\n"
|
|
)
|
|
XCTAssertEqual(violations(longerFunctionBodyWithMultilineComments), [StyleViolation(
|
|
ruleDescription: FunctionBodyLengthRule.description,
|
|
location: Location(file: nil, line: 1, character: 1),
|
|
reason: "Function body should span 40 lines or less excluding comments and " +
|
|
"whitespace: currently spans 41 lines")])
|
|
}
|
|
}
|