Skip to content

Commit

Permalink
Address PR feedback on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OneSadCookie committed Jan 28, 2025
1 parent 54bd5bc commit 2200b50
Showing 1 changed file with 41 additions and 42 deletions.
83 changes: 41 additions & 42 deletions Tests/FrameworkTests/EmptyFileTests.swift
Original file line number Diff line number Diff line change
@@ -1,36 +1,63 @@
import SwiftLintFramework
import XCTest

private protocol BooleanConstant {
static var boolValue: Bool { get }
final class EmptyFileTests: SwiftLintTestCase {
var collectedLinter: CollectedLinter! // swiftlint:disable:this implicitly_unwrapped_optional
var ruleStorage: RuleStorage! // swiftlint:disable:this implicitly_unwrapped_optional

override func setUpWithError() throws {
let ruleList = RuleList(rules: RuleMock<DontLintEmptyFiles>.self,
RuleMock<LintEmptyFiles>.self)
let configuration = try Configuration(dict: [:], ruleList: ruleList)
let file = SwiftLintFile(contents: "")
let linter = Linter(file: file, configuration: configuration)
ruleStorage = RuleStorage()
collectedLinter = linter.collect(into: ruleStorage)
}

override func tearDownWithError() throws {
collectedLinter = nil
ruleStorage = nil
}

func testShouldLintEmptyFileRespectedDuringLint() throws {
let styleViolations = collectedLinter.styleViolations(using: ruleStorage)
XCTAssertEqual(styleViolations.count, 1)
XCTAssertEqual(styleViolations.first?.ruleIdentifier, "rule_mock<LintEmptyFiles>")
}

func testShouldLintEmptyFileRespectedDuringCorrect() throws {
let corrections = collectedLinter.correct(using: ruleStorage)
XCTAssertEqual(corrections.count, 1)
XCTAssertEqual(corrections.first?.ruleDescription.identifier, "rule_mock<LintEmptyFiles>")
}
}

private protocol ShouldLintEmptyFilesProtocol {
static var shouldLintEmptyFiles: Bool { get }
}

private struct True: BooleanConstant {
static var boolValue: Bool { true }
private struct LintEmptyFiles: ShouldLintEmptyFilesProtocol {
static var shouldLintEmptyFiles: Bool { true }
}

private struct False: BooleanConstant {
static var boolValue: Bool { false }
private struct DontLintEmptyFiles: ShouldLintEmptyFilesProtocol {
static var shouldLintEmptyFiles: Bool { false }
}

private struct RuleWithLintEmptyFilesMock<ShouldLintEmptyFiles: BooleanConstant>: CorrectableRule {
private struct RuleMock<ShouldLintEmptyFiles: ShouldLintEmptyFilesProtocol>: CorrectableRule {
var configuration = SeverityConfiguration<Self>(.warning)

static var description: RuleDescription {
RuleDescription(identifier: "lint_empty_files_mock<\(ShouldLintEmptyFiles.boolValue)>",
RuleDescription(identifier: "rule_mock<\(ShouldLintEmptyFiles.self)>",
name: "",
description: "",
kind: .style,
deprecatedAliases: ["mock"])
}

init() { /* conformance for test */ }
init(configuration _: Any) throws {
self.init()
}

var shouldLintEmptyFiles: Bool {
ShouldLintEmptyFiles.boolValue
ShouldLintEmptyFiles.shouldLintEmptyFiles
}

func validate(file: SwiftLintFile) -> [StyleViolation] {
Expand All @@ -45,31 +72,3 @@ private struct RuleWithLintEmptyFilesMock<ShouldLintEmptyFiles: BooleanConstant>
]
}
}

final class EmptyFileTests: SwiftLintTestCase {
func testShouldLintEmptyFileRespectedDuringLint() {
let ruleList = RuleList(rules: RuleWithLintEmptyFilesMock<False>.self,
RuleWithLintEmptyFilesMock<True>.self)
let configuration = try! Configuration(dict: [:], ruleList: ruleList) // swiftlint:disable:this force_try
let file = SwiftLintFile(contents: "")
let linter = Linter(file: file, configuration: configuration)
let ruleStorage = RuleStorage()
let collectedLinter = linter.collect(into: ruleStorage)
let styleViolations = collectedLinter.styleViolations(using: ruleStorage)
XCTAssertEqual(styleViolations.count, 1)
XCTAssertEqual(styleViolations.first?.ruleIdentifier, "lint_empty_files_mock<true>")
}

func testShouldLintEmptyFileRespectedDuringCorrect() {
let ruleList = RuleList(rules: RuleWithLintEmptyFilesMock<False>.self,
RuleWithLintEmptyFilesMock<True>.self)
let configuration = try! Configuration(dict: [:], ruleList: ruleList) // swiftlint:disable:this force_try
let file = SwiftLintFile(contents: "")
let linter = Linter(file: file, configuration: configuration)
let ruleStorage = RuleStorage()
let collectedLinter = linter.collect(into: ruleStorage)
let corrections = collectedLinter.correct(using: ruleStorage)
XCTAssertEqual(corrections.count, 1)
XCTAssertEqual(corrections.first?.ruleDescription.identifier, "lint_empty_files_mock<true>")
}
}

0 comments on commit 2200b50

Please sign in to comment.