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
@@ -8,13 +8,14 @@
@testable import ElementX
import MatrixRustSDK
import XCTest
import Testing
class RoomEventStringBuilderTests: XCTestCase {
var ownUserID: String!
var stringBuilder: RoomEventStringBuilder!
@Suite
struct RoomEventStringBuilderTests {
private let ownUserID: String
private let stringBuilder: RoomEventStringBuilder
override func setUp() {
init() {
ownUserID = "@alice:matrix.org"
let stateEventStringBuilder = RoomStateEventStringBuilder(userID: ownUserID)
let attributedStringBuilder = AttributedStringBuilder(mentionBuilder: MentionBuilder())
@@ -26,36 +27,37 @@ class RoomEventStringBuilderTests: XCTestCase {
shouldPrefixSenderName: true)
}
func testSenderPrefix() {
@Test
func senderPrefix() {
let ownMessageString = stringBuilder.buildAttributedString(for: makeMessageItem(senderID: ownUserID, senderDisplayName: "Alice"))
XCTAssertEqual(ownMessageString?.string, "You: Hello, World!", "Your own messages should be prefixed with 'You'")
#expect(ownMessageString?.string == "You: Hello, World!", "Your own messages should be prefixed with 'You'")
let otherMessageString = stringBuilder.buildAttributedString(for: makeMessageItem(senderID: "@bob:matrix.org", senderDisplayName: "Bob"))
XCTAssertEqual(otherMessageString?.string, "Bob: Hello, World!", "Everyone else's messages should be prefixed with their display name.")
#expect(otherMessageString?.string == "Bob: Hello, World!", "Everyone else's messages should be prefixed with their display name.")
let ambiguousMessageString = stringBuilder.buildAttributedString(for: makeMessageItem(senderID: "@charlie:matrix.org",
senderDisplayName: "Charlie",
senderDisplayNameAmbiguous: true))
XCTAssertEqual(ambiguousMessageString?.string, "Charlie (@charlie:matrix.org): Hello, World!",
"Messages from senders with ambiguous display names should include their user ID in the prefix.")
#expect(ambiguousMessageString?.string == "Charlie (@charlie:matrix.org): Hello, World!",
"Messages from senders with ambiguous display names should include their user ID in the prefix.")
let ownEmoteString = stringBuilder.buildAttributedString(for: makeMessageItem(senderID: ownUserID,
senderDisplayName: "Alice",
type: .emote,
message: "laughs"))
XCTAssertEqual(ownEmoteString?.string, "* Alice laughs", "Your own emotes shouldn't contain 'You'")
#expect(ownEmoteString?.string == "* Alice laughs", "Your own emotes shouldn't contain 'You'")
let otherEmoteString = stringBuilder.buildAttributedString(for: makeMessageItem(senderID: "@bob:matrix.org",
senderDisplayName: "Bob",
type: .emote,
message: "sighs"))
XCTAssertEqual(otherEmoteString?.string, "* Bob sighs", "Everyone else's emotes should contain their display name.")
#expect(otherEmoteString?.string == "* Bob sighs", "Everyone else's emotes should contain their display name.")
let ownPollString = stringBuilder.buildAttributedString(for: makePollItem(senderID: ownUserID, senderDisplayName: "Alice"))
XCTAssertEqual(ownPollString?.string, "You: Poll: Which is better?", "Your own polls should be prefixed with 'You'")
#expect(ownPollString?.string == "You: Poll: Which is better?", "Your own polls should be prefixed with 'You'")
let otherPollString = stringBuilder.buildAttributedString(for: makePollItem(senderID: "@bob:matrix.org", senderDisplayName: "Bob"))
XCTAssertEqual(otherPollString?.string, "Bob: Poll: Which is better?", "Everyone else's polls should be prefixed with their display name.")
#expect(otherPollString?.string == "Bob: Poll: Which is better?", "Everyone else's polls should be prefixed with their display name.")
}
// MARK: - Helpers