Skip to content

Commit 39e820c

Browse files
Merge pull request #3681 from SwiftPackageIndex/issue-3655-swift-testing-part-9
Issue 3655 swift testing part 9
2 parents 720141c + c491936 commit 39e820c

File tree

6 files changed

+281
-265
lines changed

6 files changed

+281
-265
lines changed

Tests/AppTests/AuthorControllerTests.swift

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

15-
import XCTest
16-
1715
@testable import App
1816

1917
import Dependencies
18+
import Testing
2019
import Vapor
2120

2221

23-
class AuthorControllerTests: AppTestCase {
24-
25-
func test_query() async throws {
26-
// setup
27-
let p = try await savePackage(on: app.db, "1")
28-
try await Repository(package: p, owner: "owner").save(on: app.db)
29-
try await Version(package: p, latest: .defaultBranch).save(on: app.db)
22+
@Suite struct AuthorControllerTests {
3023

31-
// MUT
32-
let pkg = try await AuthorController.query(on: app.db, owner: "owner")
33-
34-
// validate
35-
XCTAssertEqual(pkg.map(\.model.id), [p.id])
36-
}
24+
@Test func query() async throws {
25+
try await withApp { app in
26+
// setup
27+
let p = try await savePackage(on: app.db, "1")
28+
try await Repository(package: p, owner: "owner").save(on: app.db)
29+
try await Version(package: p, latest: .defaultBranch).save(on: app.db)
3730

38-
func test_query_no_version() async throws {
39-
// setup
40-
let p = try await savePackage(on: app.db, "1")
41-
try await Repository(package: p, owner: "owner").save(on: app.db)
31+
// MUT
32+
let pkg = try await AuthorController.query(on: app.db, owner: "owner")
4233

43-
// MUT
44-
do {
45-
_ = try await AuthorController.query(on: app.db, owner: "owner")
46-
XCTFail("Expected Abort.notFound")
47-
} catch let error as Abort {
4834
// validate
49-
XCTAssertEqual(error.status, .notFound)
50-
} catch {
51-
XCTFail("Unexpected error: \(error)")
35+
#expect(pkg.map(\.model.id) == [p.id])
5236
}
5337
}
5438

55-
func test_query_sort_alphabetically() async throws {
56-
// setup
57-
for packageName in ["gamma", "alpha", "beta"] {
58-
let p = Package(url: "\(packageName)".url)
59-
try await p.save(on: app.db)
39+
@Test func query_no_version() async throws {
40+
try await withApp { app in
41+
// setup
42+
let p = try await savePackage(on: app.db, "1")
6043
try await Repository(package: p, owner: "owner").save(on: app.db)
61-
try await Version(package: p, latest: .defaultBranch, packageName: packageName).save(on: app.db)
44+
45+
// MUT
46+
do {
47+
_ = try await AuthorController.query(on: app.db, owner: "owner")
48+
Issue.record("Expected Abort.notFound")
49+
} catch let error as Abort {
50+
// validate
51+
#expect(error.status == .notFound)
52+
} catch {
53+
Issue.record("Unexpected error: \(error)")
54+
}
6255
}
56+
}
57+
58+
@Test func query_sort_alphabetically() async throws {
59+
try await withApp { app in
60+
// setup
61+
for packageName in ["gamma", "alpha", "beta"] {
62+
let p = Package(url: "\(packageName)".url)
63+
try await p.save(on: app.db)
64+
try await Repository(package: p, owner: "owner").save(on: app.db)
65+
try await Version(package: p, latest: .defaultBranch, packageName: packageName).save(on: app.db)
66+
}
6367

64-
// MUT
65-
let pkg = try await AuthorController.query(on: app.db, owner: "owner")
68+
// MUT
69+
let pkg = try await AuthorController.query(on: app.db, owner: "owner")
6670

67-
// validate
68-
XCTAssertEqual(pkg.map(\.model.url), ["alpha", "beta", "gamma"])
71+
// validate
72+
#expect(pkg.map(\.model.url) == ["alpha", "beta", "gamma"])
73+
}
6974
}
7075

71-
func test_show_owner() async throws {
76+
@Test func show_owner() async throws {
7277
try await withDependencies {
7378
$0.environment.dbId = { nil }
7479
} operation: {
75-
let p = try await savePackage(on: app.db, "1")
76-
try await Repository(package: p, owner: "owner").save(on: app.db)
77-
try await Version(package: p, latest: .defaultBranch).save(on: app.db)
78-
79-
// MUT
80-
try await app.test(.GET, "/owner", afterResponse: { response async in
81-
XCTAssertEqual(response.status, .ok)
82-
})
80+
try await withApp { app in
81+
let p = try await savePackage(on: app.db, "1")
82+
try await Repository(package: p, owner: "owner").save(on: app.db)
83+
try await Version(package: p, latest: .defaultBranch).save(on: app.db)
84+
85+
// MUT
86+
try await app.test(.GET, "/owner", afterResponse: { response async in
87+
#expect(response.status == .ok)
88+
})
89+
}
8390
}
8491
}
8592

86-
func test_show_owner_empty() async throws {
93+
@Test func show_owner_empty() async throws {
8794
try await withDependencies {
8895
$0.environment.dbId = { nil }
8996
} operation: {
90-
let p = try await savePackage(on: app.db, "1")
91-
try await Repository(package: p, owner: "owner").save(on: app.db)
92-
try await Version(package: p, latest: .defaultBranch).save(on: app.db)
93-
94-
// MUT
95-
try await app.test(.GET, "/fake-owner", afterResponse: { response async in
96-
XCTAssertEqual(response.status, .notFound)
97-
})
97+
try await withApp { app in
98+
let p = try await savePackage(on: app.db, "1")
99+
try await Repository(package: p, owner: "owner").save(on: app.db)
100+
try await Version(package: p, latest: .defaultBranch).save(on: app.db)
101+
102+
// MUT
103+
try await app.test(.GET, "/fake-owner", afterResponse: { response async in
104+
#expect(response.status == .notFound)
105+
})
106+
}
98107
}
99108
}
100109

Tests/AppTests/BadgeTests.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@
1414

1515
@testable import App
1616

17-
import XCTest
17+
import Testing
1818

1919

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

22-
func test_badgeMessage_swiftVersions() throws {
23-
XCTAssertEqual(Badge.badgeMessage(swiftVersions: [.v1, .v2, .v3, .v4]), "6.0 | 5.10 | 5.9 | 5.8")
24-
XCTAssertNil(Badge.badgeMessage(swiftVersions: []))
22+
@Test func badgeMessage_swiftVersions() throws {
23+
#expect(Badge.badgeMessage(swiftVersions: [.v1, .v2, .v3, .v4]) == "6.0 | 5.10 | 5.9 | 5.8")
24+
#expect(Badge.badgeMessage(swiftVersions: []) == nil)
2525
}
2626

27-
func test_badgeMessage_platforms() throws {
28-
XCTAssertEqual(Badge.badgeMessage(platforms: [.linux, .iOS, .macosXcodebuild, .macosSpm]),
29-
"iOS | macOS | Linux")
30-
XCTAssertNil(Badge.badgeMessage(platforms: []))
27+
@Test func badgeMessage_platforms() throws {
28+
#expect(Badge.badgeMessage(platforms: [.linux, .iOS, .macosXcodebuild, .macosSpm]) == "iOS | macOS | Linux")
29+
#expect(Badge.badgeMessage(platforms: []) == nil)
3130
}
3231

3332
}

Tests/AppTests/BlogActionsModelTests.swift

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

15-
import XCTest
15+
import Foundation
1616

1717
@testable import App
1818

1919
import Dependencies
20+
import Testing
2021

2122

22-
class BlogActionsModelTests: AppTestCase {
23+
@Suite struct BlogActionsModelTests {
2324

24-
func test_init_loadSummaries() async throws {
25+
@Test func init_loadSummaries() async throws {
2526
try withDependencies {
2627
$0.fileManager.contents = { @Sendable _ in
2728
"""
@@ -45,19 +46,21 @@ class BlogActionsModelTests: AppTestCase {
4546
// MUT
4647
let summaries = try BlogActions.Model().summaries
4748

48-
XCTAssertEqual(summaries.count, 2)
49-
XCTAssertEqual(summaries.map(\.slug), ["post-2", "post-1"])
50-
XCTAssertEqual(summaries.map(\.published), [false, true])
49+
#expect(summaries.count == 2)
50+
#expect(summaries.map(\.slug) == ["post-2", "post-1"])
51+
#expect(summaries.map(\.published) == [false, true])
5152

52-
let firstSummary = try XCTUnwrap(summaries).first
53+
let firstSummary = try #require(summaries).first
5354

5455
// Note that we are testing that the first item in this list is the *last* item in the source YAML
5556
// as the init should reverse the order of posts so that they display in reverse chronological order
56-
XCTAssertEqual(firstSummary, BlogActions.Model.PostSummary(slug: "post-2",
57-
title: "Blog post title two",
58-
summary: "Summary of blog post two",
59-
publishedAt: DateFormatter.yearMonthDayDateFormatter.date(from: "2024-01-02")!,
60-
published: false))
57+
#expect(firstSummary == BlogActions.Model.PostSummary(
58+
slug: "post-2",
59+
title: "Blog post title two",
60+
summary: "Summary of blog post two",
61+
publishedAt: DateFormatter.yearMonthDayDateFormatter.date(from: "2024-01-02")!,
62+
published: false)
63+
)
6164
}
6265

6366
try withDependencies { // Ensure prod shows only published summaries
@@ -67,13 +70,13 @@ class BlogActionsModelTests: AppTestCase {
6770
let summaries = try BlogActions.Model().summaries
6871

6972
// validate
70-
XCTAssertEqual(summaries.map(\.slug), ["post-1"])
71-
XCTAssertEqual(summaries.map(\.published), [true])
73+
#expect(summaries.map(\.slug) == ["post-1"])
74+
#expect(summaries.map(\.published) == [true])
7275
}
7376
}
7477
}
7578

76-
func test_postSummary_postMarkdown_load() async throws {
79+
@Test func postSummary_postMarkdown_load() async throws {
7780
withDependencies {
7881
$0.fileManager.contents = { @Sendable _ in
7982
"""
@@ -86,21 +89,21 @@ class BlogActionsModelTests: AppTestCase {
8689
// MUT
8790
let markdown = summary.postMarkdown
8891

89-
XCTAssertEqual(markdown, "<p>This is some Markdown with <a href=\"https://example.com\">a link</a> and some <em>formatting</em>.</p>")
92+
#expect(markdown == "<p>This is some Markdown with <a href=\"https://example.com\">a link</a> and some <em>formatting</em>.</p>")
9093
}
9194
}
9295

93-
func test_decode_posts_yml() async throws {
96+
@Test func decode_posts_yml() async throws {
9497
// setup
9598
try withDependencies {
9699
$0.fileManager.contents = FileManagerClient.liveValue.contents(atPath:)
97100
$0.timeZone = .utc
98101
} operation: {
99102
// MUT
100103
let summaries = try BlogActions.Model.allSummaries()
101-
104+
102105
// validate
103-
XCTAssert(summaries.count > 0)
106+
#expect(summaries.count > 0)
104107
}
105108
}
106109

0 commit comments

Comments
 (0)