Skip to content

Commit 93dcd2d

Browse files
committed
Add testCaseAccessControl rule
1 parent 95c9105 commit 93dcd2d

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4689,6 +4689,90 @@ _You can enable the following settings in Xcode by running [this script](resourc
46894689
```
46904690
</details>
46914691

4692+
* <a id='test-suite-access-control'></a>(<a href='#test-suite-access-control'>link</a>) **Test case functions should be `internal`, and helper methods and properties should be `private`**. This ensures test cases are accessible to the test runner while keeping test implementation details encapsulated. [![SwiftFormat: testSuiteAccessControl](https://img.shields.io/badge/SwiftFormat-testSuiteAccessControl-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#testSuiteAccessControl)
4693+
4694+
<details>
4695+
4696+
```swift
4697+
import Testing
4698+
4699+
// WRONG
4700+
struct SpaceshipTests {
4701+
let spaceship = Spaceship()
4702+
4703+
func launchSpaceship() {
4704+
spaceship.launch()
4705+
}
4706+
4707+
@Test
4708+
func spaceshipCanLaunch() {
4709+
launchSpaceship()
4710+
#expect(spaceship.hasLaunched)
4711+
}
4712+
}
4713+
4714+
// RIGHT
4715+
struct SpaceshipTests {
4716+
4717+
// MARK: Internal
4718+
4719+
@Test
4720+
func spaceshipCanLaunch() {
4721+
launchSpaceship()
4722+
#expect(spaceship.hasLaunched)
4723+
}
4724+
4725+
// MARK: Private
4726+
4727+
private let spaceship = Spaceship()
4728+
4729+
private func launchSpaceship() {
4730+
spaceship.launch()
4731+
}
4732+
4733+
}
4734+
```
4735+
4736+
```swift
4737+
import XCTest
4738+
4739+
// WRONG
4740+
final class SpaceshipTests: XCTestCase {
4741+
let spaceship = Spaceship()
4742+
4743+
func launchSpaceship() {
4744+
spaceship.launch()
4745+
}
4746+
4747+
func testSpaceshipCanLaunch() {
4748+
launchSpaceship()
4749+
XCTAssertTrue(spaceship.hasLaunched)
4750+
}
4751+
}
4752+
4753+
// RIGHT
4754+
final class SpaceshipTests: XCTestCase {
4755+
4756+
// MARK: Internal
4757+
4758+
func testSpaceshipCanLaunch() {
4759+
launchSpaceship()
4760+
XCTAssertTrue(spaceship.hasLaunched)
4761+
}
4762+
4763+
// MARK: Private
4764+
4765+
private let spaceship = Spaceship()
4766+
4767+
private func launchSpaceship() {
4768+
spaceship.launch()
4769+
}
4770+
4771+
}
4772+
```
4773+
4774+
</details>
4775+
46924776
* <a id='avoid-force-unwrap-in-tests'></a>(<a href='#avoid-force-unwrap-in-tests'>link</a>) **Avoid force-unwrapping in unit tests**. Force-unwrapping (`!`) will crash your test suite. Use safe alternatives like `try XCTUnwrap` or `try #require`, which will throw an error instead, or standard optional unwrapping (`?`). [![SwiftFormat: noForceUnwrapInTests](https://img.shields.io/badge/SwiftFormat-noForceUnwrapInTests-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#noForceUnwrapInTests)
46934777

46944778
<details>

Sources/AirbnbSwiftFormatTool/airbnb.swiftformat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
--rules redundantThrows
139139
--rules redundantAsync
140140
--rules noGuardInTests
141+
--rules testSuiteAccessControl
141142
--rules validateTestCases
142143
--rules redundantMemberwiseInit
143144
--rules redundantBreak

0 commit comments

Comments
 (0)