mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
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:
@@ -9,20 +9,21 @@
|
||||
import Combine
|
||||
@testable import ElementX
|
||||
import Foundation
|
||||
import XCTest
|
||||
import Testing
|
||||
|
||||
@MainActor
|
||||
class VoiceMessageCacheTests: XCTestCase {
|
||||
private var voiceMessageCache: VoiceMessageCache!
|
||||
private var mediaSource: MediaSourceProxy!
|
||||
private var fileManager: FileManager!
|
||||
@Suite
|
||||
final class VoiceMessageCacheTests {
|
||||
private var voiceMessageCache: VoiceMessageCache
|
||||
private var mediaSource: MediaSourceProxy
|
||||
private let fileManager: FileManager
|
||||
|
||||
private let someURL = URL.mockMXCAudio
|
||||
private let testFilename = "test-file"
|
||||
private let mpeg4aacFileExtension = "m4a"
|
||||
private let testTemporaryDirectory = URL.temporaryDirectory.appendingPathComponent("test-voice-messsage-cache")
|
||||
|
||||
override func setUp() async throws {
|
||||
init() throws {
|
||||
voiceMessageCache = VoiceMessageCache()
|
||||
voiceMessageCache.clearCache()
|
||||
|
||||
@@ -33,61 +34,63 @@ class VoiceMessageCacheTests: XCTestCase {
|
||||
try fileManager.createDirectory(at: testTemporaryDirectory, withIntermediateDirectories: true)
|
||||
}
|
||||
|
||||
override func tearDown() async throws {
|
||||
deinit {
|
||||
voiceMessageCache.clearCache()
|
||||
|
||||
// clear the test temporary directory
|
||||
try fileManager.removeItem(at: testTemporaryDirectory)
|
||||
try? fileManager.removeItem(at: testTemporaryDirectory)
|
||||
}
|
||||
|
||||
func testFileURL() throws {
|
||||
@Test
|
||||
func fileURL() throws {
|
||||
// If the file is not already in the cache, no URL is expected
|
||||
XCTAssertNil(voiceMessageCache.fileURL(for: mediaSource))
|
||||
#expect(voiceMessageCache.fileURL(for: mediaSource) == nil)
|
||||
|
||||
// If the file is present in the cache, its URL must be returned
|
||||
let temporaryFileURL = try createTemporaryFile(named: testFilename, withExtension: mpeg4aacFileExtension)
|
||||
guard case .success(let cachedURL) = voiceMessageCache.cache(mediaSource: mediaSource, using: temporaryFileURL, move: true) else {
|
||||
XCTFail("A success is expected")
|
||||
Issue.record("A success is expected")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertEqual(cachedURL, voiceMessageCache.fileURL(for: mediaSource))
|
||||
#expect(cachedURL == voiceMessageCache.fileURL(for: mediaSource))
|
||||
}
|
||||
|
||||
func testCacheInvalidFileExtension() throws {
|
||||
@Test
|
||||
func cacheInvalidFileExtension() throws {
|
||||
// An error should be raised if the file extension is not "m4a"
|
||||
let mpegFileURL = try createTemporaryFile(named: testFilename, withExtension: "mpg")
|
||||
guard case .failure(let error) = voiceMessageCache.cache(mediaSource: mediaSource, using: mpegFileURL, move: true) else {
|
||||
XCTFail("An error is expected")
|
||||
Issue.record("An error is expected")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertEqual(error, .invalidFileExtension)
|
||||
#expect(error == .invalidFileExtension)
|
||||
}
|
||||
|
||||
func testCacheCopy() throws {
|
||||
@Test
|
||||
func cacheCopy() throws {
|
||||
let fileURL = try createTemporaryFile(named: testFilename, withExtension: mpeg4aacFileExtension)
|
||||
guard case .success(let cacheURL) = voiceMessageCache.cache(mediaSource: mediaSource, using: fileURL, move: false) else {
|
||||
XCTFail("A success is expected")
|
||||
Issue.record("A success is expected")
|
||||
return
|
||||
}
|
||||
|
||||
// The source file must remain in its original location
|
||||
XCTAssertTrue(fileManager.fileExists(atPath: fileURL.path()))
|
||||
#expect(fileManager.fileExists(atPath: fileURL.path()))
|
||||
// A copy must be present in the cache
|
||||
XCTAssertTrue(fileManager.fileExists(atPath: cacheURL.path()))
|
||||
#expect(fileManager.fileExists(atPath: cacheURL.path()))
|
||||
}
|
||||
|
||||
func testCacheMove() throws {
|
||||
@Test
|
||||
func cacheMove() throws {
|
||||
let fileURL = try createTemporaryFile(named: testFilename, withExtension: mpeg4aacFileExtension)
|
||||
guard case .success(let cacheURL) = voiceMessageCache.cache(mediaSource: mediaSource, using: fileURL, move: true) else {
|
||||
XCTFail("A success is expected")
|
||||
Issue.record("A success is expected")
|
||||
return
|
||||
}
|
||||
|
||||
// The file must have been moved
|
||||
XCTAssertFalse(fileManager.fileExists(atPath: fileURL.path()))
|
||||
XCTAssertTrue(fileManager.fileExists(atPath: cacheURL.path()))
|
||||
#expect(!fileManager.fileExists(atPath: fileURL.path()))
|
||||
#expect(fileManager.fileExists(atPath: cacheURL.path()))
|
||||
}
|
||||
|
||||
private func createTemporaryFile(named filename: String, withExtension fileExtension: String) throws -> URL {
|
||||
|
||||
Reference in New Issue
Block a user