|
1 | 1 | import SwiftFormatRules
|
2 | 2 |
|
3 | 3 | final class UseLetInEveryBoundCaseVariableTests: LintOrFormatRuleTestCase {
|
4 |
| - func testInvalidLetBoundCase() { |
| 4 | + override func setUp() { |
| 5 | + super.setUp() |
| 6 | + self.shouldCheckForUnassertedDiagnostics = true |
| 7 | + } |
| 8 | + |
| 9 | + func testSwitchCase() { |
5 | 10 | let input =
|
6 | 11 | """
|
7 | 12 | switch DataPoint.labeled("hello", 100) {
|
8 |
| - case let .labeled(label, value): |
9 |
| - break |
| 13 | + case let .labeled(label, value): break |
| 14 | + case .labeled(label, let value): break |
| 15 | + case .labeled(let label, let value): break |
| 16 | + case let .labeled(label, value)?: break |
| 17 | + case let .labeled(label, value)!: break |
| 18 | + case let .labeled(label, value)??: break |
| 19 | + case let (label, value): break |
| 20 | + case let x as SomeType: break |
10 | 21 | }
|
| 22 | + """ |
| 23 | + performLint(UseLetInEveryBoundCaseVariable.self, input: input) |
11 | 24 |
|
12 |
| - switch DataPoint.labeled("hello", 100) { |
13 |
| - case .labeled(label, let value): |
14 |
| - break |
15 |
| - } |
| 25 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 2, column: 6) |
| 26 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 5, column: 6) |
| 27 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 6, column: 6) |
| 28 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 7, column: 6) |
| 29 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 8, column: 6) |
| 30 | + } |
16 | 31 |
|
17 |
| - switch DataPoint.labeled("hello", 100) { |
18 |
| - case .labeled(let label, let value): |
19 |
| - break |
20 |
| - } |
| 32 | + func testIfCase() { |
| 33 | + let input = |
| 34 | + """ |
| 35 | + if case let .labeled(label, value) = DataPoint.labeled("hello", 100) {} |
| 36 | + if case .labeled(label, let value) = DataPoint.labeled("hello", 100) {} |
| 37 | + if case .labeled(let label, let value) = DataPoint.labeled("hello", 100) {} |
| 38 | + if case let .labeled(label, value)? = DataPoint.labeled("hello", 100) {} |
| 39 | + if case let .labeled(label, value)! = DataPoint.labeled("hello", 100) {} |
| 40 | + if case let .labeled(label, value)?? = DataPoint.labeled("hello", 100) {} |
| 41 | + if case let (label, value) = DataPoint.labeled("hello", 100) {} |
| 42 | + if case let x as SomeType = someValue {} |
21 | 43 | """
|
22 | 44 | performLint(UseLetInEveryBoundCaseVariable.self, input: input)
|
23 |
| - XCTAssertDiagnosed(.useLetInBoundCaseVariables) |
24 |
| - XCTAssertNotDiagnosed(.useLetInBoundCaseVariables) |
| 45 | + |
| 46 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 1, column: 9) |
| 47 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 4, column: 9) |
| 48 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 5, column: 9) |
| 49 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 6, column: 9) |
| 50 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 7, column: 9) |
| 51 | + } |
| 52 | + |
| 53 | + func testGuardCase() { |
| 54 | + let input = |
| 55 | + """ |
| 56 | + guard case let .labeled(label, value) = DataPoint.labeled("hello", 100) else {} |
| 57 | + guard case .labeled(label, let value) = DataPoint.labeled("hello", 100) else {} |
| 58 | + guard case .labeled(let label, let value) = DataPoint.labeled("hello", 100) else {} |
| 59 | + guard case let .labeled(label, value)? = DataPoint.labeled("hello", 100) else {} |
| 60 | + guard case let .labeled(label, value)! = DataPoint.labeled("hello", 100) else {} |
| 61 | + guard case let .labeled(label, value)?? = DataPoint.labeled("hello", 100) else {} |
| 62 | + guard case let (label, value) = DataPoint.labeled("hello", 100) else {} |
| 63 | + guard case let x as SomeType = someValue else {} |
| 64 | + """ |
| 65 | + performLint(UseLetInEveryBoundCaseVariable.self, input: input) |
| 66 | + |
| 67 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 1, column: 12) |
| 68 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 4, column: 12) |
| 69 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 5, column: 12) |
| 70 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 6, column: 12) |
| 71 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 7, column: 12) |
| 72 | + } |
| 73 | + |
| 74 | + func testForCase() { |
| 75 | + let input = |
| 76 | + """ |
| 77 | + for case let .labeled(label, value) in dataPoints {} |
| 78 | + for case .labeled(label, let value) in dataPoints {} |
| 79 | + for case .labeled(let label, let value) in dataPoints {} |
| 80 | + for case let .labeled(label, value)? in dataPoints {} |
| 81 | + for case let .labeled(label, value)! in dataPoints {} |
| 82 | + for case let .labeled(label, value)?? in dataPoints {} |
| 83 | + for case let (label, value) in dataPoints {} |
| 84 | + for case let x as SomeType in {} |
| 85 | + """ |
| 86 | + performLint(UseLetInEveryBoundCaseVariable.self, input: input) |
| 87 | + |
| 88 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 1, column: 10) |
| 89 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 4, column: 10) |
| 90 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 5, column: 10) |
| 91 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 6, column: 10) |
| 92 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 7, column: 10) |
| 93 | + } |
| 94 | + |
| 95 | + func testWhileCase() { |
| 96 | + let input = |
| 97 | + """ |
| 98 | + while case let .labeled(label, value) = iter.next() {} |
| 99 | + while case .labeled(label, let value) = iter.next() {} |
| 100 | + while case .labeled(let label, let value) = iter.next() {} |
| 101 | + while case let .labeled(label, value)? = iter.next() {} |
| 102 | + while case let .labeled(label, value)! = iter.next() {} |
| 103 | + while case let .labeled(label, value)?? = iter.next() {} |
| 104 | + while case let (label, value) = iter.next() {} |
| 105 | + while case let x as SomeType = iter.next() {} |
| 106 | + """ |
| 107 | + performLint(UseLetInEveryBoundCaseVariable.self, input: input) |
| 108 | + |
| 109 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 1, column: 12) |
| 110 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 4, column: 12) |
| 111 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 5, column: 12) |
| 112 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 6, column: 12) |
| 113 | + XCTAssertDiagnosed(.useLetInBoundCaseVariables, line: 7, column: 12) |
25 | 114 | }
|
26 | 115 | }
|
0 commit comments