Skip to content

Commit edecb03

Browse files
committed
Convert String tests
1 parent d45c35a commit edecb03

File tree

2 files changed

+39
-45
lines changed

2 files changed

+39
-45
lines changed

Tests/FoundationInternationalizationTests/StringTests+Data.swift renamed to Tests/FoundationInternationalizationTests/StringICUEncodingTests.swift

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,34 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import Testing
14+
1315
#if FOUNDATION_FRAMEWORK
1416
@testable import Foundation
1517
#else
1618
@testable import FoundationEssentials
1719
@testable import FoundationInternationalization
1820
#endif // FOUNDATION_FRAMEWORK
1921

20-
#if canImport(TestSupport)
21-
import TestSupport
22-
#endif
23-
24-
final class StringConverterTests: XCTestCase {
22+
@Suite("String (ICU Encoding)")
23+
private struct StringICUEncodingTests {
2524
private func _test_roundTripConversion(
2625
string: String,
2726
data: Data,
28-
encoding: String._Encoding,
29-
file: StaticString = #filePath,
30-
line: UInt = #line
27+
encoding: String.Encoding,
28+
sourceLocation: SourceLocation = #_sourceLocation
3129
) {
32-
XCTAssertEqual(
33-
string.data(using: encoding), data, "Failed to convert string to data.",
34-
file: file, line: line
30+
#expect(
31+
string.data(using: encoding) == data, "Failed to convert string to data.",
32+
sourceLocation: sourceLocation
3533
)
36-
XCTAssertEqual(
37-
string, String(data: data, encoding: encoding), "Failed to convert data to string.",
38-
file: file, line: line
34+
#expect(
35+
string == String(data: data, encoding: encoding), "Failed to convert data to string.",
36+
sourceLocation: sourceLocation
3937
)
4038
}
4139

42-
func test_japaneseEUC() {
40+
@Test func japaneseEUC() {
4341
// Confirm that https://github.com/swiftlang/swift-foundation/issues/1016 is fixed.
4442

4543
// ASCII
@@ -117,22 +115,22 @@ final class StringConverterTests: XCTestCase {
117115
// Unsupported characters
118116
let onsen = "Onsen♨" // BMP emoji
119117
let sushi = "Sushi🍣" // non-BMP emoji
120-
XCTAssertNil(onsen.data(using: String._Encoding.japaneseEUC))
121-
XCTAssertNil(sushi.data(using: String._Encoding.japaneseEUC))
122-
XCTAssertEqual(
123-
onsen.data(using: String._Encoding.japaneseEUC, allowLossyConversion: true),
118+
#expect(onsen.data(using: .japaneseEUC) == nil)
119+
#expect(sushi.data(using: .japaneseEUC) == nil)
120+
#expect(
121+
onsen.data(using: .japaneseEUC, allowLossyConversion: true) ==
124122
"Onsen?".data(using: .utf8)
125123
)
126124
#if FOUNDATION_FRAMEWORK
127125
// NOTE: Foundation framework replaces an unsupported non-BMP character
128126
// with "??"(two question marks).
129-
XCTAssertEqual(
130-
sushi.data(using: String._Encoding.japaneseEUC, allowLossyConversion: true),
127+
#expect(
128+
sushi.data(using: .japaneseEUC, allowLossyConversion: true) ==
131129
"Sushi??".data(using: .utf8)
132130
)
133131
#else
134-
XCTAssertEqual(
135-
sushi.data(using: String._Encoding.japaneseEUC, allowLossyConversion: true),
132+
#expect(
133+
sushi.data(using: .japaneseEUC, allowLossyConversion: true) ==
136134
"Sushi?".data(using: .utf8)
137135
)
138136
#endif

Tests/FoundationInternationalizationTests/StringTests+Locale.swift

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,28 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import Testing
14+
1315
#if FOUNDATION_FRAMEWORK
1416
@testable import Foundation
1517
#else
1618
@testable import FoundationEssentials
1719
@testable import FoundationInternationalization
1820
#endif // FOUNDATION_FRAMEWORK
1921

20-
#if canImport(TestSupport)
21-
import TestSupport
22-
#endif
23-
2422
extension String {
2523
var _scalarViewDescription: String {
2624
return unicodeScalars.map { "\\u{\(String($0.value, radix: 16, uppercase: true))}" }.joined()
2725
}
2826
}
2927

30-
final class StringLocaleTests: XCTestCase {
28+
@Suite("String (Locale)")
29+
private struct StringLocaleTests {
3130

32-
func testCapitalize_localized() {
31+
@Test func capitalize_localized() {
3332
var locale: Locale?
34-
// `extension StringProtocol { func capitalized(with: Locale) }` is
35-
// declared twice on Darwin: once in FoundationInternationalization
36-
// and once in SDK. Therefore it is ambiguous when building the package
37-
// on Darwin. Workaround it by testing the internal implementation.
38-
func test(_ string: String, _ expected: String, file: StaticString = #filePath, line: UInt = #line) {
39-
XCTAssertEqual(string._capitalized(with: locale), expected, file: file, line: line)
33+
func test(_ string: String, _ expected: String, sourceLocation: SourceLocation = #_sourceLocation) {
34+
#expect(string.capitalized(with: locale) == expected, sourceLocation: sourceLocation)
4035
}
4136

4237
do {
@@ -84,18 +79,18 @@ final class StringLocaleTests: XCTestCase {
8479
}
8580
}
8681

87-
func testUppercase_localized() {
82+
@Test func uppercase_localized() {
8883

89-
func test(_ localeID: String?, _ string: String, _ expected: String, file: StaticString = #filePath, line: UInt = #line) {
84+
func test(_ localeID: String?, _ string: String, _ expected: String, sourceLocation: SourceLocation = #_sourceLocation) {
9085
let locale: Locale?
9186
if let localeID {
9287
locale = Locale(identifier: localeID)
9388
} else {
9489
locale = nil
9590
}
96-
let actual = string._uppercased(with: locale)
91+
let actual = string.uppercased(with: locale)
9792

98-
XCTAssertEqual(actual, expected, "actual: \(actual._scalarViewDescription), expected: \(expected._scalarViewDescription)", file: file, line: line)
93+
#expect(actual == expected, "actual: \(actual._scalarViewDescription), expected: \(expected._scalarViewDescription)", sourceLocation: sourceLocation)
9994
}
10095

10196
test(nil, "", "FFL") // 0xFB04
@@ -128,17 +123,17 @@ final class StringLocaleTests: XCTestCase {
128123
test("el_GR", "\u{03B9}\u{0308}\u{0301}", "\u{0399}\u{0308}")
129124
}
130125

131-
func testLowercase_localized() {
132-
func test(_ localeID: String?, _ string: String, _ expected: String, file: StaticString = #filePath, line: UInt = #line) {
126+
@Test func lowercase_localized() {
127+
func test(_ localeID: String?, _ string: String, _ expected: String, sourceLocation: SourceLocation = #_sourceLocation) {
133128
let locale: Locale?
134129
if let localeID {
135130
locale = Locale(identifier: localeID)
136131
} else {
137132
locale = nil
138133
}
139-
let actual = string._lowercased(with: locale)
134+
let actual = string.lowercased(with: locale)
140135

141-
XCTAssertEqual(actual, expected, "actual: \(actual._scalarViewDescription), expected: \(expected._scalarViewDescription)", file: file, line: line)
136+
#expect(actual == expected, "actual: \(actual._scalarViewDescription), expected: \(expected._scalarViewDescription)", sourceLocation: sourceLocation)
142137
}
143138

144139
test(nil, "", "") // 0x1F88
@@ -156,8 +151,9 @@ final class StringLocaleTests: XCTestCase {
156151
test("tr", "İİ", "ii")
157152
}
158153

159-
func testFuzzFailure() throws {
160-
let input = String(data: Data(base64Encoded: "77+977+977+977+977+977+977+977+977+977+9Cg==")!, encoding: .utf8)!
154+
@Test func fuzzFailure() throws {
155+
let data = try #require(Data(base64Encoded: "77+977+977+977+977+977+977+977+977+977+9Cg=="))
156+
let input = try #require(String(data: data, encoding: .utf8))
161157
_ = input.lowercased(with: Locale(identifier: "en_US"))
162158
_ = input.capitalized(with: Locale(identifier: "en_US"))
163159
_ = input.capitalized(with: Locale(identifier: "en_US"))

0 commit comments

Comments
 (0)