Skip to content

Commit

Permalink
transform wasn't correctly enforcing invariants
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Apr 10, 2020
1 parent 2d20cb2 commit 17807e8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.build
/Packages
/Carthage
/.swiftpm
20 changes: 12 additions & 8 deletions Rearrange/RangeMutation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public struct RangeMutation {

extension RangeMutation {
public func transform(location: Int) -> Int? {
if let l = presetLimit {
precondition(location <= l)
}

if range.location > location {
return location
}
Expand All @@ -54,19 +58,19 @@ extension RangeMutation {
return location
}

if range.max <= location {
let result = location + delta
if range.max > location {
return nil
}

precondition(result >= 0)
let result = location + delta

if let l = presetLimit {
precondition(result <= l)
}
precondition(result >= 0)

return result
if presetLimit != nil {
precondition(result <= postApplyLimit)
}

return nil
return result
}
}

Expand Down
4 changes: 2 additions & 2 deletions Rearrange/Rearrange.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
PRODUCT_NAME = Rearrange
PRODUCT_BUNDLE_IDENTIFIER = com.chimehq.Rearrange
PRODUCT_MODULE_NAME = Rearrange
CURRENT_PROJECT_VERSION = 5
MARKETING_VERSION = 1.1.2
CURRENT_PROJECT_VERSION = 6
MARKETING_VERSION = 1.1.3

INFOPLIST_FILE = Rearrange/Info.plist
MACH_O_TYPE = staticlib
Expand Down
10 changes: 8 additions & 2 deletions RearrangeTests/RangeMutationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class RangeMutationTests: XCTestCase {
XCTAssertEqual(change.transform(location: 14), 15)
}

func testMutationAtLimitLocation() {
let change = RangeMutation(range: NSRange(5..<5), delta: 1, limit: 5)

XCTAssertEqual(change.transform(location: 5), 6)
}

func testMutationAfterLocation() {
let change = RangeMutation(range: NSMakeRange(11, 1), delta: 1, limit: 15)

Expand Down Expand Up @@ -111,7 +117,7 @@ class RangeMutationTests: XCTestCase {
}

func testMutationAtBeginning() {
let change = RangeMutation(range: NSMakeRange(5, 1), delta: -1, limit: 11)
let change = RangeMutation(range: NSMakeRange(5, 1), delta: -1, limit: 12)

XCTAssertEqual(change.transform(range: NSMakeRange(10, 2)), NSMakeRange(9, 2))
}
Expand All @@ -123,7 +129,7 @@ class RangeMutationTests: XCTestCase {
}

func testMutationDecreasesLengthOfRange() {
let change = RangeMutation(range: NSMakeRange(5, 1), delta: -1, limit: 9)
let change = RangeMutation(range: NSMakeRange(5, 1), delta: -1, limit: 10)

XCTAssertEqual(change.transform(range: NSMakeRange(0, 10)), NSMakeRange(0, 9))
}
Expand Down

0 comments on commit 17807e8

Please sign in to comment.