This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-10-21
Channels
- # announcements (13)
- # babashka (29)
- # beginners (52)
- # calva (95)
- # cider (18)
- # clj-commons (7)
- # cljs-dev (42)
- # clojure (121)
- # clojure-australia (1)
- # clojure-dev (39)
- # clojure-europe (36)
- # clojure-france (4)
- # clojure-greece (1)
- # clojure-italy (20)
- # clojure-nl (3)
- # clojure-portugal (1)
- # clojure-uk (7)
- # clojurescript (47)
- # conjure (2)
- # cursive (9)
- # datalevin (5)
- # datascript (8)
- # datomic (68)
- # defnpodcast (2)
- # deps-new (5)
- # fulcro (18)
- # graalvm (21)
- # gratitude (9)
- # jobs (6)
- # jobs-discuss (17)
- # leiningen (3)
- # lsp (80)
- # lumo (1)
- # malli (9)
- # mount (2)
- # off-topic (16)
- # other-languages (8)
- # podcasts-discuss (19)
- # reitit (5)
- # remote-jobs (5)
- # shadow-cljs (29)
- # sql (5)
- # tools-deps (13)
- # vim (11)
- # xtdb (19)
How do i pass on every non destructured parameter, such that e.g. :color
and others is also included in the resulting map?
(defn testfn [{:keys [icon size]
:or {size 24}} & rest]
(merge {:icon icon :size size} rest))
(testfn {:icon "icon" :size "big" :color "yellow"})
i could pass on everything like this, but then i would not be able to set default values for some of them:
(defn testfn [everything]
everything)
(testfn {:icon "icon" :size "big" :color "yellow"})
This solves the problem, but i don't know if it is idiomatic:
(defn testfn [everything]
(let [ {:keys [size]
:or {size 24}} everything]
(merge everything {:size size})))
(testfn {:icon "icon" :size "big" :color "yellow"})
That's what I do when I need to distinguish between a value being set to nil
and not being set at all:
(let [params (cond-> params
(not (contains? params :size))
(assoc :size 24))]
(do-something params))
And when such distinction is not needed (would need to be altered a bit if you need false
as well):
(let [params (update params :size #(or % 24))]
(do-something params))
I have an atom with values as (def values (atom {:data [{:a "test"} {:a "new"}]}))
I want to remove map from :data
where :a = "test"
Please read the docstring for reset!
. In REPL, it's as easy as (doc reset!)
.
And then do the same for the functions I mentioned above.
Why don't we have a macro that makes it more convenient to do async tests? instead of this:
(deftest test-async
(async done
(http/get ""
(fn [res]
(is (= res :awesome))
(done)))))
@U024A5W9WBG Because this gives you all you need to write your own situational macros :)
I have one such macro here: https://github.com/borkdude/nbb/blob/015c5618c0b0cc173a0f5e1fe3335529f2d04d59/test/nbb/test_macros.clj#L3 But this is specific to my promise based use cases.
What do you use instead of thrown?
from clojure in cljs.test
?
I am running this test:
(deftest test-filter-cities-by-flight-time
(let [cities [{:name "portugal"
:flight-time 10}
{:name "portugal15"
:flight-time 15}
{:name "portugal20"
:flight-time 20}]]
...
(testing "max is less than min"
(-> (filter-cities-by-flight-time cities 10 0)
(= js/Error) ; <<<<
(is)))))
(ns app.components.city-list.subs
(:require [re-frame.core :refer [reg-sub]]
[clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as stest]))
(defn filter-cities-by-flight-time [cities min max]
(filter (fn [city]
(<= min (:flight-time city) max))
cities))
(filter-cities-by-flight-time [{:one 1} {:two 2}] 0 10)
(s/fdef filter-cities-by-flight-time
:args (s/and (s/cat :cities coll? :min int? :max int?)
#(<= (:min %) (:max %))))
(stest/instrument 'app.components.city-list.subs/filter-cities-by-flight-time)
and the returned value from the function call given parameters that doesn't follow the spec is this:
#error {:message "Call to #'app.components.city-list.subs/filter-cities-by-flight-time did not conform to spec.", :data {:cljs.spec.alpha/problems [{:path [], :pred (cljs.core/fn [%] (cljs.core/<= (:min %) (:max %))), :val {:cities [{:name "portugal", :flight-time 10} {:name "portugal15", :flight-time 15}....
There might also be a better way of using clojure.spec with cljs.test if at all possible?
I would like to use the specs in cljs.core.specs.alpha
to conform
/ unform
forms but it seems that they are only available via macros (says @thheller and he seems, as usual, to be right)
specs are all runtime stuff, the macros primarily provide sugar - not sure what you want to do - but it should be possible
the issue is that the core specs .cljc
namespace is loaded only via :require-macros
in a same-named .cljs
namespace which makes them not defined in .cljs
at runtime, but only when loading macros from the .cljc
file
in the end I just copied the specs into my own ns and removed the fdef
stuff and it all works ... easiest hack available
@dnolen if the specs were in a CLJS namespace (which is what I ended up doing) they would all be available directly to conform / unform
in CLJS. At the moment that direct route is not possible. Unless you have a trick?
there is also a https://clojure.atlassian.net/browse/CLJ-2021 in unform (that's a Clojure bug) but still, quite the time sink cos https://blog.klipse.tech/clojure/2019/03/08/spec-custom-defn.html also had a bug (now fixed by the lovely @viebel)
I’ll have to look at it - but the specs are in a private atom - you could just get them
That for instrumentation there has to be another thing for storing spec names to specs
I'm gonna bail and if you have a moment to look @dnolen it would be great, otherwise I'll follow up tomorrow. Thanks for the tip in any case!
that sounds like it to me based on the clj spec
you can get the spec using s/get-spec
but in the CLJS runtime this returns nil
because those specs aren't defined in the runtime, only at JVM compilation time during macro loading
I know this is in general not true, but for this specific example it works like that because of the .cljs
+ .cljc
combination where the .cljs
file only does (:require-macros ...)