0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-10 14:51:37 -05:00

🔧 Enhance debug trace helpers for common tests

This commit is contained in:
Andrés Moya 2024-05-07 15:16:17 +02:00
parent b4ea749388
commit ea6a9c87ec
3 changed files with 78 additions and 47 deletions

View file

@ -11,6 +11,7 @@
[app.common.files.changes :as cfc] [app.common.files.changes :as cfc]
[app.common.files.validate :as cfv] [app.common.files.validate :as cfv]
[app.common.pprint :refer [pprint]] [app.common.pprint :refer [pprint]]
[app.common.types.component :as ctk]
[app.common.types.file :as ctf] [app.common.types.file :as ctf]
[app.common.types.page :as ctp] [app.common.types.page :as ctp]
[app.common.types.pages-list :as ctpl] [app.common.types.pages-list :as ctpl]
@ -87,7 +88,7 @@
;; ----- Debug ;; ----- Debug
(defn dump-file-type (defn dump-tree
"Dump a file using dump-tree function in common.types.file." "Dump a file using dump-tree function in common.types.file."
[file & {:keys [page-label libraries] :as params}] [file & {:keys [page-label libraries] :as params}]
(let [params (-> params (let [params (-> params
@ -115,44 +116,74 @@
(println "}")) (println "}"))
(defn- stringify-keys [m keys] (defn- stringify-keys [m keys]
(apply str (interpose ", " (map #(str % ": " (get m %)) keys)))) (let [kv (-> (select-keys m keys)
(assoc :swap-slot (when ((set keys) :swap-slot)
(ctk/get-swap-slot m)))
(assoc :swap-slot-label (when ((set keys) :swap-slot-label)
(when-let [slot (ctk/get-swap-slot m)]
(thi/label slot))))
(d/without-nils))
pretty-uuid (fn [id]
(let [id (str id)]
(str "#" (subs id (- (count id) 6)))))
format-kv (fn [[k v]]
(cond
(uuid? v)
(str k " " (pretty-uuid v))
:else
(str k " " v)))]
(when (seq kv)
(str " [" (apply str (interpose ", " (map format-kv kv))) "]"))))
(defn- dump-page-shape (defn- dump-page-shape
[shape keys padding] [shape keys padding show-refs?]
(println (str/pad (str padding (println (str/pad (str padding
(when (:main-instance shape) "{") (when (and (:main-instance shape) show-refs?) "{")
(or (thi/label (:id shape)) "<no-label>") (thi/label (:id shape))
(when (:main-instance shape) "}") (when (and (:main-instance shape) show-refs?) "}")
(when keys (when (seq keys)
(str " [" (stringify-keys shape keys) "]"))) (stringify-keys shape keys)))
{:length 40 :type :right}) {:length 50 :type :right})
(if (nil? (:shape-ref shape)) (if (nil? (:shape-ref shape))
(if (:component-root shape) (if (and (:component-root shape) show-refs?)
(str "# [Component " (or (thi/label (:component-id shape)) "<no-label>") "]") (str "# [Component " (thi/label (:component-id shape)) "]")
"") "")
(str/format "%s--> %s%s" (if show-refs?
(cond (:component-root shape) "#" (str/format "%s--> %s%s"
(:component-id shape) "@" (cond (:component-root shape) "#"
:else "-") (:component-id shape) "@"
(if (:component-root shape) :else "-")
(str "[Component " (or (thi/label (:component-id shape)) "<no-label>") "] ") (if (:component-root shape)
"") (str "[Component " (thi/label (:component-id shape)) "] ")
(or (thi/label (:shape-ref shape)) "<no-label>"))))) "")
(thi/label (:shape-ref shape)))
""))))
(defn dump-page (defn dump-page
"Dump the layer tree of the page. Print the label of each shape, and the specified keys." "Dump the layer tree of the page, showing labels of the shapes.
([page keys] - keys: a list of attributes of the shapes you want to show. In addition, you
(dump-page page uuid/zero "" keys)) can add :swap-slot to show the slot id (if any) or :swap-slot-label
([page root-id padding keys] to show the corresponding label.
(let [lookupf (d/getf (:objects page)) - show-refs?: if true, the component references will be shown."
root-shape (lookupf root-id) [page & {:keys [keys root-id padding show-refs?]
shapes (map lookupf (:shapes root-shape))] :or {keys [:name :swap-slot-label] root-id uuid/zero padding "" show-refs? true}}]
(doseq [shape shapes] (let [lookupf (d/getf (:objects page))
(dump-page-shape shape keys padding) root-shape (lookupf root-id)
(dump-page page (:id shape) (str padding " ") keys))))) shapes (map lookupf (:shapes root-shape))]
(doseq [shape shapes]
(dump-page-shape shape keys padding show-refs?)
(dump-page page
:keys keys
:root-id (:id shape)
:padding (str padding " ")
:show-refs? show-refs?))))
(defn dump-file (defn dump-file
"Dump the current page of the file, using dump-page above. "Dump the current page of the file, using dump-page above.
Example: (thf/dump-file file [:id :touched])" Example: (thf/dump-file file :keys [:name :swap-slot-label] :show-refs? false)"
([file] (dump-file file [])) [file & {:keys [] :as params}]
([file keys] (dump-page (current-page file) keys))) (dump-page (current-page file) params))

View file

@ -36,7 +36,8 @@
(f)) (f))
(defn label [id] (defn label [id]
(->> @idmap (or (->> @idmap
(filter #(= id (val %))) (filter #(= id (val %)))
(map key) (map key)
(first))) (first))
(str "<no-label #" (subs id (- (count id) 6)) ">")))

View file

@ -23,17 +23,16 @@
;; Related .penpot file: common/test/cases/remove-swap-slots.penpot ;; Related .penpot file: common/test/cases/remove-swap-slots.penpot
(defn- setup-file (defn- setup-file
[] []
;; :frame-b1 [:id: 3aee2370-44e4-81c8-8004-46e56a459d70, :touched: ] ;; {:frame-red} [:name Frame1] # [Component :red]
;; :blue1 [:id: 3aee2370-44e4-81c8-8004-46e56a45fc55, :touched: #{:swap-slot-3aee2370-44e4-81c8-8004-46e56a459d75}] ;; {:frame-blue} [:name Frame1] # [Component :blue]
;; :green-copy [:id: 3aee2370-44e4-81c8-8004-46e56a45fc56, :touched: ] ;; {:frame-green} [:name Frame1] # [Component :green]
;; :blue-copy-in-green-copy [:id: 3aee2370-44e4-81c8-8004-46e56a4631a4, :touched: #{:swap-slot-3aee2370-44e4-81c8-8004-46e56a459d6f}] ;; :red-copy-green [:name Frame1] @--> :frame-red
;; :frame-yellow [:id: 3aee2370-44e4-81c8-8004-46e56a459d73, :touched: ] ;; {:frame-b1} [:name Frame1] # [Component :b1]
;; :frame-green [:id: 3aee2370-44e4-81c8-8004-46e56a459d6c, :touched: ] ;; :blue1 [:name Frame1, :swap-slot-label :red-copy] @--> :frame-blue
;; :red-copy-green [:id: 3aee2370-44e4-81c8-8004-46e56a459d6f, :touched: ] ;; :frame-yellow [:name Frame1]
;; :frame-blue [:id: 3aee2370-44e4-81c8-8004-46e56a459d69, :touched: ] ;; :green-copy [:name Frame1] @--> :frame-green
;; :frame-b2 [:id: 3aee2370-44e4-81c8-8004-46e56a4631a5, :touched: ] ;; :blue-copy-in-green-copy [:name Frame1, :swap-slot-label :red-copy-green] @--> :frame-blue
;; :frame-red [:id: 3aee2370-44e4-81c8-8004-46e56a459d66, :touched: ] ;; {:frame-b2} [:name Frame1] # [Component :b2]
(-> (thf/sample-file :file1) (-> (thf/sample-file :file1)
(tho/add-frame :frame-red) (tho/add-frame :frame-red)
(thc/make-component :red :frame-red) (thc/make-component :red :frame-red)