Skip to content

Commit

Permalink
Moved some comments into proper tests which will make sure that the b…
Browse files Browse the repository at this point in the history
…ehavior of the parsing is consistent across the runtime environments.
  • Loading branch information
green-coder committed Apr 5, 2019
1 parent aecd094 commit 2644c25
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
20 changes: 0 additions & 20 deletions src/hopen/syntax/handlebars.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
(re-quote close-delim)))]
(re-find re segment)))

(comment
(parse-change-delim "= < > =}}blah blah" "}}"))

(defn- parse-text-segments
"Parses and collects all the text segments until a syntax block is found.
Expand Down Expand Up @@ -59,18 +56,6 @@
open-delim
close-delim])))

(comment
(parse-text-segments (text->segment "")
"{{" "}}")
(parse-text-segments (text->segment "{{ aa }} bb")
"{{" "}}")
(parse-text-segments (text->segment "aa {{ bb }} cc")
"{{" "}}")
(parse-text-segments (text->segment "aa {{= { } =}} bb { cc } dd")
"{{" "}}")
(parse-text-segments (text->segment "aa {{= {{{ }}} =}} bb")
"{{" "}}"))

(defn- parse-syntax-segment
"Parses the syntax segment, assuming that this function is provided a segment
at the start of that syntax segment.
Expand All @@ -82,11 +67,6 @@
(sub-sequence segment (+ index (count close-delim)))]
(throw-exception close-delim-not-found-msg)))

(comment
(parse-syntax-segment (text->segment "aoeu") "}}")
(parse-syntax-segment (text->segment "aoeu}}") "}}")
(parse-syntax-segment (text->segment "aoeu}}blah blah") "}}"))

(defn partition-template [delimiters]
(fn [rf]
(fn
Expand Down
46 changes: 45 additions & 1 deletion test/hopen/syntax/handlebars_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,51 @@
(:require #?(:clj [clojure.test :refer [deftest testing is are]]
:cljs [cljs.test :refer [deftest testing is are]
:include-macros true])
[hopen.syntax.handlebars :refer [parser]]))
[hopen.util :refer [triml]]
[hopen.syntax.handlebars :as hb :refer [parser]]
[instaparse.gll :refer [text->segment]]))

(deftest parse-change-delim-test
(is (= (#'hb/parse-change-delim "= < > =}}blah blah" "}}")
["= < > =}}" "<" ">"])))

(deftest parse-text-segments-test
(let [tx (fn [[text-segments next-segment << >>]]
[(when text-segments (into (empty text-segments) (map str) text-segments))
(when next-segment (str next-segment))
<<
>>])]
(are [template expected-result]
(= (tx (#'hb/parse-text-segments (text->segment template) "{{" "}}"))
(tx expected-result))

""
[[] nil "{{" "}}"]

"{{ aa }} bb"
[[] " aa }} bb" "{{" "}}"]

"aa {{ bb }} cc"
[["aa "] " bb }} cc" "{{" "}}"]

"aa {{= { } =}} bb { cc } dd"
[["aa " " bb "] " cc } dd" "{" "}"]

"aa {{= {{{ }}} =}} bb"
[["aa " " bb"] nil "{{{" "}}}"])))

(deftest parse-syntax-segment-test
(let [tx (fn [[syntax-segment next-segment]]
[(when syntax-segment (str syntax-segment))
(when next-segment (str next-segment))])]
(are [syntax-subs expected-result]
(= (tx (#'hb/parse-syntax-segment (text->segment syntax-subs) "}}"))
(tx expected-result))
"aoeu}}" ["aoeu" ""]
"aoeu}}blah blah" ["aoeu" "blah blah"]))

(is (thrown? Exception
(#'hb/parse-syntax-segment (text->segment "aoeu") "}}"))))

(deftest parser-test
(testing "Parser's conformity"
Expand Down

0 comments on commit 2644c25

Please sign in to comment.