Skip to content

Commit e343554

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 33a937c commit e343554

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
@@ -1046,6 +1047,9 @@ extension RegexTests {
10461047
}
10471048

10481049
func testMatchAnchors() throws {
1050+
// Must have new stdlib for character class ranges and word boundaries.
1051+
guard ensureNewStdlib() else { return }
1052+
10491053
// MARK: Anchors
10501054
firstMatchTests(
10511055
#"^\d+"#,
@@ -1094,8 +1098,6 @@ extension RegexTests {
10941098
(" 123\n456\n", nil),
10951099
("123 456", "456"))
10961100

1097-
// FIXME: Keep this until _wordIndex and friends are
1098-
#if os(Linux)
10991101
firstMatchTests(
11001102
#"\d+\b"#,
11011103
("123", "123"),
@@ -1113,7 +1115,6 @@ extension RegexTests {
11131115
("123", "23"),
11141116
(" 123", "23"),
11151117
("123 456", "23"))
1116-
#endif
11171118

11181119
// TODO: \G and \K
11191120
do {
@@ -1144,9 +1145,10 @@ extension RegexTests {
11441145
("Sol Cafe", nil), xfail: true)
11451146
}
11461147

1147-
// FIXME: Keep this until _wordIndex and friends are
1148-
#if os(Linux)
11491148
func testLevel2WordBoundaries() {
1149+
// Must have new stdlib for character class ranges and word boundaries.
1150+
guard ensureNewStdlib() else { return }
1151+
11501152
// MARK: Level 2 Word Boundaries
11511153
firstMatchTest(#"\b😊\b"#, input: "🔥😊👍", match: "😊")
11521154
firstMatchTest(#"\b👨🏽\b"#, input: "👩🏻👶🏿👨🏽🧑🏾👩🏼", match: "👨🏽")
@@ -1162,8 +1164,7 @@ extension RegexTests {
11621164
firstMatchTest(#"can\B\'\Bt"#, input: "I can't do that.", match: "can't")
11631165
firstMatchTest(#"\b÷\b"#, input: "3 ÷ 3 = 1", match: "÷")
11641166
}
1165-
#endif
1166-
1167+
11671168
func testMatchGroups() {
11681169
// MARK: Groups
11691170

@@ -1388,6 +1389,9 @@ extension RegexTests {
13881389
}
13891390

13901391
func testMatchExamples() {
1392+
// Must have new stdlib for character class ranges and word boundaries.
1393+
guard ensureNewStdlib() else { return }
1394+
13911395
// Backreferences
13921396
matchTest(
13931397
#"(sens|respons)e and \1ibility"#,
@@ -1437,8 +1441,6 @@ extension RegexTests {
14371441
xfail: true
14381442
)
14391443

1440-
// FIXME: Keep this until _wordIndex and friends are
1441-
#if os(Linux)
14421444
// HTML tags
14431445
matchTest(
14441446
#"<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>.*?</\1>"#,
@@ -1456,7 +1458,6 @@ extension RegexTests {
14561458
("pass me the the kettle", ["the"]),
14571459
("this doesn't have any", nil)
14581460
)
1459-
#endif
14601461

14611462
// Floats
14621463
flatCaptureTest(
@@ -1536,6 +1537,9 @@ extension RegexTests {
15361537
}
15371538

15381539
func testASCIIClasses() {
1540+
// Must have new stdlib for character class ranges and word boundaries.
1541+
guard ensureNewStdlib() else { return }
1542+
15391543
// 'D' ASCII-only digits
15401544
matchTest(
15411545
#"\d+"#,
@@ -1564,8 +1568,6 @@ extension RegexTests {
15641568
("aeiou", true),
15651569
("åe\u{301}ïôú", false))
15661570

1567-
// FIXME: Keep this until _wordIndex and friends are
1568-
#if os(Linux)
15691571
matchTest(
15701572
#"abcd\b.+"#,
15711573
("abcd ef", true),
@@ -1581,7 +1583,6 @@ extension RegexTests {
15811583
("abcd ef", true),
15821584
("abcdef", false),
15831585
("abcdéf", false))
1584-
#endif
15851586

15861587
// 'S' ASCII-only spaces
15871588
matchTest(

0 commit comments

Comments
 (0)