Skip to content

Commit 2d0f746

Browse files
committed
Add test for new trait filtering behavior
1 parent 98111a3 commit 2d0f746

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
14+
/// A trait that only applies to suites
15+
private struct SuiteOnlyTrait: SuiteTrait {}
16+
17+
/// A trait that only applies to test functions
18+
private struct TestOnlyTrait: TestTrait {}
19+
20+
/// A trait that applies to both
21+
private struct TestAndSuiteTrait: SuiteTrait, TestTrait {}
22+
23+
/// A stub test suite.
24+
@Suite
25+
private struct DemoTestSuite {}
26+
27+
28+
@Suite("Test.Trait Tests")
29+
struct Test_TraitTests {
30+
@Test("Setting traits on a suite filters out non-suite traits", .tags(.traitRelated))
31+
func filterTraitsOnSuites() async throws {
32+
let mixedTraits: [any Trait] = [SuiteOnlyTrait(), TestOnlyTrait(), TestAndSuiteTrait()]
33+
var suite = try #require(await test(for: DemoTestSuite.self))
34+
35+
// Setting the traits property should filter out non-suite traits
36+
suite.traits = mixedTraits
37+
#expect(suite.traits.count == 2)
38+
#expect(suite.traits[0] is SuiteOnlyTrait)
39+
#expect(suite.traits[1] is TestAndSuiteTrait)
40+
}
41+
42+
@Test("Setting traits on a test filters out non-test traits", .tags(.traitRelated))
43+
func filterTraitsOnTests() async throws {
44+
let mixedTraits: [any Trait] = [SuiteOnlyTrait(), TestOnlyTrait(), TestAndSuiteTrait()]
45+
var test = Test {}
46+
47+
// Setting the traits property should filter out non-test traits
48+
test.traits = mixedTraits
49+
#expect(test.traits.count == 2)
50+
#expect(test.traits[0] is TestOnlyTrait)
51+
#expect(test.traits[1] is TestAndSuiteTrait)
52+
}
53+
54+
}

0 commit comments

Comments
 (0)