Skip to content

Commit

Permalink
[Code style] Require explicitly specified types for public properties (
Browse files Browse the repository at this point in the history
…#926)

This amends the code style guidelines for the project to require an
explicit type for all properties whose access level is `public` or
greater, and adjusts the code accordingly.

### Motivation:

See the explanation in the code style document for rationale. This topic
recently [came
up](#915 (comment))
in a PR discussion.

### Checklist:

- [x] Code and documentation should follow the style of the [Style
Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md).
- [x] If public symbols are renamed or modified, DocC references should
be updated.
  • Loading branch information
stmontgomery authored Jan 30, 2025
1 parent 00bb5ac commit 6df23a4
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
8 changes: 8 additions & 0 deletions Documentation/StyleGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public var errorCount: Int {
}
```

Properties, variables, and constants in Swift whose access level is `public` or
greater, or which have the `@usableFromInline` attribute, must have an
explicitly specified type even if they have an initialization expression and the
compiler could infer their type. This is meant to protect against future changes
to the code called by the initialization expression causing the inferred type of
its property to change unknowingly, which could break clients. Properties with
lower access levels may have an inferred type.

Exported C and C++ symbols that are exported should be given the prefix `swt_`
and should otherwise be named using the same lowerCamelCase naming rules as in
Swift. Use the `SWT_EXTERN` macro to ensure that symbols are consistently
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension Event {
/// On Windows, `GetFileType()` returns `FILE_TYPE_CHAR` for console file
/// handles, and the [Console API](https://learn.microsoft.com/en-us/windows/console/)
/// can be used to perform more complex console operations.
public var useANSIEscapeCodes = false
public var useANSIEscapeCodes: Bool = false

/// The supported color bit depth when adding color to the output using
/// [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code).
Expand All @@ -58,7 +58,7 @@ extension Event {
/// If the SF Symbols app is not installed on the system where the
/// output is being rendered, the effect of setting the value of this
/// property to `true` is unspecified.
public var useSFSymbols = false
public var useSFSymbols: Bool = false
#endif

/// Storage for ``tagColors``.
Expand Down
4 changes: 2 additions & 2 deletions Sources/Testing/ExitTests/ExitTestArtifacts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public struct ExitTestArtifacts: Sendable {
///
/// If you did not request standard output content when running an exit test,
/// the value of this property is the empty array.
public var standardOutputContent = [UInt8]()
public var standardOutputContent: [UInt8] = []

/// All bytes written to the standard error stream of the exit test before
/// it exited.
Expand All @@ -81,7 +81,7 @@ public struct ExitTestArtifacts: Sendable {
///
/// If you did not request standard error content when running an exit test,
/// the value of this property is the empty array.
public var standardErrorContent = [UInt8]()
public var standardErrorContent: [UInt8] = []

@_spi(ForToolsIntegrationOnly)
public init(exitCondition: ExitCondition) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Testing/Issues/Issue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public struct Issue: Sendable {

/// Whether or not this issue is known to occur.
@_spi(ForToolsIntegrationOnly)
public var isKnown = false
public var isKnown: Bool = false

/// Initialize an issue instance with the specified details.
///
Expand Down Expand Up @@ -254,7 +254,7 @@ extension Issue {
public var sourceContext: SourceContext

/// Whether or not this issue is known to occur.
public var isKnown = false
public var isKnown: Bool = false

/// Initialize an issue instance with the specified details.
///
Expand Down
10 changes: 5 additions & 5 deletions Sources/Testing/Running/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public struct Configuration: Sendable {
// MARK: - Parallelization

/// Whether or not to parallelize the execution of tests and test cases.
public var isParallelizationEnabled = true
public var isParallelizationEnabled: Bool = true

/// How to symbolicate backtraces captured during a test run.
///
Expand Down Expand Up @@ -185,7 +185,7 @@ public struct Configuration: Sendable {
/// By default, events of this kind are not delivered to event handlers
/// because they occur frequently in a typical test run and can generate
/// significant backpressure on the event handler.
public var deliverExpectationCheckedEvents = false
public var deliverExpectationCheckedEvents: Bool = false

/// The event handler to which events should be passed when they occur.
public var eventHandler: Event.Handler = { _, _ in }
Expand Down Expand Up @@ -237,7 +237,7 @@ public struct Configuration: Sendable {
/// is provided. When the value of this property is less than `0`, some
/// output is suppressed. The exact effects of this property are determined by
/// the instance's event handler.
public var verbosity = 0
public var verbosity: Int = 0

// MARK: - Test selection

Expand Down Expand Up @@ -286,7 +286,7 @@ public struct Configuration: Sendable {
/// this maximum count. After this maximum is reached, all subsequent
/// elements are omitted and a single placeholder child is added indicating
/// the number of elements which have been truncated.
public var maximumCollectionCount = 10
public var maximumCollectionCount: Int = 10

/// The maximum depth of children that can be included in the reflection of
/// a checked expectation value.
Expand All @@ -303,7 +303,7 @@ public struct Configuration: Sendable {
/// Since optionals are common, the default value of this property is
/// somewhat larger than it otherwise would be in an attempt to make the
/// defaults useful for real-world tests.
public var maximumChildDepth = 10
public var maximumChildDepth: Int = 10
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Testing/Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public struct Test: Sendable {
/// being added to the plan. For such suites, the value of this property is
/// `true`.
@_spi(ForToolsIntegrationOnly)
public var isSynthesized = false
public var isSynthesized: Bool = false

/// Initialize an instance of this type representing a test suite type.
init(
Expand Down

0 comments on commit 6df23a4

Please sign in to comment.