Skip to content

Commit

Permalink
Merge pull request #8 from thecoolwinter/feat/any-newline-char
Browse files Browse the repository at this point in the history
Add Newline String Option
  • Loading branch information
mattmassicotte authored May 26, 2024
2 parents 284f403 + 8eaf6d6 commit 453bc35
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sources/TextFormation/NewlineProcessingFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import TextStory
public class NewlineProcessingFilter {
private let recognizer: ConsecutiveCharacterRecognizer

public init() {
self.recognizer = ConsecutiveCharacterRecognizer(matching: "\n")
public init(newline: String = "\n") {
self.recognizer = ConsecutiveCharacterRecognizer(matching: newline)
}

private func filterHandler(_ mutation: TextMutation, in interface: TextInterface, with providers: WhitespaceProviders) -> FilterAction {
Expand Down
37 changes: 37 additions & 0 deletions Tests/TextFormationTests/NewlineProcessingFilterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,41 @@ final class NewlineProcessingFilterTests: XCTestCase {
XCTAssertEqual(interface.string, "\t\n\t\n\t")
XCTAssertEqual(interface.insertionLocation, 5)
}

func testMatchingWithUnknownNewline() {
let interface = TextInterfaceAdapter("")
let filter = NewlineProcessingFilter(newline: "crlf")

let mutation = TextMutation(insert: "crlf", at: 0, limit: 0)

XCTAssertEqual(filter.processMutation(mutation, in: interface, with: Self.providers), .discard)

XCTAssertEqual(interface.string, "crlf\t")
XCTAssertEqual(interface.insertionLocation, 5)
}

func testMatchingWithUnknownNewlineAndTrailingWhitespace() {
let interface = TextInterfaceAdapter(" ")
let filter = NewlineProcessingFilter(newline: "crlf")

let mutation = TextMutation(insert: "crlf", at: 1, limit: 1)

XCTAssertEqual(interface.insertionLocation, 1)
XCTAssertEqual(filter.processMutation(mutation, in: interface, with: Self.providers), .discard)

XCTAssertEqual(interface.string, " crlf\t")
XCTAssertEqual(interface.insertionLocation, 6)
}

func testMatchingWithUnknownNewlineAndTrailingTab() {
let interface = TextInterfaceAdapter("abc\t")
let filter = NewlineProcessingFilter(newline: "crlf")

let mutation = TextMutation(insert: "crlf", at: 4, limit: 4)

XCTAssertEqual(filter.processMutation(mutation, in: interface, with: Self.providers), .discard)

XCTAssertEqual(interface.string, "abc crlf\t")
XCTAssertEqual(interface.insertionLocation, 9)
}
}

0 comments on commit 453bc35

Please sign in to comment.