test/newui-tests.lisp
(defpackage murja.tests.newui
(:use :cl :fiveam :murja.newui)
(:import-from :murja.tests :prepare-db-and-server :drakma->string :url :main-suite :prepare-db-and-server))
(in-package :murja.tests.newui)
(in-suite main-suite)
;; testaa sulkeutuvien tagien sulkeutuminen
;; html:n doctypet, että niitä on vain 1 kerran, ei enempää ei vähempää
;; https://rosettacode.org/wiki/Count_occurrences_of_a_substring#Common_Lisp
(defun count-sub (str pat)
(loop with z = 0 with s = 0 while s do
(when (setf s (search pat str :start2 s))
(incf z) (incf s (length pat)))
finally (return z)))
(def-test html-generator-tests ()
;; basic html transformations
(is (string= (render (c :p (:id "lol") "Sisältö"))
"
<P ID=\"lol\">
Sisältö
</P>
"))
;; children are rendered somewhat sensibly
(is (string= (render (c :body ()
(c :h1 (:id "lol" :class "header") "HEADER")
(c :article (:id "lol2") "article")))
"
<BODY>
<H1 CLASS=\"header\" ID=\"lol\">
HEADER
</H1>
<ARTICLE ID=\"lol2\">
article
</ARTICLE>
</BODY>
"))
;; with-state doesn't screw up rendering
(is (string= (render (c :body ()
(c :h1 (:id "lol" :class "header") "HEADER")
(with-state ((example-variable "Example content")) (:root-id "testitila")
(c :article (:id "lol2") example-variable))))
"
<BODY>
<H1 CLASS=\"header\" ID=\"lol\">
HEADER
</H1>
<ARTICLE ID=\"idtestitila\">
Example content
</ARTICLE>
</BODY>
"))
;; basic with-state renders too
(is (string= (render (with-state ((example-variable "Example content")) (:root-id "testitila")
;; this :article IS the root element so the second id is eaten I guessy
(c :article (:id "lol2") example-variable)))
"
<ARTICLE ID=\"idtestitila\">
Example content
</ARTICLE>
"))
;; doctype tests
(let ((html-result (render (c :html (:lang "en") (c :body () (c :p () "Testing"))))))
(is (equalp 1 (count-sub html-result "DOCTYPE html"))))
;; :link is rendered as <link .../> instead of <link> ... </link>
(is (string= "<LINK TYPE=\"text/css\" REL=\"stylesheet\" HREF=\"/resources/murja.css\" />"
(render (c :link (:href "/resources/murja.css" :rel "stylesheet" :type "text/css") "random child that shouldn't be")))))
;; (fiveam:explain!
;; (fiveam:run 'html-generator-tests))