;; This file is part of Beastie <https://purl.org/nxg/dist/beastie>
;; SPDX-FileCopyrightText: 2023 Norman Gray <https://nxg.me.uk>
;; SPDX-License-Identifier: BSD-2-Clause

(module "s7unit.scm" 'aux 'bst)

(print-warning 'push #f)

(test-suite
 ".aux file parsing"
 (assert-equal (parse-aux-string "\\citation{*}\n\\citation{foo}\n\\bibstyle{mybst}\n")
               '(all #f "mybst"))
 (assert-equal (parse-aux-string
                ;; comma-separated arguments in \citation and \bibdata
                (string-join '("\\citation{cite1}"
                               "\\command"
                               "\\citation{cite2}"
                               "\\bibdata{bib1,bib2}"
                               "\\another{command}"
                               "\\bibstyle{mybib}"
                               "\\citation{cite3,cite4}")
                             "\n"))
               '((cite1 cite2 cite3 cite4) ("bib1" "bib2") "mybib"))
 (assert-equal (parse-aux-string
                ;; \citation commands plus \citation{*}
                (string-join '("\\citation{cite1}"
                               "\\command"
                               "\\citation{*}"
                               "\\bibdata{bib1,bib2}"
                               "\\another{command}"
                               "\\citation{cite3}")
                             "\n"))
               '(all ("bib1" "bib2") #f))
 (let ((nw (print-warning 'get-count)))
   ;; two \bibstyle commands -- should get a warning
   (assert-equal (parse-aux-string
                  (string-join '("\\citation{cite1}"
                                 "\\command"
                                 "\\citation{*}"
                                 "\\bibdata{bib1,bib2}"
                                 "\\bibstyle{bibstyle1}"
                                 "\\another{command}"
                                 "\\bibstyle{bibstyle2}"
                                 "\\citation{cite3}")
                               "\n"))
                 '(all ("bib1" "bib2") "bibstyle1"))
   (assert-equal (print-warning 'get-count) (+ nw 1)))

 (let ((hyperref-aux (parse-aux-file "aux-t01-with-hyperref.aux")))
   (assert-equal hyperref-aux
                 '(all
                   ("bib-t01-simple.bib")
                   "bst-t01-minimal")))

 (let ((result (with-output-to-string
                 (lambda ()
                   (call-with-aux-file "aux-t02-crossrefs.aux"
                                       process-bibs/bst)))))
   ;; (printf "result:~%")
   ;; (for-each (λ (line) (printf "~a~%" line))
   ;;           (string-split result #\newline))
   (assert-equal (filter (lambda (line)
                           (let ((ll (string-length line)))
                             (and (>= ll 8)
                                  (string=? (substring line 0 8) "\\bibitem"))))
                         (string-split result #\newline))
                 '("\\bibitem{circular-1}"
                   "\\bibitem{gg-more}"
                   "\\bibitem{gg-series}"
                   "\\bibitem{missing-xr}"
                   "\\bibitem{no-gnats}"))))

(exit/failures)
