Skip to content

Commit 92e33a4

Browse files
committed
test: Increase HashComputation test coverage to 93.75%
Added tests for: - HashComputationError.errorDescription (both error cases) - Data.sha256 convenience property - Data.sha256Hex convenience property - Empty data edge cases Coverage improved: 78.12% → 93.75% Total tests: 144 → 149 (5 new tests, all passing)
1 parent 28d2a80 commit 92e33a4

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

Tests/DesignAlgorithmsKitTests/Algorithms/Cryptography/HashComputationTests.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,56 @@ final class HashComputationTests: XCTestCase {
197197
XCTAssertEqual(HashAlgorithm.crc32.rawValue, "crc32")
198198
}
199199

200+
// MARK: - Error Tests
201+
202+
func testHashComputationError_AlgorithmNotSupported() {
203+
let error = HashComputationError.algorithmNotSupported("test-algorithm")
204+
205+
XCTAssertEqual(error.errorDescription, "Hash algorithm 'test-algorithm' is not supported on this platform")
206+
}
207+
208+
func testHashComputationError_ComputationFailed() {
209+
let error = HashComputationError.computationFailed("test error message")
210+
211+
XCTAssertEqual(error.errorDescription, "Hash computation failed: test error message")
212+
}
213+
214+
// MARK: - Data Extension Tests
215+
216+
func testDataExtension_SHA256() {
217+
let data = "Test Data".data(using: .utf8)!
218+
let hash = data.sha256
219+
220+
// SHA256 produces 32 bytes
221+
XCTAssertEqual(hash.count, 32)
222+
XCTAssertFalse(hash.isEmpty)
223+
}
224+
225+
func testDataExtension_SHA256Hex() {
226+
let data = "Test Data".data(using: .utf8)!
227+
let hex = data.sha256Hex
228+
229+
// SHA256 hex should be 64 characters (32 bytes * 2)
230+
XCTAssertEqual(hex.count, 64)
231+
XCTAssertFalse(hex.isEmpty)
232+
233+
// Should match manual computation
234+
let manualHex = try! HashComputation.computeHashHex(data: data, algorithm: .sha256)
235+
XCTAssertEqual(hex, manualHex)
236+
}
237+
238+
func testDataExtension_EmptyDataFallback() {
239+
// Test that extensions handle errors gracefully
240+
let data = Data()
241+
242+
let hash = data.sha256
243+
let hex = data.sha256Hex
244+
245+
// Should not crash, should return empty/default values
246+
XCTAssertEqual(hash.count, 32) // SHA256 of empty data
247+
XCTAssertEqual(hex.count, 64)
248+
}
249+
200250
// MARK: - Performance Tests
201251

202252
func testPerformance_SHA256() {

0 commit comments

Comments
 (0)