Browse Source

test: add frontend.text tests

Related to #1017
Tienson Qin 5 years ago
parent
commit
4c5eaec5a2
3 changed files with 151 additions and 4 deletions
  1. 5 3
      src/main/frontend/text.cljs
  2. 1 1
      src/main/frontend/version.cljs
  3. 145 0
      src/test/frontend/text_test.cljs

+ 5 - 3
src/main/frontend/text.cljs

@@ -23,7 +23,7 @@
       s)))
 
 ;; E.g "Foo Bar \"Bar Baz\""
-(defn- sep-by-comma-or-quote
+(defn sep-by-comma-or-quote
   [s]
   (when s
     (some->>
@@ -99,8 +99,10 @@
         [title-lines properties-and-body] (split-with (fn [l] (not (string/starts-with? (string/upper-case (string/triml l)) ":PROPERTIES:"))) lines)
         body (drop-while (fn [l]
                            (let [l' (string/lower-case (string/trim l))]
-                             (and (string/starts-with? l' ":")
-                                  (not (string/starts-with? l' ":end:")))))
+                             (or
+                              (and (string/starts-with? l' ":")
+                                   (not (string/starts-with? l' ":end:")))
+                              (string/blank? l))))
                          properties-and-body)
         body (if (and (seq body)
                       (string/starts-with? (string/lower-case (string/triml (first body))) ":end:"))

+ 1 - 1
src/main/frontend/version.cljs

@@ -1,3 +1,3 @@
 (ns frontend.version)
 
-(defonce version "0.0.4.9-13")
+(defonce version "0.0.4.9-14")

+ 145 - 0
src/test/frontend/text_test.cljs

@@ -2,6 +2,119 @@
   (:require [frontend.text :as text]
             [cljs.test :refer [deftest is are testing use-fixtures]]))
 
+(deftest page-ref?
+  []
+  (are [x y] (= (text/page-ref? x) y)
+    "[[page]]" true
+    "[[another page]]" true
+    "[single bracket]" false
+    "no brackets" false))
+
+(deftest page-ref-un-brackets!
+  []
+  (are [x y] (= (text/page-ref-un-brackets! x) y)
+    "[[page]]" "page"
+    "[[another page]]" "another page"
+    "[[nested [[page]]]]" "nested [[page]]"
+    "[single bracket]" "[single bracket]"
+    "no brackets" "no brackets"))
+
+(deftest sep-by-comma-or-quote
+  []
+  (are [x y] (= (text/sep-by-comma-or-quote x) y)
+    "foo,bar" ["foo" "bar"]
+    "foo, bar" ["foo" "bar"]
+    "foo bar" ["foo bar"]
+    "foo \"bar\"" ["foo" "bar"]
+    "[[foo]] [[bar]]" ["[[foo]] [[bar]]"]
+    "[[foo]],[[bar]]" ["[[foo]]", "[[bar]]"]
+    "[[foo]], [[bar]]" ["[[foo]]", "[[bar]]"]
+    "[[foo]]" ["[[foo]]"]
+    "[[nested [[foo]]]]" ["[[nested [[foo]]]]"]))
+
+(defn split-page-refs-without-brackets
+  []
+  (are [x y] (= (text/split-page-refs-without-brackets x) y)
+    "foobar" "foobar"
+    "foo bar" "foo bar"
+    "foo, bar" #{"foo" "bar"}
+    "foo \"bar\"" #{"foo" "bar"}
+    "[[foo]] [[bar]]" #{"[[foo]] [[bar]]"}
+    "[[foo]],[[bar]]" #{"foo", "bar"}
+    "[[foo]], [[bar]]" #{"foo", "bar"}
+    "[[foo]]" "foo"
+    "[[nested [[foo]]]]" "nested [[foo]]"
+    "[[nested [[foo]]]], [[foo]]" #{"nested [[foo]]" "foo"}))
+
+(defn extract-level-spaces
+  []
+  (testing "markdown"
+    (are [x y] (= (text/extract-level-spaces x :markdown) y)
+     "# foobar" "# "
+     "##   foobar" "##   "
+     "#####################   foobar" "#####################   "))
+  (testing "org mode"
+    (are [x y] (= (text/extract-level-spaces x :org) y)
+      "* foobar" "* "
+      "**   foobar" "**   "
+      "*********************  foobar" "*********************  ")))
+
+(defn remove-level-spaces
+  []
+  (testing "markdown"
+    (are [x y] (= (text/remove-level-spaces x :markdown true) y)
+      "# foobar" "foobar"
+      "##   foobar" "foobar"
+      "#####################   foobar" "foobar"))
+  (testing "markdown without spaces between the `#` and title"
+    (are [x y] (= (text/remove-level-spaces x :markdown) y)
+      "#foobar" "foobar"
+      "##foobar" "foobar"
+      "#####################foobar" "foobar"))
+  (testing "org"
+    (are [x y] (= (text/remove-level-spaces x :org true) y)
+      "* foobar" "foobar"
+      "**   foobar" "foobar"
+      "*********************   foobar" "foobar"))
+  (testing "org without spaces between the `#` and title"
+      (are [x y] (= (text/remove-level-spaces x :org) y)
+        "*foobar" "foobar"
+        "**foobar" "foobar"
+        "*********************foobar" "foobar")))
+
+(defn append-newline-after-level-spaces
+  []
+  (are [x y] (= (text/append-newline-after-level-spaces x :markdown) y)
+    "# foobar" "#\nfoobar"
+    "# foobar\nfoo" "#\nfoobar\nfoo"
+    "## foobar\nfoo" "##\nfoobar\nfoo")
+
+  (are [x y] (= (text/append-newline-after-level-spaces x :org) y)
+    "* foobar" "*\nfoobar"
+    "* foobar\nfoo" "*\nfoobar\nfoo"
+    "** foobar\nfoo" "**\nfoobar\nfoo"))
+
+(defn remove-id-property
+  []
+  (are [x y] (= (text/remove-id-property x) y)
+    "hello\n:PROPERTIES:\n:id: f9873a81-07b9-4246-b910-53a6f5ec7e04\n:END:\n"
+    "hello\n:PROPERTIES:\n:END:"
+
+    "hello\n:PROPERTIES:\n:id: f9873a81-07b9-4246-b910-53a6f5ec7e04\na: b\n:END:\n"
+    "hello\n:PROPERTIES:\na: b\n:END:"))
+
+(defn remove-timestamp-property!
+  []
+  (are [x y] (= (text/remove-timestamp-property! x) y)
+    "hello\n:PROPERTIES:\n:created_at: 1\n:END:\n"
+    "hello\n:PROPERTIES:\n:END:"
+
+    "hello\n:PROPERTIES:\n:created_at: 1\n:last_modified_at: 2\n:END:\n"
+    "hello\n:PROPERTIES:\n:END:"
+
+    "hello\n:PROPERTIES:\n:a: b\n:created_at: 1\n:last_modified_at: 2\n:END:\n"
+    "hello\n:PROPERTIES:\n:a: b\n:END:"))
+
 (deftest re-construct-block-properties
   []
   (testing "block content without a title"
@@ -41,4 +154,36 @@ test
       (text/re-construct-block-properties :markdown "## hello\n:PROPERTIES:\n:x: y\n:END:\n" {"x" "y" "a" "b"} true)
       "## hello\n:PROPERTIES:\n:x: y\n:a: b\n:END:\n")))
 
+(defn test-remove-properties!
+  []
+  (testing "properties with non-blank lines"
+    (are [x y] (= x y)
+      (text/remove-properties! "** hello\n:PROPERTIES:\n:x: y\n:END:\n")
+      "** hello"
+
+      (text/remove-properties! "** hello\n:PROPERTIES:\n:x: y\na:b\n:END:\n")
+      "** hello"))
+  (testing "properties with blank lines"
+    (are [x y] (= x y)
+      (text/remove-properties! "** hello\n:PROPERTIES:\n\n:x: y\n:END:\n")
+      "** hello"
+
+      (text/remove-properties! "** hello\n:PROPERTIES:\n:x: y\n\na:b\n:END:\n")
+      "** hello")))
+
+(defn test-insert-property
+  []
+  (are [x y] (= x y)
+    (text/insert-property "hello" "a" "b")
+    "hello\n:PROPERTIES:\n:a: b\n:END:\n"
+
+    (text/insert-property "hello" "a" false)
+    "hello\n:PROPERTIES:\n:a: false\n:END:\n"
+
+    (text/insert-property "hello\n:PROPERTIES:\n:a: b\n:END:\n" "c" "d")
+    "hello\n:PROPERTIES:\n:a: b\n:c: d\n:END:"
+
+    (text/insert-property "hello\n:PROPERTIES:\n:a: b\n:END: world\n" "c" "d")
+    "hello\n:PROPERTIES:\n:c: d\n:END:\n:PROPERTIES:\n:a: b\n:END: world\n"))
+
 #_(cljs.test/test-ns 'frontend.text-test)