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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
import _SwiftFormatTestSupport
@_spi(Rules) import SwiftFormat
final class AlwaysUseLowerCamelCaseTests: LintOrFormatRuleTestCase {
func testInvalidVariableCasing() {
assertLint(
AlwaysUseLowerCamelCase.self,
"""
let 1️⃣Test = 1
var foo = 2
var 2️⃣bad_name = 20
var _okayName = 20
if let 3️⃣Baz = foo { }
""",
findings: [
FindingSpec("1️⃣", message: "rename the constant 'Test' using lowerCamelCase"),
FindingSpec("2️⃣", message: "rename the variable 'bad_name' using lowerCamelCase"),
FindingSpec("3️⃣", message: "rename the constant 'Baz' using lowerCamelCase"),
]
)
}
func testInvalidFunctionCasing() {
assertLint(
AlwaysUseLowerCamelCase.self,
"""
struct Foo {
func 1️⃣FooFunc() {}
}
class UnitTests: XCTestCase {
// This is flagged because XCTest is not imported.
func 2️⃣test_HappyPath_Through_GoodCode() {}
}
func wellNamedFunc(_ 3️⃣BadFuncArg1: Int, 4️⃣BadFuncArgLabel goodFuncArg: String) {
var 5️⃣PoorlyNamedVar = 0
}
""",
findings: [
FindingSpec("1️⃣", message: "rename the function 'FooFunc' using lowerCamelCase"),
FindingSpec("2️⃣", message: "rename the function 'test_HappyPath_Through_GoodCode' using lowerCamelCase"),
FindingSpec("3️⃣", message: "rename the function parameter 'BadFuncArg1' using lowerCamelCase"),
FindingSpec("4️⃣", message: "rename the argument label 'BadFuncArgLabel' using lowerCamelCase"),
FindingSpec("5️⃣", message: "rename the variable 'PoorlyNamedVar' using lowerCamelCase"),
]
)
}
func testInvalidEnumCaseCasing() {
assertLint(
AlwaysUseLowerCamelCase.self,
"""
enum FooBarCases {
case 1️⃣UpperCamelCase
case lowerCamelCase
}
""",
findings: [
FindingSpec("1️⃣", message: "rename the enum case 'UpperCamelCase' using lowerCamelCase"),
]
)
}
func testInvalidClosureCasing() {
assertLint(
AlwaysUseLowerCamelCase.self,
"""
var fooVar = [1, 2, 3, 4].first(where: { 1️⃣BadNameInFooVar -> Bool in
let 2️⃣TerribleNameInFooVar = BadName
return TerribleName != 0
})
var abc = array.first(where: { (3️⃣CParam1, _ 4️⃣CParam2: Type, cparam3) -> Bool in return true })
guard let foo = [1, 2, 3, 4].first(where: { 5️⃣BadName -> Bool in
let 6️⃣TerribleName = BadName
return TerribleName != 0
}) else { return }
""",
findings: [
FindingSpec("1️⃣", message: "rename the closure parameter 'BadNameInFooVar' using lowerCamelCase"),
FindingSpec("2️⃣", message: "rename the constant 'TerribleNameInFooVar' using lowerCamelCase"),
FindingSpec("3️⃣", message: "rename the closure parameter 'CParam1' using lowerCamelCase"),
FindingSpec("4️⃣", message: "rename the closure parameter 'CParam2' using lowerCamelCase"),
FindingSpec("5️⃣", message: "rename the closure parameter 'BadName' using lowerCamelCase"),
FindingSpec("6️⃣", message: "rename the constant 'TerribleName' using lowerCamelCase"),
]
)
}
func testIgnoresUnderscoresInTestNames() {
assertLint(
AlwaysUseLowerCamelCase.self,
"""
import XCTest
let 1️⃣Test = 1
class UnitTests: XCTestCase {
static let 2️⃣My_Constant_Value = 0
func test_HappyPath_Through_GoodCode() {}
private func 3️⃣FooFunc() {}
private func 4️⃣helperFunc_For_HappyPath_Setup() {}
private func 5️⃣testLikeMethod_With_Underscores(_ arg1: ParamType) {}
private func 6️⃣testLikeMethod_With_Underscores2() -> ReturnType {}
func test_HappyPath_Through_GoodCode_ReturnsVoid() -> Void {}
func test_HappyPath_Through_GoodCode_ReturnsShortVoid() -> () {}
func test_HappyPath_Through_GoodCode_Throws() throws {}
}
""",
findings: [
FindingSpec("1️⃣", message: "rename the constant 'Test' using lowerCamelCase"),
FindingSpec("2️⃣", message: "rename the constant 'My_Constant_Value' using lowerCamelCase"),
FindingSpec("3️⃣", message: "rename the function 'FooFunc' using lowerCamelCase"),
FindingSpec("4️⃣", message: "rename the function 'helperFunc_For_HappyPath_Setup' using lowerCamelCase"),
FindingSpec("5️⃣", message: "rename the function 'testLikeMethod_With_Underscores' using lowerCamelCase"),
FindingSpec("6️⃣", message: "rename the function 'testLikeMethod_With_Underscores2' using lowerCamelCase"),
]
)
}
func testIgnoresUnderscoresInTestNamesWhenImportedConditionally() {
assertLint(
AlwaysUseLowerCamelCase.self,
"""
#if SOME_FEATURE_FLAG
import XCTest
let 1️⃣Test = 1
class UnitTests: XCTestCase {
static let 2️⃣My_Constant_Value = 0
func test_HappyPath_Through_GoodCode() {}
private func 3️⃣FooFunc() {}
private func 4️⃣helperFunc_For_HappyPath_Setup() {}
private func 5️⃣testLikeMethod_With_Underscores(_ arg1: ParamType) {}
private func 6️⃣testLikeMethod_With_Underscores2() -> ReturnType {}
func test_HappyPath_Through_GoodCode_ReturnsVoid() -> Void {}
func test_HappyPath_Through_GoodCode_ReturnsShortVoid() -> () {}
func test_HappyPath_Through_GoodCode_Throws() throws {}
}
#endif
""",
findings: [
FindingSpec("1️⃣", message: "rename the constant 'Test' using lowerCamelCase"),
FindingSpec("2️⃣", message: "rename the constant 'My_Constant_Value' using lowerCamelCase"),
FindingSpec("3️⃣", message: "rename the function 'FooFunc' using lowerCamelCase"),
FindingSpec("4️⃣", message: "rename the function 'helperFunc_For_HappyPath_Setup' using lowerCamelCase"),
FindingSpec("5️⃣", message: "rename the function 'testLikeMethod_With_Underscores' using lowerCamelCase"),
FindingSpec("6️⃣", message: "rename the function 'testLikeMethod_With_Underscores2' using lowerCamelCase"),
]
)
}
func testIgnoresUnderscoresInConditionalTestNames() {
assertLint(
AlwaysUseLowerCamelCase.self,
"""
import XCTest
class UnitTests: XCTestCase {
#if SOME_FEATURE_FLAG
static let 1️⃣My_Constant_Value = 0
func test_HappyPath_Through_GoodCode() {}
private func 2️⃣FooFunc() {}
private func 3️⃣helperFunc_For_HappyPath_Setup() {}
private func 4️⃣testLikeMethod_With_Underscores(_ arg1: ParamType) {}
private func 5️⃣testLikeMethod_With_Underscores2() -> ReturnType {}
func test_HappyPath_Through_GoodCode_ReturnsVoid() -> Void {}
func test_HappyPath_Through_GoodCode_ReturnsShortVoid() -> () {}
func test_HappyPath_Through_GoodCode_Throws() throws {}
#else
func 6️⃣testBadMethod_HasNonVoidReturn() -> ReturnType {}
func testGoodMethod_HasVoidReturn() {}
#if SOME_OTHER_FEATURE_FLAG
func 7️⃣testBadMethod_HasNonVoidReturn2() -> ReturnType {}
func testGoodMethod_HasVoidReturn2() {}
#endif
#endif
}
#endif
""",
findings: [
FindingSpec("1️⃣", message: "rename the constant 'My_Constant_Value' using lowerCamelCase"),
FindingSpec("2️⃣", message: "rename the function 'FooFunc' using lowerCamelCase"),
FindingSpec("3️⃣", message: "rename the function 'helperFunc_For_HappyPath_Setup' using lowerCamelCase"),
FindingSpec("4️⃣", message: "rename the function 'testLikeMethod_With_Underscores' using lowerCamelCase"),
FindingSpec("5️⃣", message: "rename the function 'testLikeMethod_With_Underscores2' using lowerCamelCase"),
FindingSpec("6️⃣", message: "rename the function 'testBadMethod_HasNonVoidReturn' using lowerCamelCase"),
FindingSpec("7️⃣", message: "rename the function 'testBadMethod_HasNonVoidReturn2' using lowerCamelCase"),
]
)
}
func testIgnoresFunctionOverrides() {
assertLint(
AlwaysUseLowerCamelCase.self,
"""
class ParentClass {
var 1️⃣poorly_named_variable: Int = 5
func 2️⃣poorly_named_method() {}
}
class ChildClass: ParentClass {
override var poorly_named_variable: Int = 5
override func poorly_named_method() {}
}
""",
findings: [
FindingSpec("1️⃣", message: "rename the variable 'poorly_named_variable' using lowerCamelCase"),
FindingSpec("2️⃣", message: "rename the function 'poorly_named_method' using lowerCamelCase"),
]
)
}
}
|