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 c92e847ed7
commit 173b39a07f
118 changed files with 4630 additions and 4129 deletions

View File

@@ -7,46 +7,50 @@
//
@testable import ElementX
import Foundation
import MatrixRustSDK
import XCTest
import Testing
/// Just for API sanity checking, they're already properly tested in the SDK/Ruma
class PermalinkTests: XCTestCase {
func testUserIdentifierPermalink() {
@Suite
struct PermalinkTests {
@Test
func userIdentifierPermalink() throws {
let invalidUserId = "This1sN0tV4lid!@#$%^&*()"
XCTAssertNil(try? matrixToUserPermalink(userId: invalidUserId))
#expect(throws: (any Error).self) { try matrixToUserPermalink(userId: invalidUserId) }
let validUserId = "@abcdefghijklmnopqrstuvwxyz1234567890._-=/:matrix.org"
XCTAssertEqual(try? matrixToUserPermalink(userId: validUserId), .some("https://matrix.to/#/@abcdefghijklmnopqrstuvwxyz1234567890._-=%2F:matrix.org"))
#expect(try matrixToUserPermalink(userId: validUserId) == "https://matrix.to/#/@abcdefghijklmnopqrstuvwxyz1234567890._-=%2F:matrix.org")
}
func testPermalinkDetection() {
@Test
func permalinkDetection() {
var url: URL = "https://www.matrix.org"
XCTAssertNil(parseMatrixEntityFrom(uri: url.absoluteString))
#expect(parseMatrixEntityFrom(uri: url.absoluteString) == nil)
url = "https://matrix.to/#/@bob:matrix.org?via=matrix.org"
XCTAssertEqual(parseMatrixEntityFrom(uri: url.absoluteString),
MatrixEntity(id: .user(id: "@bob:matrix.org"),
via: ["matrix.org"]))
#expect(parseMatrixEntityFrom(uri: url.absoluteString) ==
MatrixEntity(id: .user(id: "@bob:matrix.org"),
via: ["matrix.org"]))
url = "https://matrix.to/#/!roomidentifier:matrix.org?via=matrix.org"
XCTAssertEqual(parseMatrixEntityFrom(uri: url.absoluteString),
MatrixEntity(id: .room(id: "!roomidentifier:matrix.org"),
via: ["matrix.org"]))
#expect(parseMatrixEntityFrom(uri: url.absoluteString) ==
MatrixEntity(id: .room(id: "!roomidentifier:matrix.org"),
via: ["matrix.org"]))
url = "https://matrix.to/#/%23roomalias:matrix.org?via=matrix.org"
XCTAssertEqual(parseMatrixEntityFrom(uri: url.absoluteString),
MatrixEntity(id: .roomAlias(alias: "#roomalias:matrix.org"),
via: ["matrix.org"]))
#expect(parseMatrixEntityFrom(uri: url.absoluteString) ==
MatrixEntity(id: .roomAlias(alias: "#roomalias:matrix.org"),
via: ["matrix.org"]))
url = "https://matrix.to/#/!roomidentifier:matrix.org/$eventidentifier?via=matrix.org"
XCTAssertEqual(parseMatrixEntityFrom(uri: url.absoluteString),
MatrixEntity(id: .eventOnRoomId(roomId: "!roomidentifier:matrix.org", eventId: "$eventidentifier"),
via: ["matrix.org"]))
#expect(parseMatrixEntityFrom(uri: url.absoluteString) ==
MatrixEntity(id: .eventOnRoomId(roomId: "!roomidentifier:matrix.org", eventId: "$eventidentifier"),
via: ["matrix.org"]))
url = "https://matrix.to/#/#roomalias:matrix.org/$eventidentifier?via=matrix.org"
XCTAssertEqual(parseMatrixEntityFrom(uri: url.absoluteString),
MatrixEntity(id: .eventOnRoomAlias(alias: "#roomalias:matrix.org", eventId: "$eventidentifier"),
via: ["matrix.org"]))
#expect(parseMatrixEntityFrom(uri: url.absoluteString) ==
MatrixEntity(id: .eventOnRoomAlias(alias: "#roomalias:matrix.org", eventId: "$eventidentifier"),
via: ["matrix.org"]))
}
}