improves plak
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import PlakCore
|
||||
import Testing
|
||||
|
||||
@Suite struct CSVTableTests {
|
||||
@Test("quotes hold delimiters, doubled quotes and newlines; empty lines are skipped")
|
||||
func parse() {
|
||||
let text = "name,note\n\"Smith, J\",\"say \"\"hi\"\"\nthere\"\n\n\"\",x\n"
|
||||
let table = CSVTable(text: text)
|
||||
#expect(table.delimiter == ",")
|
||||
#expect(table.rows == [["name", "note"], ["Smith, J", "say \"hi\"\nthere"], ["", "x"]])
|
||||
}
|
||||
|
||||
@Test("an open quote is no table")
|
||||
func openQuote() {
|
||||
#expect(CSVTable.records("a,\"b\n1,2", delimiter: ",").closed == false)
|
||||
#expect(CSVTable.detect("a,\"b\n1,2") == nil)
|
||||
}
|
||||
|
||||
@Test("the delimiter is the one that gives every row the same count: semicolon over comma, tab first")
|
||||
func delimiter() {
|
||||
#expect(CSVTable(text: "1,5;2\n3,1;4").delimiter == ";")
|
||||
#expect(CSVTable(text: "a\tb,c\nd\te,f").delimiter == "\t")
|
||||
#expect(CSVTable(text: "a;b\n1;2").rows == [["a", "b"], ["1", "2"]])
|
||||
}
|
||||
|
||||
@Test("written back with its own delimiter, quoting only fields with a delimiter, a quote or a newline")
|
||||
func serialise() {
|
||||
let table = CSVTable(rows: [["a;b", "c"], ["say \"hi\"", "two\nlines"], ["plain", ""]], delimiter: ";")
|
||||
#expect(table.text() == "\"a;b\";c\n\"say \"\"hi\"\"\";\"two\nlines\"\nplain;")
|
||||
#expect(CSVTable(text: table.text()) == table)
|
||||
#expect(CSVTable(rows: [["a,b"]], delimiter: ";").text() == "\"a,b\"")
|
||||
#expect(CSVTable(rows: [["x"], [""]]).text() == "x\n\"\"")
|
||||
}
|
||||
|
||||
@Test("a value with any delimiter in it is quoted, so a table reads back with its own delimiter")
|
||||
func otherDelimitersQuoted() {
|
||||
let comma = CSVTable(rows: [["a;b", "c\td"], ["e", "f"]], delimiter: ",")
|
||||
#expect(comma.text() == "\"a;b\",\"c\td\"\ne,f")
|
||||
#expect(CSVTable(text: comma.text()) == comma)
|
||||
let column = CSVTable(rows: [["a;b"], ["c,d"]], delimiter: ",")
|
||||
#expect(column.text() == "\"a;b\"\n\"c,d\"")
|
||||
#expect(CSVTable(text: column.text()).rows == column.rows)
|
||||
}
|
||||
|
||||
@Test("rows and columns are added empty and removed; no cells left is an empty table")
|
||||
func edits() {
|
||||
var table = CSVTable(text: "a,b\n1,2")
|
||||
table.addColumn()
|
||||
table.addRow()
|
||||
table[2, 2] = "x"
|
||||
#expect(table.text() == "a,b,\n1,2,\n,,x")
|
||||
table.removeColumn(at: 0)
|
||||
table.removeRow(at: 0)
|
||||
#expect(table.rows == [["2", ""], ["", "x"]])
|
||||
table.removeColumn(at: 0)
|
||||
table.removeColumn(at: 0)
|
||||
#expect(table.isEmpty)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
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\n")
|
||||
#expect(lines == [.text("<!-- 2026-10-16 08:55 -->"), .fenced(.csv, "a,b"), .image("images/x.png"), .text("")])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import PlakCore
|
||||
import Testing
|
||||
|
||||
@Suite struct CodeHighlighterTests {
|
||||
/// The tokens of `body` as (kind, text) pairs.
|
||||
private func pieces(_ body: String, _ kind: FenceKind) -> [(CodeTokenKind, String)] {
|
||||
let units = Array(body.utf16)
|
||||
return CodeHighlighter.tokens(body, kind: kind).map { token in
|
||||
(token.kind, String(decoding: units[token.range], as: UTF16.self))
|
||||
}
|
||||
}
|
||||
|
||||
private func expect(_ body: String, _ kind: FenceKind, _ expected: [(CodeTokenKind, String)]) {
|
||||
let found = pieces(body, kind)
|
||||
#expect(found.map(\.0) == expected.map(\.0), "\(found)")
|
||||
#expect(found.map(\.1) == expected.map(\.1), "\(found)")
|
||||
}
|
||||
|
||||
@Test("PHP: tags, comments, variables, strings, numbers, keywords and a heredoc")
|
||||
func php() {
|
||||
let body = "<?php\n// note\n$total = \"a\\\"b\" . 12; # end\n/* block */ function f() { return <<<EOT\n hi $x\n EOT; }"
|
||||
expect(body, .php, [
|
||||
(.tag, "<?php"), (.comment, "// note"), (.variable, "$total"), (.string, "\"a\\\"b\""), (.number, "12"),
|
||||
(.comment, "# end"), (.comment, "/* block */"), (.keyword, "function"), (.keyword, "return"),
|
||||
(.string, "<<<EOT\n hi $x\n EOT"),
|
||||
])
|
||||
}
|
||||
|
||||
@Test("JSON: keys apart from string values, numbers and literals")
|
||||
func json() {
|
||||
expect(#"{"key": "value", "n": -1.5e3, "ok": true, "none": null}"#, .json, [
|
||||
(.attribute, "\"key\""), (.string, "\"value\""), (.attribute, "\"n\""), (.number, "-1.5e3"),
|
||||
(.attribute, "\"ok\""), (.keyword, "true"), (.attribute, "\"none\""), (.keyword, "null"),
|
||||
])
|
||||
}
|
||||
|
||||
@Test("XML: tags, attribute names and values, comments and CDATA")
|
||||
func xml() {
|
||||
expect(#"<!-- c --><a b='1' c:d="2"><![CDATA[x < y]]></a>"#, .xml, [
|
||||
(.comment, "<!-- c -->"), (.tag, "<a"), (.attribute, "b"), (.string, "'1'"), (.attribute, "c:d"),
|
||||
(.string, "\"2\""), (.tag, ">"), (.string, "<![CDATA[x < y]]>"), (.tag, "</a"), (.tag, ">"),
|
||||
])
|
||||
}
|
||||
|
||||
@Test("ranges count UTF-16 units, so text before a token can hold any character")
|
||||
func utf16() {
|
||||
let tokens = CodeHighlighter.tokens("{\"é😀\": 1}", kind: .json)
|
||||
#expect(tokens.map(\.range) == [1..<6, 8..<9])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import PlakCore
|
||||
import Testing
|
||||
|
||||
@Suite struct JSONFormatterTests {
|
||||
@Test("pretty-printing keeps the key order and the spelling of numbers and strings")
|
||||
func keepsOrder() {
|
||||
let pretty = JSONFormatter.pretty(#"{"z":1.50,"a":[2e3,-0.1],"m":{"q":"a, b: \"c\" {"}}"#)
|
||||
#expect(pretty == """
|
||||
{
|
||||
"z": 1.50,
|
||||
"a": [
|
||||
2e3,
|
||||
-0.1
|
||||
],
|
||||
"m": {
|
||||
"q": "a, b: \\"c\\" {"
|
||||
}
|
||||
}
|
||||
""")
|
||||
}
|
||||
|
||||
@Test("empty objects and arrays stay on one line, and spaces outside strings go")
|
||||
func empty() {
|
||||
#expect(JSONFormatter.pretty(#"[ {}, [ ], "a b" ]"#) == "[\n {},\n [],\n \"a b\"\n]")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import PlakCore
|
||||
import Testing
|
||||
|
||||
@Suite struct PasteKindTests {
|
||||
@Test("PHP starts with <?php or <?=, trimmed")
|
||||
func php() {
|
||||
#expect(PasteKind("\n <?php\necho 1;\n\n") == .object(.php, "<?php\necho 1;"))
|
||||
#expect(PasteKind("<?= $name ?>") == .object(.php, "<?= $name ?>"))
|
||||
}
|
||||
|
||||
@Test("JSON that parses is JSON; on one line it comes pretty-printed, on several lines as pasted")
|
||||
func json() {
|
||||
#expect(PasteKind(#"{"b":1,"a":[true,null]}"#) == .object(.json, "{\n \"b\": 1,\n \"a\": [\n true,\n null\n ]\n}"))
|
||||
#expect(PasteKind("[1,\n2]") == .object(.json, "[1,\n2]"))
|
||||
}
|
||||
|
||||
@Test("XML that parses is XML")
|
||||
func xml() {
|
||||
#expect(PasteKind(#"<a x="1"><b/></a>"#) == .object(.xml, #"<a x="1"><b/></a>"#))
|
||||
#expect(PasteKind("<?xml version=\"1.0\"?>\n<r/>") == .object(.xml, "<?xml version=\"1.0\"?>\n<r/>"))
|
||||
}
|
||||
|
||||
@Test("CSV needs two or more lines with the same field count, by comma, semicolon or tab")
|
||||
func csv() {
|
||||
#expect(PasteKind("a,b\n1,2\n") == .object(.csv, "a,b\n1,2"))
|
||||
#expect(PasteKind("a;b\n1;2") == .object(.csv, "a;b\n1;2"))
|
||||
#expect(PasteKind("a\tb\n1\t2") == .object(.csv, "a\tb\n1\t2"))
|
||||
}
|
||||
|
||||
@Test("tab-separated text keeps its edge tabs, so an empty corner or last cell still makes a table")
|
||||
func tsvEdges() {
|
||||
#expect(PasteKind("\n\tQ1\tQ2\nA\t1\t\n") == .object(.csv, "\tQ1\tQ2\nA\t1\t"))
|
||||
}
|
||||
|
||||
@Test("a sentence with commas, one CSV line, uneven lines, invalid JSON and broken XML stay text")
|
||||
func negatives() {
|
||||
#expect(PasteKind("Hello, world, this is a sentence.") == .text)
|
||||
#expect(PasteKind("a,b,c") == .text)
|
||||
#expect(PasteKind("a,b\n1,2,3") == .text)
|
||||
#expect(PasteKind("{\"a\": }") == .text)
|
||||
#expect(PasteKind("[1, 2") == .text)
|
||||
#expect(PasteKind("<b>bold</b> and more") == .text)
|
||||
#expect(PasteKind("just text") == .text)
|
||||
#expect(PasteKind(" \n ") == .text)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user