Skip to content

Commit

Permalink
Re-enable uncommented test code for Checkers
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Sep 5, 2021
1 parent 4199b7a commit 5bd9ac7
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 407 deletions.
6 changes: 6 additions & 0 deletions Sources/TestSupport/TestLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Core

public final class TestLogger: Loggable {
public var loggedMessages: [String]
public var exitStatusCode: Int32?

public init() {
loggedMessages = []
Expand All @@ -20,4 +21,9 @@ public final class TestLogger: Loggable {
)
}
}

public func exit(fail: Bool) -> Never {
exitStatusCode = fail ? EXIT_FAILURE : EXIT_SUCCESS
fatalError()
}
}
311 changes: 151 additions & 160 deletions Tests/CheckersTests/FileContentsCheckerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,164 +72,155 @@ final class FileContentsCheckerTests: XCTestCase {
}
}

// func testSkipHere() {
// let temporaryFiles: [TemporaryFile] = [
// (subpath: "Sources/Hello.swift", contents: "// AnyLint.skipHere: OtherRule, Whitespacing\n\n\nlet x=5\nvar y=10"),
// (subpath: "Sources/World.swift", contents: "\n\n// AnyLint.skipHere: OtherRule, Whitespacing\nlet x=5\nvar y=10"),
// (
// subpath: "Sources/Foo.swift", contents: "\n\n\nlet x=5\nvar y=10 // AnyLint.skipHere: OtherRule, Whitespacing\n"
// ),
// (subpath: "Sources/Bar.swift", contents: "\n\n\nlet x=5\nvar y=10\n// AnyLint.skipHere: OtherRule, Whitespacing"),
// ]
//
// withTemporaryFiles(temporaryFiles) { filePathsToCheck in
// let checkInfo = CheckInfo(
// id: "Whitespacing",
// hint: "Always add a single whitespace around '='.",
// severity: .warning
// )
// let violations = try FileContentsChecker(
// checkInfo: checkInfo,
// regex: #"(let|var) \w+=\w+"#,
// filePathsToCheck: filePathsToCheck,
// autoCorrectReplacement: nil,
// repeatIfAutoCorrected: false
// )
// .performCheck()
//
// XCTAssertEqual(violations.count, 6)
//
// XCTAssertEqual(violations[0].checkInfo, checkInfo)
// XCTAssertEqual(violations[0].filePath, "\(tempDir)/Sources/Hello.swift")
// XCTAssertEqual(violations[0].locationInfo!.line, 4)
// XCTAssertEqual(violations[0].locationInfo!.charInLine, 1)
//
// XCTAssertEqual(violations[1].checkInfo, checkInfo)
// XCTAssertEqual(violations[1].filePath, "\(tempDir)/Sources/Hello.swift")
// XCTAssertEqual(violations[1].locationInfo!.line, 5)
// XCTAssertEqual(violations[1].locationInfo!.charInLine, 1)
//
// XCTAssertEqual(violations[2].checkInfo, checkInfo)
// XCTAssertEqual(violations[2].filePath, "\(tempDir)/Sources/World.swift")
// XCTAssertEqual(violations[2].locationInfo!.line, 5)
// XCTAssertEqual(violations[2].locationInfo!.charInLine, 1)
//
// XCTAssertEqual(violations[3].checkInfo, checkInfo)
// XCTAssertEqual(violations[3].filePath, "\(tempDir)/Sources/Foo.swift")
// XCTAssertEqual(violations[3].locationInfo!.line, 4)
// XCTAssertEqual(violations[3].locationInfo!.charInLine, 1)
//
// XCTAssertEqual(violations[4].checkInfo, checkInfo)
// XCTAssertEqual(violations[4].filePath, "\(tempDir)/Sources/Bar.swift")
// XCTAssertEqual(violations[4].locationInfo!.line, 4)
// XCTAssertEqual(violations[4].locationInfo!.charInLine, 1)
//
// XCTAssertEqual(violations[5].checkInfo, checkInfo)
// XCTAssertEqual(violations[5].filePath, "\(tempDir)/Sources/Bar.swift")
// XCTAssertEqual(violations[5].locationInfo!.line, 5)
// XCTAssertEqual(violations[5].locationInfo!.charInLine, 1)
// }
// }
//
// func testSkipIfEqualsToAutocorrectReplacement() {
// let temporaryFiles: [TemporaryFile] = [
// (subpath: "Sources/Hello.swift", contents: "let x = 5\nvar y = 10"),
// (subpath: "Sources/World.swift", contents: "let x =5\nvar y= 10"),
// ]
//
// withTemporaryFiles(temporaryFiles) { filePathsToCheck in
// let checkInfo = CheckInfo(
// id: "Whitespacing",
// hint: "Always add a single whitespace around '='.",
// severity: .warning
// )
// let violations = try FileContentsChecker(
// checkInfo: checkInfo,
// regex: #"(let|var) (\w+)\s*=\s*(\w+)"#,
// filePathsToCheck: filePathsToCheck,
// autoCorrectReplacement: "$1 $2 = $3",
// repeatIfAutoCorrected: false
// )
// .performCheck()
//
// XCTAssertEqual(violations.count, 2)
//
// XCTAssertEqual(violations[0].checkInfo, checkInfo)
// XCTAssertEqual(violations[0].filePath, "\(tempDir)/Sources/World.swift")
// XCTAssertEqual(violations[0].locationInfo!.line, 1)
// XCTAssertEqual(violations[0].locationInfo!.charInLine, 1)
//
// XCTAssertEqual(violations[1].checkInfo, checkInfo)
// XCTAssertEqual(violations[1].filePath, "\(tempDir)/Sources/World.swift")
// XCTAssertEqual(violations[1].locationInfo!.line, 2)
// XCTAssertEqual(violations[1].locationInfo!.charInLine, 1)
// }
// }
//
// func testRepeatIfAutoCorrected() {
// let temporaryFiles: [TemporaryFile] = [
// (subpath: "Sources/Hello.swift", contents: "let x = 500\nvar y = 10000"),
// (subpath: "Sources/World.swift", contents: "let x = 50000000\nvar y = 100000000000000"),
// ]
//
// withTemporaryFiles(temporaryFiles) { filePathsToCheck in
// let checkInfo = CheckInfo(
// id: "LongNumbers",
// hint: "Format long numbers with `_` after each triple of digits from the right.",
// severity: .warning
// )
// let violations = try FileContentsChecker(
// checkInfo: checkInfo,
// regex: #"(?<!\d)(\d+)(\d{3})(?!\d)"#,
// filePathsToCheck: filePathsToCheck,
// autoCorrectReplacement: "$1_$2",
// repeatIfAutoCorrected: true
// )
// .performCheck()
//
// XCTAssertEqual(violations.count, 7)
//
// XCTAssertEqual(violations[0].checkInfo, checkInfo)
// XCTAssertEqual(violations[0].filePath, "\(tempDir)/Sources/Hello.swift")
// XCTAssertEqual(violations[0].locationInfo!.line, 2)
// XCTAssertEqual(violations[0].locationInfo!.charInLine, 9)
// XCTAssertEqual(violations[0].appliedAutoCorrection!.after, "10_000")
//
// XCTAssertEqual(violations[1].checkInfo, checkInfo)
// XCTAssertEqual(violations[1].filePath, "\(tempDir)/Sources/World.swift")
// XCTAssertEqual(violations[1].locationInfo!.line, 1)
// XCTAssertEqual(violations[1].locationInfo!.charInLine, 9)
// XCTAssertEqual(violations[1].appliedAutoCorrection!.after, "50000_000")
//
// XCTAssertEqual(violations[2].checkInfo, checkInfo)
// XCTAssertEqual(violations[2].filePath, "\(tempDir)/Sources/World.swift")
// XCTAssertEqual(violations[2].locationInfo!.line, 2)
// XCTAssertEqual(violations[2].locationInfo!.charInLine, 9)
// XCTAssertEqual(violations[2].appliedAutoCorrection!.after, "100000000000_000")
//
// XCTAssertEqual(violations[3].checkInfo, checkInfo)
// XCTAssertEqual(violations[3].filePath, "\(tempDir)/Sources/World.swift")
// XCTAssertEqual(violations[3].locationInfo!.line, 1)
// XCTAssertEqual(violations[3].locationInfo!.charInLine, 9)
// XCTAssertEqual(violations[3].appliedAutoCorrection!.after, "50_000")
//
// XCTAssertEqual(violations[4].checkInfo, checkInfo)
// XCTAssertEqual(violations[4].filePath, "\(tempDir)/Sources/World.swift")
// XCTAssertEqual(violations[4].locationInfo!.line, 2)
// XCTAssertEqual(violations[4].locationInfo!.charInLine, 9)
// XCTAssertEqual(violations[4].appliedAutoCorrection!.after, "100000000_000")
//
// XCTAssertEqual(violations[5].checkInfo, checkInfo)
// XCTAssertEqual(violations[5].filePath, "\(tempDir)/Sources/World.swift")
// XCTAssertEqual(violations[5].locationInfo!.line, 2)
// XCTAssertEqual(violations[5].locationInfo!.charInLine, 9)
// XCTAssertEqual(violations[5].appliedAutoCorrection!.after, "100000_000")
//
// XCTAssertEqual(violations[6].checkInfo, checkInfo)
// XCTAssertEqual(violations[6].filePath, "\(tempDir)/Sources/World.swift")
// XCTAssertEqual(violations[6].locationInfo!.line, 2)
// XCTAssertEqual(violations[6].locationInfo!.charInLine, 9)
// XCTAssertEqual(violations[6].appliedAutoCorrection!.after, "100_000")
// }
// }
func testSkipHere() {
let temporaryFiles: [TemporaryFile] = [
(subpath: "Sources/Hello.swift", contents: "// AnyLint.skipHere: OtherRule, Whitespacing\n\n\nlet x=5\nvar y=10"),
(subpath: "Sources/World.swift", contents: "\n\n// AnyLint.skipHere: OtherRule, Whitespacing\nlet x=5\nvar y=10"),
(
subpath: "Sources/Foo.swift", contents: "\n\n\nlet x=5\nvar y=10 // AnyLint.skipHere: OtherRule, Whitespacing\n"
),
(subpath: "Sources/Bar.swift", contents: "\n\n\nlet x=5\nvar y=10\n// AnyLint.skipHere: OtherRule, Whitespacing"),
]

withTemporaryFiles(temporaryFiles) { filePathsToCheck in
let violations = try FileContentsChecker(
id: "Whitespacing",
hint: "Always add a single whitespace around '='.",
severity: .warning,
regex: Regex(#"(let|var) \w+=\w+"#),
filePathsToCheck: filePathsToCheck,
autoCorrectReplacement: nil,
repeatIfAutoCorrected: false
)
.performCheck()

XCTAssertEqual(violations.count, 6)

XCTAssertEqual(violations[0].matchedString, "let x=5")
XCTAssertEqual(violations[0].fileLocation?.filePath, "\(tempDir)/Sources/Hello.swift")
XCTAssertEqual(violations[0].fileLocation?.row, 4)
XCTAssertEqual(violations[0].fileLocation?.column, 1)

XCTAssertEqual(violations[1].matchedString, "var y=10")
XCTAssertEqual(violations[1].fileLocation?.filePath, "\(tempDir)/Sources/Hello.swift")
XCTAssertEqual(violations[1].fileLocation?.row, 5)
XCTAssertEqual(violations[1].fileLocation?.column, 1)

XCTAssertEqual(violations[2].matchedString, "var y=10")
XCTAssertEqual(violations[2].fileLocation?.filePath, "\(tempDir)/Sources/World.swift")
XCTAssertEqual(violations[2].fileLocation?.row, 5)
XCTAssertEqual(violations[2].fileLocation?.column, 1)

XCTAssertEqual(violations[3].matchedString, "let x=5")
XCTAssertEqual(violations[3].fileLocation?.filePath, "\(tempDir)/Sources/Foo.swift")
XCTAssertEqual(violations[3].fileLocation?.row, 4)
XCTAssertEqual(violations[3].fileLocation?.column, 1)

XCTAssertEqual(violations[4].matchedString, "let x=5")
XCTAssertEqual(violations[4].fileLocation?.filePath, "\(tempDir)/Sources/Bar.swift")
XCTAssertEqual(violations[4].fileLocation?.row, 4)
XCTAssertEqual(violations[4].fileLocation?.column, 1)

XCTAssertEqual(violations[5].matchedString, "var y=10")
XCTAssertEqual(violations[5].fileLocation?.filePath, "\(tempDir)/Sources/Bar.swift")
XCTAssertEqual(violations[5].fileLocation?.row, 5)
XCTAssertEqual(violations[5].fileLocation?.column, 1)
}
}

func testSkipIfEqualsToAutocorrectReplacement() {
let temporaryFiles: [TemporaryFile] = [
(subpath: "Sources/Hello.swift", contents: "let x = 5\nvar y = 10"),
(subpath: "Sources/World.swift", contents: "let x =5\nvar y= 10"),
]

withTemporaryFiles(temporaryFiles) { filePathsToCheck in
let violations = try FileContentsChecker(
id: "Whitespacing",
hint: "Always add a single whitespace around '='.",
severity: .warning,
regex: Regex(#"(let|var) (\w+)\s*=\s*(\w+)"#),
filePathsToCheck: filePathsToCheck,
autoCorrectReplacement: "$1 $2 = $3",
repeatIfAutoCorrected: false
)
.performCheck()

XCTAssertEqual(violations.count, 2)

XCTAssertEqual(violations[0].matchedString, "let x =5")
XCTAssertEqual(violations[0].fileLocation?.filePath, "\(tempDir)/Sources/World.swift")
XCTAssertEqual(violations[0].fileLocation?.row, 1)
XCTAssertEqual(violations[0].fileLocation?.column, 1)

XCTAssertEqual(violations[1].matchedString, "var y= 10")
XCTAssertEqual(violations[1].fileLocation?.filePath, "\(tempDir)/Sources/World.swift")
XCTAssertEqual(violations[1].fileLocation?.row, 2)
XCTAssertEqual(violations[1].fileLocation?.column, 1)
}
}

func testRepeatIfAutoCorrected() {
let temporaryFiles: [TemporaryFile] = [
(subpath: "Sources/Hello.swift", contents: "let x = 500\nvar y = 10000"),
(subpath: "Sources/World.swift", contents: "let x = 50000000\nvar y = 100000000000000"),
]

withTemporaryFiles(temporaryFiles) { filePathsToCheck in
let violations = try FileContentsChecker(
id: "LongNumbers",
hint: "Format long numbers with `_` after each triple of digits from the right.",
severity: .warning,
regex: Regex(#"(?<!\d)(\d+)(\d{3})(?!\d)"#),
filePathsToCheck: filePathsToCheck,
autoCorrectReplacement: "$1_$2",
repeatIfAutoCorrected: true
)
.performCheck()

XCTAssertEqual(violations.count, 7)

XCTAssertEqual(violations[0].matchedString, "10000")
XCTAssertEqual(violations[0].fileLocation?.filePath, "\(tempDir)/Sources/Hello.swift")
XCTAssertEqual(violations[0].fileLocation?.row, 2)
XCTAssertEqual(violations[0].fileLocation?.column, 9)
XCTAssertEqual(violations[0].appliedAutoCorrection!.after, "10_000")

XCTAssertEqual(violations[1].matchedString, "50000000")
XCTAssertEqual(violations[1].fileLocation?.filePath, "\(tempDir)/Sources/World.swift")
XCTAssertEqual(violations[1].fileLocation?.row, 1)
XCTAssertEqual(violations[1].fileLocation?.column, 9)
XCTAssertEqual(violations[1].appliedAutoCorrection!.after, "50000_000")

XCTAssertEqual(violations[2].matchedString, "100000000000000")
XCTAssertEqual(violations[2].fileLocation?.filePath, "\(tempDir)/Sources/World.swift")
XCTAssertEqual(violations[2].fileLocation?.row, 2)
XCTAssertEqual(violations[2].fileLocation?.column, 9)
XCTAssertEqual(violations[2].appliedAutoCorrection!.after, "100000000000_000")

XCTAssertEqual(violations[3].matchedString, "50000")
XCTAssertEqual(violations[3].fileLocation?.filePath, "\(tempDir)/Sources/World.swift")
XCTAssertEqual(violations[3].fileLocation?.row, 1)
XCTAssertEqual(violations[3].fileLocation?.column, 9)
XCTAssertEqual(violations[3].appliedAutoCorrection!.after, "50_000")

XCTAssertEqual(violations[4].matchedString, "100000000000")
XCTAssertEqual(violations[4].fileLocation?.filePath, "\(tempDir)/Sources/World.swift")
XCTAssertEqual(violations[4].fileLocation?.row, 2)
XCTAssertEqual(violations[4].fileLocation?.column, 9)
XCTAssertEqual(violations[4].appliedAutoCorrection!.after, "100000000_000")

XCTAssertEqual(violations[5].matchedString, "100000000")
XCTAssertEqual(violations[5].fileLocation?.filePath, "\(tempDir)/Sources/World.swift")
XCTAssertEqual(violations[5].fileLocation?.row, 2)
XCTAssertEqual(violations[5].fileLocation?.column, 9)
XCTAssertEqual(violations[5].appliedAutoCorrection!.after, "100000_000")

XCTAssertEqual(violations[6].matchedString, "100000")
XCTAssertEqual(violations[6].fileLocation?.filePath, "\(tempDir)/Sources/World.swift")
XCTAssertEqual(violations[6].fileLocation?.row, 2)
XCTAssertEqual(violations[6].fileLocation?.column, 9)
XCTAssertEqual(violations[6].appliedAutoCorrection!.after, "100_000")
}
}
}
Loading

0 comments on commit 5bd9ac7

Please sign in to comment.