From 52203a49aa544b2c11c96445d8732893160c436b Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Tue, 19 Apr 2022 17:50:19 +0300 Subject: WIP #16 Pretty much done. Needs more testing. And new documentation. --- src/clarktown/correctors/code_block.clj | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/clarktown/correctors/code_block.clj (limited to 'src/clarktown/correctors/code_block.clj') diff --git a/src/clarktown/correctors/code_block.clj b/src/clarktown/correctors/code_block.clj new file mode 100644 index 0000000..e767390 --- /dev/null +++ b/src/clarktown/correctors/code_block.clj @@ -0,0 +1,27 @@ +(ns clarktown.correctors.code-block + (:require + [clojure.string :as string])) + + +(defn empty-line-above? + [lines line index] + (and (= (string/trim line) "```") + (> index 0) + (->> (take index lines) + (filter #(= (string/trim %) "```")) + count + odd?) + (not (= (-> (nth lines (- index 1)) + string/trim) "")))) + + +(defn empty-line-below? + [lines line index] + (and (= (string/trim line) "```") + (< index (- (count lines) 1)) + (->> (take index lines) + (filter #(= (string/trim %) "```")) + count + even?) + (not (= (-> (nth lines (+ index 1)) + string/trim) "")))) \ No newline at end of file -- cgit v1.2.3 From 2b530b7c4e53596ce17e24f3fab42ab2f3c52ba8 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Fri, 22 Apr 2022 22:09:09 +0300 Subject: Implement tests for correctors --- src/clarktown/correctors/code_block.clj | 24 +++++++------- .../correctors/atx_heading_block_test.clj | 30 ++++++++++++++++++ test/clarktown/correctors/code_block_test.clj | 37 ++++++++++++++++++++++ 3 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 test/clarktown/correctors/atx_heading_block_test.clj create mode 100644 test/clarktown/correctors/code_block_test.clj (limited to 'src/clarktown/correctors/code_block.clj') diff --git a/src/clarktown/correctors/code_block.clj b/src/clarktown/correctors/code_block.clj index e767390..fcda19b 100644 --- a/src/clarktown/correctors/code_block.clj +++ b/src/clarktown/correctors/code_block.clj @@ -5,23 +5,23 @@ (defn empty-line-above? [lines line index] - (and (= (string/trim line) "```") - (> index 0) - (->> (take index lines) - (filter #(= (string/trim %) "```")) - count - odd?) - (not (= (-> (nth lines (- index 1)) - string/trim) "")))) + (let [occurences (->> (take index lines) + (filter #(string/starts-with? (string/trim %) "```")) + count)] + (and (string/starts-with? (string/trim line) "```") + (> index 0) + (even? occurences) + (not (= (-> (nth lines (- index 1)) + string/trim) ""))))) (defn empty-line-below? [lines line index] - (and (= (string/trim line) "```") + (and (string/starts-with? (string/trim line) "```") (< index (- (count lines) 1)) (->> (take index lines) - (filter #(= (string/trim %) "```")) + (filter #(string/starts-with? (string/trim %) "```")) count - even?) + odd?) (not (= (-> (nth lines (+ index 1)) - string/trim) "")))) \ No newline at end of file + string/trim) "")))) diff --git a/test/clarktown/correctors/atx_heading_block_test.clj b/test/clarktown/correctors/atx_heading_block_test.clj new file mode 100644 index 0000000..3ece304 --- /dev/null +++ b/test/clarktown/correctors/atx_heading_block_test.clj @@ -0,0 +1,30 @@ +(ns clarktown.correctors.atx-heading-block-test + (:require + [clojure.test :refer [deftest testing is]] + [clarktown.correctors.atx-heading-block :as corrector])) + + +(deftest atx-heading-block-corrector + (testing "Empty line above" + (let [line "# Hello" + lines ["Some text goes here" line] + index 1] + (is (true? (corrector/empty-line-above? lines line index))))) + + (testing "No empty line above" + (let [line "# Hello" + lines ["Some text goes here" "\n" line] + index 2] + (is (false? (corrector/empty-line-above? lines line index))))) + + (testing "Empty line below" + (let [line "# Hello" + lines [line "Some text goes here"] + index 0] + (is (true? (corrector/empty-line-below? lines line index))))) + + (testing "No empty line below" + (let [line "# Hello" + lines [line "\n" "Some text goes here"] + index 0] + (is (false? (corrector/empty-line-below? lines line index)))))) diff --git a/test/clarktown/correctors/code_block_test.clj b/test/clarktown/correctors/code_block_test.clj new file mode 100644 index 0000000..16711da --- /dev/null +++ b/test/clarktown/correctors/code_block_test.clj @@ -0,0 +1,37 @@ +(ns clarktown.correctors.code-block-test + (:require + [clojure.test :refer [deftest testing is]] + [clarktown.correctors.code-block :as corrector])) + + +(deftest code-block-corrector + (testing "Empty line above" + (let [line "```clojure" + lines ["Some text goes here" line "some code here" "```"] + index 1] + (is (true? (corrector/empty-line-above? lines line index))))) + + (testing "No empty line above" + (let [line "```" + lines ["Some text goes here" "\n" line "some code" "```"] + index 2] + (is (false? (corrector/empty-line-above? lines line index))))) + + (testing "Empty line below" + (let [line "```" + lines ["Some text goes here" line "some code" line "some text"] + index 3] + (is (true? (corrector/empty-line-below? lines line index))))) + + (testing "No empty line below" + (let [line "```" + lines ["Some text goes here" line "some code" line "\n" "some text"] + index 3] + (is (false? (corrector/empty-line-below? lines line index))))) + + (testing "No empty line below when ending with code block" + (let [line "```" + lines ["Some text goes here" line "some code" line] + index 3] + (is (false? (corrector/empty-line-below? lines line index)))))) + -- cgit v1.2.3 From 1cf289f67927c7e06cba818c00383ce1aae8ad67 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Sat, 23 Apr 2022 01:33:27 +0300 Subject: No need for `let` here --- src/clarktown/correctors/code_block.clj | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/clarktown/correctors/code_block.clj') diff --git a/src/clarktown/correctors/code_block.clj b/src/clarktown/correctors/code_block.clj index fcda19b..9b2f0f3 100644 --- a/src/clarktown/correctors/code_block.clj +++ b/src/clarktown/correctors/code_block.clj @@ -5,14 +5,14 @@ (defn empty-line-above? [lines line index] - (let [occurences (->> (take index lines) - (filter #(string/starts-with? (string/trim %) "```")) - count)] - (and (string/starts-with? (string/trim line) "```") - (> index 0) - (even? occurences) - (not (= (-> (nth lines (- index 1)) - string/trim) ""))))) + (and (string/starts-with? (string/trim line) "```") + (> index 0) + (->> (take index lines) + (filter #(string/starts-with? (string/trim %) "```")) + count + even?) + (not (= (-> (nth lines (- index 1)) + string/trim) "")))) (defn empty-line-below? -- cgit v1.2.3 From dc921cbe1c786995a8670efd9f5556ee9df064f9 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Sat, 23 Apr 2022 02:42:52 +0300 Subject: Add docstrings --- src/clarktown/correctors/atx_heading_block.clj | 12 +++++++++++- src/clarktown/correctors/code_block.clj | 10 ++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) (limited to 'src/clarktown/correctors/code_block.clj') diff --git a/src/clarktown/correctors/atx_heading_block.clj b/src/clarktown/correctors/atx_heading_block.clj index a792572..9ff789e 100644 --- a/src/clarktown/correctors/atx_heading_block.clj +++ b/src/clarktown/correctors/atx_heading_block.clj @@ -4,6 +4,11 @@ (defn empty-line-above? + "Determines whether there's a need for an empty new line + above the `line` at the current `index`. In the case of a + ATX heading block that starts with the `#` character, if + there's no empty newline above, we need to create one, and + so this function must then return `true`." [lines line index] (and (string/starts-with? (string/trim line) "#") (> index 0) @@ -12,8 +17,13 @@ (defn empty-line-below? + "Determines whether there's a need for an empty new line + below the `line` at the current `index`. In the case of a + ATX heading block that starts with the `#` character, if + there's no empty newline below, we need to create one, and + so this function must then return `true`." [lines line index] (and (string/starts-with? (string/trim line) "#") (< index (- (count lines) 1)) (not (= (-> (nth lines (+ index 1)) - string/trim) "")))) \ No newline at end of file + string/trim) "")))) diff --git a/src/clarktown/correctors/code_block.clj b/src/clarktown/correctors/code_block.clj index 9b2f0f3..73989fe 100644 --- a/src/clarktown/correctors/code_block.clj +++ b/src/clarktown/correctors/code_block.clj @@ -4,6 +4,11 @@ (defn empty-line-above? + "Determines whether there's a need for an empty new line + above the `line` at the current `index`. In the case of a + code block, which starts with three backticks (```), if there's + no empty newline above, we need to create one, and so this + function must then return `true`." [lines line index] (and (string/starts-with? (string/trim line) "```") (> index 0) @@ -16,6 +21,11 @@ (defn empty-line-below? + "Determines whether there's a need for an empty new line + below the `line` at the current `index`. In the case of a + code block, which ends with three backticks (```), if there's + no empty newline above, we need to create one, and so this + function must then return `true`." [lines line index] (and (string/starts-with? (string/trim line) "```") (< index (- (count lines) 1)) -- cgit v1.2.3