Fix bugs with tests

- Quoted property values were extracting refs
- Punctuation at end of tags was getting included in tag
- Property values were only getting displayed as refs/relationships
  instead of full property value
- cli wasn't passing along user config
This commit is contained in:
Gabriel Horner
2022-08-11 16:45:35 -04:00
parent 8d471bd0c1
commit 509422901a
10 changed files with 78 additions and 24 deletions

View File

@@ -72,7 +72,8 @@ frontend.util/trace!
frontend.util.pool/terminate-pool!
;; Repl fn
frontend.util.property/add-page-properties
;; Test runner used by shadow
;; Test runners used by shadow
frontend.test.node-test-runner/main
frontend.test.frontend-node-test-runner/main
;; Test runner for nbb
logseq.graph-parser.nbb-test-runner/run-tests

View File

@@ -18,3 +18,7 @@ logseq.graph-parser.util.page-ref/left-and-right-brackets
logseq.graph-parser.util.page-ref/->page-ref
;; API
logseq.graph-parser.util.page-ref/get-page-name!
;; API
logseq.graph-parser.property/->block-content
;; API
logseq.graph-parser.property/property-value-from-content

View File

@@ -157,6 +157,8 @@
distinct)
[]))
;; TODO: Use text/parse-property to determine refs rather than maintain this similar
;; implementation to parse-property
(defn- get-page-ref-names-from-properties
[format properties user-config]
(let [page-refs (->>
@@ -175,7 +177,9 @@
(not (gp-mldoc/link? format v)))
(let [v (string/trim v)
result (if (:property-values-allow-links-and-text? user-config)
(text/extract-page-refs-and-tags v)
(if (gp-util/wrapped-by-quotes? v)
[]
(text/extract-page-refs-and-tags v))
(text/split-page-refs-without-brackets v {:un-brackets? false}))]
(if (coll? result)
(map text/page-ref-un-brackets! result)

View File

@@ -44,7 +44,8 @@ TODO: Fail fast when process exits 1"
(defn- parse-files
[conn files {:keys [config] :as options}]
(let [extract-options (merge {:date-formatter (gp-config/get-date-formatter config)}
(let [extract-options (merge {:date-formatter (gp-config/get-date-formatter config)
:user-config config}
(select-keys options [:verbose]))]
(mapv
(fn [{:file/keys [path content]}]

View File

@@ -15,6 +15,12 @@
(map #(str (name (key %)) (str colons " ") (val %)))
(string/join "\n")))
(defn property-value-from-content
"Extracts full property value from block content"
[property content]
(second (re-find (re-pattern (str property colons "\\s+(.*)"))
content)))
(defn properties-ast?
[block]
(and

View File

@@ -214,7 +214,9 @@
(parse-long v)))
(def ^:private page-ref-or-tag-re
(re-pattern (str "#?" (page-ref/->page-ref-re-str "(.*?)") "|#(\\S+)")))
(re-pattern (str "#?" (page-ref/->page-ref-re-str "(.*?)") "|"
;; Don't capture punctuation at end of a tag
"#([\\S]+[^\\s.!,])")))
(defn extract-page-refs-and-tags
"Returns set of page-refs and tags in given string or returns string if none

View File

@@ -1,5 +1,5 @@
(ns logseq.graph-parser.property-test
(:require [cljs.test :refer [are deftest]]
(:require [cljs.test :refer [are deftest is]]
[logseq.graph-parser.property :as gp-property]))
(deftest test->new-properties
@@ -24,3 +24,16 @@
"hello\n:PROPERTIES:\n:foo: bar\n:nice\n:END:\nnice"
"hello\nfoo:: bar\n:nice\nnice"))
(deftest property-value-from-content
(is (= "62b38254-4be7-4627-a2b7-6d9ee20999e5"
(gp-property/property-value-from-content
"id"
"type:: blog-posting\ndesc:: nice walkthrough on creating a blog with #nbb\nid:: 62b38254-4be7-4627-a2b7-6d9ee20999e5"))
"Pulls value from end of block content")
(is (= "nice walkthrough on creating a blog with #nbb"
(gp-property/property-value-from-content
"desc"
"type:: blog-posting\ndesc:: nice walkthrough on creating a blog with #nbb\nid:: 62b38254-4be7-4627-a2b7-6d9ee20999e5"))
"Pulls value from middle of block content"))

View File

@@ -1,5 +1,5 @@
(ns logseq.graph-parser.text-test
(:require [cljs.test :refer [are deftest testing]]
(:require [cljs.test :refer [are deftest testing is]]
[logseq.graph-parser.text :as text]))
(deftest test-get-page-name
@@ -109,4 +109,9 @@
:tags "\"[[foo]], [[bar]]\"" "\"[[foo]], [[bar]]\""
:tags "baz, \"[[foo]], [[bar]]\"" #{"baz"})))
(deftest extract-page-refs-and-tags
(is (= #{"cljs" "nbb" "js" "amazing"}
(text/extract-page-refs-and-tags "This project is written with #cljs, #nbb and #js. #amazing!"))
"Don't extract punctation at end of a tag"))
#_(cljs.test/test-ns 'logseq.graph-parser.text-test)

View File

@@ -70,6 +70,32 @@
(testing "Sort order and persistence of 10 properties"
(test-property-order 10)))
(defn- quoted-property-values-test
[user-config]
(let [conn (ldb/start-conn)
_ (graph-parser/parse-file conn
"foo.md"
"- desc:: \"#foo is not a ref\""
{:extract-options {:user-config user-config}})
block (->> (d/q '[:find (pull ?b [* {:block/refs [*]}])
:in $
:where [?b :block/properties]]
@conn)
(map first)
first)]
(is (= {:desc "\"#foo is not a ref\""}
(:block/properties block))
"Quoted value is unparsed")
(is (= ["desc"]
(map :block/original-name (:block/refs block)))
"No refs from property value")))
(deftest quoted-property-values
(testing "With default config"
(quoted-property-values-test {}))
(testing "With :property-values-allow-links-and-text config"
(quoted-property-values-test {:property-values-allow-links-and-text? true})))
(deftest page-properties-persistence
(testing "Non-string property values"
(let [conn (ldb/start-conn)]
@@ -85,21 +111,6 @@
(map (comp :block/properties first))
first)))))
(testing "Other special property cases"
(let [conn (ldb/start-conn)]
(graph-parser/parse-file conn
"foo.md"
"- desc:: \"true\""
{})
(is (= {:desc "\"true\""}
(->> (d/q '[:find (pull ?b [*])
:in $
:where [?b :block/properties]]
@conn)
(map (comp :block/properties first))
first))
"Quoted values are unparsed")))
(testing "Linkable built-in properties"
(let [conn (ldb/start-conn)
_ (graph-parser/parse-file conn

View File

@@ -63,6 +63,7 @@
[logseq.graph-parser.config :as gp-config]
[logseq.graph-parser.mldoc :as gp-mldoc]
[logseq.graph-parser.text :as text]
[logseq.graph-parser.property :as gp-property]
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.util.page-ref :as page-ref]
[logseq.graph-parser.util.block-ref :as block-ref]
@@ -1803,9 +1804,15 @@
[:span ", "])
(rum/defc property-cp
[config block k v]
(let [date (and (= k :date) (date/get-locale-string (str v)))
property-pages-enabled? (contains? #{true nil} (:property-pages/enabled? (state/get-config)))]
[config block k value]
(let [date (and (= k :date) (date/get-locale-string (str value)))
user-config (state/get-config)
;; In this mode and when value is a set of refs, display full property text
;; because :block/properties value only contains refs but user wants to see text
v (if (and (:property-values-allow-links-and-text? user-config) (coll? value))
(gp-property/property-value-from-content (name k) (:block/content block))
value)
property-pages-enabled? (contains? #{true nil} (:property-pages/enabled? user-config))]
[:div
(if property-pages-enabled?
(page-cp (assoc config :property? true) {:block/name (subs (str k) 1)})