1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
import _SwiftFormatTestSupport
@_spi(Rules) import SwiftFormat
final class NoLeadingUnderscoresTests: LintOrFormatRuleTestCase {
func testVars() {
assertLint(
NoLeadingUnderscores.self,
"""
let 1️⃣_foo = foo
var good_name = 20
var 2️⃣_badName, okayName, 3️⃣_wor_sEName = 20
""",
findings: [
FindingSpec("1️⃣", message: "remove the leading '_' from the name '_foo'"),
FindingSpec("2️⃣", message: "remove the leading '_' from the name '_badName'"),
FindingSpec("3️⃣", message: "remove the leading '_' from the name '_wor_sEName'"),
]
)
}
func testClasses() {
assertLint(
NoLeadingUnderscores.self,
"""
class Foo { let 1️⃣_foo = foo }
class 2️⃣_Bar {}
""",
findings: [
FindingSpec("1️⃣", message: "remove the leading '_' from the name '_foo'"),
FindingSpec("2️⃣", message: "remove the leading '_' from the name '_Bar'"),
]
)
}
func testEnums() {
assertLint(
NoLeadingUnderscores.self,
"""
enum Foo {
case 1️⃣_case1
case case2, 2️⃣_case3
case caseWithAssociatedValues(3️⃣_value: Int, otherValue: String)
let 4️⃣_foo = foo
}
enum 5️⃣_Bar {}
""",
findings: [
FindingSpec("1️⃣", message: "remove the leading '_' from the name '_case1'"),
FindingSpec("2️⃣", message: "remove the leading '_' from the name '_case3'"),
FindingSpec("3️⃣", message: "remove the leading '_' from the name '_value'"),
FindingSpec("4️⃣", message: "remove the leading '_' from the name '_foo'"),
FindingSpec("5️⃣", message: "remove the leading '_' from the name '_Bar'"),
]
)
}
func testProtocols() {
assertLint(
NoLeadingUnderscores.self,
"""
protocol Foo {
associatedtype 1️⃣_Quux
associatedtype Florb
var 2️⃣_foo: Int { get set }
}
protocol 3️⃣_Bar {}
""",
findings: [
FindingSpec("1️⃣", message: "remove the leading '_' from the name '_Quux'"),
FindingSpec("2️⃣", message: "remove the leading '_' from the name '_foo'"),
FindingSpec("3️⃣", message: "remove the leading '_' from the name '_Bar'"),
]
)
}
func testStructs() {
assertLint(
NoLeadingUnderscores.self,
"""
struct Foo { let 1️⃣_foo = foo }
struct 2️⃣_Bar {}
""",
findings: [
FindingSpec("1️⃣", message: "remove the leading '_' from the name '_foo'"),
FindingSpec("2️⃣", message: "remove the leading '_' from the name '_Bar'"),
]
)
}
func testFunctions() {
assertLint(
NoLeadingUnderscores.self,
"""
func 1️⃣_foo<T1, 2️⃣_T2: Equatable>(_ ok: Int, 3️⃣_notOK: Int, _ok 4️⃣_butNotThisOne: Int) {}
func bar() {}
""",
findings: [
FindingSpec("1️⃣", message: "remove the leading '_' from the name '_foo'"),
FindingSpec("2️⃣", message: "remove the leading '_' from the name '_T2'"),
FindingSpec("3️⃣", message: "remove the leading '_' from the name '_notOK'"),
FindingSpec("4️⃣", message: "remove the leading '_' from the name '_butNotThisOne'"),
]
)
}
func testInitializerArguments() {
assertLint(
NoLeadingUnderscores.self,
"""
struct X {
init<T1, 1️⃣_T2: Equatable>(_ ok: Int, 2️⃣_notOK: Int, _ok 3️⃣_butNotThisOne: Int) {}
}
""",
findings: [
FindingSpec("1️⃣", message: "remove the leading '_' from the name '_T2'"),
FindingSpec("2️⃣", message: "remove the leading '_' from the name '_notOK'"),
FindingSpec("3️⃣", message: "remove the leading '_' from the name '_butNotThisOne'"),
]
)
}
func testPrecedenceGroups() {
assertLint(
NoLeadingUnderscores.self,
"""
precedencegroup FooPrecedence {
associativity: left
higherThan: BarPrecedence
}
precedencegroup 1️⃣_FooPrecedence {
associativity: left
higherThan: BarPrecedence
}
infix operator <> : _BazPrecedence
""",
findings: [
FindingSpec("1️⃣", message: "remove the leading '_' from the name '_FooPrecedence'"),
]
)
}
func testTypealiases() {
assertLint(
NoLeadingUnderscores.self,
"""
typealias Foo = _Foo
typealias 1️⃣_Bar = Bar
""",
findings: [
FindingSpec("1️⃣", message: "remove the leading '_' from the name '_Bar'"),
]
)
}
func testIdentifiersAreIgnoredAtUsage() {
assertLint(
NoLeadingUnderscores.self,
"""
let x = _y + _z
_foo(_bar)
""",
findings: []
)
}
}
|