From da0389e304fad1da20a8321b7ab3b59f95f4e0f7 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Mon, 8 Jul 2024 14:38:19 +0200 Subject: [PATCH] Improved logic to run once for all shapes --- .../app/main/ui/workspace/tokens/token.cljs | 17 +++++++- frontend/test/token_tests/token_test.cljs | 43 ++++++++++--------- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/frontend/src/app/main/ui/workspace/tokens/token.cljs b/frontend/src/app/main/ui/workspace/tokens/token.cljs index 74df395c0..4df33b285 100644 --- a/frontend/src/app/main/ui/workspace/tokens/token.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/token.cljs @@ -1,6 +1,7 @@ (ns app.main.ui.workspace.tokens.token (:require - [cuerdas.core :as str])) + [cuerdas.core :as str] + [clojure.set :as set])) (defn attributes-map "Creats an attributes map using collection of `attributes` for `id`." @@ -45,8 +46,20 @@ [token shapes token-attributes] (some #(token-applied? token % token-attributes) shapes)) +(defn shapes-ids-by-applied-attributes [token shapes token-attributes] + (reduce (fn [acc shape] + (let [applied-ids-by-attribute (->> (map #(when (token-attribute-applied? token shape %) + [% #{(:id shape)}]) + token-attributes) + (filter some?) + (into {}))] + (merge-with into acc applied-ids-by-attribute))) + {} shapes)) + +(defn shapes-applied-all? [ids-by-attributes shape-ids attributes] + (every? #(set/superset? (get ids-by-attributes %) shape-ids) attributes)) + (defn group-shapes-by-all-applied - "" [token shapes token-attributes] (reduce (fn [acc cur-shape] diff --git a/frontend/test/token_tests/token_test.cljs b/frontend/test/token_tests/token_test.cljs index dcf0b69ef..bcf86fcc9 100644 --- a/frontend/test/token_tests/token_test.cljs +++ b/frontend/test/token_tests/token_test.cljs @@ -29,34 +29,35 @@ (t/is (= #{:x} (wtt/token-applied-attributes {:id :a} {:applied-tokens {:x :a :y :b}} #{:x :missing})))) -(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 (true? (wtt/group-shapes-by-all-applied-all? expected))) - (t/is (= (:all expected) shapes)) - (t/is (empty? (:other expected))) - (t/is (empty? (:some expected))))) - +(t/deftest shapes-ids-by-applied-attributes (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}} + shape-applied-x {:id :shape-applied-x + :applied-tokens {:x 1}} + shape-applied-y {:id :shape-applied-y + :applied-tokens {:y 1}} + shape-applied-x-y {:id :shape-applied-x-y + :applied-tokens {:x 1 :y 1}} + shape-applied-none {:id :shape-applied-none + :applied-tokens {}} + shape-applied-all {:id :shape-applied-all + :applied-tokens {:x 1 :y 1 :z 1}} + ids-set (fn [& xs] (into #{} (map :id xs))) 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])))))) + expected (wtt/shapes-ids-by-applied-attributes {:id 1} shapes attributes)] + (t/is (= (:x expected) (ids-set shape-applied-x + shape-applied-x-y + shape-applied-all))) + (t/is (= (:y expected) (ids-set shape-applied-y + shape-applied-x-y + shape-applied-all))) + (t/is (= (:z expected) (ids-set shape-applied-all))) + (t/is (true? (wtt/shapes-applied-all? expected (ids-set shape-applied-all) attributes))) + (t/is (false? (wtt/shapes-applied-all? expected (apply ids-set shapes) attributes)))))) (t/deftest tokens-applied-test (t/testing "is true when single shape matches the token and attributes"