Files
SwiftLint/Tests/SwiftLintFrameworkTests/ObjectLiteralRuleTests.swift
Zev Eisenberg fcf848608e Add Inline test failure messages (#3040)
* Add Example wrapper in order to display test failures inline when running in Xcode.
* Stop using Swift 5.1-only features so we can compile on Xcode 10.2.
* Wrap strings in Example.
* Add Changelog entry.
* Wrap all examples in Example struct.
* Better and more complete capturing of line numbers.
* Fix broken test.
* Better test traceability.
* Address or disable linting warnings.
* Add documentation comments.
* Disable linter for a few cases.
* Limit mutability and add copy-and-mutate utility functions.
* Limit scope of mutability.
2020-02-02 10:35:37 +02:00

63 lines
2.9 KiB
Swift

import SwiftLintFramework
import XCTest
class ObjectLiteralRuleTests: XCTestCase {
// MARK: - Instance Properties
private let imageLiteralTriggeringExamples = ["", ".init"].flatMap { (method: String) -> [Example] in
["UI", "NS"].flatMap { (prefix: String) -> [Example] in
[
Example("let image = ↓\(prefix)Image\(method)(named: \"foo\")")
]
}
}
private let colorLiteralTriggeringExamples = ["", ".init"].flatMap { (method: String) -> [Example] in
["UI", "NS"].flatMap { (prefix: String) -> [Example] in
[
Example("let color = ↓\(prefix)Color\(method)(red: 0.3, green: 0.3, blue: 0.3, alpha: 1)"),
Example("let color = ↓\(prefix)Color\(method)(red: 100 / 255.0, green: 50 / 255.0, blue: 0, alpha: 1)"),
Example("let color = ↓\(prefix)Color\(method)(white: 0.5, alpha: 1)")
]
}
}
private var allTriggeringExamples: [Example] {
return imageLiteralTriggeringExamples + colorLiteralTriggeringExamples
}
// MARK: - Test Methods
func testObjectLiteralWithDefaultConfiguration() {
verifyRule(ObjectLiteralRule.description)
}
func testObjectLiteralWithImageLiteral() {
// Verify ObjectLiteral rule for when image_literal is true.
let baseDescription = ObjectLiteralRule.description
let nonTriggeringColorLiteralExamples = colorLiteralTriggeringExamples.removingViolationMarkers()
let nonTriggeringExamples = baseDescription.nonTriggeringExamples + nonTriggeringColorLiteralExamples
let description = baseDescription.with(nonTriggeringExamples: nonTriggeringExamples)
.with(triggeringExamples: imageLiteralTriggeringExamples)
verifyRule(description, ruleConfiguration: ["image_literal": true, "color_literal": false])
}
func testObjectLiteralWithColorLiteral() {
// Verify ObjectLiteral rule for when color_literal is true.
let baseDescription = ObjectLiteralRule.description
let nonTriggeringImageLiteralExamples = imageLiteralTriggeringExamples.removingViolationMarkers()
let nonTriggeringExamples = baseDescription.nonTriggeringExamples + nonTriggeringImageLiteralExamples
let description = baseDescription.with(nonTriggeringExamples: nonTriggeringExamples)
.with(triggeringExamples: colorLiteralTriggeringExamples)
verifyRule(description, ruleConfiguration: ["image_literal": false, "color_literal": true])
}
func testObjectLiteralWithImageAndColorLiteral() {
// Verify ObjectLiteral rule for when image_literal & color_literal are true.
let description = ObjectLiteralRule.description.with(triggeringExamples: allTriggeringExamples)
verifyRule(description, ruleConfiguration: ["image_literal": true, "color_literal": true])
}
}