This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-06-30
Channels
- # announcements (40)
- # babashka (41)
- # beginners (32)
- # calva (15)
- # clara (8)
- # clj-kondo (14)
- # cljs-dev (30)
- # clojure (37)
- # clojure-dev (8)
- # clojure-europe (21)
- # clojure-norway (21)
- # clojure-uk (4)
- # clojured (3)
- # clojurescript (4)
- # community-development (10)
- # core-async (13)
- # cursive (23)
- # datomic (15)
- # emacs (9)
- # fulcro (3)
- # google-cloud (4)
- # graphql (24)
- # gratitude (2)
- # holy-lambda (4)
- # honeysql (5)
- # hyperfiddle (9)
- # keechma (1)
- # klipse (5)
- # lsp (23)
- # malli (4)
- # missionary (32)
- # pathom (28)
- # re-frame (2)
- # reagent (40)
- # reitit (17)
- # releases (2)
- # remote-jobs (1)
- # shadow-cljs (25)
- # specter (3)
- # vim (19)
- # xtdb (41)
I would like to get the identity of an object for the purpose of debugging. (identical?) only compares if two objs are the same, but I’d like something to shows the identity of it. Memory address or simillar (understand it is hard to achieve), but as long as it is some unique identity then it is enough.
https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#identityHashCode(java.lang.Object)
Do you have a favorite way of asserting that sequences are in a specific sort order in unit tests?
sort it once more and compare that original collection is the same as result
(is (= coll-under-test (sort-by :whatever coll-under-test)))
is what I went with and it works good!
I like to extract ids, or any other unique field, and compare them explicitly (is (= [3 2 4] (extract-ids result))
but this requires that data under test has manageable size
I second the sort-by solution because it tests a property that should be true no matter what the data is, it doesn't depend on specific values.
Just checking to see that my brain hasn’t completely melted from this ☀️ weather, is the most sensible way to test that map a
contains everything in map b
is (clojure.set/subset? (set a) (set b))
?
The reason I ask is that I have a bunch of tests where results return generated id:s from database sequence and I want to ignore those in test assertion (as long as it’s there, it’s fine, don’t care about value) but I don’t want to remove the id key itself as it’s useful elsewhere in further queries.
or:
(every? #(= (a %) (b %)) (keys b))
which is maybe a bit more performant since you don't have to construct a new map for thisWon't work for when the value in b
is nil
but the value in a
is missing. But might be not important in the OP's case.
Yeah, also the performance isn't too far off for small things, so probably doesn't matter
I can always test for nilness as well, I was thinking of wrapping this to a helper function anyways so that the test level assertion looks something like (deftest x (testing "..." (are-similar a b)))
or whatever the name will end up being 🙂
And I do care about fast tests, the faster the tests, the less friction there will be in adding even more 🙂
@U8SFC8HLP I've used this in all my projects: https://github.com/babashka/cli/blob/0f8e22f2bb95633f8804f05e17039c0d019c0ea8/test/babashka/cli_test.cljc#L16
Then consider also reduce-kv
with reduced
. Haven't measured anything, but I'd suspect it's faster for larger maps.
@U04V15CAJ What’s that regex doing there? 😄
of course the semantics of this can be discussed, this is why you should just copy this function and change it to how you like it
@U8SFC8HLP can you pull in a third-party dependency? If so, I highly recommend <recommendhttps://github.com/nubank/matcher-combinators|https://github.com/nubank/matcher-combinators>
I've also seen people use https://github.com/HealthSamurai/matcho for this sort of thing, but at first glance matcher-combinators seems more powerful?
If this is for a test, consider error messages as well. (every? ...)
style answers will just leave a message like “expected true, got false” and give not much information. Same for subset. It will dump a large subset. (doseq [k (keys b)] (contains? thing b))
might make it easier with error reporting
Is there a kind of "reverse resolve" capability for values? E.g. given a value, is there a way to search the current binding context or namespace for any variables/symbols bound to that value?
I guess I could pretty easily hack together something with var-get
and ns-publics
but was wondering if anyone has considered this problem from a different angle.
Logging or tracing the values as the code is executed? Depends on the problem you are trying to solve.
the specific motivating problem is less about execution tracing: I'm thinking of using a var name as a shorthand for a large value when printing the data structures where that value appears.
Do you really have all such big data as vars? I would think they just become results of a computation or let bindings / fn args at best...
Malli schemas can get pretty complex sometimes 😉