Skip to content

Commit e2de394

Browse files
authored
Merge pull request #629 from glessard/NSStringAPI-testfix
[test] fix for hasPrefix and hasSuffix test
2 parents d2d66f7 + 5a41eaa commit e2de394

File tree

1 file changed

+83
-48
lines changed

1 file changed

+83
-48
lines changed

test/1_stdlib/StringAPI.swift

Lines changed: 83 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct ComparisonTest {
2424
init(
2525
_ expectedUnicodeCollation: ExpectedComparisonResult,
2626
_ lhs: String, _ rhs: String,
27-
xfail: TestRunPredicate = .custom({false}, reason: ""),
27+
xfail: TestRunPredicate = .never,
2828
file: String = #file, line: UInt = #line
2929
) {
3030
self.expectedUnicodeCollation = expectedUnicodeCollation
@@ -33,25 +33,28 @@ struct ComparisonTest {
3333
self.loc = SourceLoc(file, line, comment: "test data")
3434
self.xfail = xfail
3535
}
36+
37+
func replacingPredicate(_ xfail: TestRunPredicate) -> ComparisonTest {
38+
return ComparisonTest(expectedUnicodeCollation, lhs, rhs,
39+
xfail: xfail, file: loc.file, line: loc.line)
40+
}
3641
}
3742

38-
let comparisonTests = [
43+
// List test cases for comparisons and prefix/suffix. Ideally none fail.
44+
45+
let tests = [
3946
ComparisonTest(.eq, "", ""),
4047
ComparisonTest(.lt, "", "a"),
4148

4249
// ASCII cases
4350
ComparisonTest(.lt, "t", "tt"),
44-
ComparisonTest(.gt, "t", "Tt",
45-
xfail: .nativeRuntime(
46-
"Compares in reverse with ICU, https://bugs.swift.org/browse/SR-530")),
47-
ComparisonTest(.gt, "\u{0}", "",
48-
xfail: .nativeRuntime(
49-
"Null-related issue: https://bugs.swift.org/browse/SR-630")),
51+
ComparisonTest(.gt, "t", "Tt"),
52+
ComparisonTest(.gt, "\u{0}", ""),
5053
ComparisonTest(.eq, "\u{0}", "\u{0}"),
51-
// Currently fails:
52-
// ComparisonTest(.lt, "\r\n", "t"),
53-
// ComparisonTest(.gt, "\r\n", "\n"),
54-
// ComparisonTest(.lt, "\u{0}", "\u{0}\u{0}"),
54+
55+
ComparisonTest(.lt, "\r\n", "t"),
56+
ComparisonTest(.gt, "\r\n", "\n"),
57+
ComparisonTest(.lt, "\u{0}", "\u{0}\u{0}"),
5558

5659
// Whitespace
5760
// U+000A LINE FEED (LF)
@@ -89,9 +92,7 @@ let comparisonTests = [
8992
ComparisonTest(.eq, "\u{212b}", "A\u{30a}"),
9093
ComparisonTest(.eq, "\u{212b}", "\u{c5}"),
9194
ComparisonTest(.eq, "A\u{30a}", "\u{c5}"),
92-
ComparisonTest(.lt, "A\u{30a}", "a",
93-
xfail: .nativeRuntime(
94-
"Compares in reverse with ICU, https://bugs.swift.org/browse/SR-530")),
95+
ComparisonTest(.lt, "A\u{30a}", "a"),
9596
ComparisonTest(.lt, "A", "A\u{30a}"),
9697

9798
// U+2126 OHM SIGN
@@ -114,6 +115,10 @@ let comparisonTests = [
114115
ComparisonTest(.eq, "\u{fb01}", "\u{fb01}"),
115116
ComparisonTest(.lt, "fi", "\u{fb01}"),
116117

118+
// U+1F1E7 REGIONAL INDICATOR SYMBOL LETTER B
119+
// \u{1F1E7}\u{1F1E7} Flag of Barbados
120+
ComparisonTest(.lt, "\u{1F1E7}", "\u{1F1E7}\u{1F1E7}"),
121+
117122
// Test that Unicode collation is performed in deterministic mode.
118123
//
119124
// U+0301 COMBINING ACUTE ACCENT
@@ -128,10 +133,8 @@ let comparisonTests = [
128133
// U+0301 and U+0954 don't decompose in the canonical decomposition mapping.
129134
// U+0341 has a canonical decomposition mapping of U+0301.
130135
ComparisonTest(.eq, "\u{0301}", "\u{0341}"),
131-
ComparisonTest(.lt, "\u{0301}", "\u{0954}",
132-
xfail: .nativeRuntime("Compares as equal with ICU")),
133-
ComparisonTest(.lt, "\u{0341}", "\u{0954}",
134-
xfail: .nativeRuntime("Compares as equal with ICU")),
136+
ComparisonTest(.lt, "\u{0301}", "\u{0954}"),
137+
ComparisonTest(.lt, "\u{0341}", "\u{0954}"),
135138
]
136139

137140
func checkStringComparison(
@@ -169,6 +172,28 @@ func checkStringComparison(
169172
#endif
170173
}
171174

175+
// Mark the test cases that are expected to fail in checkStringComparison
176+
177+
let comparisonTests = tests.map {
178+
(test: ComparisonTest) -> ComparisonTest in
179+
switch (test.expectedUnicodeCollation, test.lhs, test.rhs) {
180+
case (.gt, "t", "Tt"), (.lt, "A\u{30a}", "a"):
181+
return test.replacingPredicate(.nativeRuntime(
182+
"Comparison reversed between ICU and CFString, https://bugs.swift.org/browse/SR-530"))
183+
184+
case (.gt, "\u{0}", ""), (.lt, "\u{0}", "\u{0}\u{0}"):
185+
return test.replacingPredicate(.nativeRuntime(
186+
"Null-related issue: https://bugs.swift.org/browse/SR-630"))
187+
188+
case (.lt, "\u{0301}", "\u{0954}"), (.lt, "\u{0341}", "\u{0954}"):
189+
return test.replacingPredicate(.nativeRuntime(
190+
"Compares as equal with ICU"))
191+
192+
default:
193+
return test
194+
}
195+
}
196+
172197
for test in comparisonTests {
173198
StringTests.test("String.{Equatable,Hashable,Comparable}: line \(test.loc.line)")
174199
.xfail(test.xfail)
@@ -248,11 +273,7 @@ func checkHasPrefixHasSuffix(
248273
.starts(with: rhsNFDGraphemeClusters.lazy.reversed(), by: (==))
249274

250275
expectEqual(expectHasPrefix, lhs.hasPrefix(rhs), stackTrace: stackTrace)
251-
expectEqual(
252-
expectHasPrefix, (lhs + "abc").hasPrefix(rhs), stackTrace: stackTrace)
253276
expectEqual(expectHasSuffix, lhs.hasSuffix(rhs), stackTrace: stackTrace)
254-
expectEqual(
255-
expectHasSuffix, ("abc" + lhs).hasSuffix(rhs), stackTrace: stackTrace)
256277
#endif
257278
}
258279

@@ -261,33 +282,48 @@ StringTests.test("LosslessStringConvertible") {
261282
checkLosslessStringConvertible(comparisonTests.map { $0.rhs })
262283
}
263284

264-
StringTests.test("hasPrefix,hasSuffix")
265-
.skip(.nativeRuntime(
266-
"String.has{Prefix,Suffix} defined when _runtime(_ObjC)"))
267-
.code {
268-
for test in comparisonTests {
269-
checkHasPrefixHasSuffix(test.lhs, test.rhs, test.loc.withCurrentLoc())
270-
checkHasPrefixHasSuffix(test.rhs, test.lhs, test.loc.withCurrentLoc())
285+
// Mark the test cases that are expected to fail in checkHasPrefixHasSuffix
286+
287+
let substringTests = tests.map {
288+
(test: ComparisonTest) -> ComparisonTest in
289+
switch (test.expectedUnicodeCollation, test.lhs, test.rhs) {
290+
case (.eq, "\u{0}", "\u{0}"):
291+
return test.replacingPredicate(.objCRuntime(
292+
"https://bugs.swift.org/browse/SR-332"))
293+
294+
case (.gt, "\r\n", "\n"):
295+
return test.replacingPredicate(.objCRuntime(
296+
"blocked on rdar://problem/19036555"))
297+
298+
case (.eq, "\u{0301}", "\u{0341}"):
299+
return test.replacingPredicate(.objCRuntime(
300+
"https://bugs.swift.org/browse/SR-243"))
301+
302+
case (.lt, "\u{1F1E7}", "\u{1F1E7}\u{1F1E7}"):
303+
return test.replacingPredicate(.objCRuntime(
304+
"https://bugs.swift.org/browse/SR-367"))
305+
306+
default:
307+
return test
271308
}
272309
}
273310

274-
StringTests.test("Failures{hasPrefix,hasSuffix}-CF")
275-
.skip(.nativeRuntime(
276-
"String.has{Prefix,Suffix} defined when _runtime(_ObjC)"))
277-
.code {
278-
let test = ComparisonTest(.lt, "\u{0}", "\u{0}\u{0}")
279-
checkHasPrefixHasSuffix(test.lhs, test.rhs, test.loc.withCurrentLoc())
280-
}
311+
for test in substringTests {
312+
StringTests.test("hasPrefix,hasSuffix: line \(test.loc.line)")
313+
.skip(.nativeRuntime(
314+
"String.has{Prefix,Suffix} defined when _runtime(_ObjC)"))
315+
.xfail(test.xfail)
316+
.code {
317+
checkHasPrefixHasSuffix(test.lhs, test.rhs, test.loc.withCurrentLoc())
318+
checkHasPrefixHasSuffix(test.rhs, test.lhs, test.loc.withCurrentLoc())
281319

282-
StringTests.test("Failures{hasPrefix,hasSuffix}")
283-
.xfail(.custom({ true }, reason: "blocked on rdar://problem/19036555"))
284-
.skip(.nativeRuntime(
285-
"String.has{Prefix,Suffix} defined when _runtime(_ObjC)"))
286-
.code {
287-
let tests =
288-
[ComparisonTest(.lt, "\r\n", "t"), ComparisonTest(.gt, "\r\n", "\n")]
289-
tests.forEach {
290-
checkHasPrefixHasSuffix($0.lhs, $0.rhs, $0.loc.withCurrentLoc())
320+
let fragment = "abc"
321+
let combiner = "\u{0301}" // combining acute accent
322+
323+
checkHasPrefixHasSuffix(test.lhs + fragment, test.rhs, test.loc.withCurrentLoc())
324+
checkHasPrefixHasSuffix(fragment + test.lhs, test.rhs, test.loc.withCurrentLoc())
325+
checkHasPrefixHasSuffix(test.lhs + combiner, test.rhs, test.loc.withCurrentLoc())
326+
checkHasPrefixHasSuffix(combiner + test.lhs, test.rhs, test.loc.withCurrentLoc())
291327
}
292328
}
293329

@@ -308,8 +344,7 @@ StringTests.test("SameTypeComparisons") {
308344

309345
StringTests.test("CompareStringsWithUnpairedSurrogates")
310346
.xfail(
311-
.custom({ true },
312-
reason: "<rdar://problem/18029104> Strings referring to underlying " +
347+
.always("<rdar://problem/18029104> Strings referring to underlying " +
313348
"storage with unpaired surrogates compare unequal"))
314349
.code {
315350
let donor = "abcdef"

0 commit comments

Comments
 (0)