Fork me on GitHub
#clojure
<
2022-06-30
>
pinkfrog03:06:31

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.

hiredman03:06:56

System/identityHashcode

emilaasa07:06:20

Do you have a favorite way of asserting that sequences are in a specific sort order in unit tests?

delaguardo07:06:05

sort it once more and compare that original collection is the same as result

emilaasa07:06:38

(is (= coll-under-test (sort-by :whatever coll-under-test))) is what I went with and it works good!

nbardiuk08:06:28

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

👍 1
pavlosmelissinos09:06:59

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.

eskos10:06:23

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.

p-himik10:06:05

It's a sensible way. I'd probably use (= (select-keys a (keys b)) b).

💡 1
borkdude10:06:22

or:

(every? #(= (a %) (b %)) (keys b))
which is maybe a bit more performant since you don't have to construct a new map for this

👍 2
p-himik10:06:18

Won'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.

borkdude10:06:08

Yeah, also the performance isn't too far off for small things, so probably doesn't matter

eskos10:06:33

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 🙂

eskos10:06:56

And I do care about fast tests, the faster the tests, the less friction there will be in adding even more 🙂

p-himik10:06:55

Then consider also reduce-kv with reduced. Haven't measured anything, but I'd suspect it's faster for larger maps.

eskos10:06:34

@U04V15CAJ What’s that regex doing there? 😄

eskos10:06:53

But thanks! I’ll definitely nab that!

borkdude10:06:49

@U8SFC8HLP

(submap? {:a #"foo"} {:a "foobar"}) ;;=> true

nice 1
borkdude10:06:22

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

p-himik10:06:39

It's very rare that I have such mixed feelings about a line of code. :D

borkdude10:06:32

this is why I it's not a public API and I use it for myself

borkdude10:06:44

it's very useful in the context of my tests

p-himik10:06:28

Yeah, of course.

jcf11:06:24

@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>

👍 1
1
eskos12:06:52

Indeed I can, those look pretty awesome partyparrot

❤️ 1
💯 1
Max13:06:21

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?

dpsutton20:06:46

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

👍 2
respatialized13:06:59

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.

jumar13:06:27

Logging or tracing the values as the code is executed? Depends on the problem you are trying to solve.

respatialized13:06:26

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.

p-himik13:06:10

Perhaps a good use case for metadata.

☝️ 1
1
jumar14:06:06

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...

respatialized16:06:00

Malli schemas can get pretty complex sometimes 😉