updates plug with all sorts of awesome improvements

This commit is contained in:
2026-11-09 13:09:09 +01:00
parent a92d6f8871
commit 3afdb6d67f
553 changed files with 22780 additions and 13155 deletions
+34
View File
@@ -0,0 +1,34 @@
import Foundation
import PlakCore
import Testing
@Suite struct BookmarkTests {
@Test("a line that starts with = directly followed by a title is a bookmark, with the trimmed title and the range of the line")
func finds() {
let text = "=update on this\nsome text\n=second one \n" as NSString
#expect(Bookmark.find(in: text) == [
Bookmark(title: "update on this", range: NSRange(location: 0, length: 15)),
Bookmark(title: "second one", range: NSRange(location: 26, length: 13)),
])
}
@Test("a bookmark on the last line, without a newline, runs to the end of the text")
func lastLine() {
let text = "a\n\n=end" as NSString
#expect(Bookmark.find(in: text) == [Bookmark(title: "end", range: NSRange(location: 3, length: 4))])
}
@Test("= alone, = with a space before the title, an = inside a line and an indented = are no bookmarks")
func none() {
for text in ["=", "= ", "= \n", "= title", "=\ttitle", "a = b", " =title", "x\n=\n= \t\n"] {
#expect(Bookmark.find(in: text as NSString).isEmpty, "text: \(text)")
}
}
@Test("a line that starts with two = is no bookmark, so a == title == line is none")
func doubleEquals() {
for text in ["==twice", "== Contact ==", "== Contact, eerdere versie (entry 974, 1 okt 2026) ==\n=real"] {
#expect(Bookmark.find(in: text as NSString).map(\.title) == (text.hasSuffix("=real") ? ["real"] : []), "text: \(text)")
}
}
}
+48
View File
@@ -0,0 +1,48 @@
import Foundation
import PlakCore
import Testing
@Suite struct HelpTextTests {
private let page = PageFile(text: HelpText.text)
private func fenced(_ page: PageFile, _ kind: FenceKind) -> [String] {
page.lines.compactMap { line in
if case let .fenced(found, body) = line, found == kind { return body }
return nil
}
}
@Test("help.md loads and holds the json code block and the csv table as fenced blocks")
func fileLoads() throws {
#expect(LoremText.fileURL(named: "help.md") != nil)
#expect(!page.lines.isEmpty)
#expect(fenced(page, .json).count == 1)
#expect(fenced(page, .csv).count == 1)
#expect(!page.lines.contains { if case let .text(text) = $0 { text.hasPrefix("```") } else { false } })
}
@Test("the table has 11 rows of 2 columns, with ⌘. / ⌘, as one value")
func table() throws {
let body = try #require(fenced(page, .csv).first)
let table = CSVTable(text: body)
#expect(table.rows.count == 11)
#expect(table.rows.allSatisfy { $0.count == 2 })
#expect(table.rows.contains(["⌘. / ⌘,", "column wider / narrower"]))
}
@Test("the bookmarks include welcome to plak and shortcuts")
func bookmarks() {
let titles = Bookmark.find(in: HelpText.text as NSString).map(\.title)
#expect(titles.first == "welcome to plak")
#expect(titles.contains("shortcuts"))
}
@Test("the help text, every block dated, reads back unchanged from its page file text")
func roundTrip() {
var dated = page
let now = Date(timeIntervalSince1970: 1_793_000_040)
dated.dates = Dictionary(uniqueKeysWithValues: dated.blocks.map { ($0.lowerBound, now) })
#expect(dated.blocks.count > 1)
#expect(PageFile(text: dated.text()) == dated)
}
}
+164
View File
@@ -0,0 +1,164 @@
import Foundation
import PlakCore
import Testing
/// A repeatable generator, so a failing seed can be run again.
struct SeededGenerator: RandomNumberGenerator {
var state: UInt64
mutating func next() -> UInt64 {
state &+= 0x9E37_79B9_7F4A_7C15
var z = state
z = (z ^ (z >> 30)) &* 0xBF58_476D_1CE4_E5B9
z = (z ^ (z >> 27)) &* 0x94D0_49BB_1331_11EB
return z ^ (z >> 31)
}
}
@Suite struct LoremTextTests {
private func picks(_ count: Int = 200) throws -> [LoremText] {
let sentences = LoremText.sentences
return try (0..<UInt64(count)).map { seed in
var generator = SeededGenerator(state: seed)
return try #require(LoremText.pick(from: sentences, using: &generator), "seed: \(seed)")
}
}
@Test("lorem.txt loads from the resource bundle with at least 200 lines, some of exactly 16 words")
func fileLoads() {
#expect(LoremText.fileURL() != nil)
#expect(LoremText.sentences.count >= 200)
#expect(LoremText.sentences.contains { LoremText.wordCount($0) == 16 })
}
@Test("the opening sentence has exactly 16 words")
func openingWords() throws {
for lorem in try picks() {
#expect(LoremText.wordCount(lorem.opening) == 16)
}
}
@Test("there are two paragraphs of 3 to 5 sentences each")
func paragraphSizes() throws {
for lorem in try picks() {
#expect(lorem.paragraphs.count == 2)
for paragraph in lorem.paragraphs {
#expect((3...5).contains(paragraph.count))
}
}
}
@Test("no line of the file shows twice in one insertion")
func noRepeats() throws {
for lorem in try picks() {
let all = [lorem.opening] + lorem.paragraphs.flatMap { $0 }
#expect(Set(all).count == all.count)
#expect(all.allSatisfy(LoremText.sentences.contains))
}
}
@Test("the text is six blocks: sentence, two paragraphs, phones, addresses, e-mails, each after one blank line")
func layout() throws {
for lorem in try picks(50) {
let parts = lorem.text.components(separatedBy: "\n\n")
#expect(parts.count == 6)
#expect(!lorem.text.contains("\n\n\n"))
#expect(parts[0] == lorem.opening)
#expect(parts[1] == lorem.paragraphs[0].joined(separator: " "))
#expect(parts[2] == lorem.paragraphs[1].joined(separator: " "))
#expect(!parts[1].contains("\n") && !parts[2].contains("\n"))
#expect(parts[3] == lorem.contacts.phones.joined(separator: "\n"))
#expect(parts[4] == lorem.contacts.addresses.joined(separator: "\n"))
#expect(parts[5] == lorem.contacts.emails.joined(separator: "\n"))
}
}
@Test("contacts.json loads with streets, cities, first names and last names")
func namesLoad() {
let names = ContactNames.bundled
#expect(names.streets.count >= 50)
#expect(names.cities.count >= 30)
#expect(names.firstNames.count >= 50)
#expect(names.lastNames.count >= 50)
}
@Test("each contact group has 4 items and no duplicates")
func contactCounts() throws {
for lorem in try picks() {
for group in [lorem.contacts.phones, lorem.contacts.addresses, lorem.contacts.emails] {
#expect(group.count == 4)
#expect(Set(group).count == group.count)
}
}
}
@Test("phones are Dutch numbers, mobile and landline mixed")
func phones() throws {
let mobile = /^(06-[1-9]\d{7}|\+31 6 [1-9]\d{7})$/
let landline = /^(0[1-9]\d-[1-9]\d{6}|0[1-9]\d{2}-[1-9]\d{5})$/
for lorem in try picks() {
for phone in lorem.contacts.phones {
#expect(phone.wholeMatch(of: mobile) != nil || phone.wholeMatch(of: landline) != nil, "\(phone)")
}
#expect(lorem.contacts.phones.contains { $0.wholeMatch(of: mobile) != nil })
#expect(lorem.contacts.phones.contains { $0.wholeMatch(of: landline) != nil })
}
}
@Test("addresses are one line: street, number, postcode without SS, SD or SA, city")
func addresses() throws {
let pattern = /^.+ (\d{1,3})[a-z]?, (\d{4}) ([A-Z]{2}) .+$/
for lorem in try picks() {
for address in lorem.contacts.addresses {
let match = try #require(address.wholeMatch(of: pattern), "\(address)")
#expect((1...250).contains(Int(match.output.1) ?? 0))
#expect((1000...9999).contains(Int(match.output.2) ?? 0))
#expect(!["SS", "SD", "SA"].contains(String(match.output.3)))
}
}
}
@Test("e-mails are lower case ASCII on a reserved example domain")
func emails() throws {
let pattern = /^[a-z]+\.[a-z]+@example\.(nl|com|org)$/
for lorem in try picks() {
for email in lorem.contacts.emails {
#expect(email.wholeMatch(of: pattern) != nil, "\(email)")
}
}
}
@Test("a name with a tussenvoegsel or an accent becomes plain letters in an e-mail")
func mailPart() throws {
let one = ContactNames(streets: ["Kerkstraat"], cities: ["Gouda"], firstNames: ["Zoë"], lastNames: ["van den Berg"])
var generator = SeededGenerator(state: 3)
#expect(ContactSample.pick(from: one, using: &generator) == nil)
let two = ContactNames(streets: ["Kerkstraat"], cities: ["Gouda"], firstNames: ["Zoë", "René"], lastNames: ["van den Berg", "de Wit"])
let sample = try #require(ContactSample.pick(from: two, using: &generator))
let expected: Set = ["zoe.vandenberg", "zoe.dewit", "rene.vandenberg", "rene.dewit"]
#expect(sample.emails.allSatisfy { expected.contains($0.components(separatedBy: "@")[0]) })
}
@Test("the same seed gives the same pick; another seed gives another one")
func injectedGenerator() {
var first = SeededGenerator(state: 7)
var again = SeededGenerator(state: 7)
let sentences = LoremText.sentences
#expect(LoremText.pick(from: sentences, using: &first) == LoremText.pick(from: sentences, using: &again))
let others = (100..<110).map { seed -> LoremText? in
var generator = SeededGenerator(state: UInt64(seed))
return LoremText.pick(from: sentences, using: &generator)
}
#expect(Set(others.compactMap { $0?.text }).count > 1)
}
@Test("without a line of 16 words, or with too few lines, there is no pick")
func notEnough() {
let short = (1...20).map { "w\($0) two three." }
let words16 = Array(repeating: "w", count: 16).joined(separator: " ")
var generator = SeededGenerator(state: 1)
#expect(LoremText.pick(from: short, using: &generator) == nil)
#expect(LoremText.pick(from: [words16, "a.", "b."], using: &generator) == nil)
#expect(LoremText.pick(from: [words16] + short, using: &generator) != nil)
}
}
+28
View File
@@ -0,0 +1,28 @@
import Foundation
import PlakCore
import Testing
@Suite struct PageLinkTests {
private let text = "see https://plak.app/a?b=1 and\nmail jo@example.com\nplain words" as NSString
@Test("an index inside a web address gives the address and its range")
func web() throws {
let link = try #require(PageLink.link(in: text, at: 10))
#expect(link.url.absoluteString == "https://plak.app/a?b=1")
#expect(link.range == NSRange(location: 4, length: 22))
}
@Test("a mail address on a later line is a mailto link")
func mail() throws {
let link = try #require(PageLink.link(in: text, at: 40))
#expect(link.url.absoluteString == "mailto:jo@example.com")
}
@Test("an index outside an address, or out of the text, gives no link")
func none() {
#expect(PageLink.link(in: text, at: 1) == nil)
#expect(PageLink.link(in: text, at: text.length - 2) == nil)
#expect(PageLink.link(in: text, at: text.length) == nil)
#expect(PageLink.link(in: text, at: -1) == nil)
}
}
+11
View File
@@ -43,4 +43,15 @@ import Testing
#expect(PasteKind("just text") == .text)
#expect(PasteKind(" \n ") == .text)
}
@Test("nginx config and CSS split evenly on semicolons but hold braces, so they stay text")
func bracesStayText() {
let nginx = """
location = /favicon.svg { try_files $uri /index.php?$query_string; access_log off; log_not_found off; }
location = /favicon.ico { try_files $uri /index.php?$query_string; access_log off; log_not_found off; }
location = /robots.txt { try_files $uri /index.php?$query_string; access_log off; log_not_found off; }
"""
#expect(PasteKind(nginx) == .text)
#expect(PasteKind("a { color: red; margin: 0; }\nb { color: blue; margin: 1; }") == .text)
}
}
+39
View File
@@ -0,0 +1,39 @@
import Foundation
import PlakCore
import Testing
@Suite struct URLCommandTests {
private func command(_ string: String) throws -> URLCommand? {
URLCommand(try #require(URL(string: string)))
}
@Test("append takes the percent-decoded text; a + stays a +")
func append() throws {
#expect(try command("plak://append?text=hello%20world") == .append("hello world"))
#expect(try command("plak://append?text=1+1") == .append("1+1"))
#expect(try command("PLAK://append?text=a") == .append("a"))
}
@Test("append keeps newlines and unicode")
func appendMultiline() throws {
#expect(try command("plak://append?text=one%0Atwo%0A%0Ad%C3%A9j%C3%A0%20%F0%9F%93%8B") == .append("one\ntwo\n\ndéjà 📋"))
}
@Test("append without text, or with an empty one, appends the empty text")
func appendWithoutText() throws {
#expect(try command("plak://append") == .append(""))
#expect(try command("plak://append?text=") == .append(""))
}
@Test("paste appends the pasteboard")
func paste() throws {
#expect(try command("plak://paste") == .paste)
}
@Test("an unknown host or another scheme is no command")
func unknown() throws {
#expect(try command("plak://delete") == nil)
#expect(try command("plak:") == nil)
#expect(try command("https://append?text=a") == nil)
}
}
@@ -0,0 +1,14 @@
import Foundation
import PlakCore
import Testing
@Suite struct WelcomeTextTests {
@Test("the welcome page is the one welcome line in one block, created at the given date")
func page() {
let now = Date(timeIntervalSince1970: 1_793_000_000)
let page = WelcomeText.page(createdAt: now)
#expect(page.lines == [.text("Welcome to plak. For the docs, the tricks and the shortcuts, choose Plak Help in the menu bar icon.")])
#expect(page.blocks.count == 1)
#expect(page.dates == [0: now])
}
}