;;;; tests/toolkit.lisp

;; Toolkit Tests
;;
;; TODO I thought after not looking at this file that it was a toolkit
;; *for* tests. Maybe I should rename the tests?

(in-package #:cl-deck-builder2-test)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(is (grouped (loop for _ upto 10 collect _) 10)
    '((0 1 2 3 4 5 6 7 8 9) (10)))

(is (grouped (loop for _ upto 10 collect _) 3)
    '((0 1 2) (3 4 5) (6 7 8) (9 10)))

(is (grouped (loop for _ upto 10 collect _) 1)
    '((0) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(is (normalize-newlines (format nil "as~%df"))
    (format nil "as~%df"))

(is (normalize-newlines (format nil "as~C~%df" #\Return))
    (format nil "as~%df"))

(is (normalize-newlines (format nil "as~Cdf~C~C" #\Return #\Return #\Newline))
    (format nil "as~%df~%"))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Test removal of UTF8 BOMs
;;
;; TODO Surely there has got to be a better way to handle this.
(defparameter *utf8-bom*
  (flexi-streams:octets-to-string #(239 187 191)
                                  :external-format :utf-8))

(is (strip-bom (format nil "~aTest!" *utf8-bom*))
    "Test!")

(is (normalize-newlines
     (strip-bom
      (format nil "~aTest!~C~%" *utf8-bom* #\Return)))
    (format nil "Test!~%"))

(is (strip-bom
     (normalize-newlines
      (format nil "~aTest!~C~%" *utf8-bom* #\Return)))
    (format nil "Test!~%"))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(is (query-param nil nil) nil)

(is (query-param nil nil) nil)

(is (query-param "name" '(("name" . "user")))
    "user")

;; QUERY-PARAM always returns a string
(is (query-param "name" '(("name" . "15")))
    "15")

;; Empty string is the same as not passing a value / unset
;;
;; TODO Surely there has got to be a better way to handle this.
(is (query-param "name" '(("name" . "")))
    nil)

(is (query-param "name" '(("name" . "")))
    nil)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; No results, no offset, LIMIT 1
(is (generate-pages 0 0 1)
    '((:id 0 :offset 0)))

;; No results, OFFSET 10, LIMIT 10
(is (generate-pages 0 10 10)
    '((:id 0 :offset 0)))

;; One result, OFFSET 10, LIMIT 10
;;
;; TODO I generate one extra page. Maybe don't?
(is (generate-pages 1 10 10)
    '((:id 0 :offset 0)
      (:id 1 :offset 10)))

;; Ten results, OFFSET 10, LIMIT 10
;;
(is (generate-pages 10 10 10)
    '((:id 0 :offset 0)
      (:id 1 :offset 10)))

;; One hundred reults, OFFSET 100, LIMIT 10
;;
;; So we show a lot of extra pages here. If there are 100 results and
;; OFFSET 100 items into our 10 per page list, we should start at page
;; 5 and go up to page 10. But this returns out to page 13.
(is (generate-pages 100 100 10)
    '((:id 5 :offset 50) (:id 6 :offset 60) (:id 7 :offset 70)
      (:id 8 :offset 80) (:id 9 :offset 90) (:id 10 :offset 100)
      (:id 11 :offset 110) (:id 12 :offset 120) (:id 13 :offset 130)))

(is (get-opposite-direction "asc") "desc")
(is (get-opposite-direction "desc") "asc")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
