58 lines
2.6 KiB
Swift
58 lines
2.6 KiB
Swift
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)
|
|
}
|
|
|
|
@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)
|
|
}
|
|
}
|