@@ -16,6 +16,8 @@ import SwiftParser
1616import SwiftSyntax
1717import SwiftSyntaxMacrosGenericTestSupport
1818import XCTest
19+ import _SwiftSyntaxGenericTestSupport
20+ import _SwiftSyntaxTestSupport
1921
2022public class ActiveRegionTests : XCTestCase {
2123 let linuxBuildConfig = TestingBuildConfiguration (
@@ -99,3 +101,45 @@ public class ActiveRegionTests: XCTestCase {
99101 )
100102 }
101103}
104+
105+ /// Assert that the various marked positions in the source code have the
106+ /// expected active states.
107+ func assertActiveCode(
108+ _ markedSource: String ,
109+ configuration: some BuildConfiguration = TestingBuildConfiguration ( ) ,
110+ states: [ String : IfConfigRegionState ] ,
111+ file: StaticString = #filePath,
112+ line: UInt = #line
113+ ) throws {
114+ // Pull out the markers that we'll use to dig out nodes to query.
115+ let ( markerLocations, source) = extractMarkers ( markedSource)
116+
117+ var parser = Parser ( source)
118+ let tree = SourceFileSyntax . parse ( from: & parser)
119+
120+ let configuredRegions = tree. configuredRegions ( in: configuration)
121+
122+ for (marker, location) in markerLocations {
123+ guard let expectedState = states [ marker] else {
124+ XCTFail ( " Missing marker \( marker) in expected states " , file: file, line: line)
125+ continue
126+ }
127+
128+ guard let token = tree. token ( at: AbsolutePosition ( utf8Offset: location) ) else {
129+ XCTFail ( " Unable to find token at location \( location) " , file: file, line: line)
130+ continue
131+ }
132+
133+ let ( actualState, _) = token. isActive ( in: configuration)
134+ XCTAssertEqual ( actualState, expectedState, " isActive(in:) at marker \( marker) " , file: file, line: line)
135+
136+ let actualViaRegions = token. isActive ( inConfiguredRegions: configuredRegions)
137+ XCTAssertEqual (
138+ actualViaRegions,
139+ expectedState,
140+ " isActive(inConfiguredRegions:) at marker \( marker) " ,
141+ file: file,
142+ line: line
143+ )
144+ }
145+ }
0 commit comments