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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user