Skip to content

Commit 10fb0f8

Browse files
committed
Guard against testing with older stdlibs
Replace a couple of `#if os(Linux)` checks with a check to see if we have a newer stdlib available. This lets us emit an expected failure in the case where we're testing on an older stdlib.
1 parent 96fb215 commit 10fb0f8

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

Package.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,17 @@ let package = Package(
7575
name: "RegexBuilder",
7676
dependencies: ["_StringProcessing", "_RegexParser"],
7777
swiftSettings: publicStdlibSettings),
78+
.target(name: "TestSupport",
79+
swiftSettings: [availabilityDefinition]),
7880
.testTarget(
7981
name: "RegexTests",
80-
dependencies: ["_StringProcessing"],
82+
dependencies: ["_StringProcessing", "TestSupport"],
8183
swiftSettings: [
8284
.unsafeFlags(["-Xfrontend", "-disable-availability-checking"]),
8385
]),
8486
.testTarget(
8587
name: "RegexBuilderTests",
86-
dependencies: ["_StringProcessing", "RegexBuilder"],
88+
dependencies: ["_StringProcessing", "RegexBuilder", "TestSupport"],
8789
swiftSettings: [
8890
.unsafeFlags(["-Xfrontend", "-disable-availability-checking"])
8991
]),

Sources/TestSupport/TestSupport.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
import XCTest
13+
14+
// We need to split this out of the test files, as it needs to be compiled
15+
// *without* `-disable-availability-checking` to ensure the #available check is
16+
// not compiled into a no-op.
17+
18+
#if os(Linux)
19+
public func XCTExpectFailure(
20+
_ message: String? = nil, body: () throws -> Void
21+
) rethrows {}
22+
#endif
23+
24+
/// Guards certain tests to make sure we have a new stdlib available.
25+
public func ensureNewStdlib(
26+
file: StaticString = #file, line: UInt = #line
27+
) -> Bool {
28+
guard #available(SwiftStdlib 5.7, *) else {
29+
XCTExpectFailure { XCTFail("Unsupported stdlib", file: file, line: line) }
30+
return false
31+
}
32+
return true
33+
}

Tests/RegexBuilderTests/RegexDSLTests.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
import XCTest
1313
import _StringProcessing
1414
import RegexBuilder
15-
16-
#if os(Linux)
17-
func XCTExpectFailure(_ message: String? = nil, body: () throws -> Void) rethrows {}
18-
#endif
15+
import TestSupport
1916

2017
class RegexDSLTests: XCTestCase {
2118
func _testDSLCaptures<Content: RegexComponent, MatchType>(

Tests/RegexTests/MatchTests.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import XCTest
1313
@testable import _RegexParser
1414
@testable import _StringProcessing
15+
import TestSupport
1516

1617
struct MatchError: Error {
1718
var message: String
@@ -1037,6 +1038,9 @@ extension RegexTests {
10371038
}
10381039

10391040
func testMatchAnchors() throws {
1041+
// Must have new stdlib for character class ranges and word boundaries.
1042+
guard ensureNewStdlib() else { return }
1043+
10401044
// MARK: Anchors
10411045
firstMatchTests(
10421046
#"^\d+"#,
@@ -1085,8 +1089,6 @@ extension RegexTests {
10851089
(" 123\n456\n", nil),
10861090
("123 456", "456"))
10871091

1088-
// FIXME: Keep this until _wordIndex and friends are
1089-
#if os(Linux)
10901092
firstMatchTests(
10911093
#"\d+\b"#,
10921094
("123", "123"),
@@ -1104,7 +1106,6 @@ extension RegexTests {
11041106
("123", "23"),
11051107
(" 123", "23"),
11061108
("123 456", "23"))
1107-
#endif
11081109

11091110
// TODO: \G and \K
11101111
do {
@@ -1135,9 +1136,10 @@ extension RegexTests {
11351136
("Sol Cafe", nil), xfail: true)
11361137
}
11371138

1138-
// FIXME: Keep this until _wordIndex and friends are
1139-
#if os(Linux)
11401139
func testLevel2WordBoundaries() {
1140+
// Must have new stdlib for character class ranges and word boundaries.
1141+
guard ensureNewStdlib() else { return }
1142+
11411143
// MARK: Level 2 Word Boundaries
11421144
firstMatchTest(#"\b😊\b"#, input: "🔥😊👍", match: "😊")
11431145
firstMatchTest(#"\b👨🏽\b"#, input: "👩🏻👶🏿👨🏽🧑🏾👩🏼", match: "👨🏽")
@@ -1153,8 +1155,7 @@ extension RegexTests {
11531155
firstMatchTest(#"can\B\'\Bt"#, input: "I can't do that.", match: "can't")
11541156
firstMatchTest(#"\b÷\b"#, input: "3 ÷ 3 = 1", match: "÷")
11551157
}
1156-
#endif
1157-
1158+
11581159
func testMatchGroups() {
11591160
// MARK: Groups
11601161

@@ -1379,6 +1380,9 @@ extension RegexTests {
13791380
}
13801381

13811382
func testMatchExamples() {
1383+
// Must have new stdlib for character class ranges and word boundaries.
1384+
guard ensureNewStdlib() else { return }
1385+
13821386
// Backreferences
13831387
matchTest(
13841388
#"(sens|respons)e and \1ibility"#,
@@ -1428,8 +1432,6 @@ extension RegexTests {
14281432
xfail: true
14291433
)
14301434

1431-
// FIXME: Keep this until _wordIndex and friends are
1432-
#if os(Linux)
14331435
// HTML tags
14341436
matchTest(
14351437
#"<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>.*?</\1>"#,
@@ -1447,7 +1449,6 @@ extension RegexTests {
14471449
("pass me the the kettle", ["the"]),
14481450
("this doesn't have any", nil)
14491451
)
1450-
#endif
14511452

14521453
// Floats
14531454
flatCaptureTest(
@@ -1527,6 +1528,9 @@ extension RegexTests {
15271528
}
15281529

15291530
func testASCIIClasses() {
1531+
// Must have new stdlib for character class ranges and word boundaries.
1532+
guard ensureNewStdlib() else { return }
1533+
15301534
// 'D' ASCII-only digits
15311535
matchTest(
15321536
#"\d+"#,
@@ -1555,8 +1559,6 @@ extension RegexTests {
15551559
("aeiou", true),
15561560
("åe\u{301}ïôú", false))
15571561

1558-
// FIXME: Keep this until _wordIndex and friends are
1559-
#if os(Linux)
15601562
matchTest(
15611563
#"abcd\b.+"#,
15621564
("abcd ef", true),
@@ -1572,7 +1574,6 @@ extension RegexTests {
15721574
("abcd ef", true),
15731575
("abcdef", false),
15741576
("abcdéf", false))
1575-
#endif
15761577

15771578
// 'S' ASCII-only spaces
15781579
matchTest(

0 commit comments

Comments
 (0)