diff --git a/project.clj b/project.clj index 547388b..ee5c8e5 100644 --- a/project.clj +++ b/project.clj @@ -11,9 +11,11 @@ :all (constantly true)} :auto-clean false - :aliases {"cleantest" ["do" "clean," "test"] - "install" ["do" "clean," "install"] - "deploy" ["do" "clean," "deploy" "clojars"]} + :aliases {"test-cljs" ["doo" "rhino" "test" "once"] + "test" ["do" "test," "test-cljs"] + "cleantest" ["do" "clean," "test"] + "install" ["do" "clean," "install"] + "deploy" ["do" "clean," "deploy" "clojars"]} :source-paths ["src/clj" "src/cljc" "src/cljs"] :cljsbuild @@ -25,11 +27,21 @@ :pretty-print false}} :dev {:compiler {:optimizations :whitespace - :pretty-print true}}}} + :pretty-print true}} + + :test + {:source-paths ["src/cljc" "src/cljs" "test"] + :compiler {:output-to "target/unit-test.js" + :output-dir "target" + :main mdtest.runner + :optimizations :whitespace}}}} :profiles {:dev {:jvm-opts ["-XX:-TieredCompilation"] :dependencies [[criterium "0.4.3" :scope "test"] [commons-lang "2.6" :scope "test"] - [org.clojure/clojurescript "1.7.145"]] - :plugins [[lein-cljsbuild "1.0.6"]]}}) + [org.clojure/clojurescript "1.7.145"] + [org.mozilla/rhino "1.7.7"]] + :plugins [[lein-cljsbuild "1.0.6"] + [lein-doo "0.1.6"]]}} + :doo {:paths {:rhino "lein run -m org.mozilla.javascript.tools.shell.Main"}}) diff --git a/test/mdtest_runner.cljs b/test/mdtest_runner.cljs new file mode 100644 index 0000000..d0e1d11 --- /dev/null +++ b/test/mdtest_runner.cljs @@ -0,0 +1,5 @@ +(ns mdtest.runner + (:require [doo.runner :refer-macros [doo-tests]] + [mdtests])) + +(doo-tests 'mdtests) diff --git a/test/mdtests.clj b/test/mdtests.cljc similarity index 51% rename from test/mdtests.clj rename to test/mdtests.cljc index 3aabc8e..e52d51c 100644 --- a/test/mdtests.clj +++ b/test/mdtests.cljc @@ -1,112 +1,118 @@ (ns mdtests (:require [markdown.core :as markdown] [markdown.transformers :as transformers] - [markdown.tables :as tables]) - (:use clojure.test)) + [markdown.tables :as tables] + #?(:clj [clojure.test :refer :all] + :cljs [cljs.test :refer-macros [deftest is testing]]) + #?(:cljs [goog.string]))) + +(def entry-function + #?(:clj markdown/md-to-html-string + :cljs markdown/md->html)) (deftest heading1 - (is (= "

Foo

" (markdown/md-to-html-string " # Foo"))) - (is (= "

foo

" (markdown/md-to-html-string "#foo"))) - (is (= "

foo

" (markdown/md-to-html-string "foo\n==="))) - (is (= "

foo

" (markdown/md-to-html-string "#foo#"))) - (is (= "

foo

" (markdown/md-to-html-string "#foo#\n")))) + (is (= "

Foo

" (entry-function " # Foo"))) + (is (= "

foo

" (entry-function "#foo"))) + (is (= "

foo

" (entry-function "foo\n==="))) + (is (= "

foo

" (entry-function "#foo#"))) + (is (= "

foo

" (entry-function "#foo#\n")))) (deftest heading2 - (is (= "

foo

" (markdown/md-to-html-string "##foo"))) - (is (= "

foo

" (markdown/md-to-html-string "foo\n---"))) - (is (= "

foo

" (markdown/md-to-html-string "##foo##"))) - (is (= "

foo

" (markdown/md-to-html-string "##foo##\n")))) + (is (= "

foo

" (entry-function "##foo"))) + (is (= "

foo

" (entry-function "foo\n---"))) + (is (= "

foo

" (entry-function "##foo##"))) + (is (= "

foo

" (entry-function "##foo##\n")))) (deftest heading-with-complex-anchor (is (= "

foo bar BAz

some text" - (markdown/md-to-html-string "###foo bar BAz\nsome text" :heading-anchors true))) + (entry-function "###foo bar BAz\nsome text" :heading-anchors true))) (is (= "

foo bar BAz

some text" - (markdown/md-to-html-string "###foo bar BAz##\nsome text" :heading-anchors true)))) + (entry-function "###foo bar BAz##\nsome text" :heading-anchors true)))) (deftest br - (is (= "

foo

" (markdown/md-to-html-string "foo "))) - (is (= "
foo  \n
bar" (markdown/md-to-html-string "```\nfoo \nbar```")))) + (is (= "

foo

" (entry-function "foo "))) + (is (= "
foo  \n
bar" (entry-function "```\nfoo \nbar```")))) (deftest hr - (is (= "
" (markdown/md-to-html-string "***"))) - (is (= "
" (markdown/md-to-html-string " * * * "))) - (is (= "
" (markdown/md-to-html-string " *****"))) - (is (= "
" (markdown/md-to-html-string "- - - ")))) + (is (= "
" (entry-function "***"))) + (is (= "
" (entry-function " * * * "))) + (is (= "
" (entry-function " *****"))) + (is (= "
" (entry-function "- - - ")))) (deftest em - (is (= "

foo

" (markdown/md-to-html-string "*foo*")))) + (is (= "

foo

" (entry-function "*foo*")))) (deftest italics - (is (= "

foo

" (markdown/md-to-html-string "_foo_")))) + (is (= "

foo

" (entry-function "_foo_")))) (deftest strong - (is (= "

foo

" (markdown/md-to-html-string "**foo**")))) + (is (= "

foo

" (entry-function "**foo**")))) (deftest bold - (is (= "

foo

" (markdown/md-to-html-string "__foo__")))) + (is (= "

foo

" (entry-function "__foo__")))) (deftest strong-inside-em - (is (= "

foobarbaz

" (markdown/md-to-html-string "*foo**bar**baz*")))) + (is (= "

foobarbaz

" (entry-function "*foo**bar**baz*")))) (deftest em-inside-strong - (is (= "

foobarbaz

" (markdown/md-to-html-string "**foo*bar*baz**")))) + (is (= "

foobarbaz

" (entry-function "**foo*bar*baz**")))) (deftest paragraph (is (= "

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore

" - (markdown/md-to-html-string "\nLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore")))) + (entry-function "\nLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore")))) (deftest paragraph-multiline (is (= "

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore

" - (markdown/md-to-html-string "\nLorem ipsum dolor\nsit amet, consectetur adipisicing elit,\nsed do eiusmod tempor incididunt ut labore")))) + (entry-function "\nLorem ipsum dolor\nsit amet, consectetur adipisicing elit,\nsed do eiusmod tempor incididunt ut labore")))) (deftest mulitple-paragraphs (is (= "

foo bar baz

foo bar baz

" - (markdown/md-to-html-string "\nfoo bar baz\n\n\nfoo bar baz")))) + (entry-function "\nfoo bar baz\n\n\nfoo bar baz")))) (deftest ul (is (= "" - (markdown/md-to-html-string "* foo\n* bar\n* baz"))) + (entry-function "* foo\n* bar\n* baz"))) (is (= "" - (markdown/md-to-html-string "- foo\n- bar\n- baz"))) + (entry-function "- foo\n- bar\n- baz"))) (is (= "" - (markdown/md-to-html-string "+ foo\n+ bar\n+ baz")))) + (entry-function "+ foo\n+ bar\n+ baz")))) (deftest ul-followed-by-paragraph (is (= "

paragraph next line

" - (markdown/md-to-html-string "* foo\n* bar\n* baz\n\nparagraph\nnext line")))) + (entry-function "* foo\n* bar\n* baz\n\nparagraph\nnext line")))) (deftest ul-followed-by-multiline-paragraph (is (= "

paragraph

" - (markdown/md-to-html-string "* foo\n* bar\n* baz\n\nparagraph")))) + (entry-function "* foo\n* bar\n* baz\n\nparagraph")))) (deftest ul-nested (is (= "" - (markdown/md-to-html-string "* first item\n * first sub-item\n * second sub-item\n * third sub-item\n* second item\n * first sub-item\n * second sub-item\n* third item"))) + (entry-function "* first item\n * first sub-item\n * second sub-item\n * third sub-item\n* second item\n * first sub-item\n * second sub-item\n* third item"))) (is (= "" - (markdown/md-to-html-string "* first item\n - first sub-item\n - second sub-item\n - third sub-item\n* second item\n + first sub-item\n + second sub-item\n* third item"))) - (is (= "" (markdown.core/md-to-html-string " * abc\n\n+ def")))) + (entry-function "* first item\n - first sub-item\n - second sub-item\n - third sub-item\n* second item\n + first sub-item\n + second sub-item\n* third item"))) + (is (= "" (entry-function " * abc\n\n+ def")))) (deftest ol (is (= "
  1. Foo
  2. Bar
  3. Baz
" - (markdown/md-to-html-string "1. Foo\n2. Bar\n3. Baz")))) + (entry-function "1. Foo\n2. Bar\n3. Baz")))) (deftest ul-in-ol (is (= "
  1. Bar
    1. Subbar
      • foo
      • bar
      • baz
  2. Baz
" - (markdown/md-to-html-string "1. Bar\n 2. Subbar\n * foo\n * bar\n * baz\n3. Baz")))) + (entry-function "1. Bar\n 2. Subbar\n * foo\n * bar\n * baz\n3. Baz")))) (deftest ol-in-ul (is (= "" - (markdown/md-to-html-string "* Foo\n 1. Bar\n 1. Subbar\n* Baz"))) + (entry-function "* Foo\n 1. Bar\n 1. Subbar\n* Baz"))) (is (= "" - (markdown/md-to-html-string "* Foo\n 1. Bar")))) + (entry-function "* Foo\n 1. Bar")))) (deftest multilist (is (= "" - (markdown/md-to-html-string -"* foo + (entry-function + "* foo * bar * baz @@ -121,29 +127,29 @@ (deftest code (is (= "

foo bar baz x = y + z; foo

" - (markdown/md-to-html-string "foo bar baz `x = y + z;` foo"))) + (entry-function "foo bar baz `x = y + z;` foo"))) (is (= "

<?xml version='1.0' encoding='UTF-8'?><channel></channel>

" - (markdown/md-to-html-string "``"))) + (entry-function "``"))) (is (= "

foo bar baz (fn [x & xs] (str "x:" x)) foo

" - (markdown/md-to-html-string "foo bar baz `(fn [x & xs] (str \"x:\" x))` foo")))) + (entry-function "foo bar baz `(fn [x & xs] (str \"x:\" x))` foo")))) (deftest multiline-code (is (= "
x = 5\ny = 6\nz = x + y
" - (markdown/md-to-html-string " x = 5\n y = 6\n z = x + y"))) + (entry-function " x = 5\n y = 6\n z = x + y"))) (is (= "
x = 5\ny = 6\nz = x + y\n(fn [x & xs] (str "x"))
" - (markdown/md-to-html-string " x = 5\n y = 6\n z = x + y\n (fn [x & xs] (str \"x\"))")))) + (entry-function " x = 5\n y = 6\n z = x + y\n (fn [x & xs] (str \"x\"))")))) (deftest codeblock (is (= "
(defn- write [writer text]\n  (doseq [c text]\n    (.write writer (int c))))\n
" - (markdown/md-to-html-string "``` (defn- write [writer text]\n (doseq [c text]\n (.write writer (int c))))\n```"))) + (entry-function "``` (defn- write [writer text]\n (doseq [c text]\n (.write writer (int c))))\n```"))) (is (= "
(fn [x & xs]\n  (str "x"))\n
" - (markdown/md-to-html-string "``` (fn [x & xs]\n (str \"x\"))\n```"))) + (entry-function "``` (fn [x & xs]\n (str \"x\"))\n```"))) (is (= "
(fn [x & xs]\n  (str "x"))\n
" - (markdown/md-to-html-string "```\n(fn [x & xs]\n (str \"x\"))\n```"))) + (entry-function "```\n(fn [x & xs]\n (str \"x\"))\n```"))) (is (= "
(fn [x & xs]\n  (str "x"))\n
" - (markdown/md-to-html-string "```clojure (fn [x & xs]\n (str \"x\"))\n```"))) + (entry-function "```clojure (fn [x & xs]\n (str \"x\"))\n```"))) (is (= "
------------\n============\n    ------------\n    ============\n
" - (markdown/md-to-html-string + (entry-function " ```nohighlight ------------ @@ -151,141 +157,124 @@ ------------ ============ ``` -" - )))) +")))) (deftest stirkethrough (is (= "

foo

" - (markdown/md-to-html-string "~~foo~~")))) + (entry-function "~~foo~~")))) (deftest superscript (is (= "

foobar baz

" - (markdown/md-to-html-string "foo^bar baz")))) + (entry-function "foo^bar baz")))) (deftest link (is (= "

github

" - (markdown/md-to-html-string "[github](http://github.com)"))) + (entry-function "[github](http://github.com)"))) (is (= "

github

" - (markdown/md-to-html-string "[github](http://github.com/~)"))) + (entry-function "[github](http://github.com/~)"))) (is (= "

github

" - (markdown/md-to-html-string "[github](http://github.com/^)"))) + (entry-function "[github](http://github.com/^)"))) (is (= "

github

" - (markdown/md-to-html-string "[github](http://github.com/*)"))) + (entry-function "[github](http://github.com/*)"))) (is (= "" - (markdown/md-to-html-string "* [github](http://github.com/*)"))) + (entry-function "* [github](http://github.com/*)"))) (is (= "

a link

" - (markdown/md-to-html-string "* hi\n\n[a link](https://see-here)")))) + (entry-function "* hi\n\n[a link](https://see-here)")))) (deftest styled-link (is (= "

github

" - (markdown/md-to-html-string "[*github*](http://github.com)"))) + (entry-function "[*github*](http://github.com)"))) (is (= "

github

" - (markdown/md-to-html-string "[_github_](http://github.com)"))) + (entry-function "[_github_](http://github.com)"))) (is (= "

github

" - (markdown/md-to-html-string "[__github__](http://github.com)"))) + (entry-function "[__github__](http://github.com)"))) (is (= "

github

" - (markdown/md-to-html-string "[**github**](http://github.com)"))) + (entry-function "[**github**](http://github.com)"))) (is (= "

github

" - (markdown/md-to-html-string "[~~github~~](http://github.com)"))) - ) + (entry-function "[~~github~~](http://github.com)")))) (deftest img (is (= "

\"Alt

" - (markdown/md-to-html-string "![Alt text](/path/to/img.jpg)"))) + (entry-function "![Alt text](/path/to/img.jpg)"))) (is (= "

\"Alt

" - (markdown/md-to-html-string "![Alt text](/path/to/_img_.jpg \"Optional Title\")")))) + (entry-function "![Alt text](/path/to/_img_.jpg \"Optional Title\")")))) (deftest img-link (is (= "

\"Continuous

" - (markdown/md-to-html-string "[![Continuous Integration status](https://secure.travis-ci.org/yogthos/markdown-clj.png)](http://travis-ci.org/yogthos/markdown-clj)"))) + (entry-function "[![Continuous Integration status](https://secure.travis-ci.org/yogthos/markdown-clj.png)](http://travis-ci.org/yogthos/markdown-clj)"))) (is (= "

\"\"

" - (markdown/md-to-html-string "![](https://secure.travis-ci.org/yogthos/markdown-clj.png)")))) + (entry-function "![](https://secure.travis-ci.org/yogthos/markdown-clj.png)")))) (deftest bad-link (is (= "

[github](http://github.comfooo

" - (markdown/md-to-html-string "[github](http://github.comfooo"))) + (entry-function "[github](http://github.comfooo"))) (is (= "

[github] no way (http://github.com)

" - (markdown/md-to-html-string "[github] no way (http://github.com)")))) + (entry-function "[github] no way (http://github.com)")))) (deftest bad-link-title (is (= "

[github(http://github.comfooo)

" - (markdown/md-to-html-string "[github(http://github.comfooo)")))) + (entry-function "[github(http://github.comfooo)")))) (deftest blockquote (is (= "

Foo bar baz

" - (markdown/md-to-html-string ">Foo bar baz")))) + (entry-function ">Foo bar baz")))) (deftest blockquote-footer (is (= "

Foo bar baz

" - (markdown/md-to-html-string "> Foo bar baz\n>- Leo Tolstoy")))) + (entry-function "> Foo bar baz\n>- Leo Tolstoy")))) (deftest blockquote-empty-footer (is (= "

Foo bar baz

" - (markdown/md-to-html-string "> Foo bar baz\n>-")))) + (entry-function "> Foo bar baz\n>-")))) (deftest blockquote-multiline-without-leading-angle-bracket (is (= "

Foo bar baz

" - (markdown/md-to-html-string "> Foo bar\nbaz")))) + (entry-function "> Foo bar\nbaz")))) (deftest blockquote-multiple-paragraphs (is (= "

Foo bar

baz

" - (markdown/md-to-html-string "> Foo bar\n>\n> baz")))) + (entry-function "> Foo bar\n>\n> baz")))) (deftest escaped-characters (is (= "

*‘_{}[]footestbar{x}[y]

" - (markdown/md-to-html-string "\\*\\`\\_\\{\\}\\[\\]*foo*`test`_bar_{x}[y]")))) - + (entry-function "\\*\\`\\_\\{\\}\\[\\]*foo*`test`_bar_{x}[y]")))) (deftest paragraph-after-list (is (= "
  1. a
  2. b

test bold and italic

" - (markdown/md-to-html-string "1. a\n2. b\n\ntest **bold** and *italic*")))) + (entry-function "1. a\n2. b\n\ntest **bold** and *italic*")))) (deftest paragraph-close-before-list (is (= "

in paragraph

" - (markdown/md-to-html-string "in paragraph\n- list")))) + (entry-function "in paragraph\n- list")))) (deftest autourl (is (= "

http://example.com/

" - (markdown/md-to-html-string ""))) + (entry-function ""))) (is (= "

http://foo https://bar/baz foo bar

" - (markdown/md-to-html-string " foo bar"))) + (entry-function " foo bar"))) (is (= "

abc@google.com

" - (org.apache.commons.lang.StringEscapeUtils/unescapeHtml - (markdown/md-to-html-string ""))))) - -(deftest references - (let [wrt (java.io.StringWriter.)] - (markdown/md-to-html (str "test" java.io.File/separator "references.md") wrt :reference-links? true) - (is (= (slurp (str "test" java.io.File/separator "references.html")) (.toString wrt))))) - -(deftest footnotes - (let [wrt (java.io.StringWriter.)] - (markdown/md-to-html (str "test" java.io.File/separator "footnotes.md") wrt :footnotes? true) - (is (= (slurp (str "test" java.io.File/separator "footnotes.html")) (.toString wrt))))) - -(deftest all-tegether - (let [wrt (java.io.StringWriter.)] - (markdown/md-to-html (str "test" java.io.File/separator "test.md") wrt) - (is (= (slurp (str "test" java.io.File/separator "test.html")) (.toString wrt))))) + (#?(:clj org.apache.commons.lang.StringEscapeUtils/unescapeHtml + :cljs goog.string/unescapeEntities) + (entry-function ""))))) (deftest not-a-list (is (= "

The fish was 192.8 lbs and was amazing to see.

" - (markdown/md-to-html-string "The fish was\n192.8 lbs and was amazing to see.")))) + (entry-function "The fish was\n192.8 lbs and was amazing to see.")))) (deftest dont-encode-chars-in-hrefs (is (= "

example_link with tilde ~ and carat ^ and splat *

" - (markdown/md-to-html-string "[example_link with tilde ~ and carat ^ and splat *](http://www.google.com/example_link_foo~_^*)")))) + (entry-function "[example_link with tilde ~ and carat ^ and splat *](http://www.google.com/example_link_foo~_^*)")))) (deftest complex-link-with-terminal-encoding-inside-header (is (= "

With a link the contents of the_link

" - (markdown/md-to-html-string "##With a link [the contents of the_link](http://a.com/under_score_in_the_link/)")))) + (entry-function "##With a link [the contents of the_link](http://a.com/under_score_in_the_link/)")))) (deftest two-links-tests-link-processing (is (= "

When you have a pair of links link1 and you want both Wow

" - (markdown/md-to-html-string "## When you have a pair of links [link1](http://123.com/1) and you want both [Wow](That's crazy)")))) + (entry-function "## When you have a pair of links [link1](http://123.com/1) and you want both [Wow](That's crazy)")))) (deftest parse-table-row (is (= (tables/parse-table-row "| table cell contents |") [{:text "table cell contents"}])) @@ -328,28 +317,8 @@ [{:text "-----"} {:text ":-----"} {:text "-----:"} {:text ":-----:"}]) [nil {:alignment :left} {:alignment :right} {:alignment :center}]))) -(deftest tables - (let [wrt (java.io.StringWriter.)] - (markdown/md-to-html (str "test" java.io.File/separator "tables.md") wrt) - (is (= (slurp (str "test" java.io.File/separator "tables.html")) (.toString wrt))))) - -(deftest md-metadata - (testing "Finds all metadata and correctly parses rest of file." - (let [md (slurp (str "test/metadata.md")) - {:keys [metadata html]} (markdown/md-to-html-string-with-meta md)] - (is (= "

The Document

" html)) - (is (= {:title ["My Document"] - :summary ["A brief description of my document."] - :authors ["Justin May" - "Spooky Mulder" - "End Line At End\n"] - :date ["October 31, 2015"] - :blank-value [] - :base_url ["http://example.com"]} - metadata))))) - (deftest n-dash - (is (= "

boo – bar

" (markdown/md-to-html-string "boo -- bar")))) + (is (= "

boo – bar

" (entry-function "boo -- bar")))) (deftest m-dash - (is (= "

boo — bar

" (markdown/md-to-html-string "boo --- bar")))) + (is (= "

boo — bar

" (entry-function "boo --- bar")))) diff --git a/test/mdtests_file.clj b/test/mdtests_file.clj new file mode 100644 index 0000000..f0ce1fb --- /dev/null +++ b/test/mdtests_file.clj @@ -0,0 +1,40 @@ +(ns mdtests-file + (:require [markdown.core :as markdown] + [markdown.transformers :as transformers] + [markdown.tables :as tables] + [clojure.test :refer :all])) + +(deftest references + (let [wrt (java.io.StringWriter.)] + (markdown/md-to-html (str "test" java.io.File/separator "references.md") wrt :reference-links? true) + (is (= (slurp (str "test" java.io.File/separator "references.html")) (.toString wrt))))) + +(deftest footnotes + (let [wrt (java.io.StringWriter.)] + (markdown/md-to-html (str "test" java.io.File/separator "footnotes.md") wrt :footnotes? true) + (is (= (slurp (str "test" java.io.File/separator "footnotes.html")) (.toString wrt))))) + +(deftest all-together + (let [wrt (java.io.StringWriter.)] + (markdown/md-to-html (str "test" java.io.File/separator "test.md") wrt) + (is (= (slurp (str "test" java.io.File/separator "test.html")) (.toString wrt))))) + +(deftest tables + (let [wrt (java.io.StringWriter.)] + (markdown/md-to-html (str "test" java.io.File/separator "tables.md") wrt) + (is (= (slurp (str "test" java.io.File/separator "tables.html")) (.toString wrt))))) + +(deftest md-metadata + (testing "Finds all metadata and correctly parses rest of file." + (let [md (slurp (str "test/metadata.md")) + {:keys [metadata html]} (markdown/md-to-html-string-with-meta md)] + (is (= "

The Document

" html)) + (is (= {:title ["My Document"] + :summary ["A brief description of my document."] + :authors ["Justin May" + "Spooky Mulder" + "End Line At End\n"] + :date ["October 31, 2015"] + :blank-value [] + :base_url ["http://example.com"]} + metadata)))))