0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-21 06:02:32 -05:00

Add style dictionary find-token-reference test

This commit is contained in:
Florian Schroedl 2024-06-24 09:59:22 +02:00
parent 5f25bd8a7b
commit 5c42514170
2 changed files with 22 additions and 12 deletions

View file

@ -24,21 +24,12 @@
;; Functions -------------------------------------------------------------------
(defn find-token-references
"Finds token reference values in `str` and returns a set with all contained namespaces."
[str]
(some->> (re-seq #"\{([^}]*)\}" str)
"Finds token reference values in `value-string` and returns a set with all contained namespaces."
[value-string]
(some->> (re-seq #"\{([^}]*)\}" value-string)
(map second)
(into #{})))
(comment
(find-token-references "{foo} + {bar}")
;; => #{"foo" "bar"}
(find-token-references "{foo.bar.baz} + something")
;; => #{"foo.bar.baz"}
(find-token-references "1 + 2")
;; => nil
nil)
(defn token-self-reference? [token-name reference-string]
(let [escaped-name (str/replace token-name "." "\\.")
regex (-> (str "{" escaped-name "}")

View file

@ -0,0 +1,19 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC
(ns frontend-tests.tokens.style-dictionary-test
(:require
[app.main.ui.workspace.tokens.style-dictionary :as wtsd]
[cljs.test :as t :include-macros true]))
(t/deftest test-find-token-references
;; Return references
(t/is (= #{"foo" "bar"} (wtsd/find-token-references "{foo} + {bar}")))
;; Ignore non reference text
(t/is (= #{"foo.bar.baz"} (wtsd/find-token-references "{foo.bar.baz} + something")))
;; No references found
(t/is (nil? (wtsd/find-token-references "1 + 2")))
;; Edge-case: Ignore unmatched closing parens
(t/is (= #{"foo" "bar"} (wtsd/find-token-references "{foo}} + {bar}"))))