|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2023 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 | +@testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing |
| 12 | + |
| 13 | +/// A trait that only applies to suites |
| 14 | +private struct SuiteOnlyTrait: SuiteTrait {} |
| 15 | + |
| 16 | +/// A trait that only applies to test functions |
| 17 | +private struct TestOnlyTrait: TestTrait {} |
| 18 | + |
| 19 | +/// A trait that applies to both |
| 20 | +private struct TestAndSuiteTrait: SuiteTrait, TestTrait {} |
| 21 | + |
| 22 | +/// A stub test suite. |
| 23 | +@Suite |
| 24 | +private struct DemoTestSuite {} |
| 25 | + |
| 26 | +@Suite("Test.Trait Tests") |
| 27 | +struct Test_TraitTests { |
| 28 | + @Test("Setting traits on a suite filters out non-suite traits", .tags(.traitRelated)) |
| 29 | + func filterTraitsOnSuites() async throws { |
| 30 | + let mixedTraits: [any Trait] = [SuiteOnlyTrait(), TestOnlyTrait(), TestAndSuiteTrait()] |
| 31 | + var suite = try #require(await test(for: DemoTestSuite.self)) |
| 32 | + |
| 33 | + // Setting the traits property should filter out non-suite traits |
| 34 | + suite.traits = mixedTraits |
| 35 | + #expect(suite.traits.count == 2) |
| 36 | + #expect(suite.traits[0] is SuiteOnlyTrait) |
| 37 | + #expect(suite.traits[1] is TestAndSuiteTrait) |
| 38 | + } |
| 39 | + |
| 40 | + @Test("Setting traits on a test filters out non-test traits", .tags(.traitRelated)) |
| 41 | + func filterTraitsOnTests() async throws { |
| 42 | + let mixedTraits: [any Trait] = [SuiteOnlyTrait(), TestOnlyTrait(), TestAndSuiteTrait()] |
| 43 | + var test = Test {} |
| 44 | + |
| 45 | + // Setting the traits property should filter out non-test traits |
| 46 | + test.traits = mixedTraits |
| 47 | + #expect(test.traits.count == 2) |
| 48 | + #expect(test.traits[0] is TestOnlyTrait) |
| 49 | + #expect(test.traits[1] is TestAndSuiteTrait) |
| 50 | + } |
| 51 | + |
| 52 | +} |
0 commit comments