Files
plak/Tests/PlakCoreTests/CodeBlockTests.swift
T
2026-10-17 17:33:13 +02:00

98 lines
3.7 KiB
Swift

import Foundation
import PlakCore
import Testing
@Suite struct CodeBlockTests {
private let zone = TimeZone(identifier: "Europe/Amsterdam")!
private func date(_ year: Int, _ month: Int, _ day: Int, _ hour: Int, _ minute: Int) -> Date {
var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = zone
return calendar.date(from: DateComponents(year: year, month: month, day: day, hour: hour, minute: minute))!
}
private func roundTrip(_ text: String) -> String {
PageFile(text: text, timeZone: zone).text(timeZone: zone)
}
@Test("fenced blocks with a date line, blank lines in the body and a longer fence read and write back byte for byte")
func roundTripExact() {
let text = """
<!-- 2026-10-16 08:55 -->
```php
<?php
echo 1;
```
under the code
````xml
```not a fence
````
```csv
a,b
"x, y",2
```
"""
#expect(roundTrip(text) == text)
}
@Test("a fenced block is one line of the page file, and the date line above it dates it")
func oneLine() {
let file = PageFile(text: "<!-- 2026-10-16 08:55 -->\n```json\n{\n\n}\n```\nnext\n", timeZone: zone)
#expect(file.lines == [.fenced(.json, "{\n\n}"), .text("next")])
#expect(file.dates == [0: date(2026, 10, 16, 8, 55)])
#expect(file.blocks == [0..<2])
}
@Test("the fence is longer than any run of backticks that starts a body line")
func longerFence() {
#expect(PageFile.fencedBlock(.php, "echo 1;") == "```php\necho 1;\n```")
#expect(PageFile.fencedBlock(.xml, "```x\n````") == "`````xml\n```x\n````\n`````")
#expect(PageFile(text: PageFile.fencedBlock(.xml, "```x\n````") + "\n").lines == [.fenced(.xml, "```x\n````")])
}
@Test("a CR or CRLF in a body counts as a line break for the fence length")
func lineEndings() {
#expect(PageFile.fencedBlock(.php, "x\r\n```y").hasPrefix("````php\n"))
#expect(PageFile.unixNewlines("a\r\nb\rc\n") == "a\nb\nc\n")
}
@Test("a fence whose body holds a date line after a blank line stays text, so it cannot swallow dated blocks")
func dateLineInBody() {
let text = "```php\nstray\n\n<!-- 2026-10-16 08:55 -->\nnext block\n```\n"
let file = PageFile(text: text, timeZone: zone)
#expect(file.lines.first == .text("```php"))
#expect(file.dates == [3: date(2026, 10, 16, 8, 55)])
#expect(file.text(timeZone: zone) == text)
let inside = "```php\na\n<!-- 2026-10-16 08:55 -->\n```\n"
#expect(PageFile(text: inside, timeZone: zone).lines == [.fenced(.php, "a\n<!-- 2026-10-16 08:55 -->")])
}
@Test("an unclosed fence, an unknown kind, an empty body or a fence longer than needed stays text")
func staysText() {
let texts = [
"```php\necho 1;\n",
"```ruby\nputs 1\n```\n",
"```PHP\necho 1;\n```\n",
"``` php\necho 1;\n```\n",
"```php\n```\n",
"````php\necho 1;\n````\n",
"```php\necho 1;\n```` \n",
]
for text in texts {
let file = PageFile(text: text)
#expect(!file.lines.contains { if case .fenced = $0 { return true } else { return false } }, "\(text)")
#expect(file.text() == text)
}
}
@Test("pasted lines turn fenced blocks and image lines into objects, and a date line stays text")
func pastedLines() {
let lines = PageFile.lines(pasted: "<!-- 2026-10-16 08:55 -->\n```csv\na,b\n```\n![](images/x.png)\n")
#expect(lines == [.text("<!-- 2026-10-16 08:55 -->"), .fenced(.csv, "a,b"), .image("images/x.png"), .text("")])
}
}