|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 5 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | +// |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +/// A type which indicates a boolean value when used as a test trait. |
| 12 | +/// |
| 13 | +/// Any attribute of a test which can be represented as a boolean value may use |
| 14 | +/// an instance of this type to indicate having that attribute by adding it to |
| 15 | +/// that test's traits. |
| 16 | +/// |
| 17 | +/// Instances of this type are considered equal if they have an identical |
| 18 | +/// private reference to a value of reference type, so each unique marker must |
| 19 | +/// be a shared instance. |
| 20 | +/// |
| 21 | +/// This type is not part of the public interface of the testing library. |
| 22 | +struct MarkerTrait: TestTrait, SuiteTrait { |
| 23 | + /// A stored value of a reference type used solely for equality checking, so |
| 24 | + /// that two marker instances may be considered equal only if they have |
| 25 | + /// identical values for this property. |
| 26 | + /// |
| 27 | + /// @Comment { |
| 28 | + /// - Bug: We cannot use a custom class for this purpose because in some |
| 29 | + /// scenarios, more than one instance of the testing library may be loaded |
| 30 | + /// in to a test runner process and on certain platforms this can cause |
| 31 | + /// runtime warnings. ([148912491](rdar://148912491)) |
| 32 | + /// } |
| 33 | + nonisolated(unsafe) private let _identity: AnyObject = ManagedBuffer<Void, Void>.create(minimumCapacity: 0) { _ in () } |
| 34 | + |
| 35 | + let isRecursive: Bool |
| 36 | +} |
| 37 | + |
| 38 | +extension MarkerTrait: Equatable { |
| 39 | + static func == (lhs: Self, rhs: Self) -> Bool { |
| 40 | + lhs._identity === rhs._identity |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +#if DEBUG |
| 45 | +// MARK: - Hidden tests |
| 46 | + |
| 47 | +/// Storage for the ``Trait/hidden`` property. |
| 48 | +private let _hiddenMarker = MarkerTrait(isRecursive: true) |
| 49 | + |
| 50 | +extension Trait where Self == MarkerTrait { |
| 51 | + /// A trait that indicates that a test should be hidden from automatic |
| 52 | + /// discovery and only run if explicitly requested. |
| 53 | + /// |
| 54 | + /// This is different from disabled or skipped, and is primarily meant to be |
| 55 | + /// used on tests defined in this project's own test suite, so that example |
| 56 | + /// tests can be defined using the `@Test` attribute but not run by default |
| 57 | + /// except by the specific unit test(s) which have requested to run them. |
| 58 | + /// |
| 59 | + /// When this trait is applied to a suite, it is recursively inherited by all |
| 60 | + /// child suites and tests. |
| 61 | + static var hidden: Self { |
| 62 | + _hiddenMarker |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +extension Test { |
| 67 | + /// Whether this test is hidden, whether directly or via a trait inherited |
| 68 | + /// from a parent test. |
| 69 | + /// |
| 70 | + /// ## See Also |
| 71 | + /// |
| 72 | + /// - ``Trait/hidden`` |
| 73 | + var isHidden: Bool { |
| 74 | + containsTrait(.hidden) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// MARK: - Synthesized tests |
| 79 | + |
| 80 | +/// Storage for the ``Trait/synthesized`` property. |
| 81 | +private let _synthesizedMarker = MarkerTrait(isRecursive: false) |
| 82 | + |
| 83 | +extension Trait where Self == MarkerTrait { |
| 84 | + /// A trait that indicates a test was synthesized at runtime. |
| 85 | + /// |
| 86 | + /// During test planning, suites that are not explicitly marked with the |
| 87 | + /// `@Suite` attribute are synthesized from available type information before |
| 88 | + /// being added to the plan. This trait can be applied to such suites to keep |
| 89 | + /// track of them. |
| 90 | + /// |
| 91 | + /// When this trait is applied to a suite, it is _not_ recursively inherited |
| 92 | + /// by all child suites or tests. |
| 93 | + static var synthesized: Self { |
| 94 | + _synthesizedMarker |
| 95 | + } |
| 96 | +} |
| 97 | +#endif |
| 98 | + |
| 99 | +extension Test { |
| 100 | + /// Whether or not this instance was synthesized at runtime. |
| 101 | + /// |
| 102 | + /// During test planning, suites that are not explicitly marked with the |
| 103 | + /// `@Suite` attribute are synthesized from available type information before |
| 104 | + /// being added to the plan. For such suites, the value of this property is |
| 105 | + /// `true`. |
| 106 | + /// |
| 107 | + /// In release builds, this information is not tracked and the value of this |
| 108 | + /// property is always `false`. |
| 109 | + /// |
| 110 | + /// ## See Also |
| 111 | + /// |
| 112 | + /// - ``Trait/synthesized`` |
| 113 | + @_spi(ForToolsIntegrationOnly) |
| 114 | + public var isSynthesized: Bool { |
| 115 | +#if DEBUG |
| 116 | + containsTrait(.synthesized) |
| 117 | +#else |
| 118 | + false |
| 119 | +#endif |
| 120 | + } |
| 121 | +} |
0 commit comments