From aac78c214ea953e4d1fcd84943c867395bfc22ec Mon Sep 17 00:00:00 2001 From: Asko Nomm Date: Sat, 9 Apr 2022 14:01:31 +0200 Subject: Add full test coverage for code blocks (re #4) --- project.clj | 3 +- resources/test/parsers/code_block_no_language.md | 41 ++++++++++++++++++++++ .../parsers/code_block_no_language_result.html | 39 ++++++++++++++++++++ test/clarktown/parsers/code_block_test.clj | 8 +++-- 4 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 resources/test/parsers/code_block_no_language.md create mode 100644 resources/test/parsers/code_block_no_language_result.html diff --git a/project.clj b/project.clj index 82812a6..f03e1ab 100644 --- a/project.clj +++ b/project.clj @@ -6,5 +6,6 @@ :deploy-repositories [["releases" {:sign-releases false :url "https://repo.clojars.org"}] ["snapshots" {:sign-releases false :url "https://repo.clojars.org"}]] :dependencies [[org.clojure/clojure "1.11.0"]] - :plugins [[lein-auto "0.1.3"]] + :plugins [[lein-auto "0.1.3"] + [lein-cloverage "1.2.3"]] :repl-options {:init-ns clarktown.core}) diff --git a/resources/test/parsers/code_block_no_language.md b/resources/test/parsers/code_block_no_language.md new file mode 100644 index 0000000..dbba1f1 --- /dev/null +++ b/resources/test/parsers/code_block_no_language.md @@ -0,0 +1,41 @@ +``` +// Detect horizontal line block +function isHorizontalLineBlock(block) { + return block === "***"; +} + +// Render horizontal line block +function horizontalLineBlock(block) { + return `
`; +} + +// 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(""); +} +``` \ No newline at end of file diff --git a/resources/test/parsers/code_block_no_language_result.html b/resources/test/parsers/code_block_no_language_result.html new file mode 100644 index 0000000..0512d3e --- /dev/null +++ b/resources/test/parsers/code_block_no_language_result.html @@ -0,0 +1,39 @@ +
// 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("");
+}
\ No newline at end of file diff --git a/test/clarktown/parsers/code_block_test.clj b/test/clarktown/parsers/code_block_test.clj index 20b69aa..8b1113d 100644 --- a/test/clarktown/parsers/code_block_test.clj +++ b/test/clarktown/parsers/code_block_test.clj @@ -6,6 +6,10 @@ (deftest code-block-test - (testing "Code block" + (testing "Code block with language specification" (is (= (slurp (io/file (io/resource "test/parsers/code_block_result.html"))) - (code-block/render (slurp (io/file (io/resource "test/parsers/code_block.md"))) nil))))) \ No newline at end of file + (code-block/render (slurp (io/file (io/resource "test/parsers/code_block.md"))) nil)))) + + (testing "Code block with NO language specification" + (is (= (slurp (io/file (io/resource "test/parsers/code_block_no_language_result.html"))) + (code-block/render (slurp (io/file (io/resource "test/parsers/code_block_no_language.md"))) nil))))) \ No newline at end of file -- cgit v1.2.3