Swift Testing for Unit Tests PART 1 (#5119)

* migrated a lot of unit tests to Swift Testing and added a new implementation for deferred fulfillment

more tests migration

Cleaned the code manually to establish some good patterns

more code improvements

some more code improvements

removed empty tests

update project

* more pr suggestions and cleanups

* removed the TestSetup pattern

* fixing claude not reusing tests

* pr suggestion + added indent rule to swiftformat so that we can prevent AIs to change that
This commit is contained in:
Mauro
2026-02-19 16:20:47 +01:00
committed by GitHub
parent fc03e05697
commit 7982aef855
118 changed files with 4630 additions and 4129 deletions
+37 -30
View File
@@ -7,83 +7,90 @@
//
@testable import ElementX
import XCTest
import Foundation
import Testing
class RoomSummaryTests: XCTestCase {
@Suite
struct RoomSummaryTests {
// swiftlint:disable:next large_tuple
let roomDetails: (id: String, name: String, avatarURL: URL) = ("room_id", "Room Name", "mxc://hs.tld/room/avatar")
let heroes = [UserProfileProxy(userID: "hero_1", displayName: "Hero 1", avatarURL: "mxc://hs.tld/user/avatar")]
func testRoomAvatar() {
@Test
func roomAvatar() {
let details = makeSummary(isDirect: false, isSpace: false, hasRoomAvatar: true, isTombstoned: false)
switch details.avatar {
case .room(let id, let name, let avatarURL):
XCTAssertEqual(id, roomDetails.id)
XCTAssertEqual(name, roomDetails.name)
XCTAssertEqual(avatarURL, roomDetails.avatarURL)
#expect(id == roomDetails.id)
#expect(name == roomDetails.name)
#expect(avatarURL == roomDetails.avatarURL)
case .heroes:
XCTFail("A room shouldn't use the heroes for its avatar.")
Issue.record("A room shouldn't use the heroes for its avatar.")
case .space:
XCTFail("A room shouldn't use a space avatar.")
Issue.record("A room shouldn't use a space avatar.")
case .tombstoned:
XCTFail("A room shouldn't use the tombstone for its avatar.")
Issue.record("A room shouldn't use the tombstone for its avatar.")
}
}
func testDMAvatarSet() {
@Test
func dmAvatarSet() {
let details = makeSummary(isDirect: true, isSpace: false, hasRoomAvatar: true, isTombstoned: false)
switch details.avatar {
case .room(let id, let name, let avatarURL):
XCTAssertEqual(id, roomDetails.id)
XCTAssertEqual(name, roomDetails.name)
XCTAssertEqual(avatarURL, roomDetails.avatarURL)
#expect(id == roomDetails.id)
#expect(name == roomDetails.name)
#expect(avatarURL == roomDetails.avatarURL)
case .heroes:
XCTFail("A DM with an avatar set shouldn't use the heroes instead.")
Issue.record("A DM with an avatar set shouldn't use the heroes instead.")
case .space:
XCTFail("A DM shouldn't use a space avatar.")
Issue.record("A DM shouldn't use a space avatar.")
case .tombstoned:
XCTFail("A room shouldn't use the tombstone for its avatar.")
Issue.record("A room shouldn't use the tombstone for its avatar.")
}
}
func testDMAvatarNotSet() {
@Test
func dmAvatarNotSet() {
let details = makeSummary(isDirect: true, isSpace: false, hasRoomAvatar: false, isTombstoned: false)
switch details.avatar {
case .room:
XCTFail("A DM without an avatar should defer to the hero for the correct placeholder tint colour.")
Issue.record("A DM without an avatar should defer to the hero for the correct placeholder tint colour.")
case .heroes(let heroes):
XCTAssertEqual(heroes, self.heroes)
#expect(heroes == self.heroes)
case .space:
XCTFail("A DM shouldn't use a space avatar.")
Issue.record("A DM shouldn't use a space avatar.")
case .tombstoned:
XCTFail("A room shouldn't use the tombstone for its avatar.")
Issue.record("A room shouldn't use the tombstone for its avatar.")
}
}
func testSpaceAvatar() {
@Test
func spaceAvatar() {
let details = makeSummary(isDirect: false, isSpace: true, hasRoomAvatar: true, isTombstoned: false)
switch details.avatar {
case .room:
XCTFail("A space shouldn't use a room avatar.")
Issue.record("A space shouldn't use a room avatar.")
case .heroes:
XCTFail("A room shouldn't use the heroes for its avatar.")
Issue.record("A room shouldn't use the heroes for its avatar.")
case .space(let id, let name, let avatarURL):
XCTAssertEqual(id, roomDetails.id)
XCTAssertEqual(name, roomDetails.name)
XCTAssertEqual(avatarURL, roomDetails.avatarURL)
#expect(id == roomDetails.id)
#expect(name == roomDetails.name)
#expect(avatarURL == roomDetails.avatarURL)
case .tombstoned:
XCTFail("A room shouldn't use the tombstone for its avatar.")
Issue.record("A room shouldn't use the tombstone for its avatar.")
}
}
func testTombstonedAvatar() {
@Test
func tombstonedAvatar() {
let details = makeSummary(isDirect: false, isSpace: false, hasRoomAvatar: true, isTombstoned: true)
XCTAssertEqual(details.avatar, .tombstoned)
#expect(details.avatar == .tombstoned)
}
// MARK: - Helpers