From 4f345a2284e126613f1a89f21c338f009b19e54b Mon Sep 17 00:00:00 2001 From: Bo Yao Date: Sat, 30 Jun 2018 21:57:33 -0400 Subject: [PATCH] finish all tests --- src/flute.lisp | 8 +++ src/package.lisp | 6 ++- t/flute.lisp | 135 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+), 1 deletion(-) diff --git a/src/flute.lisp b/src/flute.lisp index 5cb2fe7..989e375 100644 --- a/src/flute.lisp +++ b/src/flute.lisp @@ -158,3 +158,11 @@ When given :ASCII and :ATTR, it's possible to insert html text as a children, e. body (and (symbolp x) (not (keywordp x)) (gethash (make-keyword x) *builtin-elements*)) (find-symbol (string x) :flute)))) + +(defmethod element-string ((element element)) + (with-output-to-string (s) + (write element :stream s))) + +(defmethod elem-str ((element element)) + (with-output-to-string (s) + (write element :stream s :pretty nil))) diff --git a/src/package.lisp b/src/package.lisp index d400e6f..0c151de 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -49,4 +49,8 @@ :escape-string :utf8-html-escape-char-p :ascii-html-escape-char-p - :attr-value-escape-char-p)) + :attr-value-escape-char-p + + ;;; helper for generate html string + :element-string + :elem-str)) diff --git a/t/flute.lisp b/t/flute.lisp index 88d5f17..17ce834 100644 --- a/t/flute.lisp +++ b/t/flute.lisp @@ -7,6 +7,7 @@ (def-suite escape) (def-suite attr-access) (def-suite user-element) +(def-suite h-macro) (in-suite builtin-element) @@ -111,6 +112,42 @@ (is (equal '((:id . "dog") (:class . "happy")) (attrs-alist (element-attrs div4)))) (is (equal (list div1 div2 div3) (element-children div4))))) +(test builtin-element-html-generation + (let* ((html (html)) + (div0 (div)) + (div1 (div "some text")) + (div2 (div :id "2")) + (div3 (div :id "3" div1 div2 "some other text")) + (div4 (div :id "4" div3 (div :id "5" (a :href "a.html" "a"))))) + (is (string= " +" (element-string html))) + (is (string= "
" (element-string div0))) + (is (string= "
some text
" (element-string div1))) + (is (string= "
" (element-string div2))) + (is (string= "
+
some text
+
+ some other text +
" (element-string div3))) + (is (string= "
+
+
some text
+
+ some other text +
+ +
" (element-string div4))) + + (is (string= " +" (elem-str html))) + (is (string= "
" (element-string div0))) + (is (string= "
some text
" (elem-str div1))) + (is (string= "
" (elem-str div2))) + (is (string= "
some text
some other text
" + (elem-str div3))) + (is (string= "
some text
some other text
" + (elem-str div4))))) + (in-suite escape) (defparameter *a-attrs* @@ -238,4 +275,102 @@ (setf (element-children dog4) (list dog1 dog2 dog3)) (is (equal (list dog1 dog2 dog3 "dog") (element-children (user-element-expand-to dog4)))))) +(test user-element-html-generation + (LET* ((dog1 (dog)) + (dog2 (dog :size 15)) + (dog3 (dog (img :src "dog.png"))) + (dog4 (dog :id "dog" :size 10 (img :src "dog4.png") "woo")) + (home (div :id "home" + (cat) + ;; dog4 below is ignored because cat not accepting children + (cat dog4) + (dog :id "doge" (cat))))) + (is (string= "
dog
" (element-string dog1))) + (is (string= "
dog
" (element-string dog2))) + (is (string= "
+ + dog +
" (element-string dog3))) + (is (string= "
+ + woo + dog +
" (element-string dog4))) + (is (string= "
+
+ + I'm a cat +
+
+ + I'm a cat +
+
+
+ + I'm a cat +
+ dog +
+
" (element-string home))) + + (let ((*expand-user-element* nil)) + (is (string= "" (element-string dog1))) + (is (string= "" (element-string dog2))) + (is (string= "" (element-string dog3))) + (is (string= " + + woo +" (element-string dog4))) + (is (string= "
+ + + + + woo + + + +
" (element-string home)))))) + +(in-suite h-macro) + +(in-package :cl-user) +(defpackage flute.h-macro.test + (:use :cl :fiveam) + (:import-from :flute + :h + :element-string + :define-element)) +(in-package :flute.h-macro.test) + +(define-element duck (id color) + (h (div :id (format nil "duck~a" id) + :style (format nil "color:~a" color) + "ga ga ga" + flute:children))) + +(test h-macro + (let ((some-var 3)) + (is (string= + "
+ +
foo
+ some text +
" (element-string + (h (div :id "a" + (img :href "a.png") + (div (if (> some-var 0) + '(:id "b") + '(:id "c")) + "foo") + "some text"))))) + (is (string= "
+ ga ga ga + +
" + (element-string + (h (duck :id 5 :color "blue" + (img :href "duck.png")))))))) + (run-all-tests)