Files
SwiftLint/Tests/SwiftLintFrameworkTests/ObjectLiteralRuleTests.swift
T
JP Simard b83e0991b9 Remove all file headers
The MIT license doesn't require that all files be prepended with this
licensing or copyright information. Realm confirmed that they're ok with this
change. This will enable some companies to contribute to SwiftLint and the
date & authorship information will remain accessible via git source control.
2018-05-04 13:42:02 -07:00

67 lines
3.0 KiB
Swift

import SwiftLintFramework
import XCTest
class ObjectLiteralRuleTests: XCTestCase {
// MARK: - Instance Properties
private let imageLiteralTriggeringExamples = ["", ".init"].flatMap { (method: String) -> [String] in
["UI", "NS"].flatMap { (prefix: String) -> [String] in
[
"let image = ↓\(prefix)Image\(method)(named: \"foo\")"
]
}
}
private let colorLiteralTriggeringExamples = ["", ".init"].flatMap { (method: String) -> [String] in
["UI", "NS"].flatMap { (prefix: String) -> [String] in
[
"let color = ↓\(prefix)Color\(method)(red: 0.3, green: 0.3, blue: 0.3, alpha: 1)",
"let color = ↓\(prefix)Color\(method)(red: 100 / 255.0, green: 50 / 255.0, blue: 0, alpha: 1)",
"let color = ↓\(prefix)Color\(method)(white: 0.5, alpha: 1)"
]
}
}
private var allTriggeringExamples: [String] {
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.map { example in
example.replacingOccurrences(of: "↓", with: "")
}
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.map { example in
example.replacingOccurrences(of: "↓", with: "")
}
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])
}
}