Function-Captures for ExUnit's Case.describe/2

84 views
Skip to first unread message

Florian Neumann

unread,
Sep 21, 2022, 12:21:22 PM9/21/22
to elixir-lang-core
The example for ExUnit's "Case.describe/2" uses a String to reference to the given Function in test:

describe "String.capitalize/1" do...
 
Changes to the actual implementation (names, arity) are currently not picked up by the compiler or test frameworks.

To help myself out i wrote a helper function "literally/1" which accepts a function as parameter and turns it into a fully qualified function capture string:

describe literally(&String.capitalize/1) do...

This removes potential code smells when referring to functions.

Having a dedicated function for this adds a lot of clutter (every describe line get's expanded and you have to maintain that helper function in each project).

So if this is something improving the developer experience for anybody else i'd like to propose the additional acceptance of function-captures as first parameter of describe/2:

describe &String.capitalize/1 do...

P.s.: i hope i got the terms function capture for function references right.

Austin Ziegler

unread,
Sep 21, 2022, 12:33:27 PM9/21/22
to elixir-l...@googlegroups.com
I like this idea. In your implementation, wouldn’t `inspect/1` do the same as your `literally/1`, more or less?

```elixir
iex(3)> inspect(&String.capitalize/1)
"&String.capitalize/1"
```

-a

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/3b47d4bf-4e91-4896-8549-a9ffab5890a0n%40googlegroups.com.


--

Florian Neumann

unread,
Sep 21, 2022, 12:36:47 PM9/21/22
to elixir-lang-core
Good find, yes indeed (at least as far as i can see)! Now i can at least get rid of manually importing the helper everywhere.. :)

Florian Neumann

unread,
Sep 26, 2022, 3:40:35 AM9/26/22
to elixir-lang-core
Okay - there is a difference in the functionality:

inspect/1 does *not* check if the function exists.

the

literally/1 function also checks if the function was "exported".
Reply all
Reply to author
Forward
0 new messages