DEV Community

Discussion on: Rust BDD tests with Cucumber

Collapse
 
tyranron profile image
Kai Ren

It's better and more ergonomic to use macros feature and proc-macro support for regular Rust functions to define steps. For some reason it has been removed from README and isn't shown in examples, but if we do look at previous versions, we can see it.

Collapse
 
rogertorres profile image
Roger Torres (he/him/ele)

Hi @tyranron , thank you for pointing that out!

Collapse
 
hakanai profile image
Hakanai • Edited

That approach brings with it a number of problems:

  1. I would have to think of a name to give the function. Naming things is hard. Anonymous functions by their nature don't have this problem.
  2. Whatever function name I come up with has to more or less match the step definition name, otherwise a reader gets confused when looking for it in a structure navigator. This initially sounds like it makes problem #1 easier, but if you have any placeholders inside the step definition, you probably can't have those in the function name, so you end up needing some kind of convention for naming them.
  3. If the step definition ever changes, there is a risk of forgetting to rename the function to match, making problem #2 harder.
  4. Sometimes the smart naming scheme you come up with to solve problem #2 results in two functions having clashing names, and then you end up disambiguating them with things like "_1", "_2".
  5. Stylistically, having to name everything twice is not DRY. (It's literally WET, because you are Writing Everything Twice.)

Literally all these issues we experienced with Cucumber Java, so I'd recommend the Rust community not to copy Java here, and to make the most of anonymous functions. You have the privileged position of having the language feature from the outset. :)

Collapse
 
tyranron profile image
Kai Ren

We've been using this approach successfully for several years, as of now. And none of the issues we've experienced were the ones you've described. I have no experience with Cucumber Java, maybe it just things work other way there, making the problems, you've described, notable.

We don't care about step functions naming in our codebase at all. They are none vital for the framework. If the failure occurs, it points directly to the file/line/column where the step function is defined, and IDE integration allows us to reach that place in one click. We don't need function names to disambiguate step function. More, the fact that they are regular functions, allow us to distribute them in the modules tree quite well, so our tests files rarely have more than 2-3 step functions inside, and so, naming collision never was a problem for us.

Using anonymous functions, on the other hand, doesn't allow us to keep steps in separate files, or makes this quite monstrous. Moreover, macro magic atop of regular functions makes things cleaner and much more ergonomic, empowering with additional batteries like compile-time regular expressions validation and similar.

You're welcome to get familiar with the cucumber-rs via the Cucumber Rust Book.