Skip to content

Commit 1c9d743

Browse files
committed
Add check to allow underscores in test functions marked with @test attribute
1 parent f42d4f2 commit 1c9d743

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

Documentation/RuleDocumentation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Underscores (except at the beginning of an identifier) are disallowed.
7979

8080
This rule does not apply to test code, defined as code which:
8181
* Contains the line `import XCTest`
82+
* The function is marked with `@Test` attribute
8283

8384
Lint: If an identifier contains underscores or begins with a capital letter, a lint error is
8485
raised.

Sources/SwiftFormat/Rules/AlwaysUseLowerCamelCase.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule {
100100

101101
// We allow underscores in test names, because there's an existing convention of using
102102
// underscores to separate phrases in very detailed test names.
103-
let allowUnderscores = testCaseFuncs.contains(node)
103+
let allowUnderscores = testCaseFuncs.contains(node) || node.attributes.contains {
104+
// Allow underscore for test functions with the `@Test` attribute.
105+
$0.as(AttributeSyntax.self)?.attributeName.as(IdentifierTypeSyntax.self)?.name.text == "Test"
106+
}
107+
104108
diagnoseLowerCamelCaseViolations(
105109
node.name, allowUnderscores: allowUnderscores,
106110
description: identifierDescription(for: node))

Tests/SwiftFormatTests/Rules/AlwaysUseLowerCamelCaseTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,24 @@ final class AlwaysUseLowerCamelCaseTests: LintOrFormatRuleTestCase {
210210
]
211211
)
212212
}
213+
214+
func testIgnoresFunctionsWithTestAttributes() {
215+
assertLint(
216+
AlwaysUseLowerCamelCase.self,
217+
"""
218+
@Test
219+
func function_With_Test_Attribute() {}
220+
@Test("Description for test functions",
221+
.tags(.testTag))
222+
func function_With_Test_Attribute_And_Args() {}
223+
func 1️⃣function_Without_Test_Attribute() {}
224+
@objc
225+
func 2️⃣function_With_Non_Test_Attribute() {}
226+
""",
227+
findings: [
228+
FindingSpec("1️⃣", message: "rename the function 'function_Without_Test_Attribute' using lowerCamelCase"),
229+
FindingSpec("2️⃣", message: "rename the function 'function_With_Non_Test_Attribute' using lowerCamelCase"),
230+
]
231+
)
232+
}
213233
}

0 commit comments

Comments
 (0)