Fix wrapped-by? utility function

Improve readability of test failures, add more unit tests
This commit is contained in:
Phoenix Eliot
2022-08-04 18:17:09 -04:00
committed by Andelf
parent d0ff784ebe
commit bb199c4de5
3 changed files with 34 additions and 7 deletions

View File

@@ -89,7 +89,7 @@
"Get all indexes of `value` in the string `s`."
[s value {:keys [before?] :or {before? true}}]
(if (= value "")
(if before? [0] [(dec (count s))])
(if before? [0] [(count s)]) ;; Hack: this prevents unnecessary work in wrapped-by?
(loop [acc []
i 0]
(if-let [i (string/index-of s value i)]
@@ -99,10 +99,12 @@
(defn wrapped-by?
"`pos` must be wrapped by `before` and `and` in string `value`, e.g. ((a|b))"
[value pos before end]
;; Increment 'before' matches by (length of before string - 0.5) to make them be just before the cursor position they precede.
;; Increment 'after' matches by 0.5 to make them be just after the cursor position they follow.
(let [before-matches (->> (get-string-all-indexes value before {:before? true})
(map (fn [i] [i :before])))
(map (fn [i] [(+ i (- (count before) 0.5)) :before])))
end-matches (->> (get-string-all-indexes value end {:before? false})
(map (fn [i] [i :end])))
(map (fn [i] [(+ i 0.5) :end])))
indexes (sort-by first (concat before-matches end-matches [[pos :between]]))
ks (map second indexes)
q [:before :between :end]]