Skip to content

Commit

Permalink
RangeMutation used for validSet transform shouldn't have limit #39
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Feb 1, 2024
1 parent c58f77f commit c2cc4f4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Sources/RangeState/RangeValidator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public final class RangeValidator<Content: VersionedContent> {
/// range = NSRange(1..<2)
/// delta = -1
public func contentChanged(in range: NSRange, delta: Int) {
let mutation = RangeMutation(range: range, delta: delta, limit: length)
let mutation = RangeMutation(range: range, delta: delta)

self.validSet = mutation.transform(set: validSet)

Expand Down
41 changes: 39 additions & 2 deletions Tests/RangeStateTests/RangeValidatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ final class RangeValidatorTests: XCTestCase {
typealias StringValidator = RangeValidator<StringContent>

@MainActor
func testContentChange() {
func testContentChange() async {
let validationExp = expectation(description: "validation")

var content = StringContent(string: "abc")

Check warning on line 12 in Tests/RangeStateTests/RangeValidatorTests.swift

View workflow job for this annotation

GitHub Actions / Test (platform=macOS)

variable 'content' was never mutated; consider changing to 'let' constant

Check warning on line 12 in Tests/RangeStateTests/RangeValidatorTests.swift

View workflow job for this annotation

GitHub Actions / Test (platform=iOS Simulator,name=iPhone 15)

variable 'content' was never mutated; consider changing to 'let' constant

Check warning on line 12 in Tests/RangeStateTests/RangeValidatorTests.swift

View workflow job for this annotation

GitHub Actions / Test (platform=tvOS Simulator,name=Apple TV)

variable 'content' was never mutated; consider changing to 'let' constant

Check warning on line 12 in Tests/RangeStateTests/RangeValidatorTests.swift

View workflow job for this annotation

GitHub Actions / Test (platform=visionOS Simulator,name=Apple Vision Pro)

variable 'content' was never mutated; consider changing to 'let' constant

Check warning on line 12 in Tests/RangeStateTests/RangeValidatorTests.swift

View workflow job for this annotation

GitHub Actions / Test (platform=watchOS Simulator,name=Apple Watch Series 6 (40mm))

variable 'content' was never mutated; consider changing to 'let' constant

Check warning on line 12 in Tests/RangeStateTests/RangeValidatorTests.swift

View workflow job for this annotation

GitHub Actions / Test (platform=macOS,variant=Mac Catalyst)

variable 'content' was never mutated; consider changing to 'let' constant
let provider = StringValidator.ValidationProvider(
syncValue: {
validationExp.fulfill()

return .success($0.value)
},
asyncValue: { contentRange, _ in
Expand All @@ -23,8 +27,41 @@ final class RangeValidatorTests: XCTestCase {
)
)

content.string = "abcd"
validator.validate(.all)

await fulfillment(of: [validationExp], timeout: 1.0)

content.string = "abcd"
validator.contentChanged(in: NSRange(3..<3), delta: 1)
}

@MainActor
func testContentDeleted() async {
let validationExp = expectation(description: "validation")

var content = StringContent(string: "abc")

Check warning on line 42 in Tests/RangeStateTests/RangeValidatorTests.swift

View workflow job for this annotation

GitHub Actions / Test (platform=macOS)

variable 'content' was never mutated; consider changing to 'let' constant

Check warning on line 42 in Tests/RangeStateTests/RangeValidatorTests.swift

View workflow job for this annotation

GitHub Actions / Test (platform=iOS Simulator,name=iPhone 15)

variable 'content' was never mutated; consider changing to 'let' constant

Check warning on line 42 in Tests/RangeStateTests/RangeValidatorTests.swift

View workflow job for this annotation

GitHub Actions / Test (platform=tvOS Simulator,name=Apple TV)

variable 'content' was never mutated; consider changing to 'let' constant

Check warning on line 42 in Tests/RangeStateTests/RangeValidatorTests.swift

View workflow job for this annotation

GitHub Actions / Test (platform=visionOS Simulator,name=Apple Vision Pro)

variable 'content' was never mutated; consider changing to 'let' constant

Check warning on line 42 in Tests/RangeStateTests/RangeValidatorTests.swift

View workflow job for this annotation

GitHub Actions / Test (platform=watchOS Simulator,name=Apple Watch Series 6 (40mm))

variable 'content' was never mutated; consider changing to 'let' constant

Check warning on line 42 in Tests/RangeStateTests/RangeValidatorTests.swift

View workflow job for this annotation

GitHub Actions / Test (platform=macOS,variant=Mac Catalyst)

variable 'content' was never mutated; consider changing to 'let' constant
let provider = StringValidator.ValidationProvider(
syncValue: {
validationExp.fulfill()

return .success($0.value)
},
asyncValue: { contentRange, _ in
return .success(contentRange.value)
})

let validator = StringValidator(
configuration: .init(
versionedContent: content,
validationProvider: provider
)
)

validator.validate(.all)

await fulfillment(of: [validationExp], timeout: 1.0)

content.string = "ac"
validator.contentChanged(in: NSRange(1..<2), delta: -1)
}
}
2 changes: 1 addition & 1 deletion Tests/RangeStateTests/StringContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation

import RangeState

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

var string: String {
Expand Down

0 comments on commit c2cc4f4

Please sign in to comment.