Skip to content

Commit

Permalink
feat: Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarceColl committed Mar 5, 2024
1 parent 7d5a714 commit 556df92
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 9 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: test
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
lisp:
- sbcl-bin
- ccl-bin

defaults:
run:
shell: bash
env:
LISP: ${{ matrix.lisp }}

steps:
- uses: actions/checkout@v4
- name: Restore cache
id: cache-ql
uses: actions/cache@v3
with:
path: |
~/.roswell
~/.cache/common-lisp
key: ${{ runner.os }}-ql
- name: Install Lisp
uses: 40ants/setup-lisp@v2
with:
asdf-system: chobun
- name: Run tests
with:
asdf-system: chobun

13 changes: 10 additions & 3 deletions chobun.asd
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
(asdf:defsystem "chobun"
:depends-on ("bedrock")
:components ((:file "chobun")))
(asdf:defsystem :chobun
:depends-on (:bedrock)
:components ((:file "chobun"))
:in-order-to ((test-op (test-op :chobun/test))))

(asdf:defsystem :chobun/test
:depends-on (:chobun :parachute :split-sequence)
:components ((:module "t"
:components ((:file "chobun"))))
:perform (asdf:test-op (op c) (uiop:symbol-call :parachute :test-toplevel :chobun/test)))
19 changes: 13 additions & 6 deletions chobun.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

(define-step parse-tag-tree-node (first-element rest)
(multiple-value-bind (args subtrees) (get-args-and-subtrees rest)
(add-code `(write-string ,(format nil "<~(~a~)~{ ~(~a~)=~s~}>" (symbol-name first-element) args) *html-stream*))
(add-code `(format *html-stream* "<~(~a~)~{ ~(~a~)=~s~}>" ,(symbol-name first-element) `,(list ,@args)))
(dolist (next-tree-node subtrees)
(add-codes (parse-tree-node next-tree-node)))
(add-code `(write-string ,(format nil "</~(~a~)>" (symbol-name first-element)) *html-stream*))))
Expand All @@ -51,9 +51,9 @@
((in first-element *hoistable-special-forms*)
(add-code `(,first-element ,(car rest) ,@(parse-tree-node (cadr rest)))))
((in first-element *inline-special-forms*)
(add-code `(format *html-stream* "~@[~a~]" (,first-element ,(car rest)
(progn ,@(parse-tree-node (cadr rest)) nil)
(progn ,@(parse-tree-node (caddr rest)) nil)))))
(add-code `(,first-element ,(car rest)
(progn ,@(parse-tree-node (cadr rest)) nil)
(progn ,@(parse-tree-node (caddr rest)) nil))))
(t (add-code `(format *html-stream* "~@[~a~]" (maybe-eval-html (apply #',first-element (list ,@rest))))))))

(define-step parse-list-tree-node (tree-node)
Expand All @@ -79,7 +79,8 @@
(str "")
(format-args nil))
(flet ((commit-curr-str ()
(pushback `(format *html-stream* ,str ,@format-args) res)
(if (> (length str) 0)
(pushback `(format *html-stream* ,str ,@format-args) res))
(setf str "" format-args nil))
(append-to-str (val) (setf str (concatenate 'string str val))))
(dolist (l c)
Expand All @@ -93,6 +94,12 @@
(commit-curr-str)
(pushback `(dolist ,(cadr l)
,@(optimize-html-codegen (cddr l))) res))
((eq 'if (car l))
(commit-curr-str)
(pushback `(if ,(cadr l)
(progn ,@(optimize-html-codegen (cddr l)))
(progn ,@(optimize-html-codegen (cdddr l))))
res))
(t (commit-curr-str)
(pushback l res))))
(commit-curr-str)
Expand Down Expand Up @@ -149,7 +156,7 @@ The HTML tree is a nested Lisp S-Expression, each nested list can start by eithe
If it's a keyword then it's interpreted as an HTML tag. For example (:div \"Hola\") is interpreted as \"<div>Hola</div>\".
If it's a symbol what happens depends on the symbol, if it's one of the supported Lisp control structures, in includes that
in the generated code, so it feels like using Lisp."
(let ((html-gen (optimize-html-codegen (parse-html html-tree))))
(let ((html-gen (parse-html html-tree)))
`(progn
(let ((*html-stream* (make-string-output-stream)))
(with-output-to-string (*html-stream*)
Expand Down
42 changes: 42 additions & 0 deletions t/chobun.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(in-package :cl-user)
(defpackage chobun/test
(:use :cl :bedrock :chobun :parachute :split-sequence))
(in-package :chobun/test)

(eval-when (:compile-toplevel :load-toplevel :execute)
(defun to-kebab-case (str)
(intern (format nil "~{~:@(~a~)~#[~:;-~]~}" (split-sequence #\Space str)))))

(defmacro html-test (desc html-tree expected-output &optional (vars nil))
(let ((test-name (to-kebab-case desc)))
`(define-test ,test-name
(let ,vars
(is string= ,expected-output (html ,html-tree))))))

(html-test "Single tag" (:div "Hola") "<div>Hola</div>")
(html-test "Nested tags" (:div (:span "Hola")) "<div><span>Hola</span></div>")
(html-test "Same level tags" (:div (:span "Hola") (:h1 "Title")) "<div><span>Hola</span><h1>Title</h1></div>")
(html-test "Tag properties" (:div :class "test" :id "wassup" (:span :id "inner" "Hola"))
"<div class=\"test\" id=\"wassup\"><span id=\"inner\">Hola</span></div>")
(html-test "Variables"
(:div :class class-name :id "wassup" (:span :id "inner" value))
"<div class=\"test\" id=\"wassup\"><span id=\"inner\">Hola</span></div>"
((class-name "test") (value "Hola")))
(html-test "dolist"
(:div (dolist (i '(1 2 3 4))
(:span i)))
"<div><span>1</span><span>2</span><span>3</span><span>4</span></div>")
(html-test "dotimes"
(:div (dotimes (i 4)
(:span i)))
"<div><span>0</span><span>1</span><span>2</span><span>3</span></div>")
(html-test "if"
(:div (if t
(:span "TRUE")
(:span "FALSE")))
"<div><spanTRUE</span></div>")
(html-test "if inside dotimes"
(:div (dotimes (i 6)
(:div (if (= (mod i 2) 0)
(:span (format nil "~a is EVEN" i))))))
"<div><div><span>0 is EVEN</span></div><div></div><div><span>2 is EVEN</span></div><div></div><div><span>4 is EVEN</span></div><div></div></div>")

0 comments on commit 556df92

Please sign in to comment.