Add testing of CSV lines parsing.

This commit is contained in:
Dominik Pantůček 2023-03-30 21:42:43 +02:00
parent 0d7a7f765c
commit e1b673a519
2 changed files with 11 additions and 4 deletions

View file

@ -80,11 +80,10 @@
(cons (string-append (car res) token) (cdr res))
2)))))))) ; Continue inside quoted data
;; Loads given CSV file and parses its lines into lists
(define (csv-parse fn . args)
;; Parses given CSV lines list
(define (csv-parse-lines lines . args)
(let* ((separator (get-keyword #:separator args (lambda () ";")))
(string-delimiter (get-keyword #:string-delimiter args (lambda () "\"")))
(lines (read-lines (open-input-file fn)))
(csv-parse-line (make-csv-line-parser separator string-delimiter))
(total (max (sub1 (length lines)) 1)))
(let loop ((lines lines)
@ -98,6 +97,11 @@
(add1 idx)
(cons (csv-parse-line line)
res)))))))
;; Loads given CSV file and parses its lines into lists
(define (csv-parse fn . args)
(let ((lines (read-lines (open-input-file fn))))
(apply csv-parse-lines lines args)))
;; Splits CSV into header and body based on the first empty row.
(define (csv-split-header csv)
@ -128,6 +132,9 @@
(test-equal? csv-split-header
(csv-split-header '((1 2) (5 6) (3 4)))
'(((1 2) (5 6) (3 4)) ()))
(test-equal? csv-parse-lines
(csv-parse-lines '("a;b;c" "1;2"))
'(("a" "b" "c") ("1" "2")))
))
)