Skip to content

Commit

Permalink
Remove equal periods duration after inserting period into TimePeriodC…
Browse files Browse the repository at this point in the history
…hain
  • Loading branch information
AsioOtus committed Mar 21, 2021
1 parent 8cfed3b commit 57592c0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Sources/SwiftDate/TimePeriod/Groups/TimePeriodChain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ open class TimePeriodChain: TimePeriodGroup {
//Shift all periods after inserted period
for i in 0..<periods.count {
if i > index && i > 0 {
let currentPeriod = TimePeriod(start: period.start, end: period.end)
let duration = periods[i].duration
periods[i].start = periods[i - 1].end
periods[i].end = periods[i].start!.addingTimeInterval(currentPeriod.duration)
periods[i].end = periods[i].start!.addingTimeInterval(duration)
}
}

Expand Down
21 changes: 19 additions & 2 deletions Tests/SwiftDateTests/TestTimePeriodChain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// Copyright © 2019 Daniele Margutti. Licensed under MIT License.
//

import SwiftDate
@testable import SwiftDate
import XCTest

class TestTimePeriodChain: XCTestCase {
Expand All @@ -25,5 +25,22 @@ class TestTimePeriodChain: XCTestCase {
XCTAssert(chain.start == periods.first!.start, "TimePeriodChain initial start not equals to source period start")
XCTAssert(chain.end == periods.last!.end, "TimePeriodChain initial end not equals to source period end")
}


func testInsert () {
let chain = TimePeriodChain(periods)
let period = TimePeriod(start: .init(year: 2020, month: 1, day: 1), end: .init(year: 2020, month: 1, day: 20))

let originalDuration_0 = chain.periods[0].duration
let originalDuration_1 = chain.periods[1].duration
let originalDuration_2 = chain.periods[2].duration
let originalDuration_3 = chain.periods[3].duration

chain.insert(period, at: 2)

XCTAssert(chain.periods[0].duration == originalDuration_0, "Unexpected duration of 0 period– Actual: \(chain.periods[0].duration) – Expected: \(originalDuration_0)")
XCTAssert(chain.periods[1].duration == originalDuration_1, "Unexpected duration of 1 period – Actual: \(chain.periods[1].duration) – Expected: \(originalDuration_1)")
XCTAssert(chain.periods[2].duration == period.duration, "Unexpected duration of 2 period – Actual: \(chain.periods[2].duration) – Expected: \(period.duration)")
XCTAssert(chain.periods[3].duration == originalDuration_2, "Unexpected duration of 3 period – Actual: \(chain.periods[3].duration) – Expected: \(originalDuration_2)")
XCTAssert(chain.periods[4].duration == originalDuration_3, "Unexpected duration of 4 period – Actual: \(chain.periods[4].duration) – Expected: \(originalDuration_3)")
}
}

0 comments on commit 57592c0

Please sign in to comment.