Skip to content

Commit

Permalink
fix error in related to direct-bindings
Browse files Browse the repository at this point in the history
did not exit bindings loop correctly and "matched" empty bindings instead
  • Loading branch information
simongray committed Mar 8, 2021
1 parent 054e6f7 commit a9d6a59
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/cuphic/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@
(cond
(= k v) kvs
(symbol? k) (conj kvs kv)
(vector? v) (apply conj kvs (bindings-delta k v))
(vector? v) (if-let [delta (bindings-delta k v)]
(apply conj kvs delta)
(reduced nil))
:else (reduced nil)))
[]
(map vector capture-pattern nodes))))
Expand Down Expand Up @@ -194,10 +196,11 @@
(when (<= min-count (count hnodes))
(case quantifier
;; With no quantifier, we simply capture the direct bindings.
nil (let [bound (subvec hnodes 0 min-count)
unbound (subvec hnodes min-count)
ret* (into ret (direct-bindings pattern bound))]
(recur ret* unbound patterns))
nil (let [bound (subvec hnodes 0 min-count)
unbound (subvec hnodes min-count)
dbindings (direct-bindings pattern bound)]
(when dbindings
(recur (into ret dbindings) unbound patterns)))

:prefix (let [[[quantifier] after] parts
bound (nodes-until next-pattern hnodes min-count)
Expand Down
21 changes: 19 additions & 2 deletions test/cuphic/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,16 @@
[:span {:id "b"}]]]
'[?tag {:id "nada"}]
'[:span {:id ?id}]
'[?tag {:id ?id}]))))
'[?tag {:id ?id}]))

(testing "should return only nil bindings for no matches"
(let [actual (scan [:div {}
[:p {:id "p"}
[:span {:id "span"}]]]

'[:div {} [:p {:id "nada"}]])]
(is (every? vector? (map first actual)))
(is (every? nil? (map second actual)))))))

(testing "scraping a Hiccup tree"
(let [actual (scrape [:div {}
Expand All @@ -298,7 +307,15 @@
:y [{?id "b"}]}
actual))

(testing "scrape includes metadata for the zipper loc"
(testing "should return an empty map when no bindings are found"
(is (= {}
(scrape [:div {}
[:p {:id "p"}
[:span {:id "span"}]]]

{:nada '[:div {} [:p {:id "nada"}]]}))))

(testing "should include metadata containing zipper locs"
(let [locs (->> (vals actual)
(apply concat)
(map (comp :loc meta)))
Expand Down

0 comments on commit a9d6a59

Please sign in to comment.