cryogen-core/test/cryogen_core/compiler_test.clj

119 lines
3.7 KiB
Clojure
Raw Normal View History

(ns cryogen-core.compiler-test
(:require [clojure.test :refer :all]
2017-01-16 07:37:19 +00:00
[me.raynes.fs :as fs]
[cryogen-core.compiler :refer :all]
2017-01-16 07:37:19 +00:00
[cryogen-core.markup :as m])
(:import [java.io File]))
; Test that the content-until-more-marker return nil or correct html text.
(deftest test-content-until-more-marker
; text without more marker, return nil
(is (nil? (content-until-more-marker "<div id=\"post\">
<div class=\"post-content\">
this post does not have more marker
</div>
</div>")))
; text with more marker, return text before more marker with closing tags.
(is (= (content-until-more-marker "<div id='post'>
<div class='post-content'>
this post has more marker
<!--more-->
and more content.
</div>
</div>")
"<div id=\"post\">
<div class=\"post-content\">
this post has more marker
</div></div>")))
(defn- markdown []
(reify m/Markup
(dir [this] "md")
(ext [this] ".md")))
(defn- asciidoc []
(reify m/Markup
(dir [this] "asc")
(ext [this] ".asc")))
(defn- create-entry [dir file]
(fs/mkdirs (File. dir))
(fs/create (File. (str dir File/separator file))))
(defn- reset-resources []
(fs/delete-dir "resources")
(create-entry "resources" ".gitkeep"))
(defn- check-for-pages [mu]
(find-pages {:page-root "pages"} mu))
(defn- check-for-posts [mu]
(find-posts {:post-root "posts"} mu))
(deftest test-find-entries
(reset-resources)
(let [mu (markdown)]
(testing "Finds no files"
(is (empty? (check-for-posts mu))
2019-12-20 14:55:53 +00:00
(is (empty? (check-for-pages mu))))
2019-12-20 14:55:53 +00:00
(let [dir->file
[[check-for-posts "resources/templates/md/posts" "post.md"]
[check-for-posts "resources/templates/posts" "post.md"]
[check-for-pages "resources/templates/md/pages" "page.md"]
[check-for-pages "resources/templates/pages" "page.md"]]]
(doseq [[check-fn dir file] dir->file]
(testing (str "Finds files in " dir)
(create-entry dir file)
(let [entries (check-fn mu)]
(is (= 1 (count entries)))
(is (= (.getAbsolutePath (File. (str dir File/separator file)))
(.getAbsolutePath (first entries)))))
(reset-resources)))))))
(defmacro with-markup [mu & body]
`(do
(m/register-markup ~mu)
(try
~@body
(finally
(m/clear-registry)))))
(defn- copy-and-check-markup-folders
"Create entries in the markup folders. If `with-dir?` is set to true, include
the Markup implementation's `dir` in the path. Check that the folders exist
in the output folder."
[[pages-root posts-root :as dirs] mu with-dir?]
(doseq [dir dirs]
(let [path (if with-dir?
(str (m/dir mu) "/" dir)
dir)]
(create-entry (str "resources/templates/" path)
(str "entry" (m/ext mu)))))
(with-markup mu
(copy-resources-from-markup-folders
2019-12-20 14:55:53 +00:00
{:post-root posts-root
:page-root pages-root
:blog-prefix "/blog"}))
(doseq [dir dirs]
(is (.isDirectory (File. (str "resources/public/blog/" dir))))))
(deftest test-copy-resources-from-markup-folders
2016-05-27 14:19:03 +00:00
(reset-resources)
(testing "No pages or posts nothing to copy"
(copy-resources-from-markup-folders
2019-12-20 14:55:53 +00:00
{:post-root "pages"
:page-root "posts"
:blog-prefix "/blog"})
2016-05-27 14:19:03 +00:00
(is (not (.isDirectory (File. (str "resources/public/blog/pages")))))
(is (not (.isDirectory (File. (str "resources/public/blog/posts"))))))
(reset-resources)
(doseq [mu [(markdown) (asciidoc)]]
(testing (str "Test copy from markup folders (" (m/dir mu) ")")
(let [dirs ["pages" "posts"]]
(copy-and-check-markup-folders dirs mu true)
(reset-resources)
(copy-and-check-markup-folders dirs mu false))))
(reset-resources))