add Clarktown lib tests (#1247)

* add lib tests for clarktown

* add clarktown to library list

* make code block test agnostic to line separator
This commit is contained in:
Bob 2022-04-17 14:11:33 -04:00 committed by GitHub
parent 1f7dba35c9
commit 1113037b51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 545 additions and 2 deletions

View file

@ -134,7 +134,8 @@
aysylu/loom {:mvn/version "1.0.2"}
com.layerware/hugsql-core {:mvn/version "0.5.3"}
com.github.seancorfield/expectations {:mvn/version "2.0.157"}
com.rpl/specter {:mvn/version "1.1.4"}}
com.rpl/specter {:mvn/version "1.1.4"}
com.github.askonomm/clarktown {:mvn/version "1.1.2"}}
:classpath-overrides {org.clojure/clojure nil
org.clojure/spec.alpha nil}}
:clj-nvd

View file

@ -17,6 +17,7 @@ clojure-csv/clojure-csv,https://github.com/davidsantiago/clojure-csv
clojure-msgpack/clojure-msgpack,https://github.com/edma2/clojure-msgpack
clojure-term-colors/clojure-term-colors,https://github.com/trhura/clojure-term-colors
com.exoscale/lingo,https://github.com/exoscale/lingo
com.github.askonomm/clarktown,https://github.com/askonomm/clarktown
com.github.seancorfield/expectations,https://github.com/clojure-expectations/clojure-test
com.github.seancorfield/honeysql,https://github.com/seancorfield/honeysql
com.grammarly/omniconf,https://github.com/grammarly/omniconf

1 maven-name git-url
17 clojure-msgpack/clojure-msgpack https://github.com/edma2/clojure-msgpack
18 clojure-term-colors/clojure-term-colors https://github.com/trhura/clojure-term-colors
19 com.exoscale/lingo https://github.com/exoscale/lingo
20 com.github.askonomm/clarktown https://github.com/askonomm/clarktown
21 com.github.seancorfield/expectations https://github.com/clojure-expectations/clojure-test
22 com.github.seancorfield/honeysql https://github.com/seancorfield/honeysql
23 com.grammarly/omniconf https://github.com/grammarly/omniconf

View file

@ -808,6 +808,10 @@ Ahead-of-time function scheduler. Compatible with babashka 0.7.7+.
Graph library for Clojure. Compatible with babashka 0.7.8+.
### [Clarktown](https://github.com/askonomm/clarktown)
An extensible and modular zero-dependency, pure-Clojure Markdown parser.
## Pods
[Babashka pods](https://github.com/babashka/babashka.pods) are programs that can
@ -875,7 +879,7 @@ A babashka script to obtain covid-19 related information.
### [bb-spotify](https://github.com/kolharsam/bb-spotify)
Contol your spotify player using babashka.
Control your spotify player using babashka.
### [lambdaisland/open-source](https://github.com/lambdaisland/open-source)

View file

@ -112,4 +112,5 @@
com.layerware/hugsql-core {:test-namespaces (hugsql.babashka-test)}
com.github.seancorfield/expectations {:git-url "https://github.com/clojure-expectations/clojure-test", :test-namespaces (expectations.clojure.test-test), :git-sha "b30fefd97d9eb7d1f47e06956521f354cb926b03"}
com.rpl/specter {:git-url "https://github.com/redplanetlabs/specter", :test-namespaces (com.rpl.specter.cljs-test-helpers com.rpl.specter.test-helpers com.rpl.specter.core-test com.rpl.specter.zipper-test), :git-sha "67e86806020b9d02fbca8cdb1efad3002fc81a32"}
com.github.askonomm/clarktown {:git-url "https://github.com/askonomm/clarktown", :test-namespaces (clarktown.core-test clarktown.parsers.horizontal-line-block-test clarktown.parsers.italic-test clarktown.parsers.link-and-image-test clarktown.parsers.empty-block-test clarktown.parsers.inline-code-test clarktown.parsers.heading-block-test clarktown.parsers.bold-test clarktown.parsers.quote-block-test clarktown.parsers.code-block-test clarktown.parsers.strikethrough-test), :git-sha "059bfa7bd9bfdde0c75646bf1dfc20d23da8a02c"}
}

View file

@ -0,0 +1,93 @@
Lorem ipsum dolor **sit** amet. Lorem ipsum *dolor* _sit_ __amet__.
There's a [link here](https://example.com/that_has_things?!???!#in-it).
1. List item
2. Another list item
1. Sub list item
2. Another sub list item
1. Sub sub list item
3. Continuing sub list item
3. Continuing list item
```javascript
// Detect horizontal line block
function isHorizontalLineBlock(block) {
return block === "***";
}
// Render horizontal line block
function horizontalLineBlock(block) {
return `<hr>`;
}
// Compose an array of parsers
const parsers = [{
matcher: isHorizontalLineBlock,
renderers: [horizontalLineBlock]
}];
// And finally, our parser itself
function markdownToHTML(markdown) {
// Create blocks
const blocks = content.split(/\n\n/);
// Parse blocks
const parsedBlocks = blocks.map((block) => {
// Let's find a parser that has a matcher that matches
const parser = parsers.find((parser) => parser.matcher(block));
// If match was found, let's run our renderers over `block`
if (parser) {
for (const renderer of match.renderers) {
block = renderer(block);
}
}
return block;
});
// And at last, join the blocks together for one big block.
return parsedBlocks.join("");
}
```
- Test 123
- Test 223
- Test 334
1. Test test
This is ___bold italic text___ and ***this is also***. *What about italic text that **has bold text***?
## Hi there, world!
* List item
* Another list ~~item~~
* Sub list item
* Another sub list item
* Sub sub list item
* Continuing sub list item
* Continuing list item
***
* List item
* Another list item
* Sub list item
* Another sub list item
1. Sub sub list item
2. Continuing sub list item
* Continuing list item
This is a H1 heading with settext
=================================
And this is a H2 heading with settext
-------------------------------------
Testing paragraph right before a code block
```
code goes here
```
# Heading goes here
Paragraph right after heading

View file

@ -0,0 +1,69 @@
<p>Lorem ipsum dolor <strong>sit</strong> amet. Lorem ipsum <em>dolor</em> <em>sit</em> <strong>amet</strong>.</p>
<p>There's a <a href="https://example.com/that&#95;has&#95;things?!???!#in-it">link here</a>.</p>
<ol><li>List item</li><li>Another list item<ol><li>Sub list item</li><li>Another sub list item<ol><li>Sub sub list item</li></ol></li><li>Continuing sub list item</li></ol></li><li>Continuing list item</li></ol>
<pre><code class="language-javascript">// Detect horizontal line block
function isHorizontalLineBlock(block) {
return block === "***";
}
// Render horizontal line block
function horizontalLineBlock(block) {
return `&lt;hr&gt;`;
}
// Compose an array of parsers
const parsers = [{
matcher: isHorizontalLineBlock,
renderers: [horizontalLineBlock]
}];
// And finally, our parser itself
function markdownToHTML(markdown) {
// Create blocks
const blocks = content.split(/\n\n/);
// Parse blocks
const parsedBlocks = blocks.map((block) =&gt; {
// Let's find a parser that has a matcher that matches
const parser = parsers.find((parser) =&gt; parser.matcher(block));
// If match was found, let's run our renderers over `block`
if (parser) {
for (const renderer of match.renderers) {
block = renderer(block);
}
}
return block;
});
// And at last, join the blocks together for one big block.
return parsedBlocks.join("");
}</code></pre>
<ul><li>Test 123</li><li>Test 223<ul><li>Test 334<ol><li>Test test</li></ol></li></ul></li></ul>
<p>This is <em><strong>bold italic text</strong></em> and <em><strong>this is also</strong></em>. <em>What about italic text that <strong>has bold text</strong></em>?</p>
<h2>Hi there, world!</h2>
<ul><li>List item</li><li>Another list <del>item</del><ul><li>Sub list item</li><li>Another sub list item<ul><li>Sub sub list item</li><li>Continuing sub list item</li></ul></li></ul></li><li>Continuing list item</li></ul>
<hr>
<ul><li>List item</li><li>Another list item<ul><li>Sub list item</li><li>Another sub list item<ol><li>Sub sub list item</li><li>Continuing sub list item</li></ol></li></ul></li><li>Continuing list item</li></ul>
<h1>This is a H1 heading with settext</h1>
<h2>And this is a H2 heading with settext</h2>
<p>Testing paragraph right before a code block</p>
<pre><code>code goes here</code></pre>
<h1>Heading goes here</h1>
<p>Paragraph right after heading</p>

View file

@ -0,0 +1,15 @@
(ns clarktown.core-test
(:require
;; BB-TEST-PATCH: require clojure.string for split-lines patch below
[clojure.string :as str]
[clojure.test :refer [deftest testing is]]
[clojure.java.io :as io]
[clarktown.core :as core]))
(deftest overall-test
(testing "Overall"
;; BB-TEST-PATCH: library uses hard-coded \n, so using split-lines for platform-agnostic testing
;; BB-TEST-PATCH: change file paths to match bb folder structure (and copy resource files)
(is (= (str/split-lines (core/render (slurp (io/file (io/resource "clarktown/core.md")))))
(str/split-lines (slurp (io/file (io/resource "clarktown/core_result.html"))))))))

View file

@ -0,0 +1,18 @@
(ns clarktown.parsers.bold-test
(:require
[clojure.test :refer [deftest testing is]]
[clarktown.parsers.bold :as bold]))
(deftest bold-test
(testing "Creating bold text with two surrounding asterisk characters"
(is (= "<strong>This is bold.</strong>"
(bold/render "**This is bold.**" nil))))
(testing "Creating bold text with two surrounding underscore characters"
(is (= "<strong>This is bold.</strong>"
(bold/render "__This is bold.__" nil))))
(testing "Creating bold text with both underscores and asterisks mixed"
(is (= "Hi, my name is <strong>John</strong>, what is <strong>your name?</strong>"
(bold/render "Hi, my name is **John**, what is __your name?__" nil)))))

View file

@ -0,0 +1,41 @@
```javascript
// Detect horizontal line block
function isHorizontalLineBlock(block) {
return block === "***";
}
// Render horizontal line block
function horizontalLineBlock(block) {
return `<hr>`;
}
// Compose an array of parsers
const parsers = [{
matcher: isHorizontalLineBlock,
renderers: [horizontalLineBlock]
}];
// And finally, our parser itself
function markdownToHTML(markdown) {
// Create blocks
const blocks = content.split(/\n\n/);
// Parse blocks
const parsedBlocks = blocks.map((block) => {
// Let's find a parser that has a matcher that matches
const parser = parsers.find((parser) => parser.matcher(block));
// If match was found, let's run our renderers over `block`
if (parser) {
for (const renderer of match.renderers) {
block = renderer(block);
}
}
return block;
});
// And at last, join the blocks together for one big block.
return parsedBlocks.join("");
}
```

View file

@ -0,0 +1,41 @@
```
// Detect horizontal line block
function isHorizontalLineBlock(block) {
return block === "***";
}
// Render horizontal line block
function horizontalLineBlock(block) {
return `<hr>`;
}
// Compose an array of parsers
const parsers = [{
matcher: isHorizontalLineBlock,
renderers: [horizontalLineBlock]
}];
// And finally, our parser itself
function markdownToHTML(markdown) {
// Create blocks
const blocks = content.split(/\n\n/);
// Parse blocks
const parsedBlocks = blocks.map((block) => {
// Let's find a parser that has a matcher that matches
const parser = parsers.find((parser) => parser.matcher(block));
// If match was found, let's run our renderers over `block`
if (parser) {
for (const renderer of match.renderers) {
block = renderer(block);
}
}
return block;
});
// And at last, join the blocks together for one big block.
return parsedBlocks.join("");
}
```

View file

@ -0,0 +1,39 @@
<pre><code>// Detect horizontal line block
function isHorizontalLineBlock(block) {
return block === "***";
}
// Render horizontal line block
function horizontalLineBlock(block) {
return `&lt;hr&gt;`;
}
// Compose an array of parsers
const parsers = [{
matcher: isHorizontalLineBlock,
renderers: [horizontalLineBlock]
}];
// And finally, our parser itself
function markdownToHTML(markdown) {
// Create blocks
const blocks = content.split(/\n\n/);
// Parse blocks
const parsedBlocks = blocks.map((block) =&gt; {
// Let's find a parser that has a matcher that matches
const parser = parsers.find((parser) =&gt; parser.matcher(block));
// If match was found, let's run our renderers over `block`
if (parser) {
for (const renderer of match.renderers) {
block = renderer(block);
}
}
return block;
});
// And at last, join the blocks together for one big block.
return parsedBlocks.join("");
}</code></pre>

View file

@ -0,0 +1,39 @@
<pre><code class="language-javascript">// Detect horizontal line block
function isHorizontalLineBlock(block) {
return block === "***";
}
// Render horizontal line block
function horizontalLineBlock(block) {
return `&lt;hr&gt;`;
}
// Compose an array of parsers
const parsers = [{
matcher: isHorizontalLineBlock,
renderers: [horizontalLineBlock]
}];
// And finally, our parser itself
function markdownToHTML(markdown) {
// Create blocks
const blocks = content.split(/\n\n/);
// Parse blocks
const parsedBlocks = blocks.map((block) =&gt; {
// Let's find a parser that has a matcher that matches
const parser = parsers.find((parser) =&gt; parser.matcher(block));
// If match was found, let's run our renderers over `block`
if (parser) {
for (const renderer of match.renderers) {
block = renderer(block);
}
}
return block;
});
// And at last, join the blocks together for one big block.
return parsedBlocks.join("");
}</code></pre>

View file

@ -0,0 +1,18 @@
(ns clarktown.parsers.code-block-test
(:require
;; require clojure.string to accomodate line break hack below
[clojure.string :as str]
[clojure.test :refer [deftest testing is]]
[clojure.java.io :as io]
[clarktown.parsers.code-block :as code-block]))
;; BB-TEST-PATCH: change paths to match folder structure (and copy resource files)
;; BB-TEST-PATCH: use split-lines to make tests platform-agnostic
(deftest code-block-test
(testing "Code block with language specification"
(is (= (str/split-lines (slurp (io/file (io/resource "clarktown/parsers/code_block_result.html"))))
(str/split-lines (code-block/render (slurp (io/file (io/resource "clarktown/parsers/code_block.md"))) nil)))))
(testing "Code block with NO language specification"
(is (= (str/split-lines (slurp (io/file (io/resource "clarktown/parsers/code_block_no_language_result.html"))))
(str/split-lines (code-block/render (slurp (io/file (io/resource "clarktown/parsers/code_block_no_language.md"))) nil))))))

View file

@ -0,0 +1,14 @@
(ns clarktown.parsers.empty-block-test
(:require
[clojure.test :refer [deftest testing is]]
[clarktown.parsers.empty-block :as empty-block]))
(deftest empty-block-test
(testing "Rendering an empty block"
(is (= (empty-block/render "" nil)
"")))
(testing "Checking an empty block"
(is (true? (empty-block/is? "")))
(is (true? (empty-block/is? " ")))))

View file

@ -0,0 +1,44 @@
(ns clarktown.parsers.heading-block-test
(:require
[clojure.test :refer [deftest testing is]]
[clarktown.parsers.heading-block :as heading-block]))
(deftest hashbang-heading-test
(testing "Hashbang heading block that's a H1"
(is (= "<h1>This is a heading block.</h1>"
(heading-block/render "# This is a heading block." nil))))
(testing "Hashbang heading block that's a H2"
(is (= "<h2>This is a heading block.</h2>"
(heading-block/render "## This is a heading block." nil))))
(testing "Hashbang heading block that's a H3"
(is (= "<h3>This is a heading block.</h3>"
(heading-block/render "### This is a heading block." nil))))
(testing "Hashbang heading block that's a H4"
(is (= "<h4>This is a heading block.</h4>"
(heading-block/render "#### This is a heading block." nil))))
(testing "Hashbang heading block that's a H5"
(is (= "<h5>This is a heading block.</h5>"
(heading-block/render "##### This is a heading block." nil)))))
(deftest settext-heading-text
(testing "Settext heading block that's a H1"
(is (= "<h1>This is a heading block.</h1>"
(heading-block/render "This is a heading block.\n=========" nil))))
(testing "Settext heading block that's a H1 spanning multiple lines"
(is (= "<h1>This is a \nheading block spanning multiple lines.</h1>"
(heading-block/render "This is a \nheading block spanning multiple lines.\n========" nil))))
(testing "Settext heading block that's a H2"
(is (= "<h2>This is a heading block.</h2>"
(heading-block/render "This is a heading block.\n---------" nil))))
(testing "Settext heading block that's a H2 spanning multiple lines"
(is (= "<h2>This is a \nheading block spanning multiple lines.</h2>"
(heading-block/render "This is a \nheading block spanning multiple lines.\n--------" nil)))))

View file

@ -0,0 +1,21 @@
(ns clarktown.parsers.horizontal-line-block-test
(:require
[clojure.test :refer [deftest testing is]]
[clarktown.parsers.horizontal-line-block :as horizontal-line-block]))
(deftest horizontal-line-block-test
(testing "Creating a horizontal line"
(is (= "<hr>"
(horizontal-line-block/render "***" nil)))
(is (= "<hr>"
(horizontal-line-block/render "---" nil))))
(testing "Is a horizontal line block"
(is (true? (horizontal-line-block/is? "***")))
(is (true? (horizontal-line-block/is? " ***")))
(is (false? (horizontal-line-block/is? "Test *** 123")))
(is (true? (horizontal-line-block/is? "---")))
(is (true? (horizontal-line-block/is? " ---")))
(is (false? (horizontal-line-block/is? "Test --- 123")))))

View file

@ -0,0 +1,14 @@
(ns clarktown.parsers.inline-code-test
(:require
[clojure.test :refer [deftest testing is]]
[clarktown.parsers.inline-code :as inline-code]))
(deftest inline-code-test
(testing "Creating inline code text"
(is (= "<code>This is inline code.</code>"
(inline-code/render "`This is inline code.`" nil))))
(testing "Creating inline-code text in the middle of regular text"
(is (= "This is regular text, mixed with <code>some inline code.</code>, and it's great."
(inline-code/render "This is regular text, mixed with `some inline code.`, and it's great." nil)))))

View file

@ -0,0 +1,18 @@
(ns clarktown.parsers.italic-test
(:require
[clojure.test :refer [deftest testing is]]
[clarktown.parsers.italic :as italic]))
(deftest italic-test
(testing "Creating italic text with one surrounding asterisk character"
(is (= "<em>This is italic.</em>"
(italic/render "*This is italic.*" nil))))
(testing "Creating italic text with one surrounding underscore character"
(is (= "<em>This is italic.</em>"
(italic/render "_This is italic._" nil))))
(testing "Creating italic text with both underscores and asterisks mixed"
(is (= "Hi, my name is <em>John</em>, what is <em>your name?</em>"
(italic/render "Hi, my name is *John*, what is _your name?_" nil)))))

View file

@ -0,0 +1,23 @@
(ns clarktown.parsers.link-and-image-test
(:require
[clojure.test :refer [deftest testing is]]
[clarktown.parsers.link-and-image :as link-and-image]))
(deftest link-test
(testing "Creating a link"
(is (= (link-and-image/render "[This is a link](https://example.com)" nil)
"<a href=\"https://example.com\">This is a link</a>"))
(is (= (link-and-image/render "[This-is-a-link](https://example.com)" nil)
"<a href=\"https://example.com\">This-is-a-link</a>"))
(is (= (link-and-image/render "[x] [label](link)" nil)
"[x] <a href=\"link\">label</a>"))
(is (= (link-and-image/render "[ ] [label](link)" nil)
"[ ] <a href=\"link\">label</a>")))
(testing "Creating an image"
(is (= (link-and-image/render "![This is an image](https://example.com)" nil)
"<img src=\"https://example.com\" alt=\"This is an image\">"))))

View file

@ -0,0 +1,15 @@
(ns clarktown.parsers.quote-block-test
(:require
[clojure.test :refer [deftest testing is]]
[clarktown.parsers.quote-block :as quote-block]))
(deftest quote-block-block-test
(testing "Creating a quote block line"
(is (= (quote-block/render "> First line\n> second line" nil)
"<blockquote>First line\nsecond line</blockquote>")))
(testing "Checking a quote block"
(is (true? (quote-block/is? "> Test")))
(is (true? (quote-block/is? " > Test")))
(is (true? (quote-block/is? ">")))))

View file

@ -0,0 +1,14 @@
(ns clarktown.parsers.strikethrough-test
(:require
[clojure.test :refer [deftest testing is]]
[clarktown.parsers.strikethrough :as strikethrough]))
(deftest strikethrough-test
(testing "Creating strikethrough text"
(is (= (strikethrough/render "~~This is strikethrough text.~~" nil)
"<del>This is strikethrough text.</del>")))
(testing "Creating strikethrough text mixed with regular text"
(is (= (strikethrough/render "Some other text, ~~This is strikethrough text.~~ And more text." nil)
"Some other text, <del>This is strikethrough text.</del> And more text."))))