Fork me on GitHub
#clojure
<
2020-04-18
>
jerome.prudent.ext06:04:43

Hello, does clojure.test supports parameterized tests ? I would like to inject parameters created in fixtures to my tests. I can rely on dynamic var but I would rather not. (Unfortunately, I think I know the response, I'm hoping for some magic)

slipset07:04:42

@jerome.prudent.ext remember. Tests are functions. You can call other functions from tests. The functions you can call can contain ‘is’ and ‘are’.

jerome.prudent.ext07:04:57

sure but I will just repeat the same boilerplate in each deftest won't I ?

p-himik08:04:21

You probably could write a macro for it.

didibus10:04:55

No, from a deftest call a defn

didibus10:04:21

The deftest can loop through all test cases and call the defn with the parameters one after the other

didibus10:04:06

I think that should work

jerome.prudent.ext10:04:52

I have to be more precise on this topic. What I don't want is to call the same test over and over with subtle variations of inputs

jerome.prudent.ext10:04:01

What I want is to call different tests with a fresh state

jerome.prudent.ext10:04:15

what I have now is :

(deftest my-test
  (with-database [db]
    (is (blah db))))

jerome.prudent.ext10:04:30

what I'm looking for is

jerome.prudent.ext10:04:51

(with-fixture :each (fn [t] (t (new-database)))
(deftest my-test [db] (is (blah db))))

jerome.prudent.ext10:04:37

So that I don't have to write that with-database call over and over

jerome.prudent.ext10:04:32

macro should do it, but maybe I'm missing something

noisesmith15:04:54

(doseq [db all-dbs] (with-database [db] (is ....))

noisesmith15:04:23

if you need it to be a different deftest for each one, then (defn exercise-db [db] (with-database [db] (is ...))) then (deftest db-a-test (exercise-db a)) and (deftest db-b-test (exercise-db b)) etc.

noisesmith15:04:28

no need for a macro

witek12:04:14

Hello. Is it possible for a macro to get the name of the function which called it? Example: I would like a macro (defmacro fn-name [] "placeholder-for-name-of-the-caller-fn-here") which returns the name of the calling function as string. So that when I have a function (defn foo [] (fn-name)) and I call (foo) it returns "foo". It would be nice to get the name of the outer most function definition. But even a cryptic name of a nested (anonymous) function would be ok. Is this even possible?

hindol.adhya12:04:44

I'm curious too. But given Clojure's focus on pure functions (only input determines output) I think this is unlikely. All I can think about is, throw an exception and examine the stack trace.

witek13:04:57

I hoped, macros would be different. For example I can get the namespace of the calling function in the macro: (ns-name *ns*)

witek13:04:37

I believe the stacktrace can be obtained from the current Java Thread, without raising an exception. This could work....

noisesmith15:04:31

if the name of the function was avalable, it would be found in &env or &form (the invisible, implicit, macro arguments). &env has all locals (including loop bindings, function args, let bindings) and &form has the form invoking the macro

noisesmith15:04:10

yes, you can get the current stack trace, and if the function calling you was named, then you would see it - but a macro wouldn't see the wrapping functin, it would see the clojure compiler

noisesmith15:04:39

inss)user=>(defmacro prints-stack [] (prn (seq (.getStackTrace (Thread/currentThread)))))
#'user/prints-stack
kcmds)user=>(defn x [] (prints-stack))
([java.lang.Thread getStackTrace "Thread.java" 1606] [user$prints_stack invokeStatic "NO_SOURCE_FILE" 1] [user$prints_stack invoke "NO_SOURCE_FILE" 1] [clojure.lang.AFn applyToHelper "AFn.java" 156] [clojure.lang.AFn applyTo "AFn.java" 144] [clojure.lang.Var applyTo "Var.java" 705] [clojure.lang.Compiler macroexpand1 "Compiler.java" 6993] [clojure.lang.Compiler analyzeSeq "Compiler.java" 7093] [clojure.lang.Compiler analyze "Compiler.java" 6789] [clojure.lang.Compiler analyze "Compiler.java" 6745] [clojure.lang.Compiler$BodyExpr$Parser parse "Compiler.java" 6120] [clojure.lang.Compiler$FnMethod parse "Compiler.java" 5467] [clojure.lang.Compiler$FnExpr parse "Compiler.java" 4029] [clojure.lang.Compiler analyzeSeq "Compiler.java" 7105] [clojure.lang.Compiler analyze "Compiler.java" 6789] [clojure.lang.Compiler analyzeSeq "Compiler.java" 7095] [clojure.lang.Compiler analyze "Compiler.java" 6789] [clojure.lang.Compiler access$300 "Compiler.java" 38] [clojure.lang.Compiler$DefExpr$Parser parse "Compiler.java" 596] [clojure.lang.Compiler analyzeSeq "Compiler.java" 7107] [clojure.lang.Compiler analyze "Compiler.java" 6789] [clojure.lang.Compiler analyze "Compiler.java" 6745] [clojure.lang.Compiler eval "Compiler.java" 7181] [clojure.lang.Compiler eval "Compiler.java" 7132] [clojure.core$eval invokeStatic "core.clj" 3214] [clojure.core$eval invoke "core.clj" 3210] [clojure.main$repl$read_eval_print__9086$fn__9089 invoke "main.clj" 437] [clojure.main$repl$read_eval_print__9086 invoke "main.clj" 437] [clojure.main$repl$fn__9095 invoke "main.clj" 458] [clojure.main$repl invokeStatic "main.clj" 458] [clojure.main$repl_opt invokeStatic "main.clj" 522] [clojure.main$main invokeStatic "main.clj" 667] [clojure.main$main doInvoke "main.clj" 616] [clojure.lang.RestFn invoke "RestFn.java" 397] [clojure.lang.AFn applyToHelper "AFn.java" 152] [clojure.lang.RestFn applyTo "RestFn.java" 132] [clojure.lang.Var applyTo "Var.java" 705] [clojure.main main "main.java" 40])
#'user/x

joshkh16:04:21

can anyone recommend a way to standardise indentation for projects whose contributors use IntelliJ, emacs, and calva? enforce a policy with a subset of indentation features found amongst all editors? preprocess commits when they reach your favourite git server?

mpenet17:04:52

clj-fmt is good for that

mpenet17:04:25

Supported by most editors too, there's also a graal build

mpenet17:04:24

Then have ci fail on linting errors

kwladyka21:04:22

What is the simplest way to return string (which I got from bash command, because of convertion to pdf) in ring as a pdf file?

kelveden23:04:21

I've never had to do such a thing but the way I'd probably approach it would be to treat the string as a series of bytes (you'd need to know the encoding used in the string) and create an output stream from them with a content-type corresponding to a pdf (`application/pdf`?).

gerritjvv23:04:52

Content type application/pdf seems right, I think you need an Input stream. You could use java's ByteArrayInputStream (https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayInputStream.html)

kwladyka09:04:03

thank you, I will try this