Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/CoreTests/MarkdownEditingTests.swift3// Description: The note editor's formatting commands as pure text edits: wrap, unwrap, caret placement, line prefixes.4import Foundation5import Testing6@testable import VQuantCore78@Suite struct MarkdownEditingTests {9 func apply(_ e: MarkdownEditing.Edit, to text: String) -> (String, NSRange) { ((text as NSString).replacingCharacters(in: e.range, with: e.replacement), e.selection) }1011 @Test func boldWrapsAndUnwraps() {12 let text = "Margins expand fast"13 let (bold, sel) = apply(MarkdownEditing.toggle("**", in: text, selection: NSRange(location: 8, length: 6)), to: text)14 #expect(bold == "Margins **expand** fast" && sel == NSRange(location: 10, length: 6))15 // the selection is inside the markers: the same command removes them16 let (plain, sel2) = apply(MarkdownEditing.toggle("**", in: bold, selection: sel), to: bold)17 #expect(plain == text && sel2 == NSRange(location: 8, length: 6))18 // the selection includes the markers19 let (plain2, sel3) = apply(MarkdownEditing.toggle("**", in: bold, selection: NSRange(location: 8, length: 10)), to: bold)20 #expect(plain2 == text && sel3 == NSRange(location: 8, length: 6))21 }2223 @Test func emptySelectionGetsCaretBetweenMarkers() {24 let (t, sel) = apply(MarkdownEditing.toggle("*", in: "ab", selection: NSRange(location: 1, length: 0)), to: "ab")25 #expect(t == "a**b" && sel == NSRange(location: 2, length: 0))26 // out-of-range selections are treated as the end of the text27 let (u, _) = apply(MarkdownEditing.toggle("`", in: "x", selection: NSRange(location: 9, length: 3)), to: "x")28 #expect(u == "x``")29 }3031 @Test func italicDoesNotEatBold() {32 let bold = "**x**"33 let (t, _) = apply(MarkdownEditing.toggle("*", in: bold, selection: NSRange(location: 2, length: 1)), to: bold)34 #expect(t == "***x***")35 let (u, _) = apply(MarkdownEditing.toggle("*", in: "é *x*", selection: NSRange(location: 3, length: 1)), to: "é *x*")36 #expect(u == "é x")37 }3839 @Test func linePrefixes() {40 let text = "one\ntwo\nthree"41 let (list, sel) = apply(MarkdownEditing.togglePrefix("- ", in: text, selection: NSRange(location: 1, length: 5)), to: text)42 #expect(list == "- one\n- two\nthree" && sel == NSRange(location: 0, length: 11))43 let (back, _) = apply(MarkdownEditing.togglePrefix("- ", in: list, selection: sel), to: list)44 #expect(back == text)45 let (h, hs) = apply(MarkdownEditing.togglePrefix("# ", in: text, selection: NSRange(location: 5, length: 0)), to: text)46 #expect(h == "one\n# two\nthree" && hs == NSRange(location: 7, length: 0))47 }48}49