Skip to content

Commit

Permalink
Add missing limit for changes #39
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Jan 31, 2024
1 parent 28d6802 commit c58f77f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 20 deletions.
7 changes: 6 additions & 1 deletion Sources/RangeState/RangeProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,12 @@ extension RangeProcessor {
}

public func didChangeContent(in range: NSRange, delta: Int) {
let mutation = RangeMutation(range: range, delta: delta)
let limit = (maximumProcessedLocation ?? 0) + delta

precondition(limit >= 0)
precondition(limit <= contentLength)

let mutation = RangeMutation(range: range, delta: delta, limit: limit)

didChangeContent(mutation)
}
Expand Down
51 changes: 51 additions & 0 deletions Tests/RangeStateTests/RangeProcessorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ import XCTest
import RangeState
import Rearrange

@MainActor
final class MockChangeHandler {
var mutations = [RangeMutation]()

var changeCompleted: @MainActor () -> Void = { }

func handleChange(_ mutation: RangeMutation, completion: @MainActor @escaping () -> Void) {
mutations.append(mutation)

changeCompleted()
completion()
}
}

final class RangeProcessorTests: XCTestCase {
@MainActor
func testSynchronousFill() {
Expand Down Expand Up @@ -51,4 +65,41 @@ final class RangeProcessorTests: XCTestCase {

XCTAssert(processor.processed(10))
}

@MainActor
func testInsertWithEverythingProcessed() {
let exp = expectation(description: "mutation")
exp.expectedFulfillmentCount = 2

let handler = MockChangeHandler()

handler.changeCompleted = {
exp.fulfill()
}

var content = StringContent(string: "abcde")

let processor = RangeProcessor(
configuration: .init(
lengthProvider: { content.currentLength },
changeHandler: handler.handleChange
)
)

XCTAssertTrue(processor.processLocation(5, mode: .required))
XCTAssertTrue(processor.processed(5))

// insert a character
content.string = "abcdef"
processor.didChangeContent(in: NSRange(5..<5), delta: 1)

wait(for: [exp], enforceOrder: true)

let expected = [
RangeMutation(range: NSRange(0..<0), delta: 5, limit: nil),
RangeMutation(range: NSRange(5..<5), delta: 1, limit: 6),
]

XCTAssertEqual(handler.mutations, expected)
}
}
19 changes: 0 additions & 19 deletions Tests/RangeStateTests/RangeValidatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,6 @@ import RangeState
final class RangeValidatorTests: XCTestCase {
typealias StringValidator = RangeValidator<StringContent>

struct StringContent: VersionedContent {
private var version: Int = 0
var string: String {
didSet { version += 1 }
}

init(string: String) {
self.string = string
}

var currentVersion: Int { version }

func length(for version: Version) -> Int? {
guard version == currentVersion else { return nil }

return string.utf16.count
}
}

@MainActor
func testContentChange() {
var content = StringContent(string: "abc")
Expand Down
23 changes: 23 additions & 0 deletions Tests/RangeStateTests/StringContent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Foundation

import RangeState

struct StringContent: VersionedContent {
private var version: Int = 0

var string: String {
didSet { version += 1 }
}

init(string: String) {
self.string = string
}

var currentVersion: Int { version }

func length(for version: Int) -> Int? {
guard version == currentVersion else { return nil }

return string.utf16.count
}
}

0 comments on commit c58f77f

Please sign in to comment.