import Foundation import Testing @testable import DocsKit #if os(macOS) import AppKit #else import UIKit #endif @Test func nativeInputRulesMatchWorkingWebPatterns() { 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)] { let text = NSAttributedString(string: prefix + "\n") let result = DocumentInputRules.plan(in: text, range: NSRange(location: prefix.utf16.count, length: 0), replacement: " ") #expect(result?.kind == kind) #expect(result?.level == level) #expect(result?.start == 1) #expect(result?.removeLength == prefix.utf16.count) #expect(result?.prefix == prefix + " ") } } @Test func nativeInputRulesRespectTextblockOffsetsAndRestorationMetadata() { let style = NSMutableParagraphStyle(); style.alignment = .right let attrs = ["align": "right", "indent": "2"] let text = NSMutableAttributedString(string: "🧪 Before\n##\n") let location = ("🧪 Before\n" as NSString).length text.addAttributes([.docsBlockType: "paragraph", .docsBlockAttributes: try! JSONEncoder().encode(attrs), .paragraphStyle: style], range: NSRange(location: location, length: 3)) let result = DocumentInputRules.plan(in: text, range: NSRange(location: location + 2, length: 0), replacement: " ") #expect(result?.paragraphLocation == location) #expect(result?.removeLength == 2) #expect(result?.priorBlockType == "paragraph") #expect(result?.priorAttrs == attrs) #expect(result?.priorParagraphStyle.alignment == .right) } @Test func nativeInputRulesDoNotTransformCodeOrOrdinaryTyping() { for prefix in ["word-", "3.14", "####", " #", " 7.", "hello >", "", String(repeating: " ", count: 600) + "-"] { #expect(DocumentInputRules.plan(in: NSAttributedString(string: prefix + "\n"), range: NSRange(location: prefix.utf16.count, length: 0), replacement: " ") == nil) } let code = NSAttributedString(string: "-\n", attributes: [.docsBlockType: "code_block"]) #expect(DocumentInputRules.plan(in: code, range: NSRange(location: 1, length: 0), replacement: " ") == nil) let text = NSAttributedString(string: "-\n") for replacement in [" ", "- ", "x", "\n"] { #expect(DocumentInputRules.plan(in: text, range: NSRange(location: 1, length: 0), replacement: replacement) == nil) } #expect(DocumentInputRules.plan(in: text, range: NSRange(location: 0, length: 1), replacement: " ") == nil) #expect(DocumentInputRules.plan(in: text, range: NSRange(location: Int.max, length: 0), replacement: " ") == nil) }