From 977f504cded830cf759e7606a81152d2894c912d Mon Sep 17 00:00:00 2001 From: Eva Marco Date: Wed, 11 Mar 2026 11:44:27 +0100 Subject: [PATCH] :recycle: Add more test --- common/src/app/common/types/token.cljc | 82 ++++++++++++++++--- .../test/common_tests/types/token_test.cljc | 48 ++++++++++- 2 files changed, 119 insertions(+), 11 deletions(-) diff --git a/common/src/app/common/types/token.cljc b/common/src/app/common/types/token.cljc index 8781da0b1f..512be8c570 100644 --- a/common/src/app/common/types/token.cljc +++ b/common/src/app/common/types/token.cljc @@ -639,6 +639,9 @@ (cond-> {:weight weight} italic? (assoc :style "italic"))))) + +;;;;;; combobox token parsing + (defn inside-ref? [value position] (let [left-part (str/slice value 0 position) @@ -651,28 +654,87 @@ :else (< last-index-close last-index-open)))) +(defn nth-last-index-of + [string char n] + (loop [string' string + count 1] + (let [index (str/last-index-of string' char)] + (cond + (nil? index) nil + (= count n) index + :else (recur (str/slice string' 0 index) + (inc count)))))) + +(defn nth-index-of + [string char n] + (loop [string' string + offset 0 + count 1] + (let [index (str/index-of string' char)] + (cond + (nil? index) nil + (= count n) (+ index offset) + :else (recur (str/slice string' (inc index)) + (+ offset index 1) + (inc count)))))) + (defn start-ref-position [value position] (let [left-part (str/slice value 0 position) - last-index-open (str/last-index-of left-part "{")] + open-pos (str/last-index-of left-part "{") + space-pos (str/last-index-of left-part " ") + space-pos (when space-pos (+ 1 space-pos)) + first-position (->> [open-pos space-pos] + (remove nil?) + (sort) + (last))] + first-position)) + +(defn start-ref-position-inside-ref + [value position] + (let [left-part (str/slice value 0 position) + last-index-open (nth-last-index-of left-part "{" 1)] last-index-open)) -(defn end-ref-position +(defn end-ref-position-inside-ref [value position] (let [right-part (str/slice value position) - next-index-close (str/index-of right-part "}")] - (if (some? next-index-close) - (+ position next-index-close 1) - position))) + first-index-close (nth-index-of right-part "}" 1)] + first-index-close)) + +(defn inside-closed-ref? + [value position] + (let [left-part (str/slice value 0 position) + open-pos (nth-last-index-of left-part "{" 1) + spaces-pos-before (nth-last-index-of left-part " " 1) + right-part (str/slice value position) + close-pos (nth-index-of right-part "}" 1) + spaces-pos-after (nth-index-of right-part " " 1) + open-after-space? (or (nil? spaces-pos-before) + (> open-pos spaces-pos-before)) + close-before-space? (or (nil? spaces-pos-after) + (< close-pos spaces-pos-after))] + (and open-pos + close-pos + open-after-space? + close-before-space?))) (defn insert-ref [value position name] (let [reference (str "{" name "}")] (if (inside-ref? value position) - (let [first-part (str/slice value 0 (start-ref-position value position)) - second-part (str/slice value (end-ref-position value position))] - {:result (str first-part reference second-part) - :position (+ (count first-part) (count reference))}) + (if (inside-closed-ref? value position) + (let [first-part (str/slice value 0 (start-ref-position-inside-ref value position)) + end-position (+ position (end-ref-position-inside-ref value position) 1) + second-part (str/slice value end-position)] + {:result (str first-part reference second-part) + :position (+ (count first-part) (count reference))}) + + (let [first-part (str/slice value 0 (start-ref-position value position)) + second-part (str/slice value position)] + {:result (str first-part reference second-part) + :position (+ (count first-part) (count reference))})) + (let [first-part (str/slice value 0 position) second-part (str/slice value position)] {:result (str first-part reference second-part) diff --git a/common/test/common_tests/types/token_test.cljc b/common/test/common_tests/types/token_test.cljc index 08b06d99db..48e8e58d65 100644 --- a/common/test/common_tests/types/token_test.cljc +++ b/common/test/common_tests/types/token_test.cljc @@ -30,6 +30,7 @@ (t/testing "empty value" (t/is (= (cto/insert-ref "" 0 "token1") {:result "{token1}" :position 8}))) + (t/testing "value without references" (t/is (= (cto/insert-ref "ABC" 0 "token1") {:result "{token1}ABC" :position 8})) @@ -37,8 +38,53 @@ {:result "23 + {token1}" :position 13})) (t/is (= (cto/insert-ref "23 + " 5 "token1") {:result "23 + {token1}" :position 13}))) + (t/testing "value with closed references" (t/is (= (cto/insert-ref "{token2}" 8 "token1") {:result "{token2}{token1}" :position 16})) (t/is (= (cto/insert-ref "{token2}" 6 "token1") - {:result "{token1}" :position 8})))) \ No newline at end of file + {:result "{token1}" :position 8})) + (t/is (= (cto/insert-ref "{token2} + + {token3}" 10 "token1") + {:result "{token2} +{token1} + {token3}" :position 18})) + (t/is (= (cto/insert-ref "{token2} + {token3}" 16 "token1") + {:result "{token2} + {token1}" :position 19}))) + + (t/testing "value with open references" + (t/is (= (cto/insert-ref "{tok" 4 "token1") + {:result "{token1}" :position 8})) + (t/is (= (cto/insert-ref "{tok" 2 "token1") + {:result "{token1}ok" :position 8})) + (t/is (= (cto/insert-ref "{token2}{" 9 "token1") + {:result "{token2}{token1}" :position 16})) + (t/is (= (cto/insert-ref "{token2{}" 8 "token1") + {:result "{token2{token1}" :position 15})) + (t/is (= (cto/insert-ref "{token2} + { + token3}" 12 "token1") + {:result "{token2} + {token1} + token3}" :position 19})) + (t/is (= (cto/insert-ref "{token2{}" 8 "token1") + {:result "{token2{token1}" :position 15}))) + + (t/testing "value with broken references" + (t/is (= (cto/insert-ref "{tok {en2}" 6 "token1") + {:result "{tok {token1}" :position 13})) + (t/is (= (cto/insert-ref "{tok en2}" 5 "token1") + {:result "{tok {token1}en2}" :position 13})))) + +;; TODO: pasar a common data +(t/deftest nth-last-index-of-test + (t/is (= (cto/nth-last-index-of "" "*" 1) nil)) + (t/is (= (cto/nth-last-index-of "" "*" 2) nil)) + (t/is (= (cto/nth-last-index-of "abc*" "*" 1) 3)) + (t/is (= (cto/nth-last-index-of "abc*" "*" 2) nil)) + (t/is (= (cto/nth-last-index-of "*abc[*" "*" 1) 5)) + (t/is (= (cto/nth-last-index-of "abc*def*ghi" "*" 1) 7)) + (t/is (= (cto/nth-last-index-of "abc*def*ghi" "*" 2) 3))) + +;; TODO: pasar a common data +(t/deftest nth-index-of-test + (t/is (= (cto/nth-index-of "" "*" 1) nil)) + (t/is (= (cto/nth-index-of "" "*" 2) nil)) + (t/is (= (cto/nth-index-of "abc*" "*" 1) 3)) + (t/is (= (cto/nth-index-of "abc*" "*" 2) nil)) + (t/is (= (cto/nth-index-of "*abc[*" "*" 1) 0)) + (t/is (= (cto/nth-index-of "abc*def*ghi" "*" 1) 3)) + (t/is (= (cto/nth-index-of "abc*def*ghi" "*" 2) 7))) \ No newline at end of file