Files
SwiftLint/Source/SwiftLintFrameworkTests/FunctionBodyLengthRuleTests.swift
T
JP Simard 04267a499e rewrite FunctionBodyLengthRuleTests to compile 500x faster
before:
  10828.9ms @objc func testFunctionBodyLengthsWithComments()
  10250.3ms @objc func testFunctionBodyLengthsWithMultilineComments()
  1236.7ms  @objc func testFunctionBodyLengths()

after:
  18.5ms @objc func testFunctionBodyLengths()
  16.0ms private func funcWithBody(body: String) -> String
  6.5ms  @objc func testFunctionBodyLengthsWithMultilineComments()
  5.7ms  @objc func testFunctionBodyLengthsWithComments()
2016-01-18 14:15:39 -08:00

74 lines
3.1 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 {
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")])
}
}