cleanup tests
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
@testable import phbar
|
||||
|
||||
// MARK: - Payload Types for Testing
|
||||
|
||||
private struct TestPayload: Codable, Equatable {
|
||||
let message: String
|
||||
let count: Int
|
||||
}
|
||||
|
||||
// MARK: - Notification Name
|
||||
|
||||
@Test func notificationNameIsHashable() async throws {
|
||||
let a = IPCNotificationName("com.example.test")
|
||||
let b = IPCNotificationName("com.example.test")
|
||||
let c = IPCNotificationName("com.example.other")
|
||||
|
||||
#expect(a == b)
|
||||
#expect(a != c)
|
||||
#expect(a.hashValue == b.hashValue)
|
||||
}
|
||||
|
||||
@Test func notificationNameIsExpressibleByStringLiteral() async throws {
|
||||
let name: IPCNotificationName = "com.example.test"
|
||||
#expect(name.rawValue == "com.example.test")
|
||||
}
|
||||
|
||||
@Test func notificationNameRawRepresentable() async throws {
|
||||
let name = IPCNotificationName(rawValue: "com.example.test")
|
||||
#expect(name.rawValue == "com.example.test")
|
||||
}
|
||||
|
||||
// MARK: - Payload Decoding
|
||||
|
||||
@Test func decodePayloadFromNotification() async throws {
|
||||
let payload = TestPayload(message: "hello", count: 42)
|
||||
let userInfo: [String: Any] = ["message": "hello", "count": 42]
|
||||
let notification = IPCNotification(name: .refresh, userInfo: userInfo)
|
||||
|
||||
let decoded: TestPayload? = notification.decode(TestPayload.self)
|
||||
#expect(decoded == payload)
|
||||
}
|
||||
|
||||
@Test func decodePayloadReturnsNilForMissingUserInfo() async throws {
|
||||
let notification = IPCNotification(name: .refresh, userInfo: nil)
|
||||
let decoded: TestPayload? = notification.decode(TestPayload.self)
|
||||
#expect(decoded == nil)
|
||||
}
|
||||
|
||||
@Test func decodePayloadReturnsNilForInvalidData() async throws {
|
||||
let userInfo: [String: Any] = ["wrong": "data"]
|
||||
let notification = IPCNotification(name: .refresh, userInfo: userInfo)
|
||||
let decoded: TestPayload? = notification.decode(TestPayload.self)
|
||||
#expect(decoded == nil)
|
||||
}
|
||||
|
||||
// MARK: - Posting
|
||||
|
||||
@Test func postWithoutPayloadDoesNotCrash() async throws {
|
||||
IPC.post(.refresh)
|
||||
}
|
||||
|
||||
@Test func postWithUserInfoDoesNotCrash() async throws {
|
||||
IPC.post(.refresh, userInfo: ["key": "value"])
|
||||
}
|
||||
|
||||
@Test func postWithEncodablePayloadDoesNotCrash() async throws {
|
||||
let payload = TestPayload(message: "test", count: 1)
|
||||
let result = IPC.post(.refresh, payload: payload)
|
||||
#expect(result == true)
|
||||
}
|
||||
|
||||
// MARK: - Observer
|
||||
|
||||
@Test func observerCleansUpOnDeinit() async throws {
|
||||
// Verify that creating and releasing an observer doesn't crash.
|
||||
// The token removes itself from the distributed center on deinit.
|
||||
let token = IPC.observe(.refresh) { _ in }
|
||||
withExtendedLifetime(token) {}
|
||||
}
|
||||
Reference in New Issue
Block a user