
;;(test-group
;; "make-ini-file-accumulator"

 (define res
   (let* ((port (open-output-string))
          (acc (make-ini-file-accumulator port)))

     ;; write leading section-less data
     (acc '(|| key1 "value1"))

     ;; write comment
     (acc "test comment")

     ;; write new section
     (acc '(section key2 "value2"))

     (get-output-string port)))

(test "srfi-233.1"
      "key1=value1\n; test comment\n[section]\nkey2=value2\n"
      res)

;; (test-group
;;  "make-ini-file-generator"

(define (read-to-list generator)
  (let loop ((lst '()))
    (define entry-lst (generator))
    (cond
     ((eof-object? entry-lst)
      (reverse lst))
     (else (loop (cons entry-lst lst))))))

(define source "key1 = value1\n
; comment\n
\n
[section]\n
 key2 = value2\n
[section2]\n
key3\n
[key]4\n
\n
\n")

(define res (read-to-list (make-ini-file-generator (open-input-string source))))

(test "srfi-233.2"
      '((|| key1 "value1")
        (section key2 "value2")
        (section2 #f "key3")
        (section2 #f "[key]4"))
res)


;; (test-group
;;  "make-ini-file-accumulator custom delimeters"

(define res
  (let* ((port (open-output-string))
         (acc (make-ini-file-accumulator port #\- #\#)))

    ;; write leading section-less data
    (acc '(|| key1 "value1"))

    ;; write comment
    (acc "test comment")

    ;; write new section
    (acc '(section key2 "value2"))

    (get-output-string port)))

(test "srfi-233.3"
      "key1-value1\n# test comment\n[section]\nkey2-value2\n"
      res)

;; (test-group
;;  "make-ini-file-generator custom delimeters"

(define (read-to-list generator)
  (let loop ((lst '()))
    (define entry-lst (generator))
    (cond
     ((eof-object? entry-lst)
      (reverse lst))
     (else (loop (cons entry-lst lst))))))

(define source "key1 - value1\n
# comment\n
\n
[section]\n
 key2 - value2\n
[section2]\n
key3\n
[key]4\n
\n
\n")

(define res (read-to-list (make-ini-file-generator (open-input-string source) #\- #\#)))

(test "srfi-233.4"
    '((|| key1 "value1")
      (section key2 "value2")
      (section2 #f "key3")
      (section2 #f "[key]4"))
  res)

;; (test-end)
