1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
|
(defpackage :cl-csv-test
(:use :cl :cl-user :cl-csv :lisp-unit2 :iter))
(in-package :cl-csv-test)
(cl-interpol:enable-interpol-syntax)
(defun run-all-tests ()
(run-tests
:package :cl-csv-test
:name :cl-csv
:run-contexts #'with-summary-context ))
(defmacro assert-length (exp it &rest them)
`(assert-eql ,exp (length ,it) ,@them))
(defparameter +test-csv-quoted-path+
(asdf:system-relative-pathname :cl-csv "tests/test-csv-quoted.csv"))
(defparameter +test-csv-unquoted-path+
(asdf:system-relative-pathname :cl-csv "tests/test-csv-unquoted.csv"))
(defparameter +test-csv-unquoted-no-trailing-path+
(asdf:system-relative-pathname :cl-csv "tests/test-csv-unquoted-no-trailing.csv"))
(defparameter +test-multiline+
(asdf:system-relative-pathname :cl-csv "tests/test-multiline-data.csv"))
(defparameter +test-backslash-escapes+
(asdf:system-relative-pathname :cl-csv "tests/test-backslash-escapes.csv"))
(defparameter +test-files+
(list
+test-csv-quoted-path+
+test-csv-unquoted-path+
+test-csv-unquoted-no-trailing-path+))
(defparameter *test-csv1-rows*
'(("first name" "last name" "job \"title\"" "number of hours" "id")
("Russ" "Tyndall" "Software Developer's, \"Position\"" "26.2" "1")
("Adam" "Smith" "Economist" "37.5" "2")
("John" "Doe" "Anonymous Human" "42.1" "3")
("Chuck" "Darwin" "Natural Philosipher" "17.68" "4")
("Bill" "Shakespear" "Bard" "12.2" "5")
("James" "Kirk" "Starship Captain" "13.1" "6")
("Bob" "Anon" "" "13.1" "6")
("Mr" "Iñtërnâtiônàlizætiøn" "" "1.1" "0")))
(defparameter *test-csv1*
"\"first name\",\"last name\",\"job \"\"title\"\"\",\"number of hours\",\"id\"
\"Russ\",\"Tyndall\",\"Software Developer's, \"\"Position\"\"\",\"26.2\",\"1\"
\"Adam\",\"Smith\",\"Economist\",\"37.5\",\"2\"
\"John\",\"Doe\",\"Anonymous Human\",\"42.1\",\"3\"
\"Chuck\",\"Darwin\",\"Natural Philosipher\",\"17.68\",\"4\"
\"Bill\",\"Shakespear\",\"Bard\",\"12.2\",\"5\"
\"James\",\"Kirk\",\"Starship Captain\",\"13.1\",\"6\"
\"Bob\",\"Anon\",\"\",\"13.1\",\"6\"
\"Mr\",\"Iñtërnâtiônàlizætiøn\",\"\",\"1.1\",\"0\"
")
(defparameter *test-csv1-v2*
"first name,last name,\"job \"\"title\"\"\",number of hours,id
Russ,Tyndall,\"Software Developer's, \"\"Position\"\"\",26.2,1
Adam,Smith,Economist,37.5,2
John,Doe,Anonymous Human,42.1,3
Chuck,Darwin,Natural Philosipher,17.68,4
Bill,Shakespear,Bard,12.2,5
James,Kirk,Starship Captain,13.1,6
Bob,Anon,,13.1,6
Mr,Iñtërnâtiônàlizætiøn,,1.1,0
")
(defparameter *test-csv-no-trailing-newline*
"first name,last name,\"job \"\"title\"\"\",number of hours,id
Russ,Tyndall,\"Software Developer's, \"\"Position\"\"\",26.2,1")
(defparameter *test-csv-data-with-newlines*
"first name,last name,\"job \"\"title\"\"\",number of hours,id
Russ,Tyndall,\"Software Developer's,
\"\"Position\"\"\",26.2,1")
(defparameter *test-csv-data-waiting-next-error*
"\"Which of the following is an appropriate calming technique or statement:
A. \"\"I can help you.\"\"
B. \"\"Shut up.\"\"
C. \"\"If you don't calm down I'm not sending anyone.\"\"
D. \"\"Ma'am, ma'am\ ma'am!\"\"\",A")
(define-test parsing-1 (:tags '(parsing))
(assert-equal *test-csv1-rows* (read-csv *test-csv1*))
(assert-equal *test-csv1-rows* (read-csv *test-csv1-v2*)))
(define-test writing-1 (:tags '(writing))
(assert-equal *test-csv1* (write-csv *test-csv1-rows* :always-quote t)))
(define-test parsing-errors (:tags '(parsing errors))
(assert-error 'csv-parse-error
(read-csv-row
"first name, a test\" broken quote, other stuff"))
(assert-error 'csv-parse-error
(read-csv-row
"first name,\"a test broken quote\" what are these chars, other stuff"))
(assert-error 'csv-parse-error
(read-csv-row
"first name,\"a test unfinished quote, other stuff"))
(assert-eql 3 (length (read-csv-row "first name, \"a test broken quote\", other stuff
")))
)
(define-test no-trailing-parse (:tags '(parsing errors))
(let* ((data (read-csv *test-csv-no-trailing-newline*))
(str (write-csv data :always-quote t))
(data2 (read-csv str)))
(assert-equal 2 (length data))
(assert-equal 5 (length (first data)))
(assert-equal 5 (length (second data)))
(assert-equal data data2)))
(define-test data-with-newlines (:tags '(whitespace parsing writing))
(let* ((data (read-csv *test-csv-data-with-newlines*))
(str (write-csv data :always-quote t))
(data2 (read-csv str)))
(assert-equal 2 (length data))
(assert-equal 5 (length (first data)))
(assert-equal 5 (length (second data)))
(assert-equal
"Software Developer's,
\"Position\""
(third (second data)))
(assert-equal data data2)))
(define-test data-with-whitespace-trim (:tags '(whitespace parsing trim))
(assert-equal
'("first" "last" " other " "" nil nil)
(read-csv-row " first , last , ' other ','',, "
:unquoted-empty-string-is-nil t
:quoted-empty-string-is-nil nil
:trim-outer-whitespace t
:quote #\'))
(assert-equal
'(" first " " last " " other " "" nil " ")
(read-csv-row " first , last ,' other ','',, "
:unquoted-empty-string-is-nil t
:quoted-empty-string-is-nil nil
:trim-outer-whitespace nil
:quote #\'))
(assert-error 'csv-parse-error
(read-csv-row " first , last , ' other ','',, "
:unquoted-empty-string-is-nil t
:quoted-empty-string-is-nil nil
:trim-outer-whitespace nil
:quote #\')
"whitespace before quoted values is a parse error if we are
not trimming ")
(assert-error 'csv-parse-error
(read-csv-row " first , last ,' other ' ,'',, "
:unquoted-empty-string-is-nil t
:quoted-empty-string-is-nil nil
:trim-outer-whitespace nil
:quote #\')
"whitespace after quoted values is a parse error if we are
not trimming ")
)
(define-test data-with-whitespace-nilling (:tags '(whitespace parsing trim))
(assert-equal
'("first" "last" " other " nil nil nil)
(read-csv-row " first , last , ' other ' ,'',, "
:quoted-empty-string-is-nil t
:unquoted-empty-string-is-nil t
:quote #\'))
(assert-equal
'("first" "last" " other " "" "" "")
(read-csv-row " first , last ,' other ','',, "
:quoted-empty-string-is-nil nil
:unquoted-empty-string-is-nil nil
:quote #\'))
(assert-equal
'("first" "last" " other " nil "" "")
(read-csv-row " first , last , ' other ','',, "
:quoted-empty-string-is-nil T
:unquoted-empty-string-is-nil nil
:quote #\')
"whitespace before quoted values is a parse error if we are
not trimming ")
(assert-equal
'("first" "last" " other " "" nil nil)
(read-csv-row " first , last ,' other ' ,'',, "
:quoted-empty-string-is-nil nil
:unquoted-empty-string-is-nil t
:quote #\')
"whitespace after quoted values is a parse error if we are
not trimming ")
)
(define-test files (:tags '(parsing files))
(iter (for csv in +test-files+)
(for data = (read-csv csv))
(assert-equal *test-csv1-rows* data csv)))
(define-test multi-line-file (:tags '(parsing files))
(let ((data (read-csv +test-multiline+)))
(assert-equal 2 (length data) data)
(assert-equal "test
of
multiline" (nth 3 (first data)) ))
)
(define-test dont-always-quote-and-newline (:tags '(writing whitespace quotation))
(let* ((row '("Russ" "Tyndall" "Software Developer's, \"Position\"" "26.2" "1" ","))
(res (write-csv-row row :always-quote nil :newline #?"\n")))
(assert-equal #?"Russ,Tyndall,\"Software Developer's, \"\"Position\"\"\",26.2,1,\",\"\n"
res)))
(define-test dont-always-quote-and-newline-2 (:tags '(writing whitespace quotation))
(let* ((row '("," #?"a\r\nnewline\r\ntest\r\n"))
(res (write-csv-row row :always-quote nil :newline #?"\n")))
(assert-equal #?"\",\",\"a\r\nnewline\r\ntest\r\n\"\n"
res)))
(define-test cause-error (:tags '(parsing errors))
(let ((data (read-csv *test-csv-data-waiting-next-error*)))
(assert-true data)))
(define-test chars-in-test (:tags '(utils parsing))
(assert-true (cl-csv::chars-in "a" "abcdef"))
(assert-false (cl-csv::chars-in "qu" "abcdef"))
(assert-true (cl-csv::chars-in "qu" "asdfqasdf"))
(assert-true (cl-csv::chars-in "qu" "asdfuasdf"))
(assert-true (cl-csv::chars-in (list "q" "u") "asdfuasdf"))
(assert-true (cl-csv::chars-in (list #\q #\u) "asdfuasdf"))
(assert-true (cl-csv::chars-in (list "q" #\u) "asdfqasdf")))
(define-test iterate-clauses (:tags '(utils iterate))
(iter
(for (a b c) in-csv "1,2,3
4,5,6")
(assert-equal (if (first-time-p) "1" "4") a)
(assert-equal (if (first-time-p) "2" "5") b)
(assert-equal (if (first-time-p) "3" "6") c)
(for i from 0)
(finally (assert-equal 1 i)))
;; test SKIPPING-HEADER option
(iter
(for (a b c) in-csv "1,2,3
4,5,6" SKIPPING-HEADER T)
(assert-equal "4" a)
(assert-equal "5" b)
(assert-equal "6" c)
(for i from 0)
(finally (assert-equal 0 i)))
;; test SEPARATOR
(iter
(for (a b c) in-csv "1|2|3
4|5|6" SKIPPING-HEADER T SEPARATOR #\|)
(assert-equal "4" a)
(assert-equal "5" b)
(assert-equal "6" c)
(for i from 0)
(finally (assert-equal 0 i))))
(define-test sampling-iterate (:tags '(parsing iterate))
(assert-length
9 (iter (for row in-csv *test-csv1*)
(cl-csv:sampling row)))
(assert-length
2 (iter (for row in-csv *test-csv1*)
(cl-csv:sampling row into sample size 2)
(finally (return sample))))
(assert-length
2 (read-csv-sample *test-csv1* 2))
(assert-length
3 (iter (for row in-csv *test-csv1* skipping-header t)
(cl-csv::sampling row size 3)))
(assert-length
9 (iter (for row in-csv *test-csv1*)
(cl-csv:sampling row into sample size 25)
(finally (return sample)))))
(define-test csv-signal-enabling (:tags '(signals))
(assert-signal
'csv-row-read
(assert-signal
'csv-data-read
(let ((*enable-signals* t))
(cl-csv:read-csv "1,2,3"))))
(assert-no-signal
'csv-row-read
(assert-no-signal
'csv-data-read
(let ((*enable-signals* nil))
(cl-csv:read-csv "1,2,3")))))
(define-test csv-filter (:tags '(signals))
(assert-equal
'(1 2 3)
(let ((*enable-signals* t))
(handler-bind ((csv-data-read
(lambda (c) (invoke-restart 'filter (parse-integer (cl-csv::data c))))))
(cl-csv:read-csv-row "1,2,3"))))
(assert-equal
'(1 2 3)
(let ((*enable-signals* t))
(handler-bind ((csv-row-read
(lambda (c) (invoke-restart 'filter (mapcar #'parse-integer (cl-csv::row c))))))
(cl-csv:read-csv-row "1,2,3")))))
(defun displaced-sub-string (s &key (start 0) (end (length s)))
(make-array (- end start)
:element-type (array-element-type s)
:displaced-to s
:displaced-index-offset start))
(define-test csv-continue-signals (:tags '(signals))
(handler-bind ((csv-parse-error #'continue))
(assert-equal
'(("1" "2" "3")
("3" "4" "5"))
(cl-csv:read-csv "1,2,3
2,3',4
3,4,5" :quote #\'))))
(define-test early-end-of-stream (:tags '(errors parsing))
(let ((line #?|"1","2|))
(assert-error
'cl-csv:csv-parse-error
(cl-csv:read-csv-row line)))
(let ((line ""))
(assert-error
'end-of-file
(cl-csv:read-csv-row line)))
(let ((line #?|"1","2|))
(assert-equal
'("1" "2")
(handler-bind ((cl-csv:csv-parse-error
(lambda (c)
(declare (ignore c))
(invoke-restart 'cl-csv::finish-item))))
(cl-csv:read-csv-row line))))
)
(define-test read-into-buffer-until-test (:tags '(read-until))
;; \r\l newline
(with-input-from-string (in #?"test this\r\n thing")
(let* ((s (make-string 80))
(l (cl-csv::read-into-buffer-until s in :nl #?"\r\n")))
(assert-eql 11 l)
(assert-equal #?"test this\r\n" (displaced-sub-string s :end l))))
;; newline
(with-input-from-string (in #?"t\nest this\n thing")
(let* ((s (make-string 80))
(l (cl-csv::read-into-buffer-until s in :nl #\newline)))
(assert-eql 2 l)
(assert-equal #?"t\n" (displaced-sub-string s :end l))
(let ((l (cl-csv::read-into-buffer-until s in :nl #\newline)))
(assert-eql 9 l)
(assert-equal #?"est this\n" (displaced-sub-string s :end l)))))
;; EOF
(with-input-from-string (in #?"test this thing")
(let* ((s (make-string 80))
(l (cl-csv::read-into-buffer-until s in :nl #\newline)))
(assert-eql (length "test this thing") l)
(assert-equal "test this thing" (displaced-sub-string s :end l))
))
;; filled buffer
(with-input-from-string (in #?"test this thing")
(let* ((s (make-string 4))
(l (cl-csv::read-into-buffer-until s in :nl #\newline)))
(assert-eql 4 l)
(assert-equal "test" (displaced-sub-string s :end l))
(assert-eql 4 (cl-csv::read-into-buffer-until s in :nl #\newline))
(assert-eql 4 (cl-csv::read-into-buffer-until s in :nl #\newline))
(assert-eql 3 (cl-csv::read-into-buffer-until s in :nl #\newline))
(assert-error 'end-of-file (cl-csv::read-into-buffer-until s in :nl #\newline))
)))
(define-test buffer-spanning-new-lines
(:tags '(read-until whitespace parsing))
(with-input-from-string (in "testRNtest")
(let* ((s (make-string 5))
len)
(setf len
(cl-csv::read-into-buffer-until s in :nl "RN"))
(assert-eql 5 len)
(setf len
(cl-csv::read-into-buffer-until
s in :nl "RN"
:nl-match 0))
(assert-eql 1 len )
(setf len
(cl-csv::read-into-buffer-until s in :nl "RN"))
(assert-eql 4 len))))
(define-test buffer-spanning-new-lines2
(:tags '(read-until newlines whitespace parsing))
;; ** newline
(with-input-from-string (in "test**tes**te**test")
(let* ((s (make-string 5))
len (nl-idx -1))
(flet ((rebind ( &optional new-nl-idx)
(when new-nl-idx
(setf nl-idx new-nl-idx))
(multiple-value-setq
(len)
(cl-csv::read-into-buffer-until
s in :nl "**"
:nl-match nl-idx))))
(rebind)
(assert-eql 5 len :first s)
(rebind 0)
(assert-eql 1 len :second)
(rebind -1)
(assert-eql 5 len :third)
(rebind)
(assert-eql 4 len :third))
)))
(define-test different-newlines (:tags '(read-until newlines whitespace parsing))
(with-input-from-string (in "a|b|c|d**1|2|3|4")
(let* ((cl-csv::*buffer-size* 8)
(rows (cl-csv:read-csv in :newline "**" :separator "|")))
(assert-equal 2 (length rows))
(assert-equal '("a" "b" "c" "d") (first rows))
(assert-equal '("1" "2" "3" "4") (second rows)))
)
(with-input-from-string (in "a|b|c|d**1|2|3|4")
(let ((rows (cl-csv:read-csv in :newline "**" :separator "|")))
(assert-equal 2 (length rows))
(assert-equal '("a" "b" "c" "d") (first rows))
(assert-equal '("1" "2" "3" "4") (second rows)))
)
(with-input-from-string (in "a|b|c|d*1|2|3|4")
(let ((rows (cl-csv:read-csv in :newline #\* :separator #\|)))
(assert-equal 2 (length rows))
(assert-equal '("a" "b" "c" "d") (first rows))
(assert-equal '("1" "2" "3" "4") (second rows)))
)
(with-input-from-string (in "a|b|c|d*1|2|3|4")
(let ((rows (cl-csv:read-csv in :newline "*" :separator "|")))
(assert-equal 2 (length rows))
(assert-equal '("a" "b" "c" "d") (first rows))
(assert-equal '("1" "2" "3" "4") (second rows)))
))
(define-test backslash-escapes (:tags '(backslash escapes parsing))
(lisp-unit2:assert-error
'csv-parse-error
(cl-csv:read-csv +test-backslash-escapes+ :escape #?|\"|))
(let ((results (cl-csv:read-csv
+test-backslash-escapes+
:escape #\\ :escape-mode :following )))
(assert-equal 2 (length results))
(assert-equal `("id","timestamp","date","comment","something") (first results))
))
|