Skip to content

Commit cd8e010

Browse files
committed
deinit test for additional properties + fix for deinit fractionCompleted calculation
1 parent 1380f2b commit cd8e010

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

Sources/FoundationEssentials/ProgressManager/ProgressManager+State.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,13 @@ extension ProgressManager {
106106
var selfFraction: ProgressFraction
107107
var overallFraction: ProgressFraction {
108108
var overallFraction = selfFraction
109-
for child in children {
110-
if !child.childFraction.isFinished {
111-
let multiplier = ProgressFraction(completed: child.portionOfTotal, total: selfFraction.total)
112-
if let additionalFraction = multiplier * child.childFraction {
113-
overallFraction = overallFraction + additionalFraction
109+
for childState in children {
110+
if let _ = childState.child {
111+
if !childState.childFraction.isFinished {
112+
let multiplier = ProgressFraction(completed: childState.portionOfTotal, total: selfFraction.total)
113+
if let additionalFraction = multiplier * childState.childFraction {
114+
overallFraction = overallFraction + additionalFraction
115+
}
114116
}
115117
}
116118
}

Sources/FoundationEssentials/ProgressManager/ProgressManager.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,9 @@ internal import _FoundationCollections
297297
}
298298

299299
deinit {
300-
if !isFinished {
301-
self.withProperties { properties in
302-
if let totalCount = properties.totalCount {
303-
properties.completedCount = totalCount
304-
}
305-
}
300+
if let totalCount = self.totalCount {
301+
let diff = totalCount - self.completedCount
302+
self.complete(count: diff)
306303
}
307304

308305
let (propertiesInt, propertiesDouble, propertiesString, propertiesURL, propertiesUInt64, parents) = state.withLock { state in

Tests/FoundationEssentialsTests/ProgressManager/ProgressManagerPropertiesTests.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,15 +425,19 @@ import Testing
425425
properties.fileURL = URL(string: "https://www.cats.com")
426426
}
427427

428-
let childManager = manager.subprogress(assigningCount: 1).start(totalCount: 2)
428+
var childManager: ProgressManager? = manager.subprogress(assigningCount: 1).start(totalCount: 2)
429429

430-
childManager.withProperties { properties in
430+
childManager?.withProperties { properties in
431431
properties.completedCount = 1
432432
properties.fileURL = URL(string: "https://www.kittens.com")
433433
}
434434

435435
#expect(manager.fractionCompleted == 0.75)
436436
#expect(manager.summary(of: ProgressManager.Properties.FileURL.self) == [URL(string: "https://www.cats.com"), URL(string: "https://www.kittens.com")])
437+
438+
childManager = nil
439+
#expect(manager.fractionCompleted == 1.0)
440+
#expect(manager.summary(of: ProgressManager.Properties.FileURL.self) == [URL(string: "https://www.cats.com")])
437441
}
438442
}
439443

0 commit comments

Comments
 (0)