Add test for hsx/hsx package

This commit is contained in:
paku 2024-05-26 12:26:09 +09:00
parent c29e8d4882
commit 55cf69582f
4 changed files with 136 additions and 62 deletions

View file

@ -19,10 +19,13 @@
:initarg :children)))
(defun create-element (type props &rest children)
(make-instance 'element
:type type
:props props
:children (flatten children)))
(let ((elm (make-instance 'element
:type type
:props props
:children (flatten children))))
(prog1 elm
;dry-run to validate props
(expand elm))))
(defmethod expand ((elm element))
(with-accessors ((type element-type)
@ -34,8 +37,6 @@
(list :children children))))
elm)))
;;;; utils
(defun flatten (x)
(labels ((rec (x acc)
(cond ((null x) acc)

View file

@ -26,7 +26,7 @@
',props
,@children))))
(defmacro define-and-export-builtin-elements (&body names)
(defmacro define-and-export-builtin-elements (&rest names)
`(progn
,@(mapcan (lambda (name)
(list `(define-builtin-element ,name)
@ -34,7 +34,7 @@
names)))
(define-and-export-builtin-elements
a abbr address area article aside audio b base bdi bdo blockquote
a abbr address area article aside audio b base bdi bdo blockquote
body br button canvas caption cite code col colgroup data datalist
dd del details dfn dialog div dl dt em embed fieldset figcaption
figure footer form h1 h2 h3 h4 h5 h6 head header html hr i iframe

View file

@ -4,26 +4,20 @@
:hsx/element))
(in-package :hsx-test/element)
(def-suite create-element)
(def-suite element-test)
(in-suite create-element)
(in-suite element-test)
(test create-builtin-element
(let* ((inner (create-element "span"
'(:class "red")
"World!"))
(outer (create-element "p"
nil
"Hello,"
inner)))
(is (string= (element-type inner) "span"))
(is (equal (element-props inner) `(:class "red")))
(is (equal (element-children inner) (list "World!")))
(is (string= (element-type outer) "p"))
(is (null (element-props outer)))
(is (equal (element-children outer) (list "Hello," inner)))))
(test builtin-element
(let ((elm (create-element "p"
'(:class "red")
"Hello,"
"World")))
(is (string= (element-type elm) "p"))
(is (equal (element-props elm) '(:class "red")))
(is (equal (element-children elm) (list "Hello," "World")))))
(test flatten-element-children
(test flatten-children
(let* ((elm (create-element "p"
nil
"a"
@ -32,22 +26,69 @@
(cons "d" "e"))))
(is (equal (element-children elm) (list "a" "b" "c" "d" "e")))))
(test create-component-element
(labels ((comp (&key variant children)
(create-element "p"
`(:class ,variant)
"Hello,"
children)))
(let* ((inner (create-element "span"
nil
"World!"))
(outer (create-element #'comp
'(:variant "red")
inner)))
(is (eql (element-type outer) #'comp))
(is (equal (element-props outer) `(:variant "red")))
(is (equal (element-children outer) (list inner)))
(let ((expanded-elm (expand outer)))
(is (string= (element-type expanded-elm) "p"))
(is (equal (element-props expanded-elm) `(:class "red")))
(is (equal (element-children expanded-elm) (list "Hello," inner)))))))
(defun comp1 (&key title children)
(create-element "div"
nil
title
children))
(test component-elment-with-keyword-args
(let* ((elm (create-element #'comp1
'(:title "foo")
"bar"))
(expanded (expand elm)))
(is (eql (element-type elm) #'comp1))
(is (equal (element-props elm) '(:title "foo")))
(is (equal (element-children elm) (list "bar")))
(is (string= (element-type expanded) "div"))
(is (equal (element-children expanded) (list "foo" "bar")))
(signals error
(create-element #'comp1
'(:title "foo" :other-key "baz")
"bar"))))
(defun comp2 (&rest props)
(create-element "div"
nil
(getf props :title)
(getf props :children)))
(test component-element-with-property-list
(let* ((elm (create-element #'comp2
'(:title "foo")
"bar"))
(expanded (expand elm)))
(is (eql (element-type elm) #'comp2))
(is (equal (element-props elm) '(:title "foo")))
(is (equal (element-children elm) (list "bar")))
(is (string= (element-type expanded) "div"))
(is (equal (element-children expanded) (list "foo" "bar")))))
(defun comp3 (&rest props &key title children &allow-other-keys)
(create-element "div"
nil
title
children
(getf props :other-key)))
(defun comp4 (&rest props &key title children)
(create-element "div"
nil
title
children
(getf props :other-key)))
(test component-element-with-keyword-args-and-property-list
(let* ((elm (create-element #'comp3
'(:title "foo" :other-key "baz")
"bar"))
(expanded (expand elm)))
(is (eql (element-type elm) #'comp3))
(is (equal (element-props elm) '(:title "foo" :other-key "baz")))
(is (equal (element-children elm) (list "bar")))
(is (string= (element-type expanded) "div"))
(is (equal (element-children expanded) (list "foo" "bar" "baz")))
(signals error
(create-element #'comp4
'(:title "foo" :other-key "baz")
"bar"))))

View file

@ -1,31 +1,63 @@
(defpackage #:hsx-test/hsx
(:use #:cl
#:fiveam
#:hsx/element
#:hsx/hsx))
#:hsx/hsx)
(:import-from #:hsx/element
#:create-element))
(in-package #:hsx-test/hsx)
(def-suite builtin-element-hsx)
(def-suite component-element-hsx)
(in-suite builtin-element-hsx)
(def-suite hsx-test)
(in-suite hsx-test)
(test empty-hsx
(let ((elm (div)))
(is (null (element-props elm)))
(is (null (element-children elm)))))
(is (equal (macroexpand-1
'(div))
'(create-element
"div"
'nil))))
(test hsx-with-props
(let ((elm (div :prop1 "value1" :prop2 "value2")))
(is (equal (element-props elm) '(:prop1 "value1" :prop2 "value2")))
(is (null (element-children elm)))))
(is (equal (macroexpand-1
'(div :prop1 "value1" :prop2 "value2"))
'(create-element
"div"
'(:prop1 "value1" :prop2 "value2")))))
(test hsx-with-children
(let ((elm (div "child1" "child2")))
(is (null (element-props elm)))
(is (equal (element-children elm) (list "child1" "child2")))))
(is (equal (macroexpand-1
'(div
"child1"
"child2"))
'(create-element
"div"
'nil
"child1"
"child2"))))
(test hsx-with-props-and-children
(let ((elm (div :prop1 "value1" :prop2 "value2"
"child1" "child2")))
(is (equal (element-props elm) '(:prop1 "value1" :prop2 "value2")))
(is (equal (element-children elm) (list "child1" "child2")))))
(is (equal (macroexpand-1
'(div :prop1 "value1" :prop2 "value2"
"child1"
"child2"))
'(create-element
"div"
'(:prop1 "value1" :prop2 "value2")
"child1"
"child2"))))
(defcomp comp (&key prop1 prop2 children)
(div
prop1
prop2
children))
(test component-hsx
(is (equal (macroexpand-1
'(comp :prop1 "value1" :prop2 "value2"
"child1"
"child2"))
'(create-element
#'%comp
'(:prop1 "value1" :prop2 "value2")
"child1"
"child2"))))