Skip to content

Commit e7e647b

Browse files
committed
add tests for function formatting
1 parent 6ff3a68 commit e7e647b

File tree

4 files changed

+183
-14
lines changed

4 files changed

+183
-14
lines changed

test/src/tests/devtools/tests/format.cljs

+143-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
(ns devtools.tests.format
22
(:refer-clojure :exclude [range = > < + str])
33
(:require-macros [devtools.utils.macros :refer [range = > < + str]]) ; prefs aware versions
4-
(:require [cljs.test :refer-macros [deftest testing is]]
5-
[devtools.utils.test :refer [js-equals is-header want? is-body has-body? unroll remove-empty-styles]]
4+
(:require [cljs.test :refer-macros [deftest testing is are]]
5+
[devtools.utils.test :refer [js-equals is-header want? is-body has-body? unroll remove-empty-styles pref-str]]
66
[devtools.format :refer [surrogate? header-api-call has-body-api-call body-api-call]]
77
[devtools.prefs :refer [default-prefs merge-prefs! set-pref! set-prefs! update-pref! get-prefs pref]]
8-
[devtools.format :as f]))
8+
[devtools.format :as f]
9+
[devtools.utils.batteries :as b]))
910

1011
(def REF ["object" {"object" "##REF##"
1112
"config" "##CONFIG##"}])
@@ -378,4 +379,142 @@
378379
"}"
379380
"]"]
380381
"}"
381-
"]"]]))))
382+
"]"]]))))
383+
384+
(deftest test-function-formatting
385+
(testing "cljs-function?"
386+
(testing "these should NOT be recognized as cljs functions"
387+
(are [f] (not (f/cljs-function? f))
388+
b/simplest-fn))
389+
(testing "these should be recognized as cljs functions"
390+
(are [f] (f/cljs-function? f)
391+
b/minimal-fn
392+
b/cljs-lambda-multi-arity
393+
b/clsj-fn-with-fancy-name#$%!?
394+
b/cljs-fn-var
395+
b/cljs-fn-multi-arity-var
396+
b/cljs-fn-multi-arity
397+
b/cljs-fn-with-vec-destructuring
398+
b/inst-type-ifn0
399+
b/inst-type-ifn1
400+
b/inst-type-ifn2
401+
b/inst-type-ifn2va
402+
b/inst-type-ifn4va))
403+
(testing "these should be recognized as cljs functions"
404+
(set-pref! :disable-cljs-fn-formatting true)
405+
(are [f] (not (f/cljs-function? f))
406+
b/minimal-fn
407+
b/clsj-fn-with-fancy-name#$%!?
408+
b/cljs-fn-var
409+
b/cljs-fn-multi-arity-var
410+
b/cljs-fn-multi-arity
411+
b/cljs-fn-with-vec-destructuring
412+
b/inst-type-ifn0
413+
b/inst-type-ifn1
414+
b/inst-type-ifn2
415+
b/inst-type-ifn2va
416+
b/inst-type-ifn4va)
417+
(set-prefs! default-prefs)))
418+
(testing "minimal function formatting"
419+
(is-header b/minimal-fn
420+
["span"
421+
{"style" :cljs-style}
422+
["span" {"style" :header-style}
423+
REF]]
424+
(fn [ref]
425+
(is (surrogate? ref))
426+
(has-body? ref true)
427+
(is-header ref
428+
["span" {"style" :fn-header-style}
429+
["span" {"style" :fn-prefix-style}
430+
["span" {"style" :fn-lambda-symbol-style} :fn-lambda-symbol]]
431+
["span" {"style" :fn-args-style} (pref-str :args-open-symbol :args-close-symbol)]])
432+
(is-body ref
433+
["span" {"style" :body-style}
434+
["ol" {"style" :standard-ol-no-margin-style}
435+
["li" {"style" :aligned-li-style}
436+
["span" {"style" :fn-native-symbol-style} :fn-native-symbol]
437+
REF]]]))))
438+
(testing "cljs-lambda-multi-arity function formatting"
439+
(is-header b/cljs-lambda-multi-arity
440+
["span"
441+
{"style" :cljs-style}
442+
["span" {"style" :header-style}
443+
REF]]
444+
(fn [ref]
445+
(is (surrogate? ref))
446+
(has-body? ref true)
447+
(is-header ref
448+
["span" {"style" :fn-header-style}
449+
["span" {"style" :fn-prefix-style}
450+
["span" {"style" :fn-lambda-symbol-style} :fn-lambda-symbol]]
451+
["span" {"style" :fn-args-style} (pref-str :args-open-symbol :multi-arity-symbol :args-close-symbol)]])
452+
(is-body ref
453+
["span" {"style" :body-style}
454+
["ol" {"style" :standard-ol-no-margin-style}
455+
["li" {"style" :aligned-li-style}
456+
["span" {"style" :fn-multi-arity-args-indent-style}
457+
["span" {"style" :fn-prefix-style}
458+
["span" {"style" :fn-lambda-symbol-style} :fn-lambda-symbol]]]
459+
["span" {"style" :fn-args-style} (pref-str :args-open-symbol :args-close-symbol)]]
460+
["li" {"style" :aligned-li-style}
461+
["span" {"style" :fn-multi-arity-args-indent-style}
462+
["span" {"style" :fn-prefix-style}
463+
["span" {"style" :fn-lambda-symbol-style} :fn-lambda-symbol]]]
464+
["span" {"style" :fn-args-style} (pref-str :args-open-symbol "a b" :args-close-symbol)]]
465+
["li" {"style" :aligned-li-style}
466+
["span" {"style" :fn-multi-arity-args-indent-style}
467+
["span" {"style" :fn-prefix-style}
468+
["span" {"style" :fn-lambda-symbol-style} :fn-lambda-symbol]]]
469+
["span" {"style" :fn-args-style} (pref-str :args-open-symbol "c d e f" :args-close-symbol)]]
470+
["li" {"style" :aligned-li-style}
471+
["span" {"style" :fn-native-symbol-style} :fn-native-symbol]
472+
REF]]]))))
473+
(testing "cljs-fn-multi-arity-var function formatting"
474+
(is-header b/cljs-fn-multi-arity-var
475+
["span"
476+
{"style" :cljs-style}
477+
["span" {"style" :header-style}
478+
REF]]
479+
(fn [ref]
480+
(is (surrogate? ref))
481+
(has-body? ref true)
482+
(is-header ref
483+
["span" {"style" :fn-header-style}
484+
["span" {"style" :fn-prefix-style}
485+
["span" {"style" :fn-symbol-style} :fn-symbol]
486+
["span" {"style" :fn-name-style} "cljs-fn-multi-arity-var"]]
487+
["span" {"style" :fn-args-style} (pref-str :args-open-symbol :multi-arity-symbol :args-close-symbol)]])
488+
(is-body ref
489+
["span" {"style" :body-style}
490+
["ol" {"style" :standard-ol-no-margin-style}
491+
["li" {"style" :aligned-li-style}
492+
["span" {"style" :fn-multi-arity-args-indent-style}
493+
["span" {"style" :fn-prefix-style}
494+
["span" {"style" :fn-symbol-style} :fn-symbol]
495+
["span" {"style" :fn-name-style} "cljs-fn-multi-arity-var"]]]
496+
["span" {"style" :fn-args-style} (pref-str :args-open-symbol "a1" :args-close-symbol)]]
497+
["li" {"style" :aligned-li-style}
498+
["span" {"style" :fn-multi-arity-args-indent-style}
499+
["span" {"style" :fn-prefix-style}
500+
["span" {"style" :fn-symbol-style} :fn-symbol]
501+
["span" {"style" :fn-name-style} "cljs-fn-multi-arity-var"]]]
502+
["span" {"style" :fn-args-style} (pref-str :args-open-symbol "a2-1 a2-2" :args-close-symbol)]]
503+
["li" {"style" :aligned-li-style}
504+
["span" {"style" :fn-multi-arity-args-indent-style}
505+
["span" {"style" :fn-prefix-style}
506+
["span" {"style" :fn-symbol-style} :fn-symbol]
507+
["span" {"style" :fn-name-style} "cljs-fn-multi-arity-var"]]]
508+
["span" {"style" :fn-args-style} (pref-str :args-open-symbol "a3-1 a3-2 a3-3 a3-4" :args-close-symbol)]]
509+
["li" {"style" :aligned-li-style}
510+
["span" {"style" :fn-multi-arity-args-indent-style}
511+
["span" {"style" :fn-prefix-style}
512+
["span" {"style" :fn-symbol-style} :fn-symbol]
513+
["span" {"style" :fn-name-style} "cljs-fn-multi-arity-var"]]]
514+
["span" {"style" :fn-args-style} (pref-str :args-open-symbol "va1 va2 & rest" :args-close-symbol)]]
515+
["li" {"style" :aligned-li-style}
516+
["span" {"style" :fn-ns-symbol-style} :fn-ns-symbol]
517+
["span" {"style" :fn-ns-name-style} "devtools.utils.batteries"]]
518+
["li" {"style" :aligned-li-style}
519+
["span" {"style" :fn-native-symbol-style} :fn-native-symbol]
520+
REF]]])))))

test/src/tests/devtools/tests/munging.cljs

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@
197197
b/cljs-fn-with-map-destructuring ["p"]
198198
b/cljs-fn-with-map-destructuring-var ["& p"]
199199
b/cljs-fn-var ["first second & rest"]
200-
b/cljs-fn-multi-arity ["a1" "a2-1 a2-2" "a3-1 a3-2 a3-3 a3-4"]
200+
b/cljs-fn-multi-arity ["a1" "a2-1 a2-2" "a3-1 a3 a3 a3-4"]
201201
b/cljs-fn-multi-arity-var ["a1" "a2-1 a2-2" "a3-1 a3-2 a3-3 a3-4" "va1 va2 & rest"]
202202
b/inst-type-ifn0 [""]
203203
b/inst-type-ifn1 ["p1"]

test/src/tests/devtools/utils/batteries.cljs

+10-1
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,19 @@
8686
(defn cljs-fn-multi-arity
8787
([a1])
8888
([a2_1 a2-2])
89-
([a3_1 a3-2 a3-3 a3-4]))
89+
([a3_1 a3--2 a3--3 a3-4]))
9090

9191
(defn cljs-fn-multi-arity-var
9292
([a1])
9393
([a2_1 a2-2])
9494
([a3_1 a3-2 a3-3 a3-4])
9595
([va1 va2 & rest]))
96+
97+
(def cljs-lambda-multi-arity (fn
98+
([] 1)
99+
([a b])
100+
([c d e f])))
101+
102+
(def cljs-lambda-multi-arity-var (fn
103+
([p1] 1)
104+
([first & rest])))

test/src/tests/devtools/utils/test.cljs

+29-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns devtools.utils.test
22
(:require [cljs.test :refer-macros [is]]
33
[clojure.walk :refer [postwalk]]
4+
[cljs.pprint :refer [pprint]]
45
[goog.array :as garr]
56
[goog.json :as json]
67
[goog.object :as gobj]
@@ -30,12 +31,23 @@
3031
false))))
3132

3233
(defn replace-refs [template placeholder]
33-
(let [filter (fn [key value] (if (= key "object") placeholder value))]
34-
(json/parse (json/serialize template filter))))
34+
(let [replacer (fn [v]
35+
(if (and (vector? v)
36+
(= 2 (count v))
37+
(= (first v) "object")
38+
(not (some? (get (second v) "object"))))
39+
["object" placeholder]
40+
v))]
41+
(postwalk replacer template)))
3542

3643
(defn replace-configs [template placeholder]
37-
(let [filter (fn [key value] (if (= key "config") placeholder value))]
38-
(json/parse (json/serialize template filter))))
44+
(let [replacer (fn [v]
45+
(if (and (vector? v)
46+
(= 2 (count v))
47+
(= (first v) "config"))
48+
["config" placeholder]
49+
v))]
50+
(postwalk replacer template)))
3951

4052
(defn collect-refs [template]
4153
(let [refs (atom [])
@@ -69,15 +81,21 @@
6981
x))]
7082
(postwalk empty-style-remover v)))
7183

84+
(defn should-unroll? [o]
85+
(and (fn? o)
86+
(:unroll (meta o))))
87+
7288
(defn unroll-fns [v]
7389
(if (vector? v)
74-
(mapcat (fn [item] (if (fn? item) (unroll-fns (item)) [(unroll-fns item)])) v)
90+
(mapcat (fn [item] (if (should-unroll? item) (unroll-fns (item)) [(unroll-fns item)])) v)
7591
v))
7692

7793
(defn is-template [template expected & callbacks]
7894
(let [sanitized-template (-> template
95+
(js->clj)
7996
(replace-refs "##REF##")
80-
(replace-configs "##CONFIG##"))
97+
(replace-configs "##CONFIG##")
98+
(clj->js))
8199
refs (collect-refs template)
82100
expected-template (-> expected
83101
(unroll-fns)
@@ -134,7 +152,7 @@
134152
(str (pr-str value) " SHOULD return false to hasBody call"))))
135153

136154
(defn unroll [& args]
137-
(apply partial (concat [mapcat] args)))
155+
(with-meta (apply partial (concat [mapcat] args)) {:unroll true}))
138156

139157
(defn match? [[returned expected]]
140158
(cond
@@ -146,6 +164,9 @@
146164
(if (= (count c1) (count c2))
147165
(every? match? (partition 2 (interleave c1 c2)))))
148166

167+
(defn pref-str [& args]
168+
(apply str (map #(if (keyword? %) (pref %) %) args)))
169+
149170
; -- console capture --------------------------------------------------------------------------------------------------------
150171

151172
(defonce captured-console-output (atom []))
@@ -193,4 +214,4 @@
193214
(defn with-captured-console [f]
194215
(start-console-capture!)
195216
(f)
196-
(stop-console-capture!))
217+
(stop-console-capture!))

0 commit comments

Comments
 (0)