text_test.cljs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. (ns logseq.graph-parser.text-test
  2. (:require [cljs.test :refer [are deftest testing]]
  3. [logseq.graph-parser.text :as text]))
  4. (deftest test-get-page-name
  5. []
  6. (are [x y] (= (text/get-page-name x) y)
  7. "[[page]]" "page"
  8. "[[another page]]" "another page"
  9. "[single bracket]" nil
  10. "no brackets" nil
  11. "[[another page]]" "another page"
  12. "[[nested [[page]]]]" "nested [[page]]"
  13. "[[file:./page.org][page]]" "page"
  14. "[[file:./pages/page.org][page]]" "page"
  15. "[[file:./namespace.page.org][namespace/page]]" "namespace/page"
  16. "[[file:./pages/namespace.page.org][namespace/page]]" "namespace/page"
  17. "[[file:./pages/namespace.page.org][please don't change me]]" "namespace/page"
  18. "[page](file:./page.md)" "page"
  19. "[page](file:.pages/page.md)" "page"
  20. "[logseq/page](file:./logseq.page.md)" "logseq/page"
  21. "[logseq/page](file:./pages/logseq.page.md)" "logseq/page"))
  22. (deftest page-ref?
  23. []
  24. (are [x y] (= (text/page-ref? x) y)
  25. "[[page]]" true
  26. "[[another page]]" true
  27. "[single bracket]" false
  28. "no brackets" false))
  29. (deftest page-ref-un-brackets!
  30. []
  31. (are [x y] (= (text/page-ref-un-brackets! x) y)
  32. "[[page]]" "page"
  33. "[[another page]]" "another page"
  34. "[[nested [[page]]]]" "nested [[page]]"
  35. "[single bracket]" "[single bracket]"
  36. "no brackets" "no brackets"))
  37. (deftest sep-by-comma
  38. []
  39. (are [x y] (= (text/sep-by-comma x) y)
  40. "foo,bar" ["foo" "bar"]
  41. "foo, bar" ["foo" "bar"]
  42. "foo bar" ["foo bar"]
  43. "[[foo]] [[bar]]" ["[[foo]] [[bar]]"]
  44. "[[foo]],[[bar]]" ["[[foo]]", "[[bar]]"]
  45. "[[foo]], [[bar]]" ["[[foo]]", "[[bar]]"]
  46. "[[foo]]" ["[[foo]]"]
  47. "[[nested [[foo]]]]" ["[[nested [[foo]]]]"]))
  48. (deftest split-page-refs-without-brackets
  49. []
  50. (are [x y] (= (text/split-page-refs-without-brackets x) y)
  51. "foobar" "foobar"
  52. "foo bar" "foo bar"
  53. "foo, bar" #{"foo" "bar"}
  54. "[[foo]] [[bar]]" #{"foo" "bar"}
  55. "[[foo]],[[bar]]" #{"foo", "bar"}
  56. "[[foo]], [[bar]]" #{"foo", "bar"}
  57. "[[foo]]" #{"foo"}
  58. "[[nested [[foo]]]]" #{"nested [[foo]]"}
  59. "[[nested [[foo]]]], [[foo]]" #{"nested [[foo]]" "foo"}
  60. "[[nested [[foo]] [[bar]]]], [[foo]]" #{"nested [[foo]] [[bar]]" "foo"}
  61. "[[nested [[foo]], [[bar]]]], [[foo]]" #{"nested [[foo]], [[bar]]" "foo"}
  62. "#tag," #{"tag"}
  63. "#tag" #{"tag"}
  64. "#tag1,#tag2" #{"tag1" "tag2"}
  65. "[[Jan 26th, 2021]], hello" #{"hello" "Jan 26th, 2021"}))
  66. (def block-patterns
  67. {:markdown "-"
  68. :org "*"})
  69. (deftest remove-level-spaces
  70. []
  71. (testing "markdown"
  72. (are [x y] (= (text/remove-level-spaces x :markdown (block-patterns :markdown) true) y)
  73. "- foobar" "foobar"
  74. " - foobar" "foobar"))
  75. (testing "markdown without spaces between the `#` and title"
  76. (are [x y] (= (text/remove-level-spaces x :markdown (block-patterns :markdown)) y)
  77. "-foobar" "foobar"))
  78. (testing "org"
  79. (are [x y] (= (text/remove-level-spaces x :org (block-patterns :org) true) y)
  80. "* foobar" "foobar"
  81. "** foobar" "foobar"
  82. "********************* foobar" "foobar"))
  83. (testing "org without spaces between the `#` and title"
  84. (are [x y] (= (text/remove-level-spaces x :org (block-patterns :org)) y)
  85. "*foobar" "foobar"
  86. "**foobar" "foobar"
  87. "*********************foobar" "foobar")))
  88. (deftest test-add-timestamp
  89. []
  90. (are [x y] (= x y)
  91. (text/add-timestamp "LATER hello world\nhello"
  92. "scheduled"
  93. "<2021-08-25 Wed>")
  94. "LATER hello world\nSCHEDULED: <2021-08-25 Wed>\nhello"
  95. (text/add-timestamp "LATER hello world "
  96. "scheduled"
  97. "<2021-08-25 Wed>")
  98. "LATER hello world\nSCHEDULED: <2021-08-25 Wed>"
  99. (text/add-timestamp "LATER hello world\nfoo:: bar\ntest"
  100. "scheduled"
  101. "<2021-08-25 Wed>")
  102. "LATER hello world\nSCHEDULED: <2021-08-25 Wed>\nfoo:: bar\ntest"))
  103. (deftest get-string-all-indexes
  104. []
  105. (are [x y] (= x y)
  106. (text/get-string-all-indexes "[[hello]] [[world]]" "[[")
  107. [0 10]
  108. (text/get-string-all-indexes "abc abc ab" "ab")
  109. [0 4 8]
  110. (text/get-string-all-indexes "a.c a.c ab" "a.")
  111. [0 4]))
  112. (deftest test-parse-property
  113. (testing "parse-property"
  114. (are [k v y] (= (text/parse-property k v {}) y)
  115. :tags "foo" "foo"
  116. :tags "foo, bar" #{"foo" "bar"}
  117. :tags "foo,bar" #{"foo" "bar"}
  118. :tags "[[foo]]" #{"foo"}
  119. :tags "[[foo]] [[bar]]" #{"foo" "bar"}
  120. :tags "[[foo]], [[bar]]" #{"foo" "bar"}
  121. :tags "[[foo]], [[bar]], #baz" #{"foo" "bar" "baz"}
  122. :tags "#baz, [[foo]], [[bar]]" #{"foo" "bar" "baz"}
  123. :tags "[[foo [[bar]]]]" #{"foo [[bar]]"}
  124. :tags "[[foo [[bar]]]], baz" #{"baz" "foo [[bar]]"}))
  125. (testing "parse-property with quoted strings"
  126. (are [k v y] (= (text/parse-property k v {}) y)
  127. :tags "\"foo, bar\"" "\"foo, bar\""
  128. :tags "\"[[foo]], [[bar]]\"" "\"[[foo]], [[bar]]\""
  129. :tags "baz, \"[[foo]], [[bar]]\"" #{"baz"})))
  130. #_(cljs.test/test-ns 'logseq.graph-parser.text-test)