Skip to content

Commit

Permalink
Set the target to data of the last stage
Browse files Browse the repository at this point in the history
  • Loading branch information
ra1028 committed Aug 20, 2018
1 parent e9c1e9a commit 0aa3d25
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Sources/Algorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
)
}

// Set the target to `data` of the last stage.
if !changesets.isEmpty {
let index = changesets.index(before: changesets.endIndex)
changesets[index].data = target
}

self.init(changesets)
}
}
Expand Down Expand Up @@ -363,6 +369,12 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
)
}

// Set the target to `data` of the last stage.
if !changesets.isEmpty {
let index = changesets.index(before: changesets.endIndex)
changesets[index].data = target
}

self.init(changesets)
}
}
Expand Down
23 changes: 23 additions & 0 deletions Tests/AlgorithmTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ final class AlgorithmTestCase: XCTestCase {}

/// Test algorithm for linear collection.
extension AlgorithmTestCase {
func testEmptyChangesets() {
let source = [Int]()
let target = [Int]()

XCTAssertExactDifferences(
source: source,
target: target,
section: 0,
expected: []
)
}

func testDeleted() {
let section = 1

Expand Down Expand Up @@ -227,6 +239,17 @@ extension AlgorithmTestCase {

/// Test algorithm for sectioned collection.
extension AlgorithmTestCase {
func testSectionedEmptyChangesets() {
let source = [Section<Int, Int>]()
let target = [Section<Int, Int>]()

XCTAssertExactDifferences(
source: source,
target: target,
expected: []
)
}

func testSectionInserted() {
let source1 = [
Section(model: D.a, elements: [0])
Expand Down

0 comments on commit 0aa3d25

Please sign in to comment.