Files
SwiftLint/Tests/SwiftLintFrameworkTests/ExampleTests.swift
T
JP Simard d10ccacb45 Add unused_import config options to require imports for each module used (#3123)
For example, if `CGFloat` is used in a file where only `UIKit` is imported but not `CoreGraphics`, this will be a violation even if the file previously compiled.

This is because Swift allows referencing some declarations that are only transitively imported.

Enabling the `require_explicit_imports` configuration option will require that the module of every declaration referenced in a source file be explicitly imported.

This will add significant noise to the imports list, but has a few advantages:

1. It will be easier to understand all the dependencies explicitly referenced in a source file.
2. Correcting the `unused_import` rule will no longer introduce compilation errors in files that compiled prior to the correction.

If missing imports are added to a file when correcting it, the `sorted_imports` rule will be automatically run on that file.

If you with to allow some imports to be implicitly importable transitively, you may specify the `allowed_transitive_imports` configuration:

```yaml
unused_import:
  require_explicit_imports: true
  allowed_transitive_imports:
    - module: Foundation
      allowed_transitive_imports:
        - CoreFoundation
        - Darwin
        - ObjectiveC
```
2020-02-22 14:39:07 -08:00

62 lines
2.0 KiB
Swift

import SwiftLintFramework
import XCTest
class ExampleTests: XCTestCase {
func testEquatableDoesNotLookAtFile() {
let first = Example("foo", file: "a", line: 1)
let second = Example("foo", file: "b", line: 1)
XCTAssertEqual(first, second)
}
func testEquatableDoesNotLookAtLine() {
let first = Example("foo", file: "a", line: 1)
let second = Example("foo", file: "a", line: 2)
XCTAssertEqual(first, second)
}
func testEquatableLooksAtCode() {
let first = Example("a", file: "a", line: 1)
let second = Example("a", file: "x", line: 2)
let third = Example("c", file: "y", line: 2)
XCTAssertEqual(first, second)
XCTAssertNotEqual(first, third)
}
func testTestMultiByteOffsets() {
XCTAssertTrue(Example("").testMultiByteOffsets)
XCTAssertTrue(Example("", testMultiByteOffsets: true).testMultiByteOffsets)
XCTAssertFalse(Example("", testMultiByteOffsets: false).testMultiByteOffsets)
}
func testTestOnLinux() {
XCTAssertTrue(Example("").testOnLinux)
XCTAssertTrue(Example("", testOnLinux: true).testOnLinux)
XCTAssertFalse(Example("", testOnLinux: false).testOnLinux)
}
func testRemovingViolationMarkers() {
let example = Example("↓T↓E↓S↓T")
XCTAssertEqual(example.removingViolationMarkers(), Example("TEST"))
}
func testComparable() {
XCTAssertLessThan(Example("a"), Example("b"))
}
func testWithCode() {
let original = Example("original code")
XCTAssertNotNil(original.file)
XCTAssertNotNil(original.line)
let new = original.with(code: "new code")
XCTAssertEqual(new.code, "new code")
XCTAssertNotNil(new.file)
XCTAssertNotNil(new.line)
// When modifying the code, it's important that the file and line
// numbers remain intact
XCTAssertEqual(new.file.description, original.file.description)
XCTAssertEqual(new.line, original.line)
}
}