| 1 | import Foundation |
| 2 | import XCTest |
| 3 | @testable import DocsKit |
| 4 | |
| 5 | final class DocumentCompositionTests: XCTestCase { |
| 6 | @MainActor func testCommittedReplacementUsesScalarSafeUTF16() throws { |
| 7 | let (range, text) = try XCTUnwrap(DocumentComposition.replacement(before: "A😀draftZ", after: "A😀日本語Z")) |
| 8 | XCTAssertEqual(range, NSRange(location: 3, length: 5)) |
| 9 | XCTAssertEqual(text, "日本語") |
| 10 | XCTAssertNil(DocumentComposition.replacement(before: "😀", after: "😀")) |
| 11 | let changed = try XCTUnwrap(DocumentComposition.replacement(before: "😀", after: "😁")) |
| 12 | XCTAssertEqual(changed.0, NSRange(location: 0, length: 2)) |
| 13 | XCTAssertEqual(changed.1, "😁") |
| 14 | } |
| 15 | #if os(macOS) |
| 16 | @MainActor func testNativeMarkedTextCommitsOnceAgainstOriginalString() { |
| 17 | let view = DocsTextView(frame: .zero) |
| 18 | view.textStorage?.setAttributedString(NSAttributedString(string: "A😀Z")) |
| 19 | view.setSelectedRange(NSRange(location: 3, length: 0)) |
| 20 | var states: [Bool] = []; var commits: [(String, String)] = [] |
| 21 | view.compositionChanged = { states.append($0) } |
| 22 | view.compositionCommitted = { before, after in commits.append((before.string, after)); return true } |
| 23 | view.setMarkedText("に", selectedRange: NSRange(location: 1, length: 0), replacementRange: NSRange(location: NSNotFound, length: 0)) |
| 24 | view.setMarkedText("日本", selectedRange: NSRange(location: 2, length: 0), replacementRange: NSRange(location: NSNotFound, length: 0)) |
| 25 | XCTAssertTrue(view.isComposing) |
| 26 | XCTAssertEqual(commits.count, 0) |
| 27 | view.unmarkText() |
| 28 | XCTAssertEqual(states, [true, false]) |
| 29 | XCTAssertEqual(commits.count, 1) |
| 30 | XCTAssertEqual(commits.first?.0, "A😀Z") |
| 31 | XCTAssertEqual(commits.first?.1, "A😀日本Z") |
| 32 | } |
| 33 | #endif |
| 34 | } |
| 35 | |