Skip to content

Commit 9061836

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 2c5797d commit 9061836

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
@@ -1003,6 +1004,9 @@ extension RegexTests {
10031004
}
10041005

10051006
func testMatchAnchors() throws {
1007+
// Must have new stdlib for character class ranges and word boundaries.
1008+
guard ensureNewStdlib() else { return }
1009+
10061010
// MARK: Anchors
10071011
firstMatchTests(
10081012
#"^\d+"#,
@@ -1051,8 +1055,6 @@ extension RegexTests {
10511055
(" 123\n456\n", nil),
10521056
("123 456", "456"))
10531057

1054-
// FIXME: Keep this until _wordIndex and friends are
1055-
#if os(Linux)
10561058
firstMatchTests(
10571059
#"\d+\b"#,
10581060
("123", "123"),
@@ -1070,7 +1072,6 @@ extension RegexTests {
10701072
("123", "23"),
10711073
(" 123", "23"),
10721074
("123 456", "23"))
1073-
#endif
10741075

10751076
// TODO: \G and \K
10761077
do {
@@ -1101,9 +1102,10 @@ extension RegexTests {
11011102
("Sol Cafe", nil), xfail: true)
11021103
}
11031104

1104-
// FIXME: Keep this until _wordIndex and friends are
1105-
#if os(Linux)
11061105
func testLevel2WordBoundaries() {
1106+
// Must have new stdlib for character class ranges and word boundaries.
1107+
guard ensureNewStdlib() else { return }
1108+
11071109
// MARK: Level 2 Word Boundaries
11081110
firstMatchTest(#"\b😊\b"#, input: "🔥😊👍", match: "😊")
11091111
firstMatchTest(#"\b👨🏽\b"#, input: "👩🏻👶🏿👨🏽🧑🏾👩🏼", match: "👨🏽")
@@ -1119,8 +1121,7 @@ extension RegexTests {
11191121
firstMatchTest(#"can\B\'\Bt"#, input: "I can't do that.", match: "can't")
11201122
firstMatchTest(#"\b÷\b"#, input: "3 ÷ 3 = 1", match: "÷")
11211123
}
1122-
#endif
1123-
1124+
11241125
func testMatchGroups() {
11251126
// MARK: Groups
11261127

@@ -1345,6 +1346,9 @@ extension RegexTests {
13451346
}
13461347

13471348
func testMatchExamples() {
1349+
// Must have new stdlib for character class ranges and word boundaries.
1350+
guard ensureNewStdlib() else { return }
1351+
13481352
// Backreferences
13491353
matchTest(
13501354
#"(sens|respons)e and \1ibility"#,
@@ -1394,8 +1398,6 @@ extension RegexTests {
13941398
xfail: true
13951399
)
13961400

1397-
// FIXME: Keep this until _wordIndex and friends are
1398-
#if os(Linux)
13991401
// HTML tags
14001402
matchTest(
14011403
#"<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>.*?</\1>"#,
@@ -1413,7 +1415,6 @@ extension RegexTests {
14131415
("pass me the the kettle", ["the"]),
14141416
("this doesn't have any", nil)
14151417
)
1416-
#endif
14171418

14181419
// Floats
14191420
flatCaptureTest(
@@ -1493,6 +1494,9 @@ extension RegexTests {
14931494
}
14941495

14951496
func testASCIIClasses() {
1497+
// Must have new stdlib for character class ranges and word boundaries.
1498+
guard ensureNewStdlib() else { return }
1499+
14961500
// 'D' ASCII-only digits
14971501
matchTest(
14981502
#"\d+"#,
@@ -1521,8 +1525,6 @@ extension RegexTests {
15211525
("aeiou", true),
15221526
("åe\u{301}ïôú", false))
15231527

1524-
// FIXME: Keep this until _wordIndex and friends are
1525-
#if os(Linux)
15261528
matchTest(
15271529
#"abcd\b.+"#,
15281530
("abcd ef", true),
@@ -1538,7 +1540,6 @@ extension RegexTests {
15381540
("abcd ef", true),
15391541
("abcdef", false),
15401542
("abcdéf", false))
1541-
#endif
15421543

15431544
// 'S' ASCII-only spaces
15441545
matchTest(

0 commit comments

Comments
 (0)