Fork me on GitHub
#off-topic
<
2022-04-18
>
adi19:04:17

#orgmode metal(with live example featuring Clojure in org-babel) https://www.evalapply.org/posts/why-and-how-i-use-org-mode/

❤️ 2
teodorlu07:04:56

> I have never used org for managing TODOs, calendaring, or any “productivity” use cases (time tracking, GTD etc.), which it has become popular for. Nor, as is also popular these days, is it my “second brain”. (Or maybe it is because my ~/org directory is a total disaster zone. I just disorganise and use Emacs to search through the files.). This made me laugh out loud 😄 ❤️

teodorlu08:04:52

> I discovered it about a decade ago (circa 2013), right after I changed careers to “tech”, because all the gentlenerds at the little company used Emacs. Told ya. Not normal. (Although, surprisingly, lots of normies use it too. Hi friends!). What did you do before tech?

teodorlu08:04:11

> Yes m’lorx, this way please typo - "m'lord"? (though m'lorx undoubtably sounds cool)

adi10:04:32

m'lorx is like cljx ... all-purpose. I couldn't come up with a better one :D

😄 2
adi10:04:28

> What did you do before tech? business / management stuff

teodorlu10:04:32

what made you switch? Are you happy you did?

adi14:04:41

It's complicated :)

teodorlu14:04:05

interesting things often are complicated 😄

🥲 1
niquola20:04:39

Why not to put tests into source file right behind function you are testing?

niquola20:04:53

And why test is not just a function with meta and context params?

seancorfield21:04:48

@nicola The TL;DR is that test tooling doesn't generally expect tests to be in source files so default test runners don't always find such tests. The Expectations library docs talks about that in more detail: https://cljdoc.org/d/com.github.seancorfield/expectations/2.0.160/doc/getting-started#test-placement

niquola05:04:40

They are functions, but without arguments :(

vemv10:04:49

Would be an interesting exercise to count which runners do accept src and which do not. I'd healthily question whether most and everyone are accurate wording in that cljdoc :)

vemv10:04:26

And an even more useful exercise would be to report flaws in runners and get them fixed - no real reason to get stuck in suboptimality

seancorfield14:04:11

@U45T93RA6 that doc explains how to get the test runners to run tests in src - the point I'm making is that it's not the default behavior and it's not what any of the books or tutorials teach. I think co-located tests are fine and clojure.test inherently supports them (`with-test`) - modulo the caveats.

seancorfield21:04:16

(and tests are "just functions with metadata")

☝️ 1
p-himik21:04:52

When I feel like a test belongs with the function in question (e.g. when it would also serve as a part of the documentation) and when it can be expressed as a sequence of arg vectors, I just put that sequence somewhere in the functions metadata and create a separate test that simply runs that function in the right context with all those arguments from the sequence, one by one.

👍 1
seancorfield21:04:47

That's a neat approach -- I'll try to remember that!

🙂 1
Oliver George22:04:18

Can you post an example please

p-himik22:04:33

Sure, something like this:

;; In `/src`
(ns foo.bar)

(defn do-stuff
  "Does some stuff - usually adds two numbers together."
  {:test-data {[1 1]  2
               [1 2]  3
               [-1 1] 0}}
  [x y]
  (+ x y))

;; In `/test`
(ns foo.bar-test
  (:require [clojure.test :refer :all]))

(deftest test-data-in-bar []
  (doseq [[sym var] (ns-publics 'foo.bar)]
    (when-some [td (:test-data (meta var))]
      (doseq [[args expected] td]
        (let [result (apply @var args)]
          (is (= result expected) (pr-str (list sym args))))))))

👍 13
didibus06:04:34

Hum... That's pretty neat

Martynas Maciulevičius06:04:13

In golang they like to do something similar (except they don't define it on the fn (but you could (although the compiler would complain about unused var))): https://yourbasic.org/golang/table-driven-unit-test/ (also wow, getting 8 upvotes is quite rare here 😄)

p-himik07:04:22

I'm surprised with that myself. :D My code above is just a lazy man's https://github.com/Kobold/clj-doc-test (and also happens to be more flexible because you're in full control and the implementation is just a few lines).

vemv10:04:26

This alternative means cider-test-run-test (run test at <cursor>) or any equivalent feature won't work. also doseq and when-some are prone to false negatives here - one doesn't want tests to misteriously stop running

Martynas Maciulevičius10:04:32

If you'd have test boilerplate for each method then it would still work. Maybe it could be done via a macro. But in the case of a test-executing macro it wouldn't produce a testfile to run. Again, coming from golang world it could be done via a code generator... Where you could generate your test boilerplate file for this method when it has this metadata.

p-himik10:04:55

@U45T93RA6 Regarding the cider point - I think I know what you mean and I realized that I used defn instead of deftest - that's corrected now. Regarding doseq and when-some - I don't get it. Where would false negatives come from?

vemv10:04:56

I meant something else regarding cider with the original pattern you can cider-test-run-test over a defn ^{:test ... , it's super handy as you remain focused in the sources > Where would false negatives come from? If you rename tests, or have typos in the :test-data name, etc the deftest would pass, but running zero assertions

p-himik10:04:04

Ah, well, you can easily define your own function that just runs over :test-data and put it under :test. That will actually solve your second point as well - that :test function can assert that there's some test data.

p-himik10:04:22

But I myself wouldn't consider a possible typo in :test-data to be a good argument against using something like :test-data. You can make typos anywhere that introduce false negatives, there's no generic guard against that.

Martynas Maciulevičius10:04:40

@U45T93RA6 If you want you could have a build step that validates that all your metadata of the functions has only specified keywords as keys. This way you'd know that you messed up.

p-himik10:04:10

@U45T93RA6 Actually you gave me an idea, so here's an improvement that should tick all the boxes. :D Two downsides though: • Tools that check for an explicit deftest in the source code (e.g. Cursive) won't see a test there • You'll have to include your sources in the list of resources that has to be checked by a test runner

(defn with-test-data [& data]
  (assert (even? (count data)))
  (fn []
    (let [f-under-test (peek clojure.test/*testing-vars*)]
      (doseq [[args expected] (partition 2 data)]
        (let [result (apply f-under-test args)]
          (is (= expected result) (pr-str (concat (list (.-sym f-under-test)) args))))))))

(defn do-stuff
  {:test (with-test-data [1 1] 2
                         [1 2] 3
                         [-1 1] 0
                         ;; Just to see how a failure looks.
                         [42 0] 7)}
  [x y]
  (+ x y))

didibus15:04:22

The normal test meta takes too much real estate in the source file, that's why I upvoted this approach, I like the minimalism.

Martynas Maciulevičius15:04:31

@U2FRKM4TW It could also be done this way too (not sure if :pre can go before args).

(defn do-stuff
  (merge ;; This one can be something else
         {:pre [1 2 3]}
         (with-test [1 1] 2
                    [1 2] 3
                    [-1 1] 0
                    ;; Just to see how a failure looks.
                    [42 0] 7))
  [x y]
  (+ x y))