From 8eaf6d6878a20495edd6052c0c4d064b56c60cf9 Mon Sep 17 00:00:00 2001 From: Khan Winter <35942988+thecoolwinter@users.noreply.github.com> Date: Sat, 25 May 2024 10:55:48 -0500 Subject: [PATCH] Add Newline String Option --- .../NewlineProcessingFilter.swift | 4 +- .../NewlineProcessingFilterTests.swift | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/Sources/TextFormation/NewlineProcessingFilter.swift b/Sources/TextFormation/NewlineProcessingFilter.swift index 1626bee..1403e5a 100644 --- a/Sources/TextFormation/NewlineProcessingFilter.swift +++ b/Sources/TextFormation/NewlineProcessingFilter.swift @@ -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 { diff --git a/Tests/TextFormationTests/NewlineProcessingFilterTests.swift b/Tests/TextFormationTests/NewlineProcessingFilterTests.swift index 4b11eaf..8e9189c 100644 --- a/Tests/TextFormationTests/NewlineProcessingFilterTests.swift +++ b/Tests/TextFormationTests/NewlineProcessingFilterTests.swift @@ -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) + } }