Skip to content

Commit 2891ee7

Browse files
authored
Slightly nicer string description for Signal (#42)
* Slightly nicer string description for Signal * compat with swift before 5.3
1 parent 6dde12a commit 2891ee7

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

Sources/Lifecycle/Lifecycle.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,25 @@ extension ServiceLifecycle {
161161
}
162162

163163
/// A system signal
164-
public struct Signal {
164+
public struct Signal: Equatable, CustomStringConvertible {
165165
internal var rawValue: CInt
166166

167-
public static let TERM: Signal = Signal(rawValue: SIGTERM)
168-
public static let INT: Signal = Signal(rawValue: SIGINT)
167+
public static let TERM = Signal(rawValue: SIGTERM)
168+
public static let INT = Signal(rawValue: SIGINT)
169169
// for testing
170-
internal static let ALRM: Signal = Signal(rawValue: SIGALRM)
170+
internal static let ALRM = Signal(rawValue: SIGALRM)
171+
172+
public var description: String {
173+
var result = "Signal("
174+
switch self {
175+
case Signal.TERM: result += "TERM, "
176+
case Signal.INT: result += "INT, "
177+
case Signal.ALRM: result += "ALRM, "
178+
default: () // ok to ignore
179+
}
180+
result += "rawValue: \(self.rawValue))"
181+
return result
182+
}
171183
}
172184
}
173185

Tests/LifecycleTests/ServiceLifecycleTests+XCTest.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extension ServiceLifecycleTests {
3131
("testBadStartAndWait", testBadStartAndWait),
3232
("testNesting", testNesting),
3333
("testNesting2", testNesting2),
34+
("testSignalDescription", testSignalDescription),
3435
]
3536
}
3637
}

Tests/LifecycleTests/ServiceLifecycleTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,10 @@ final class ServiceLifecycleTests: XCTestCase {
171171
lifecycle.wait()
172172
subsystem.subsystem.items.forEach { XCTAssertEqual($0.state, .shutdown, "expected item to be shutdown, but \($0.state)") }
173173
}
174+
175+
func testSignalDescription() {
176+
XCTAssertEqual("\(ServiceLifecycle.Signal.TERM)", "Signal(TERM, rawValue: \(ServiceLifecycle.Signal.TERM.rawValue))")
177+
XCTAssertEqual("\(ServiceLifecycle.Signal.INT)", "Signal(INT, rawValue: \(ServiceLifecycle.Signal.INT.rawValue))")
178+
XCTAssertEqual("\(ServiceLifecycle.Signal.ALRM)", "Signal(ALRM, rawValue: \(ServiceLifecycle.Signal.ALRM.rawValue))")
179+
}
174180
}

0 commit comments

Comments
 (0)