Start updating the member-file module and add tests.

This commit is contained in:
Dominik Pantůček 2023-03-13 20:35:03 +01:00
parent 1b00b2a020
commit f4e3419ad3
3 changed files with 30 additions and 6 deletions

View file

@ -39,4 +39,8 @@
(dictionary-tests!) (dictionary-tests!)
(month-tests!) (month-tests!)
(period-tests!) (period-tests!)
(member-file-tests!)
(newline)
(load-member-file "members/joe")
(newline) (newline)

View file

@ -27,6 +27,7 @@
member-file member-file
( (
load-member-file load-member-file
member-file-tests!
) )
(import scheme (import scheme
@ -35,10 +36,16 @@
(chicken io) (chicken io)
dictionary dictionary
month month
period) period
testing)
;; Specification of known keys for various types of parsing
(define known-keys '(nick mail phone name born joined))
(define start/stop-keys '(student suspend))
(define multi-keys '(card desfire credit))
;; Member File Parser: remove comments from line and return the result ;; Member File Parser: remove comments from line and return the result
(define (mfp:line:remove-comments l) (define (line-remove-comments l)
(let ((si (substring-index "#" l))) (let ((si (substring-index "#" l)))
(if si (if si
(if (= si 0) (if (= si 0)
@ -49,7 +56,7 @@
;; Parses given key-value line. Key is up to first space, value is the ;; Parses given key-value line. Key is up to first space, value is the
;; rest of the line. If the line doesn't contain anything, returns #f. ;; rest of the line. If the line doesn't contain anything, returns #f.
(define (parse-member-line l) (define (parse-member-line l)
(let ((sp (string-split (mfp:line:remove-comments l) " "))) (let ((sp (string-split (line-remove-comments l) " ")))
(and sp (and sp
(not (null? sp)) (not (null? sp))
(list (string->symbol (car sp)) (list (string->symbol (car sp))
@ -111,17 +118,19 @@
"2015-01")) "2015-01"))
;; Processes all lines and returns a dictionary representing given ;; Processes all lines and returns a dictionary representing given
;; member. ;; member.
(define (parse-member-lines ls) (define (parse-member-lines ls)
(let loop ((ls ls) (let loop ((ls ls)
(r (make-default-member-info))) (r (make-default-member-info))
(line-number 1))
(if (null? ls) (if (null? ls)
(convert-member-keys:markers->periods r 'suspend 'student) (convert-member-keys:markers->periods r 'suspend 'student)
(let ((p (parse-member-line (car ls)))) (let ((p (parse-member-line (car ls))))
(loop (cdr ls) (loop (cdr ls)
(if p (if p
(apply process-member-line r p) (apply process-member-line r p)
r)))))) r)
(+ line-number 1))))))
;; Loads lines from given file and parses them. ;; Loads lines from given file and parses them.
(define (load-member-file ffn) (define (load-member-file ffn)
@ -131,4 +140,14 @@
(display ".") (display ".")
md)) md))
;; Performs self-tests of the member-file module.
(define (member-file-tests!)
(run-tests
member-file
(test-equal? line-remove-comments (line-remove-comments "# all comment") "")
(test-equal? line-remove-comments (line-remove-comments "") "")
(test-equal? line-remove-comments (line-remove-comments "test # comment") "test ")
(test-equal? line-remove-comments (line-remove-comments "test") "test")
))
) )

View file

@ -125,6 +125,7 @@
(map period->string ps) (map period->string ps)
", ")) ", "))
;; Performs self-tests of the period module.
(define (period-tests!) (define (period-tests!)
(run-tests (run-tests
period period