Skip to content

Commit 350be1b

Browse files
Add and use test fixtures for reading test resource files
1 parent d6efa47 commit 350be1b

File tree

1 file changed

+16
-42
lines changed

1 file changed

+16
-42
lines changed

Tests/ExtractionTests/ExtractionTests.swift

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ final class ExtractionTests: XCTestCase {
1919
try XCTUnwrap(Bundle.module.url(forResource: snapshotName, withExtension: "evosnapshot", subdirectory: "Resources"), "Unable to find snapshot \(snapshotName).evosnapshot in test bundle resources.")
2020
}
2121

22+
func data(forResource name: String, withExtension ext: String) throws -> Data {
23+
let url = try XCTUnwrap(Bundle.module.url(forResource: name, withExtension: ext, subdirectory: "Resources"), "Unable to find resource \(name).\(ext) in test bundle resources.")
24+
let data = try XCTUnwrap(Data(contentsOf: url), "Unable to read data from \(name).\(ext)")
25+
return data
26+
}
27+
28+
func string(forResource name: String, withExtension ext: String) throws -> String {
29+
let data = try data(forResource: name, withExtension: ext)
30+
let string = try XCTUnwrap(String(data: data, encoding: .utf8), "Unable to make string from contents of \(name).\(ext)")
31+
return string
32+
}
33+
2234
func testAllProposals() async throws {
2335

2436
let snapshotURL = try urlForSnapshot(named: "AllProposals")
@@ -68,24 +80,10 @@ final class ExtractionTests: XCTestCase {
6880

6981
// The lines of text in review-dates-good.txt are status headers from swift-evolution repository history
7082
func testGoodDates() throws {
71-
guard let reviewDatesURL = Bundle.module.url(forResource: "review-dates-good", withExtension: "txt", subdirectory: "Resources") else {
72-
print("Could not find review-dates.txt")
73-
return
74-
}
7583

76-
guard let reviewDatesData = try? Data(contentsOf: reviewDatesURL) else {
77-
print("Could not read review-dates.txt")
78-
return
79-
}
80-
81-
guard let reviewDatesContents = String(data: reviewDatesData, encoding: .utf8) else {
82-
print("Could not make string from contents of review-dates.txt")
83-
return
84-
}
84+
let reviewDatesContents = try string(forResource: "review-dates-good", withExtension: "txt")
8585

8686
let statusStrings = reviewDatesContents.split(separator: "\n")
87-
88-
// var errorsFound = 0
8987
for statusString in statusStrings {
9088
// NOTE: This is something that should be validated!
9189
// It seems a common mistake to leave out closing parenthesis or put strong marker inside closing paren
@@ -99,24 +97,10 @@ final class ExtractionTests: XCTestCase {
9997

10098
// The lines of text in review-dates-bad.txt are status headers from swift-evolution repository history
10199
func testBadDates() throws {
102-
guard let reviewDatesURL = Bundle.module.url(forResource: "review-dates-bad", withExtension: "txt", subdirectory: "Resources") else {
103-
print("Could not find review-dates-bad.txt")
104-
return
105-
}
106-
107-
guard let reviewDatesData = try? Data(contentsOf: reviewDatesURL) else {
108-
print("Could not read review-dates-bad.txt")
109-
return
110-
}
111-
112-
guard let reviewDatesContents = String(data: reviewDatesData, encoding: .utf8) else {
113-
print("Could not make string from contents of review-dates.txt")
114-
return
115-
}
116100

101+
let reviewDatesContents = try string(forResource: "review-dates-bad", withExtension: "txt")
102+
117103
let statusStrings = reviewDatesContents.split(separator: "\n")
118-
119-
// var errorsFound = 0
120104
for statusString in statusStrings {
121105
// NOTE: This is something that should be validated!
122106
// It seems a common mistake to leave out closing parenthesis or put strong marker inside closing paren
@@ -152,19 +136,9 @@ final class ExtractionTests: XCTestCase {
152136
The 'unknown-status.json' file contains the metadata of a single proposal with the fictional unknown status of 'appealed'.
153137
*/
154138
func testUnknownStatus() throws {
155-
guard let unknownStatusURL = Bundle.module.url(forResource: "unknown-status", withExtension: "json", subdirectory: "Resources") else {
156-
print("Could not find unknown-status.json")
157-
return
158-
}
159-
160-
guard let unknownStatusData = try? Data(contentsOf: unknownStatusURL) else {
161-
print("Could not read unknown-status.json")
162-
return
163-
}
164-
139+
let unknownStatusData = try data(forResource: "unknown-status", withExtension: "json")
165140
let proposal = try JSONDecoder().decode(Proposal.self, from: unknownStatusData)
166141
XCTAssertEqual(proposal.status, .unknownStatus("appealed"))
167-
168142
}
169143
}
170144

0 commit comments

Comments
 (0)