From 62ecee2cf80a9ba6d111dd85319942f4fab405f9 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Mon, 8 Jul 2024 10:24:23 +0200 Subject: [PATCH] Add grouping function by type --- .../app/main/ui/workspace/tokens/token.cljs | 20 +++++++++ frontend/test/token_tests/token_test.cljs | 45 +++++++++++-------- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/frontend/src/app/main/ui/workspace/tokens/token.cljs b/frontend/src/app/main/ui/workspace/tokens/token.cljs index 3cb8e1ff1..66f251db0 100644 --- a/frontend/src/app/main/ui/workspace/tokens/token.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/token.cljs @@ -29,6 +29,12 @@ [token shape token-attributes] (some #(token-attribute-applied? token shape %) token-attributes)) +(defn token-applied-attributes + "Return a set of which `token-attributes` are applied with `token`." + [token shape token-attributes] + (-> (filter #(token-attribute-applied? token shape %) token-attributes) + (set))) + (defn shapes-token-applied? "Test if `token` is applied to to any of `shapes` with at least one of the one of the given `token-attributes`." [token shapes token-attributes] @@ -39,6 +45,20 @@ [token shapes token-attributes] (some #(token-applied? token % token-attributes) shapes)) +(defn group-shapes-by-all-applied + "" + [token shapes token-attributes] + (reduce + (fn [acc cur-shape] + (let [applied-attrs (token-applied-attributes token cur-shape token-attributes)] + (cond + (empty? applied-attrs) (update acc :none (fnil conj []) cur-shape) + (= applied-attrs token-attributes) (update acc :all (fnil conj []) cur-shape) + :else (reduce (fn [acc' cur'] + (update-in acc' [:some cur'] (fnil conj []) cur-shape)) + acc applied-attrs)))) + {} shapes)) + (defn token-name->path "Splits token-name into a path vector split by `.` characters. diff --git a/frontend/test/token_tests/token_test.cljs b/frontend/test/token_tests/token_test.cljs index d78233056..9b0462955 100644 --- a/frontend/test/token_tests/token_test.cljs +++ b/frontend/test/token_tests/token_test.cljs @@ -29,24 +29,33 @@ (t/is (= #{:x} (wtt/token-applied-attributes {:id :a} {:applied-tokens {:x :a :y :b}} #{:x :missing})))) -(comment - (let [test-fn (fn [& args])] - (t/deftest token-context-menu - (let [shapes [{:applied-tokens {:x :a - :y :a}} - {:applied-tokens {:x :a - :y :a}}]] - (t/is (= :all (test-fn {:id :a} shapes #{:x :y})))) - (let [shapes [{:applied-tokens {:x :a - :y :_}} - {:applied-tokens {:x :_ - :y :a}}]] - (t/is (= #{:x :y} (test-fn {:id :a} shapes #{:x :y})))) - (let [shapes [{:applied-tokens {:x :a - :y :_}} - {:applied-tokens {:x :a - :y :_}}]] - (t/is (= #{:x} (test-fn {:id :a} shapes #{:x}))))))) +(t/deftest token-context-menu + (t/testing "Returns :all when token is applied to every shape" + (let [shapes [{:applied-tokens {:x 1 :y 1}} + {:applied-tokens {:x 1 :y 1}}] + expected (wtt/group-shapes-by-all-applied {:id 1} shapes #{:x :y})] + (t/is (= (:all expected) shapes)) + (t/is (empty? (:other expected))) + (t/is (empty? (:some expected))))) + + (t/testing "Returns set of matched attributes that fit the applied token" + (let [attributes #{:x :y :z} + shape-applied-x {:applied-tokens {:x 1}} + shape-applied-y {:applied-tokens {:y 1}} + shape-applied-x-y {:applied-tokens {:x 1 :y 1}} + shape-applied-none {:applied-tokens {}} + shape-applied-all {:applied-tokens {:x 1 :y 1 :z 1}} + shapes [shape-applied-x + shape-applied-y + shape-applied-x-y + shape-applied-all + shape-applied-none] + expected (wtt/group-shapes-by-all-applied {:id 1} shapes attributes)] + (t/is (= (:all expected) [shape-applied-all])) + (t/is (= (:none expected) [shape-applied-none])) + (t/is (= (get-in expected [:some :x]) [shape-applied-x shape-applied-x-y])) + (t/is (= (get-in expected [:some :y]) [shape-applied-y shape-applied-x-y])) + (t/is (nil? (get-in expected [:some :z])))))) (t/deftest tokens-applied-test (t/testing "is true when single shape matches the token and attributes"