forked from cryogen-project/cryogen-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler_test.clj
148 lines (131 loc) · 5.18 KB
/
compiler_test.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
(ns cryogen-core.compiler-test
(:require [clojure.java.io :as io]
[clojure.test :refer :all]
[me.raynes.fs :as fs]
[net.cgrand.enlive-html :as enlive]
[cryogen-core.compiler :refer :all]
[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 (enlive/html-snippet "<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 (enlive/html-snippet "<div id='post'>
<div class='post-content'>
this post has more marker
<!--more-->
and more content.
</div>
</div>")))
(enlive/html-snippet "<div id=\"post\">
<div class=\"post-content\">
this post has more marker
</div></div>"))))
(defn- markdown []
(reify m/Markup
(dir [this] "md")
(exts [this] #{".md"})))
(defn- asciidoc []
(reify m/Markup
(dir [this] "asc")
(exts [this] #{".asc"})))
(defn- create-entry [dir file]
(fs/mkdirs (File. dir))
(fs/create (File. (str dir File/separator file))))
(defn- reset-resources []
(doseq [dir ["public" "content"]]
(fs/delete-dir dir)
(create-entry dir ".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))
(is (empty? (check-for-pages mu))))
(let [dir->file
[[check-for-posts "content/md/posts" "post.md"]
[check-for-posts "content/posts" "post.md"]
[check-for-pages "content/md/pages" "page.md"]
[check-for-pages "content/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
ext (m/exts mu)]
(let [path (if with-dir?
(str (m/dir mu) "/" dir)
dir)]
(create-entry (str "content/" path)
(str "entry" ext))))
(with-markup mu
(copy-resources-from-markup-folders
{:post-root posts-root
:page-root pages-root
:blog-prefix "/blog"}))
(doseq [dir dirs]
(is (.isDirectory (File. (str "public/blog/" dir))))))
(deftest test-copy-resources-from-markup-folders
(reset-resources)
(testing "No pages or posts nothing to copy"
(copy-resources-from-markup-folders
{:post-root "pages"
:page-root "posts"
:blog-prefix "/blog"})
(is (not (.isDirectory (File. (str "public/blog/pages")))))
(is (not (.isDirectory (File. (str "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))
(deftest fail-test (testing "failure" (is true)))
(defn reader-string [s]
(java.io.PushbackReader. (java.io.StringReader. s)))
(deftest test-metadata-parsing
(testing "Parsing page/post configuration"
(let [valid-metadata (reader-string "{:layout :post :title \"Hello World\"}")
invalid-metadata (reader-string "{:layout \"post\" :title \"Hello World\"}")]
(is (read-page-meta nil valid-metadata))
(is (thrown? Exception (read-page-meta nil invalid-metadata))))))
(deftest tags-are-url-encoded
(testing "URL encode tags"
(let [config {:tag-root-uri "tags-output" :blog-prefix "/blog" :clean-urls :trailing-slash}]
(are [expected tag] (= expected (:uri (tag-info config tag)))
"/blog/tags-output/a-tag/" "a-tag"
"/blog/tags-output/c%23/" "c#"
"/blog/tags-output/why%3F/" "why?"
"/blog/tags-output/with%20a%20space/" "with a space")
(are [expected tag] (= expected (:file-path (tag-info config tag)))
"/blog/tags-output/a-tag/" "a-tag"
"/blog/tags-output/c#/" "c#"
"/blog/tags-output/why?/" "why?"
"/blog/tags-output/with a space/" "with a space"))))