Skip to content

Commit 2f009ad

Browse files
Merge pull request #3700 from SwiftPackageIndex/issue-3655-swift-testing-part-21
Issue 3655 swift testing part 21
2 parents 3f7715b + 71a5e87 commit 2f009ad

10 files changed

+664
-630
lines changed

Tests/AppTests/PackageInfoTests.swift

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,51 @@
1414

1515
@testable import App
1616

17-
import XCTest
17+
import Testing
1818

1919

20-
class PackageInfoTests: AppTestCase {
20+
@Suite struct PackageInfoTests {
2121

22-
func test_title_package_name() async throws {
22+
@Test func title_package_name() async throws {
2323
// Ensure title is populated from package.name()
24-
// setup
25-
let p = try await savePackage(on: app.db, "1")
26-
try await Repository(package: p, name: "repo name", owner: "owner")
27-
.save(on: app.db)
28-
try await Version(package: p, latest: .defaultBranch, packageName: "package name")
29-
.save(on: app.db)
30-
let joined = try await XCTUnwrapAsync(try await Joined3<Package, Repository, Version>
31-
.query(on: app.db, version: .defaultBranch)
32-
.first())
33-
34-
// MUT
35-
let pkgInfo = PackageInfo(package: joined)
36-
37-
// validate
38-
XCTAssertEqual(pkgInfo?.title, "package name")
24+
try await withApp { app in
25+
// setup
26+
let p = try await savePackage(on: app.db, "1")
27+
try await Repository(package: p, name: "repo name", owner: "owner")
28+
.save(on: app.db)
29+
try await Version(package: p, latest: .defaultBranch, packageName: "package name")
30+
.save(on: app.db)
31+
let joined = try await XCTUnwrapAsync(try await Joined3<Package, Repository, Version>
32+
.query(on: app.db, version: .defaultBranch)
33+
.first())
34+
35+
// MUT
36+
let pkgInfo = PackageInfo(package: joined)
37+
38+
// validate
39+
#expect(pkgInfo?.title == "package name")
40+
}
3941
}
4042

41-
func test_title_repo_name() async throws {
43+
@Test func title_repo_name() async throws {
4244
// Ensure title is populated from repoName if package.name() is nil
43-
// setup
44-
let p = try await savePackage(on: app.db, "1")
45-
try await Repository(package: p, name: "repo name", owner: "owner")
46-
.save(on: app.db)
47-
try await Version(package: p, latest: .defaultBranch, packageName: nil)
48-
.save(on: app.db)
49-
let joined = try await XCTUnwrapAsync(try await Joined3<Package, Repository, Version>
50-
.query(on: app.db, version: .defaultBranch)
51-
.first())
52-
53-
// MUT
54-
let pkgInfo = PackageInfo(package: joined)
55-
56-
// validate
57-
XCTAssertEqual(pkgInfo?.title, "repo name")
45+
try await withApp { app in
46+
// setup
47+
let p = try await savePackage(on: app.db, "1")
48+
try await Repository(package: p, name: "repo name", owner: "owner")
49+
.save(on: app.db)
50+
try await Version(package: p, latest: .defaultBranch, packageName: nil)
51+
.save(on: app.db)
52+
let joined = try await XCTUnwrapAsync(try await Joined3<Package, Repository, Version>
53+
.query(on: app.db, version: .defaultBranch)
54+
.first())
55+
56+
// MUT
57+
let pkgInfo = PackageInfo(package: joined)
58+
59+
// validate
60+
#expect(pkgInfo?.title == "repo name")
61+
}
5862
}
63+
5964
}

Tests/AppTests/PackageReadmeModelTests.swift

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,38 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import Foundation
16+
1517
@testable import App
1618

1719
import SnapshotTesting
1820
import SwiftSoup
19-
import XCTVapor
21+
import Testing
2022

2123

22-
class PackageReadmeModelTests: SnapshotTestCase {
24+
@Suite struct PackageReadmeModelTests {
2325

24-
func test_Element_extractReadme() throws {
26+
@Test func Element_extractReadme() throws {
2527
let element = Element.extractReadme("""
2628
<div id="readme">
2729
<article>
2830
<p>README content.</p>
2931
</article>
3032
</div>
3133
""")
32-
XCTAssertEqual(try element?.html(), "<p>README content.</p>")
34+
#expect(try element?.html() == "<p>README content.</p>")
3335
}
3436

35-
func test_URL_rewriteRelative() throws {
37+
@Test func URL_rewriteRelative() throws {
3638
let triple = ("owner", "repo", "main")
37-
XCTAssertEqual(URL(string: "https://example.com")?.rewriteRelative(to: triple, fileType: .raw), nil)
38-
XCTAssertEqual(URL(string: "https://example.com/foo")?.rewriteRelative(to: triple, fileType: .raw), nil)
39-
XCTAssertEqual(URL(string: "/foo")?.rewriteRelative(to: triple, fileType: .raw),
40-
"https://github.com/owner/repo/raw/main/foo")
41-
XCTAssertEqual(URL(string: "/foo")?.rewriteRelative(to: triple, fileType: .blob),
42-
"https://github.com/owner/repo/blob/main/foo")
43-
XCTAssertEqual(URL(string: "/foo/bar?query")?.rewriteRelative(to: triple, fileType: .raw),
44-
"https://github.com/owner/repo/raw/main/foo/bar?query")
39+
#expect(URL(string: "https://example.com")?.rewriteRelative(to: triple, fileType: .raw) == nil)
40+
#expect(URL(string: "https://example.com/foo")?.rewriteRelative(to: triple, fileType: .raw) == nil)
41+
#expect(URL(string: "/foo")?.rewriteRelative(to: triple, fileType: .raw) == "https://github.com/owner/repo/raw/main/foo")
42+
#expect(URL(string: "/foo")?.rewriteRelative(to: triple, fileType: .blob) == "https://github.com/owner/repo/blob/main/foo")
43+
#expect(URL(string: "/foo/bar?query")?.rewriteRelative(to: triple, fileType: .raw) == "https://github.com/owner/repo/raw/main/foo/bar?query")
4544
}
4645

47-
func test_Element_rewriteRelativeImages() throws {
46+
@Test func Element_rewriteRelativeImages() throws {
4847
// setup
4948
let element = Element.extractReadme("""
5049
<div id="readme">
@@ -64,11 +63,11 @@ class PackageReadmeModelTests: SnapshotTestCase {
6463
element?.rewriteRelativeImages(to: ("owner", "repo", "main"))
6564

6665
// validate
67-
let html = try XCTUnwrap(try element?.html())
66+
let html = try #require(try element?.html())
6867
assertSnapshot(of: html, as: .lines)
6968
}
7069

71-
func test_Element_rewriteRelativeLinks() throws {
70+
@Test func Element_rewriteRelativeLinks() throws {
7271
// setup
7372
let element = Element.extractReadme("""
7473
<div id="readme">
@@ -89,26 +88,26 @@ class PackageReadmeModelTests: SnapshotTestCase {
8988
element?.rewriteRelativeLinks(to: ("owner", "repo", "main"))
9089

9190
// validate
92-
let html = try XCTUnwrap(try element?.html())
91+
let html = try #require(try element?.html())
9392
assertSnapshot(of: html, as: .lines)
9493
}
9594

96-
func test_URL_initWithPotentiallyUnencodedPath() throws {
95+
@Test func URL_initWithPotentiallyUnencodedPath() throws {
9796
// Relative URLs
98-
XCTAssertEqual(try XCTUnwrap(URL(withPotentiallyUnencodedPath: "/root/relative/url")).absoluteString, "/root/relative/url")
99-
XCTAssertEqual(try XCTUnwrap(URL(withPotentiallyUnencodedPath: "relative/url")).absoluteString, "relative/url")
100-
XCTAssertEqual(try XCTUnwrap(URL(withPotentiallyUnencodedPath: "/encoded%20spaces")).absoluteString, "/encoded%20spaces")
101-
XCTAssertEqual(try XCTUnwrap(URL(withPotentiallyUnencodedPath: "/unencoded spaces")).absoluteString, "/unencoded%20spaces")
102-
XCTAssertEqual(try XCTUnwrap(URL(withPotentiallyUnencodedPath: "/multiple%20%7Bencoded%7D")).absoluteString, "/multiple%20%7Bencoded%7D")
103-
XCTAssertEqual(try XCTUnwrap(URL(withPotentiallyUnencodedPath: "/multiple {unencoded}")).absoluteString, "/multiple%20%7Bunencoded%7D")
97+
#expect(try #require(URL(withPotentiallyUnencodedPath: "/root/relative/url")).absoluteString == "/root/relative/url")
98+
#expect(try #require(URL(withPotentiallyUnencodedPath: "relative/url")).absoluteString == "relative/url")
99+
#expect(try #require(URL(withPotentiallyUnencodedPath: "/encoded%20spaces")).absoluteString == "/encoded%20spaces")
100+
#expect(try #require(URL(withPotentiallyUnencodedPath: "/unencoded spaces")).absoluteString == "/unencoded%20spaces")
101+
#expect(try #require(URL(withPotentiallyUnencodedPath: "/multiple%20%7Bencoded%7D")).absoluteString == "/multiple%20%7Bencoded%7D")
102+
#expect(try #require(URL(withPotentiallyUnencodedPath: "/multiple {unencoded}")).absoluteString == "/multiple%20%7Bunencoded%7D")
104103

105104
// Absolute URLs
106-
XCTAssertEqual(try XCTUnwrap(URL(withPotentiallyUnencodedPath: "https://full.host/and/path")).absoluteString, "https://full.host/and/path")
107-
XCTAssertEqual(try XCTUnwrap(URL(withPotentiallyUnencodedPath: "https://full.host/encoded%20spaces")).absoluteString, "https://full.host/encoded%20spaces")
108-
XCTAssertEqual(try XCTUnwrap(URL(withPotentiallyUnencodedPath: "https://full.host/unencoded spaces")).absoluteString, "https://full.host/unencoded%20spaces")
105+
#expect(try #require(URL(withPotentiallyUnencodedPath: "https://full.host/and/path")).absoluteString == "https://full.host/and/path")
106+
#expect(try #require(URL(withPotentiallyUnencodedPath: "https://full.host/encoded%20spaces")).absoluteString == "https://full.host/encoded%20spaces")
107+
#expect(try #require(URL(withPotentiallyUnencodedPath: "https://full.host/unencoded spaces")).absoluteString == "https://full.host/unencoded%20spaces")
109108
}
110109

111-
func test_Element_fixInlineAnchors() throws {
110+
@Test func Element_fixInlineAnchors() throws {
112111
// setup
113112
let element = Element.extractReadme("""
114113
<div id="readme">
@@ -126,11 +125,11 @@ class PackageReadmeModelTests: SnapshotTestCase {
126125
element?.fixInlineAnchors()
127126

128127
// validate
129-
let html = try XCTUnwrap(try element?.html())
128+
let html = try #require(try element?.html())
130129
assertSnapshot(of: html, as: .lines)
131130
}
132131

133-
func test_Element_fixProtectedCachedImages() throws {
132+
@Test func Element_fixProtectedCachedImages() throws {
134133
// setup
135134
let element = Element.extractReadme("""
136135
<div id="readme">
@@ -150,11 +149,11 @@ class PackageReadmeModelTests: SnapshotTestCase {
150149
element?.fixProtectedCachedImages()
151150

152151
// validate
153-
let html = try XCTUnwrap(try element?.html())
152+
let html = try #require(try element?.html())
154153
assertSnapshot(of: html, as: .lines)
155154
}
156155

157-
func test_Element_disableTurboOnLinks() throws {
156+
@Test func Element_disableTurboOnLinks() throws {
158157
// setup
159158
let element = Element.extractReadme("""
160159
<div id="readme">
@@ -172,7 +171,7 @@ class PackageReadmeModelTests: SnapshotTestCase {
172171
element?.disableTurboOnLinks()
173172

174173
// validate
175-
let html = try XCTUnwrap(try element?.html())
174+
let html = try #require(try element?.html())
176175
assertSnapshot(of: html, as: .lines)
177176
}
178177
}

Tests/AppTests/PackageReleasesModelTests.swift

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import Foundation
16+
1517
@testable import App
1618

17-
import XCTVapor
1819
import Dependencies
20+
import Testing
1921

2022

21-
class PackageReleasesModelTests: AppTestCase {
23+
@Suite struct PackageReleasesModelTests {
2224

23-
func test_initialise() async throws {
25+
@Test func initialise() async throws {
2426
// Setup
2527

2628
// Work-around to set the local time zone for time sensitive
@@ -36,36 +38,38 @@ class PackageReleasesModelTests: AppTestCase {
3638
try await withDependencies {
3739
$0.date.now = .spiBirthday
3840
} operation: {
39-
let pkg = Package(id: UUID(), url: "1".asGithubUrl.url)
40-
try await pkg.save(on: app.db)
41-
42-
try await Repository(package: pkg, releases: [
43-
.mock(description: "Release Notes", descriptionHTML: "Release Notes",
44-
publishedAt: 2, tagName: "1.0.0", url: "some url"),
45-
46-
.mock(description: nil, descriptionHTML: nil,
47-
publishedAt: 1, tagName: "0.0.1", url: "some url"),
48-
]).save(on: app.db)
49-
let jpr = try await Package.fetchCandidate(app.db, id: pkg.id!)
50-
51-
52-
// MUT
53-
let model = try XCTUnwrap(PackageReleases.Model(package: jpr))
54-
55-
// Validate
56-
XCTAssertEqual(model.releases, [
57-
.init(title: "1.0.0", date: "Released 50 years ago on 1 January 1970",
58-
html: "Release Notes", link: "some url"),
59-
60-
.init(title: "0.0.1", date: "Released 50 years ago on 1 January 1970",
61-
html: nil, link: "some url"),
62-
])
63-
// NOTE(heckj): test is sensitive to local time zones, breaks when run at GMT-7
64-
// resolves as `31 December 1969`
41+
try await withApp { app in
42+
let pkg = Package(id: UUID(), url: "1".asGithubUrl.url)
43+
try await pkg.save(on: app.db)
44+
45+
try await Repository(package: pkg, releases: [
46+
.mock(description: "Release Notes", descriptionHTML: "Release Notes",
47+
publishedAt: 2, tagName: "1.0.0", url: "some url"),
48+
49+
.mock(description: nil, descriptionHTML: nil,
50+
publishedAt: 1, tagName: "0.0.1", url: "some url"),
51+
]).save(on: app.db)
52+
let jpr = try await Package.fetchCandidate(app.db, id: pkg.id!)
53+
54+
55+
// MUT
56+
let model = try #require(PackageReleases.Model(package: jpr))
57+
58+
// Validate
59+
#expect(model.releases == [
60+
.init(title: "1.0.0", date: "Released 50 years ago on 1 January 1970",
61+
html: "Release Notes", link: "some url"),
62+
63+
.init(title: "0.0.1", date: "Released 50 years ago on 1 January 1970",
64+
html: nil, link: "some url"),
65+
])
66+
// NOTE(heckj): test is sensitive to local time zones, breaks when run at GMT-7
67+
// resolves as `31 December 1969`
68+
}
6569
}
6670
}
6771

68-
func test_dateFormatting() throws {
72+
@Test func dateFormatting() throws {
6973

7074
// Work-around to set the local time zone for time sensitive
7175
// tests. Sets the explicit default time zone to UTC for the duration
@@ -80,24 +84,22 @@ class PackageReleasesModelTests: AppTestCase {
8084
let currentDate = Date(timeIntervalSince1970: 500)
8185
let targetDate = Date(timeIntervalSince1970: 0)
8286

83-
XCTAssertEqual(PackageReleases.Model.formatDate(targetDate, currentDate: currentDate),
84-
"Released 8 minutes ago on 1 January 1970")
87+
#expect(PackageReleases.Model.formatDate(targetDate, currentDate: currentDate) == "Released 8 minutes ago on 1 January 1970")
8588
// NOTE(heckj): test is sensitive to local time zones, breaks when run at GMT-7
8689
// resolves as `31 December 1969`
8790

88-
XCTAssertNil(PackageReleases.Model.formatDate(nil, currentDate: currentDate))
91+
#expect(PackageReleases.Model.formatDate(nil, currentDate: currentDate) == nil)
8992
}
9093

91-
func test_removeDuplicateHeader() throws {
94+
@Test func removeDuplicateHeader() throws {
9295

9396
do { // First header is removed if it contains the version (positive case)
9497
let descriptionHTML = """
9598
<h2>Header for v1.0.0</h2>
9699
<h2>Second Header for v1.0.0</h2>
97100
"""
98101

99-
XCTAssertEqual(PackageReleases.Model.updateDescription(descriptionHTML, replacingTitle: "v1.0.0"),
100-
"<h2>Second Header for v1.0.0</h2>")
102+
#expect(PackageReleases.Model.updateDescription(descriptionHTML, replacingTitle: "v1.0.0") == "<h2>Second Header for v1.0.0</h2>")
101103
}
102104

103105
do { // First header is *only* removed if it contains the version
@@ -106,25 +108,24 @@ class PackageReleasesModelTests: AppTestCase {
106108
<h2>Second Header for v1.0.0</h2>
107109
"""
108110

109-
XCTAssertEqual(PackageReleases.Model.updateDescription(descriptionHTML, replacingTitle: "v1.0.0"),
110-
"<h2>Header for version 1</h2> \n<h2>Second Header for v1.0.0</h2>")
111+
#expect(PackageReleases.Model.updateDescription(descriptionHTML, replacingTitle: "v1.0.0") == "<h2>Header for version 1</h2> \n<h2>Second Header for v1.0.0</h2>")
111112
}
112113

113114
do { // Supports all header versions (h1-h6)
114115
["h1", "h2", "h3", "h4", "h5", "h6"].forEach { header in
115116
let descriptionHTML = "<\(header)>v1.0.0</\(header)>"
116-
XCTAssertEqual(PackageReleases.Model.updateDescription(descriptionHTML, replacingTitle: "v1.0.0"), "")
117+
#expect(PackageReleases.Model.updateDescription(descriptionHTML, replacingTitle: "v1.0.0") == "")
117118
}
118119
}
119120
}
120121

121-
func test_descriptionIsTrimmed() throws {
122-
XCTAssertEqual(PackageReleases.Model.updateDescription(nil, replacingTitle: ""), nil)
123-
XCTAssertEqual(PackageReleases.Model.updateDescription("", replacingTitle: ""), nil)
124-
XCTAssertEqual(PackageReleases.Model.updateDescription(" ", replacingTitle: ""), nil)
125-
XCTAssertEqual(PackageReleases.Model.updateDescription("""
122+
@Test func descriptionIsTrimmed() throws {
123+
#expect(PackageReleases.Model.updateDescription(nil, replacingTitle: "") == nil)
124+
#expect(PackageReleases.Model.updateDescription("", replacingTitle: "") == nil)
125+
#expect(PackageReleases.Model.updateDescription(" ", replacingTitle: "") == nil)
126+
#expect(PackageReleases.Model.updateDescription("""
126127
127128
128-
""", replacingTitle: ""), nil)
129+
""", replacingTitle: "") == nil)
129130
}
130131
}

0 commit comments

Comments
 (0)