| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import DocsKit |
| 4 | #if os(macOS) |
| 5 | import AppKit |
| 6 | #else |
| 7 | import UIKit |
| 8 | #endif |
| 9 | |
| 10 | @Test func nativeInputRulesMatchWorkingWebPatterns() { |
| 11 | for (prefix, kind, level) in [("-", "bullet_list", 0), (" +", "bullet_list", 0), ("*", "bullet_list", 0), ("7.", "ordered_list", 0), (">", "blockquote", 0), ("\t>", "blockquote", 0), ("#", "heading", 1), ("###", "heading", 3)] { |
| 12 | let text = NSAttributedString(string: prefix + "\n") |
| 13 | let result = DocumentInputRules.plan(in: text, range: NSRange(location: prefix.utf16.count, length: 0), replacement: " ") |
| 14 | #expect(result?.kind == kind) |
| 15 | #expect(result?.level == level) |
| 16 | #expect(result?.start == 1) |
| 17 | #expect(result?.removeLength == prefix.utf16.count) |
| 18 | #expect(result?.prefix == prefix + " ") |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | @Test func nativeInputRulesRespectTextblockOffsetsAndRestorationMetadata() { |
| 23 | let style = NSMutableParagraphStyle(); style.alignment = .right |
| 24 | let attrs = ["align": "right", "indent": "2"] |
| 25 | let text = NSMutableAttributedString(string: "🧪 Before\n##\n") |
| 26 | let location = ("🧪 Before\n" as NSString).length |
| 27 | text.addAttributes([.docsBlockType: "paragraph", .docsBlockAttributes: try! JSONEncoder().encode(attrs), .paragraphStyle: style], range: NSRange(location: location, length: 3)) |
| 28 | let result = DocumentInputRules.plan(in: text, range: NSRange(location: location + 2, length: 0), replacement: " ") |
| 29 | #expect(result?.paragraphLocation == location) |
| 30 | #expect(result?.removeLength == 2) |
| 31 | #expect(result?.priorBlockType == "paragraph") |
| 32 | #expect(result?.priorAttrs == attrs) |
| 33 | #expect(result?.priorParagraphStyle.alignment == .right) |
| 34 | } |
| 35 | |
| 36 | @Test func nativeInputRulesDoNotTransformCodeOrOrdinaryTyping() { |
| 37 | for prefix in ["word-", "3.14", "####", " #", " 7.", "hello >", "", String(repeating: " ", count: 600) + "-"] { |
| 38 | #expect(DocumentInputRules.plan(in: NSAttributedString(string: prefix + "\n"), range: NSRange(location: prefix.utf16.count, length: 0), replacement: " ") == nil) |
| 39 | } |
| 40 | let code = NSAttributedString(string: "-\n", attributes: [.docsBlockType: "code_block"]) |
| 41 | #expect(DocumentInputRules.plan(in: code, range: NSRange(location: 1, length: 0), replacement: " ") == nil) |
| 42 | let text = NSAttributedString(string: "-\n") |
| 43 | for replacement in [" ", "- ", "x", "\n"] { |
| 44 | #expect(DocumentInputRules.plan(in: text, range: NSRange(location: 1, length: 0), replacement: replacement) == nil) |
| 45 | } |
| 46 | #expect(DocumentInputRules.plan(in: text, range: NSRange(location: 0, length: 1), replacement: " ") == nil) |
| 47 | #expect(DocumentInputRules.plan(in: text, range: NSRange(location: Int.max, length: 0), replacement: " ") == nil) |
| 48 | } |
| 49 | |